GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/extensions/array_extensions.c
Date: 2026-07-11 23:13:58
Exec Total Coverage
Lines: 412 418 98.6%
Functions: 28 28 100.0%
Branches: 169 189 89.4%

Line Branch Exec Source
1 /*
2 Copyright 2022 - 2025 Joel Svensson svenssonjoel@yahoo.se
3 Copyright 2022, 2023 Benjamin Vedder
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "extensions/array_extensions.h"
20
21 #include "extensions.h"
22 #include "symrepr.h"
23 #include "lbm_memory.h"
24
25 #include <math.h>
26
27 #ifdef LBM_OPT_ARRAY_EXTENSIONS_SIZE
28 #pragma GCC optimize ("-Os")
29 #endif
30 #ifdef LBM_OPT_ARRAY_EXTENSIONS_SIZE_AGGRESSIVE
31 #pragma GCC optimize ("-Oz")
32 #endif
33
34 static lbm_uint little_endian = 0;
35 static lbm_uint big_endian = 0;
36
37 static lbm_value array_extension_unsafe_free_array(lbm_value *args, lbm_uint argn);
38 static lbm_value array_extension_buffer_append_i8(lbm_value *args, lbm_uint argn);
39 static lbm_value array_extension_buffer_append_i16(lbm_value *args, lbm_uint argn);
40 static lbm_value array_extension_buffer_append_i32(lbm_value *args, lbm_uint argn);
41 static lbm_value array_extension_buffer_append_u8(lbm_value *args, lbm_uint argn);
42 static lbm_value array_extension_buffer_append_u16(lbm_value *args, lbm_uint argn);
43 static lbm_value array_extension_buffer_append_u24(lbm_value *args, lbm_uint argn);
44 static lbm_value array_extension_buffer_append_u32(lbm_value *args, lbm_uint argn);
45 static lbm_value array_extension_buffer_append_f32(lbm_value *args, lbm_uint argn);
46
47 static lbm_value array_extension_buffer_get_i8(lbm_value *args, lbm_uint argn);
48 static lbm_value array_extension_buffer_get_i16(lbm_value *args, lbm_uint argn);
49 static lbm_value array_extension_buffer_get_i32(lbm_value *args, lbm_uint argn);
50 static lbm_value array_extension_buffer_get_u8(lbm_value *args, lbm_uint argn);
51 static lbm_value array_extension_buffer_get_u16(lbm_value *args, lbm_uint argn);
52 static lbm_value array_extension_buffer_get_u24(lbm_value *args, lbm_uint argn);
53 static lbm_value array_extension_buffer_get_u32(lbm_value *args, lbm_uint argn);
54 static lbm_value array_extension_buffer_get_f32(lbm_value *args, lbm_uint argn);
55
56 static lbm_value array_extension_buffer_length(lbm_value *args, lbm_uint argn);
57
58 static lbm_value array_extensions_bufclear(lbm_value *args, lbm_uint argn);
59 static lbm_value array_extensions_bufcpy(lbm_value *args, lbm_uint argn);
60 static lbm_value array_extensions_bufset_bit(lbm_value *args, lbm_uint argn);
61
62 66870 void lbm_array_extensions_init(void) {
63
64 66870 lbm_add_symbol_const("little-endian", &little_endian);
65 66870 lbm_add_symbol_const("big-endian", &big_endian);
66
67 66870 lbm_add_extension("free", array_extension_unsafe_free_array);
68 66870 lbm_add_extension("bufset-i8", array_extension_buffer_append_i8);
69 66870 lbm_add_extension("bufset-i16", array_extension_buffer_append_i16);
70 66870 lbm_add_extension("bufset-i32", array_extension_buffer_append_i32);
71 66870 lbm_add_extension("bufset-u8", array_extension_buffer_append_u8);
72 66870 lbm_add_extension("bufset-u16", array_extension_buffer_append_u16);
73 66870 lbm_add_extension("bufset-u24", array_extension_buffer_append_u24);
74 66870 lbm_add_extension("bufset-u32", array_extension_buffer_append_u32);
75 66870 lbm_add_extension("bufset-f32", array_extension_buffer_append_f32);
76
77 66870 lbm_add_extension("bufget-i8", array_extension_buffer_get_i8);
78 66870 lbm_add_extension("bufget-i16", array_extension_buffer_get_i16);
79 66870 lbm_add_extension("bufget-i32", array_extension_buffer_get_i32);
80 66870 lbm_add_extension("bufget-u8", array_extension_buffer_get_u8);
81 66870 lbm_add_extension("bufget-u16", array_extension_buffer_get_u16);
82 66870 lbm_add_extension("bufget-u24", array_extension_buffer_get_u24);
83 66870 lbm_add_extension("bufget-u32", array_extension_buffer_get_u32);
84 66870 lbm_add_extension("bufget-f32", array_extension_buffer_get_f32);
85
86 66870 lbm_add_extension("buflen", array_extension_buffer_length);
87 66870 lbm_add_extension("bufclear", array_extensions_bufclear);
88 66870 lbm_add_extension("bufcpy", array_extensions_bufcpy);
89 66870 lbm_add_extension("bufset-bit", array_extensions_bufset_bit);
90 66870 }
91
92 348 lbm_value array_extension_unsafe_free_array(lbm_value *args, lbm_uint argn) {
93 348 lbm_value res = ENC_SYM_EERROR;
94
2/2
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 6 times.
348 if (argn == 1) {
95
2/2
✓ Branch 0 taken 336 times.
✓ Branch 1 taken 6 times.
342 if (lbm_is_array_rw(args[0])) {
96
1/2
✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
336 if (lbm_heap_explicit_free_array(args[0])) {
97 336 res = ENC_SYM_TRUE;
98 } else {
99 ✗ res = ENC_SYM_NIL;
100 }
101 } else {
102 6 res = ENC_SYM_TERROR;
103 }
104 }
105 348 return res;
106 }
107
108 180853 static bool decode_append_args(lbm_value *error, lbm_value *args, lbm_uint argn, lbm_uint *index, bool *be, lbm_uint *a_size, uint8_t **a_data) {
109 180853 *be = true;
110 180853 *error = ENC_SYM_EERROR;
111 180853 bool res = false;
112
3/3
✓ Branch 0 taken 3009 times.
✓ Branch 1 taken 177835 times.
✓ Branch 2 taken 9 times.
180853 switch(argn) {
113 3009 case 4:
114
1/2
✓ Branch 0 taken 3009 times.
✗ Branch 1 not taken.
3009 if (lbm_type_of(args[3]) == LBM_TYPE_SYMBOL &&
115
1/2
✓ Branch 0 taken 3009 times.
✗ Branch 1 not taken.
3009 lbm_dec_sym(args[3]) == little_endian) {
116 3009 *be = false;
117 }
118 /* fall through */
119 case 3: {
120 180844 lbm_array_header_t *array = lbm_dec_array_rw(args[0]);
121
4/4
✓ Branch 0 taken 180586 times.
✓ Branch 1 taken 258 times.
✓ Branch 2 taken 180577 times.
✓ Branch 3 taken 9 times.
361430 if(array &&
122
2/2
✓ Branch 0 taken 180568 times.
✓ Branch 1 taken 9 times.
361163 lbm_is_number(args[1]) &&
123 180577 lbm_is_number(args[2])) {
124 180568 *a_size = array->size;
125 180568 *a_data = (uint8_t*)array->data;
126 180568 *index = lbm_dec_as_u32(args[1]);
127 180568 res = true;
128 } else {
129 276 *error = ENC_SYM_TERROR;
130 }
131 }
132 }
133 180853 return res;
134 }
135
136 180568 static bool buffer_append_bytes(uint8_t *data, lbm_uint d_size, bool be, lbm_uint index, lbm_uint nbytes, lbm_uint value) {
137
138 180568 lbm_uint last_index = index + (nbytes - 1);
139 180568 bool res = false;
140
2/2
✓ Branch 0 taken 179896 times.
✓ Branch 1 taken 672 times.
180568 if (last_index < d_size) {
141 179896 res = true;
142
4/4
✓ Branch 0 taken 173917 times.
✓ Branch 1 taken 1950 times.
✓ Branch 2 taken 846 times.
✓ Branch 3 taken 3183 times.
179896 switch(nbytes) {
143 173917 case 1:
144 173917 data[index] = (uint8_t) value;
145 173917 break;
146 1950 case 2:
147
2/2
✓ Branch 0 taken 1104 times.
✓ Branch 1 taken 846 times.
1950 if (be) {
148 1104 data[index+1] = (uint8_t)value;
149 1104 data[index] = (uint8_t)(value >> 8);
150 } else {
151 846 data[index] = (uint8_t)value;
152 846 data[index +1] = (uint8_t)(value >> 8);
153 }
154 1950 break;
155 846 case 3:
156
2/2
✓ Branch 0 taken 423 times.
✓ Branch 1 taken 423 times.
846 if (be) {
157 423 data[index+2] = (uint8_t)value;
158 423 data[index+1] = (uint8_t)(value >> 8);
159 423 data[index] = (uint8_t)(value >> 16);
160 } else {
161 423 data[index] = (uint8_t)value;
162 423 data[index+1] = (uint8_t)(value >> 8);
163 423 data[index+2] = (uint8_t)(value >> 16);
164 }
165 846 break;
166 3183 default:
167
2/2
✓ Branch 0 taken 2283 times.
✓ Branch 1 taken 900 times.
3183 if (be) {
168 2283 data[index+3] = (uint8_t) value;
169 2283 data[index+2] = (uint8_t) (value >> 8);
170 2283 data[index+1] = (uint8_t) (value >> 16);
171 2283 data[index] = (uint8_t) (value >> 24);
172 } else {
173 900 data[index] = (uint8_t) value;
174 900 data[index+1] = (uint8_t) (value >> 8);
175 900 data[index+2] = (uint8_t) (value >> 16);
176 900 data[index+3] = (uint8_t) (value >> 24);
177 }
178 3183 break;
179 }
180 }
181 180568 return res;
182 }
183
184 170283 lbm_value array_extension_buffer_append_i8(lbm_value *args, lbm_uint argn) {
185
186 170283 lbm_value res = ENC_SYM_EERROR;
187 170283 uint8_t *data = NULL;
188 170283 lbm_uint d_size = 0;
189 170283 bool be = false;
190 170283 lbm_uint index = 0;
191
192
2/2
✓ Branch 0 taken 170016 times.
✓ Branch 1 taken 267 times.
170283 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
193
2/2
✓ Branch 0 taken 169932 times.
✓ Branch 1 taken 84 times.
170016 if (buffer_append_bytes(data, d_size, be, index, 1, (lbm_uint)lbm_dec_as_i32(args[2]))) {
194 169932 res = ENC_SYM_TRUE;
195 }
196 }
197 170283 return res;
198 }
199
200 1773 lbm_value array_extension_buffer_append_i16(lbm_value *args, lbm_uint argn) {
201
202 1773 lbm_value res = ENC_SYM_EERROR;
203 1773 uint8_t *data = NULL;
204 1773 lbm_uint d_size = 0;
205 1773 bool be = false;
206 1773 lbm_uint index = 0;
207
208
1/2
✓ Branch 0 taken 1773 times.
✗ Branch 1 not taken.
1773 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
209
2/2
✓ Branch 0 taken 1689 times.
✓ Branch 1 taken 84 times.
1773 if (buffer_append_bytes(data, d_size, be, index, 2, (lbm_uint)lbm_dec_as_i32(args[2]))) {
210 1689 res = ENC_SYM_TRUE;
211 }
212 }
213 1773 return res;
214 }
215
216 1353 lbm_value array_extension_buffer_append_i32(lbm_value *args, lbm_uint argn) {
217
218 1353 lbm_value res = ENC_SYM_EERROR;
219 1353 uint8_t *data = NULL;
220 1353 lbm_uint d_size = 0;
221 1353 bool be = false;
222 1353 lbm_uint index = 0;
223
224
2/2
✓ Branch 0 taken 1350 times.
✓ Branch 1 taken 3 times.
1353 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
225
2/2
✓ Branch 0 taken 1266 times.
✓ Branch 1 taken 84 times.
1350 if (buffer_append_bytes(data, d_size, be, index, 4, (lbm_uint)lbm_dec_as_i32(args[2]))) {
226 1266 res = ENC_SYM_TRUE;
227 }
228 }
229 1353 return res;
230 }
231
232
233 4072 lbm_value array_extension_buffer_append_u8(lbm_value *args, lbm_uint argn) {
234
235 4072 lbm_value res = ENC_SYM_EERROR;
236 4072 uint8_t *data = NULL;
237 4072 lbm_uint d_size = 0;
238 4072 bool be = false;
239 4072 lbm_uint index = 0;
240
241
2/2
✓ Branch 0 taken 4069 times.
✓ Branch 1 taken 3 times.
4072 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
242
2/2
✓ Branch 0 taken 3985 times.
✓ Branch 1 taken 84 times.
4069 if (buffer_append_bytes(data, d_size, be, index, 1, (lbm_uint)lbm_dec_as_u32(args[2]))) {
243 3985 res = ENC_SYM_TRUE;
244 }
245 }
246 4072 return res;
247 }
248
249 348 lbm_value array_extension_buffer_append_u16(lbm_value *args, lbm_uint argn) {
250
251 348 lbm_value res = ENC_SYM_EERROR;
252 348 uint8_t *data = NULL;
253 348 lbm_uint d_size = 0;
254 348 bool be = false;
255 348 lbm_uint index = 0;
256
257
2/2
✓ Branch 0 taken 345 times.
✓ Branch 1 taken 3 times.
348 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
258
2/2
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 84 times.
345 if (buffer_append_bytes(data, d_size, be, index, 2, (lbm_uint)lbm_dec_as_u32(args[2]))) {
259 261 res = ENC_SYM_TRUE;
260 }
261 }
262 348 return res;
263 }
264
265 933 lbm_value array_extension_buffer_append_u24(lbm_value *args, lbm_uint argn) {
266
267 933 lbm_value res = ENC_SYM_EERROR;
268 933 uint8_t *data = NULL;
269 933 lbm_uint d_size = 0;
270 933 bool be = false;
271 933 lbm_uint index = 0;
272
273
2/2
✓ Branch 0 taken 930 times.
✓ Branch 1 taken 3 times.
933 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
274
2/2
✓ Branch 0 taken 846 times.
✓ Branch 1 taken 84 times.
930 if (buffer_append_bytes(data, d_size, be, index, 3, (lbm_uint)lbm_dec_as_u32(args[2]))) {
275 846 res = ENC_SYM_TRUE;
276 }
277 }
278 933 return res;
279 }
280
281 1356 lbm_value array_extension_buffer_append_u32(lbm_value *args, lbm_uint argn) {
282
283 1356 lbm_value res = ENC_SYM_EERROR;
284 1356 uint8_t *data = NULL;
285 1356 lbm_uint d_size = 0;
286 1356 bool be = false;
287 1356 lbm_uint index = 0;
288
289
2/2
✓ Branch 0 taken 1353 times.
✓ Branch 1 taken 3 times.
1356 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
290
2/2
✓ Branch 0 taken 1269 times.
✓ Branch 1 taken 84 times.
1353 if (buffer_append_bytes(data, d_size, be, index, 4, (lbm_uint)lbm_dec_as_u32(args[2]))) {
291 1269 res = ENC_SYM_TRUE;
292 }
293 }
294 1356 return res;
295 }
296
297 732 static lbm_uint float_to_u(float number) {
298 // Set subnormal numbers to 0 as they are not handled properly
299 // using this method.
300
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 717 times.
732 if (fabsf(number) < 1.5e-38) {
301 15 number = 0.0;
302 }
303
304 732 int e = 0;
305 732 float sig = frexpf(number, &e);
306 732 float sig_abs = fabsf(sig);
307 732 uint32_t sig_i = 0;
308
309
2/2
✓ Branch 0 taken 717 times.
✓ Branch 1 taken 15 times.
732 if (sig_abs >= 0.5) {
310 717 sig_i = (uint32_t)((sig_abs - 0.5f) * 2.0f * 8388608.0f);
311 717 e += 126;
312 }
313
314 732 uint32_t res = (((uint32_t)e & 0xFFu) << 23) | (uint32_t)(sig_i & 0x7FFFFFu);
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 732 times.
732 if (sig < 0) {
316 ✗ res |= 1U << 31;
317 }
318
319 732 return res;
320 }
321
322 645 static lbm_float u_to_float(uint32_t v) {
323
324 645 int e = (v >> 23) & 0xFF;
325 645 uint32_t sig_i = v & 0x7FFFFF;
326 645 bool neg = v & (1U << 31);
327
328 645 float sig = 0.0;
329
4/4
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 585 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 57 times.
645 if (e != 0 || sig_i != 0) {
330 588 sig = (float)sig_i / (8388608.0f * 2.0f) + 0.5f;
331 588 e -= 126;
332 }
333
334
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 642 times.
645 if (neg) {
335 3 sig = -sig;
336 }
337
338 645 return ldexpf(sig, e);
339 }
340
341 735 lbm_value array_extension_buffer_append_f32(lbm_value *args, lbm_uint argn) {
342
343 735 lbm_value res = ENC_SYM_EERROR;
344 735 uint8_t *data = NULL;
345 735 lbm_uint d_size = 0;
346 735 bool be = false;
347 735 lbm_uint index = 0;
348
349
2/2
✓ Branch 0 taken 732 times.
✓ Branch 1 taken 3 times.
735 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
350
2/2
✓ Branch 0 taken 648 times.
✓ Branch 1 taken 84 times.
732 if (buffer_append_bytes(data, d_size, be, index, 4, (lbm_uint)float_to_u(lbm_dec_as_float(args[2])))) {
351 648 res = ENC_SYM_TRUE;
352 }
353 }
354 735 return res;
355 }
356
357 /* (buffer-get-i8 buffer index) */
358 /* (buffer-get-i16 buffer index little-endian) */
359
360 15492 static bool decode_get_args(lbm_value *error, lbm_value *args, lbm_uint argn, lbm_uint *index, bool *be, lbm_uint *a_size, uint8_t **a_data) {
361 15492 bool res = false;
362
363 15492 *be=true;
364
365
3/3
✓ Branch 0 taken 3642 times.
✓ Branch 1 taken 11841 times.
✓ Branch 2 taken 9 times.
15492 switch(argn) {
366 3642 case 3:
367
1/2
✓ Branch 0 taken 3642 times.
✗ Branch 1 not taken.
3642 if (lbm_type_of(args[2]) == LBM_TYPE_SYMBOL &&
368
2/2
✓ Branch 0 taken 3636 times.
✓ Branch 1 taken 6 times.
3642 lbm_dec_sym(args[2]) == little_endian) {
369 3636 *be = false;
370 }
371 /* fall through */
372 case 2: {
373 15483 lbm_array_header_t *array = lbm_dec_array_r(args[0]);
374
4/4
✓ Branch 0 taken 15477 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 15465 times.
✓ Branch 3 taken 12 times.
30960 if (array &&
375 15477 lbm_is_number(args[1])) {
376 15465 *a_size = array->size;
377 15465 *a_data = (uint8_t*)array->data;
378 15465 *index = lbm_dec_as_u32(args[1]);
379 15465 res = true;
380 } else {
381 18 *error = ENC_SYM_TERROR;
382 }
383 }
384 }
385 15492 return res;
386 }
387
388 15465 static bool buffer_get_uint(lbm_uint *r_value, uint8_t *data, lbm_uint d_size, bool be, lbm_uint index, lbm_uint nbytes) {
389
390 15465 bool res = false;
391 15465 lbm_uint last_index = index + (nbytes - 1);
392
393
2/2
✓ Branch 0 taken 13617 times.
✓ Branch 1 taken 1848 times.
15465 if (last_index < d_size) {
394 13617 lbm_uint value = 0;
395 13617 res = true;
396
4/5
✓ Branch 0 taken 7368 times.
✓ Branch 1 taken 1962 times.
✓ Branch 2 taken 846 times.
✓ Branch 3 taken 3441 times.
✗ Branch 4 not taken.
13617 switch(nbytes) {
397 7368 case 1:
398 7368 value = (lbm_uint)data[index];
399 7368 break;
400 1962 case 2:
401
2/2
✓ Branch 0 taken 1116 times.
✓ Branch 1 taken 846 times.
1962 if (be) {
402 1116 value =
403 1116 (lbm_uint) data[index+1] |
404 1116 (lbm_uint) data[index] << 8;
405 } else {
406 846 value =
407 846 (lbm_uint) data[index] |
408 846 (lbm_uint) data[index+1] << 8;
409 }
410 1962 break;
411 846 case 3:
412
2/2
✓ Branch 0 taken 423 times.
✓ Branch 1 taken 423 times.
846 if (be) {
413 423 value =
414 423 (lbm_uint) data[index+2] |
415 423 (lbm_uint) data[index+1] << 8 |
416 423 (lbm_uint) data[index] << 16;
417 } else {
418 423 value =
419 423 (lbm_uint) data[index] |
420 423 (lbm_uint) data[index+1] << 8 |
421 423 (lbm_uint) data[index+2] << 16;
422 }
423 846 break;
424 3441 case 4:
425
2/2
✓ Branch 0 taken 2502 times.
✓ Branch 1 taken 939 times.
3441 if (be) {
426 2502 value =
427 2502 (uint32_t) data[index+3] |
428 2502 (uint32_t) data[index+2] << 8 |
429 2502 (uint32_t) data[index+1] << 16 |
430 2502 (uint32_t) data[index] << 24;
431 } else {
432 939 value =
433 939 (uint32_t) data[index] |
434 939 (uint32_t) data[index+1] << 8 |
435 939 (uint32_t) data[index+2] << 16 |
436 939 (uint32_t) data[index+3] << 24;
437 }
438 3441 break;
439 ✗ default:
440 ✗ res = false;
441 }
442 13617 *r_value = value;
443 }
444 15465 return res;
445 }
446
447
448
449 4212 lbm_value array_extension_buffer_get_i8(lbm_value *args, lbm_uint argn) {
450 4212 lbm_value res = ENC_SYM_EERROR;
451 4212 uint8_t *data = NULL;
452 4212 lbm_uint d_size = 0;
453 4212 bool be = false;
454 4212 lbm_uint index = 0;
455 4212 lbm_uint value = 0;
456
457
2/2
✓ Branch 0 taken 4200 times.
✓ Branch 1 taken 12 times.
4212 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
458
2/2
✓ Branch 0 taken 3948 times.
✓ Branch 1 taken 252 times.
4200 if (buffer_get_uint(&value, data, d_size, be, index, 1)) {
459 3948 res =lbm_enc_i((int8_t)value);
460 }
461 }
462 4212 return res;
463 }
464
465 2109 lbm_value array_extension_buffer_get_i16(lbm_value *args, lbm_uint argn) {
466 2109 lbm_value res = ENC_SYM_EERROR;
467 2109 uint8_t *data = NULL;
468 2109 lbm_uint d_size = 0;
469 2109 bool be = false;
470 2109 lbm_uint index = 0;
471 2109 lbm_uint value = 0;
472
473
1/2
✓ Branch 0 taken 2109 times.
✗ Branch 1 not taken.
2109 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
474
2/2
✓ Branch 0 taken 1689 times.
✓ Branch 1 taken 420 times.
2109 if (buffer_get_uint(&value, data, d_size, be, index, 2)) {
475 1689 res =lbm_enc_i((int16_t)value);
476 }
477 }
478 2109 return res;
479 }
480
481 1521 lbm_value array_extension_buffer_get_i32(lbm_value *args, lbm_uint argn) {
482 1521 lbm_value res = ENC_SYM_EERROR;
483 1521 uint8_t *data = NULL;
484 1521 lbm_uint d_size = 0;
485 1521 bool be = false;
486 1521 lbm_uint index = 0;
487 1521 lbm_uint value = 0;
488
489
2/2
✓ Branch 0 taken 1518 times.
✓ Branch 1 taken 3 times.
1521 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
490
2/2
✓ Branch 0 taken 1266 times.
✓ Branch 1 taken 252 times.
1518 if (buffer_get_uint(&value, data, d_size, be, index, 4)) {
491 1266 res =lbm_enc_i((int32_t)value);
492 }
493 }
494 1521 return res;
495 }
496
497 3675 lbm_value array_extension_buffer_get_u8(lbm_value *args, lbm_uint argn) {
498 3675 lbm_value res = ENC_SYM_EERROR;
499 3675 uint8_t *data = NULL;
500 3675 lbm_uint d_size = 0;
501 3675 bool be = false;
502 3675 lbm_uint index = 0;
503 3675 lbm_uint value = 0;
504
505
2/2
✓ Branch 0 taken 3672 times.
✓ Branch 1 taken 3 times.
3675 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
506
2/2
✓ Branch 0 taken 3420 times.
✓ Branch 1 taken 252 times.
3672 if (buffer_get_uint(&value, data, d_size, be, index, 1)) {
507 3420 res = lbm_enc_i((uint8_t)value);
508 }
509 }
510 3675 return res;
511 }
512
513 360 lbm_value array_extension_buffer_get_u16(lbm_value *args, lbm_uint argn) {
514 360 lbm_value res = ENC_SYM_EERROR;
515 360 uint8_t *data = NULL;
516 360 lbm_uint d_size = 0;
517 360 bool be = false;
518 360 lbm_uint index = 0;
519 360 lbm_uint value = 0;
520
521
2/2
✓ Branch 0 taken 357 times.
✓ Branch 1 taken 3 times.
360 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
522
2/2
✓ Branch 0 taken 273 times.
✓ Branch 1 taken 84 times.
357 if (buffer_get_uint(&value, data, d_size, be, index, 2)) {
523 273 res = lbm_enc_i((uint16_t)value);
524 }
525 }
526 360 return res;
527 }
528
529 1101 lbm_value array_extension_buffer_get_u24(lbm_value *args, lbm_uint argn) {
530 1101 lbm_value res = ENC_SYM_EERROR;
531 1101 uint8_t *data = NULL;
532 1101 lbm_uint d_size = 0;
533 1101 bool be = false;
534 1101 lbm_uint index = 0;
535 1101 lbm_uint value = 0;
536
537
2/2
✓ Branch 0 taken 1098 times.
✓ Branch 1 taken 3 times.
1101 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
538
2/2
✓ Branch 0 taken 846 times.
✓ Branch 1 taken 252 times.
1098 if (buffer_get_uint(&value, data, d_size, be, index, 3)) {
539 846 res = lbm_enc_i((int32_t)value);
540 }
541 }
542 1101 return res;
543 }
544
545 1782 lbm_value array_extension_buffer_get_u32(lbm_value *args, lbm_uint argn) {
546 1782 lbm_value res = ENC_SYM_EERROR;
547 1782 uint8_t *data = NULL;
548 1782 lbm_uint d_size = 0;
549 1782 bool be = false;
550 1782 lbm_uint index = 0;
551 1782 lbm_uint value = 0;
552
553
1/2
✓ Branch 0 taken 1782 times.
✗ Branch 1 not taken.
1782 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
554
2/2
✓ Branch 0 taken 1530 times.
✓ Branch 1 taken 252 times.
1782 if (buffer_get_uint(&value, data, d_size, be, index, 4)) {
555 1530 res = lbm_enc_u32((uint32_t)value);
556 }
557 }
558 1782 return res;
559 }
560
561 732 lbm_value array_extension_buffer_get_f32(lbm_value *args, lbm_uint argn) {
562 732 lbm_value res = ENC_SYM_EERROR;
563 732 uint8_t *data = NULL;
564 732 lbm_uint d_size = 0;
565 732 bool be = false;
566 732 lbm_uint index = 0;
567 732 lbm_uint value = 0;
568
569
2/2
✓ Branch 0 taken 729 times.
✓ Branch 1 taken 3 times.
732 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
570
2/2
✓ Branch 0 taken 645 times.
✓ Branch 1 taken 84 times.
729 if (buffer_get_uint(&value, data, d_size, be, index, 4)) {
571 645 res = lbm_enc_float(u_to_float((uint32_t)value));
572 }
573 }
574 732 return res;
575 }
576
577 822 lbm_value array_extension_buffer_length(lbm_value *args, lbm_uint argn) {
578 822 lbm_value res = ENC_SYM_EERROR;
579 lbm_array_header_t *array;
580
4/4
✓ Branch 0 taken 819 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 816 times.
✓ Branch 3 taken 3 times.
1641 if (argn == 1 &&
581
1/2
✓ Branch 0 taken 816 times.
✗ Branch 1 not taken.
1635 (array = lbm_dec_array_r(args[0])) &&
582 816 lbm_heap_array_valid(args[0])) {
583 816 res = lbm_enc_i((lbm_int)array->size);
584 }
585 822 return res;
586 }
587
588 //TODO: Have to think about 32 vs 64 bit here
589 1842 static lbm_value array_extensions_bufclear(lbm_value *args, lbm_uint argn) {
590 1842 lbm_value res = ENC_SYM_EERROR;
591
4/4
✓ Branch 0 taken 1755 times.
✓ Branch 1 taken 87 times.
✓ Branch 2 taken 1668 times.
✓ Branch 3 taken 87 times.
1842 if (argn >= 1 && argn <= 4) {
592 1668 res = ENC_SYM_TERROR;
593
2/2
✓ Branch 0 taken 1662 times.
✓ Branch 1 taken 6 times.
1668 if (lbm_is_array_rw(args[0])) {
594 1662 lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(args[0]);
595
596 1662 uint8_t clear_byte = 0;
597
2/2
✓ Branch 0 taken 1410 times.
✓ Branch 1 taken 252 times.
1662 if (argn >= 2) {
598
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1407 times.
1410 if (!lbm_is_number(args[1])) {
599 3 return res;
600 }
601 1407 clear_byte = (uint8_t)lbm_dec_as_u32(args[1]);
602 }
603
604 1659 uint32_t start = 0;
605
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 1353 times.
1659 if (argn >= 3) {
606
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 303 times.
306 if (!lbm_is_number(args[2])) {
607 3 return res;
608 }
609 303 uint32_t start_new = lbm_dec_as_u32(args[2]);
610
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 3 times.
303 if (start_new < array->size) {
611 300 start = start_new;
612 } else {
613 3 return res;
614 }
615 }
616 // Truncates size on 64 bit build
617 1653 uint32_t len = (uint32_t)array->size - start;
618
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 1437 times.
1653 if (argn >= 4) {
619
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 213 times.
216 if (!lbm_is_number(args[3])) {
620 3 return res;
621 }
622 213 uint32_t len_new = lbm_dec_as_u32(args[3]);
623
1/2
✓ Branch 0 taken 213 times.
✗ Branch 1 not taken.
213 if (len_new <= len) {
624 213 len = len_new;
625 }
626 }
627
628 1650 memset((char*)array->data + start, clear_byte, len);
629 1650 res = ENC_SYM_TRUE;
630 }
631 }
632 1830 return res;
633 }
634
635 432 static lbm_value array_extensions_bufcpy(lbm_value *args, lbm_uint argn) {
636 432 lbm_value res = ENC_SYM_EERROR;
637
638
2/2
✓ Branch 0 taken 345 times.
✓ Branch 1 taken 87 times.
432 if (argn == 5) {
639 345 res = ENC_SYM_TERROR;
640
4/6
✓ Branch 0 taken 177 times.
✓ Branch 1 taken 168 times.
✓ Branch 2 taken 177 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 177 times.
✗ Branch 5 not taken.
522 if (lbm_is_array_rw(args[0]) && lbm_is_number(args[1]) &&
641
2/4
✓ Branch 0 taken 177 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 177 times.
✗ Branch 3 not taken.
354 lbm_is_array_r(args[2]) && lbm_is_number(args[3]) &&lbm_is_number(args[4])) {
642 177 lbm_array_header_t *array1 = (lbm_array_header_t *)lbm_car(args[0]);
643
644 177 uint32_t start1 = lbm_dec_as_u32(args[1]);
645
646 177 lbm_array_header_t *array2 = (lbm_array_header_t *)lbm_car(args[2]);
647
648 177 uint32_t start2 = lbm_dec_as_u32(args[3]);
649 177 uint32_t len = lbm_dec_as_u32(args[4]);
650
651
2/4
✓ Branch 0 taken 177 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 177 times.
✗ Branch 3 not taken.
177 if (start1 < array1->size && start2 < array2->size) {
652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 177 times.
177 if (len > (array1->size - start1)) {
653 ✗ len = ((uint32_t)array1->size - start1);
654 }
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 177 times.
177 if (len > (array2->size - start2)) {
656 ✗ len = ((uint32_t)array2->size - start2);
657 }
658
659 177 memcpy((char*)array1->data + start1, (char*)array2->data + start2, len);
660 }
661 177 res = ENC_SYM_TRUE;
662 }
663 }
664 432 return res;
665 }
666
667 516 static lbm_value array_extensions_bufset_bit(lbm_value *args, lbm_uint argn) {
668 516 lbm_value res = ENC_SYM_EERROR;
669
670
2/2
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 255 times.
516 if (argn == 3) {
671 261 res = ENC_SYM_TERROR;
672
4/4
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 87 times.
✓ Branch 2 taken 171 times.
✓ Branch 3 taken 3 times.
435 if (lbm_is_array_rw(args[0]) &&
673
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 3 times.
345 lbm_is_number(args[1]) && lbm_is_number(args[2])) {
674 168 lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(args[0]);
675
676 168 unsigned int pos = lbm_dec_as_u32(args[1]);
677 168 unsigned int bit = lbm_dec_as_u32(args[2]) ? 1 : 0;
678
679 168 unsigned int bytepos = pos / 8;
680
681
1/2
✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
168 if (bytepos < array->size) {
682 168 unsigned int bitpos = pos % 8;
683 168 ((uint8_t*)array->data)[bytepos] &= (uint8_t)~(1 << bitpos);
684 168 ((uint8_t*)array->data)[bytepos] |= (uint8_t)(bit << bitpos);
685 }
686
687 168 res = ENC_SYM_TRUE;
688 }
689 }
690 516 return res;
691 }
692