| 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 | static lbm_uint dbc_sym = 0; | ||
| 37 | |||
| 38 | static lbm_value array_extension_unsafe_free_array(lbm_value *args, lbm_uint argn); | ||
| 39 | static lbm_value array_extension_buffer_append_i8(lbm_value *args, lbm_uint argn); | ||
| 40 | static lbm_value array_extension_buffer_append_i16(lbm_value *args, lbm_uint argn); | ||
| 41 | static lbm_value array_extension_buffer_append_i32(lbm_value *args, lbm_uint argn); | ||
| 42 | static lbm_value array_extension_buffer_append_u8(lbm_value *args, lbm_uint argn); | ||
| 43 | static lbm_value array_extension_buffer_append_u16(lbm_value *args, lbm_uint argn); | ||
| 44 | static lbm_value array_extension_buffer_append_u24(lbm_value *args, lbm_uint argn); | ||
| 45 | static lbm_value array_extension_buffer_append_u32(lbm_value *args, lbm_uint argn); | ||
| 46 | static lbm_value array_extension_buffer_append_f32(lbm_value *args, lbm_uint argn); | ||
| 47 | |||
| 48 | static lbm_value array_extension_buffer_get_i8(lbm_value *args, lbm_uint argn); | ||
| 49 | static lbm_value array_extension_buffer_get_i16(lbm_value *args, lbm_uint argn); | ||
| 50 | static lbm_value array_extension_buffer_get_i32(lbm_value *args, lbm_uint argn); | ||
| 51 | static lbm_value array_extension_buffer_get_u8(lbm_value *args, lbm_uint argn); | ||
| 52 | static lbm_value array_extension_buffer_get_u16(lbm_value *args, lbm_uint argn); | ||
| 53 | static lbm_value array_extension_buffer_get_u24(lbm_value *args, lbm_uint argn); | ||
| 54 | static lbm_value array_extension_buffer_get_u32(lbm_value *args, lbm_uint argn); | ||
| 55 | static lbm_value array_extension_buffer_get_f32(lbm_value *args, lbm_uint argn); | ||
| 56 | |||
| 57 | static lbm_value array_extension_buffer_length(lbm_value *args, lbm_uint argn); | ||
| 58 | |||
| 59 | static lbm_value array_extensions_bufclear(lbm_value *args, lbm_uint argn); | ||
| 60 | static lbm_value array_extensions_bufcpy(lbm_value *args, lbm_uint argn); | ||
| 61 | static lbm_value array_extensions_bufset_bit(lbm_value *args, lbm_uint argn); | ||
| 62 | |||
| 63 | static lbm_value array_extensions_bufset_bits_u(lbm_value *args, lbm_uint argn); | ||
| 64 | static lbm_value array_extensions_bufset_bits_i(lbm_value *args, lbm_uint argn); | ||
| 65 | static lbm_value array_extensions_bufget_bits_u(lbm_value *args, lbm_uint argn); | ||
| 66 | static lbm_value array_extensions_bufget_bits_i(lbm_value *args, lbm_uint argn); | ||
| 67 | static lbm_value array_extensions_bufset_bits_f32(lbm_value *args, lbm_uint argn); | ||
| 68 | static lbm_value array_extensions_bufget_bits_f32(lbm_value *args, lbm_uint argn); | ||
| 69 | |||
| 70 | 29167 | void lbm_array_extensions_init(void) { | |
| 71 | |||
| 72 | 29167 | lbm_add_symbol_const("little-endian", &little_endian); | |
| 73 | 29167 | lbm_add_symbol_const("big-endian", &big_endian); | |
| 74 | 29167 | lbm_add_symbol_const("dbc", &dbc_sym); | |
| 75 | |||
| 76 | 29167 | lbm_add_extension("free", array_extension_unsafe_free_array); | |
| 77 | 29167 | lbm_add_extension("bufset-i8", array_extension_buffer_append_i8); | |
| 78 | 29167 | lbm_add_extension("bufset-i16", array_extension_buffer_append_i16); | |
| 79 | 29167 | lbm_add_extension("bufset-i32", array_extension_buffer_append_i32); | |
| 80 | 29167 | lbm_add_extension("bufset-u8", array_extension_buffer_append_u8); | |
| 81 | 29167 | lbm_add_extension("bufset-u16", array_extension_buffer_append_u16); | |
| 82 | 29167 | lbm_add_extension("bufset-u24", array_extension_buffer_append_u24); | |
| 83 | 29167 | lbm_add_extension("bufset-u32", array_extension_buffer_append_u32); | |
| 84 | 29167 | lbm_add_extension("bufset-f32", array_extension_buffer_append_f32); | |
| 85 | |||
| 86 | 29167 | lbm_add_extension("bufget-i8", array_extension_buffer_get_i8); | |
| 87 | 29167 | lbm_add_extension("bufget-i16", array_extension_buffer_get_i16); | |
| 88 | 29167 | lbm_add_extension("bufget-i32", array_extension_buffer_get_i32); | |
| 89 | 29167 | lbm_add_extension("bufget-u8", array_extension_buffer_get_u8); | |
| 90 | 29167 | lbm_add_extension("bufget-u16", array_extension_buffer_get_u16); | |
| 91 | 29167 | lbm_add_extension("bufget-u24", array_extension_buffer_get_u24); | |
| 92 | 29167 | lbm_add_extension("bufget-u32", array_extension_buffer_get_u32); | |
| 93 | 29167 | lbm_add_extension("bufget-f32", array_extension_buffer_get_f32); | |
| 94 | |||
| 95 | 29167 | lbm_add_extension("buflen", array_extension_buffer_length); | |
| 96 | 29167 | lbm_add_extension("bufclear", array_extensions_bufclear); | |
| 97 | 29167 | lbm_add_extension("bufcpy", array_extensions_bufcpy); | |
| 98 | 29167 | lbm_add_extension("bufset-bit", array_extensions_bufset_bit); | |
| 99 | |||
| 100 | 29167 | lbm_add_extension("bufset-bits-u", array_extensions_bufset_bits_u); | |
| 101 | 29167 | lbm_add_extension("bufset-bits-i", array_extensions_bufset_bits_i); | |
| 102 | 29167 | lbm_add_extension("bufget-bits-u", array_extensions_bufget_bits_u); | |
| 103 | 29167 | lbm_add_extension("bufget-bits-i", array_extensions_bufget_bits_i); | |
| 104 | 29167 | lbm_add_extension("bufset-bits-f32", array_extensions_bufset_bits_f32); | |
| 105 | 29167 | lbm_add_extension("bufget-bits-f32", array_extensions_bufget_bits_f32); | |
| 106 | 29167 | } | |
| 107 | |||
| 108 | 156 | lbm_value array_extension_unsafe_free_array(lbm_value *args, lbm_uint argn) { | |
| 109 | 156 | lbm_value res = ENC_SYM_EERROR; | |
| 110 |
2/2✓ Branch 0 taken 150 times.
✓ Branch 1 taken 6 times.
|
156 | if (argn == 1) { |
| 111 |
2/2✓ Branch 1 taken 144 times.
✓ Branch 2 taken 6 times.
|
150 | if (lbm_is_array_rw(args[0])) { |
| 112 |
1/2✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
|
144 | if (lbm_heap_explicit_free_array(args[0])) { |
| 113 | 144 | res = ENC_SYM_TRUE; | |
| 114 | } else { | ||
| 115 | ✗ | res = ENC_SYM_NIL; | |
| 116 | } | ||
| 117 | } else { | ||
| 118 | 6 | res = ENC_SYM_TERROR; | |
| 119 | } | ||
| 120 | } | ||
| 121 | 156 | return res; | |
| 122 | } | ||
| 123 | |||
| 124 | 78805 | 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) { | |
| 125 | 78805 | *be = true; | |
| 126 | 78805 | *error = ENC_SYM_EERROR; | |
| 127 | 78805 | bool res = false; | |
| 128 |
3/3✓ Branch 0 taken 1329 times.
✓ Branch 1 taken 77467 times.
✓ Branch 2 taken 9 times.
|
78805 | switch(argn) { |
| 129 | 1329 | case 4: | |
| 130 |
1/2✓ Branch 1 taken 1329 times.
✗ Branch 2 not taken.
|
1329 | if (lbm_type_of(args[3]) == LBM_TYPE_SYMBOL && |
| 131 |
1/2✓ Branch 1 taken 1329 times.
✗ Branch 2 not taken.
|
1329 | lbm_dec_sym(args[3]) == little_endian) { |
| 132 | 1329 | *be = false; | |
| 133 | } | ||
| 134 | /* fall through */ | ||
| 135 | case 3: { | ||
| 136 | 78796 | lbm_array_header_t *array = lbm_dec_array_rw(args[0]); | |
| 137 |
4/4✓ Branch 0 taken 78682 times.
✓ Branch 1 taken 114 times.
✓ Branch 2 taken 78673 times.
✓ Branch 3 taken 9 times.
|
157478 | if(array && |
| 138 |
2/2✓ Branch 1 taken 78664 times.
✓ Branch 2 taken 9 times.
|
157355 | lbm_is_number(args[1]) && |
| 139 | 78673 | lbm_is_number(args[2])) { | |
| 140 | 78664 | *a_size = array->size; | |
| 141 | 78664 | *a_data = (uint8_t*)array->data; | |
| 142 | 78664 | *index = lbm_dec_as_u32(args[1]); | |
| 143 | 78664 | res = true; | |
| 144 | } else { | ||
| 145 | 132 | *error = ENC_SYM_TERROR; | |
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | 78805 | return res; | |
| 150 | } | ||
| 151 | |||
| 152 | 78664 | static bool buffer_append_bytes(uint8_t *data, lbm_uint d_size, bool be, lbm_uint index, lbm_uint nbytes, lbm_uint value) { | |
| 153 | |||
| 154 | 78664 | lbm_uint last_index = index + (nbytes - 1); | |
| 155 | 78664 | bool res = false; | |
| 156 |
2/2✓ Branch 0 taken 78376 times.
✓ Branch 1 taken 288 times.
|
78664 | if (last_index < d_size) { |
| 157 | 78376 | res = true; | |
| 158 |
4/4✓ Branch 0 taken 75709 times.
✓ Branch 1 taken 846 times.
✓ Branch 2 taken 366 times.
✓ Branch 3 taken 1455 times.
|
78376 | switch(nbytes) { |
| 159 | 75709 | case 1: | |
| 160 | 75709 | data[index] = (uint8_t) value; | |
| 161 | 75709 | break; | |
| 162 | 846 | case 2: | |
| 163 |
2/2✓ Branch 0 taken 480 times.
✓ Branch 1 taken 366 times.
|
846 | if (be) { |
| 164 | 480 | data[index+1] = (uint8_t)value; | |
| 165 | 480 | data[index] = (uint8_t)(value >> 8); | |
| 166 | } else { | ||
| 167 | 366 | data[index] = (uint8_t)value; | |
| 168 | 366 | data[index +1] = (uint8_t)(value >> 8); | |
| 169 | } | ||
| 170 | 846 | break; | |
| 171 | 366 | case 3: | |
| 172 |
2/2✓ Branch 0 taken 183 times.
✓ Branch 1 taken 183 times.
|
366 | if (be) { |
| 173 | 183 | data[index+2] = (uint8_t)value; | |
| 174 | 183 | data[index+1] = (uint8_t)(value >> 8); | |
| 175 | 183 | data[index] = (uint8_t)(value >> 16); | |
| 176 | } else { | ||
| 177 | 183 | data[index] = (uint8_t)value; | |
| 178 | 183 | data[index+1] = (uint8_t)(value >> 8); | |
| 179 | 183 | data[index+2] = (uint8_t)(value >> 16); | |
| 180 | } | ||
| 181 | 366 | break; | |
| 182 | 1455 | default: | |
| 183 |
2/2✓ Branch 0 taken 1035 times.
✓ Branch 1 taken 420 times.
|
1455 | if (be) { |
| 184 | 1035 | data[index+3] = (uint8_t) value; | |
| 185 | 1035 | data[index+2] = (uint8_t) (value >> 8); | |
| 186 | 1035 | data[index+1] = (uint8_t) (value >> 16); | |
| 187 | 1035 | data[index] = (uint8_t) (value >> 24); | |
| 188 | } else { | ||
| 189 | 420 | data[index] = (uint8_t) value; | |
| 190 | 420 | data[index+1] = (uint8_t) (value >> 8); | |
| 191 | 420 | data[index+2] = (uint8_t) (value >> 16); | |
| 192 | 420 | data[index+3] = (uint8_t) (value >> 24); | |
| 193 | } | ||
| 194 | 1455 | break; | |
| 195 | } | ||
| 196 | } | ||
| 197 | 78664 | return res; | |
| 198 | } | ||
| 199 | |||
| 200 | 72987 | lbm_value array_extension_buffer_append_i8(lbm_value *args, lbm_uint argn) { | |
| 201 | |||
| 202 | 72987 | lbm_value res = ENC_SYM_EERROR; | |
| 203 | 72987 | uint8_t *data = NULL; | |
| 204 | 72987 | lbm_uint d_size = 0; | |
| 205 | 72987 | bool be = false; | |
| 206 | 72987 | lbm_uint index = 0; | |
| 207 | |||
| 208 |
2/2✓ Branch 1 taken 72864 times.
✓ Branch 2 taken 123 times.
|
72987 | if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 209 |
2/2✓ Branch 2 taken 72828 times.
✓ Branch 3 taken 36 times.
|
72864 | if (buffer_append_bytes(data, d_size, be, index, 1, (lbm_uint)lbm_dec_as_i32(args[2]))) { |
| 210 | 72828 | res = ENC_SYM_TRUE; | |
| 211 | } | ||
| 212 | } | ||
| 213 | 72987 | return res; | |
| 214 | } | ||
| 215 | |||
| 216 | 765 | lbm_value array_extension_buffer_append_i16(lbm_value *args, lbm_uint argn) { | |
| 217 | |||
| 218 | 765 | lbm_value res = ENC_SYM_EERROR; | |
| 219 | 765 | uint8_t *data = NULL; | |
| 220 | 765 | lbm_uint d_size = 0; | |
| 221 | 765 | bool be = false; | |
| 222 | 765 | lbm_uint index = 0; | |
| 223 | |||
| 224 |
1/2✓ Branch 1 taken 765 times.
✗ Branch 2 not taken.
|
765 | if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 225 |
2/2✓ Branch 2 taken 729 times.
✓ Branch 3 taken 36 times.
|
765 | if (buffer_append_bytes(data, d_size, be, index, 2, (lbm_uint)lbm_dec_as_i32(args[2]))) { |
| 226 | 729 | res = ENC_SYM_TRUE; | |
| 227 | } | ||
| 228 | } | ||
| 229 | 765 | return res; | |
| 230 | } | ||
| 231 | |||
| 232 | 585 | lbm_value array_extension_buffer_append_i32(lbm_value *args, lbm_uint argn) { | |
| 233 | |||
| 234 | 585 | lbm_value res = ENC_SYM_EERROR; | |
| 235 | 585 | uint8_t *data = NULL; | |
| 236 | 585 | lbm_uint d_size = 0; | |
| 237 | 585 | bool be = false; | |
| 238 | 585 | lbm_uint index = 0; | |
| 239 | |||
| 240 |
2/2✓ Branch 1 taken 582 times.
✓ Branch 2 taken 3 times.
|
585 | if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 241 |
2/2✓ Branch 2 taken 546 times.
✓ Branch 3 taken 36 times.
|
582 | if (buffer_append_bytes(data, d_size, be, index, 4, (lbm_uint)lbm_dec_as_i32(args[2]))) { |
| 242 | 546 | res = ENC_SYM_TRUE; | |
| 243 | } | ||
| 244 | } | ||
| 245 | 585 | return res; | |
| 246 | } | ||
| 247 | |||
| 248 | |||
| 249 | 2920 | lbm_value array_extension_buffer_append_u8(lbm_value *args, lbm_uint argn) { | |
| 250 | |||
| 251 | 2920 | lbm_value res = ENC_SYM_EERROR; | |
| 252 | 2920 | uint8_t *data = NULL; | |
| 253 | 2920 | lbm_uint d_size = 0; | |
| 254 | 2920 | bool be = false; | |
| 255 | 2920 | lbm_uint index = 0; | |
| 256 | |||
| 257 |
2/2✓ Branch 1 taken 2917 times.
✓ Branch 2 taken 3 times.
|
2920 | if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 258 |
2/2✓ Branch 2 taken 2881 times.
✓ Branch 3 taken 36 times.
|
2917 | if (buffer_append_bytes(data, d_size, be, index, 1, (lbm_uint)lbm_dec_as_u32(args[2]))) { |
| 259 | 2881 | res = ENC_SYM_TRUE; | |
| 260 | } | ||
| 261 | } | ||
| 262 | 2920 | return res; | |
| 263 | } | ||
| 264 | |||
| 265 | 156 | lbm_value array_extension_buffer_append_u16(lbm_value *args, lbm_uint argn) { | |
| 266 | |||
| 267 | 156 | lbm_value res = ENC_SYM_EERROR; | |
| 268 | 156 | uint8_t *data = NULL; | |
| 269 | 156 | lbm_uint d_size = 0; | |
| 270 | 156 | bool be = false; | |
| 271 | 156 | lbm_uint index = 0; | |
| 272 | |||
| 273 |
2/2✓ Branch 1 taken 153 times.
✓ Branch 2 taken 3 times.
|
156 | if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 274 |
2/2✓ Branch 2 taken 117 times.
✓ Branch 3 taken 36 times.
|
153 | if (buffer_append_bytes(data, d_size, be, index, 2, (lbm_uint)lbm_dec_as_u32(args[2]))) { |
| 275 | 117 | res = ENC_SYM_TRUE; | |
| 276 | } | ||
| 277 | } | ||
| 278 | 156 | return res; | |
| 279 | } | ||
| 280 | |||
| 281 | 405 | lbm_value array_extension_buffer_append_u24(lbm_value *args, lbm_uint argn) { | |
| 282 | |||
| 283 | 405 | lbm_value res = ENC_SYM_EERROR; | |
| 284 | 405 | uint8_t *data = NULL; | |
| 285 | 405 | lbm_uint d_size = 0; | |
| 286 | 405 | bool be = false; | |
| 287 | 405 | lbm_uint index = 0; | |
| 288 | |||
| 289 |
2/2✓ Branch 1 taken 402 times.
✓ Branch 2 taken 3 times.
|
405 | if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 290 |
2/2✓ Branch 2 taken 366 times.
✓ Branch 3 taken 36 times.
|
402 | if (buffer_append_bytes(data, d_size, be, index, 3, (lbm_uint)lbm_dec_as_u32(args[2]))) { |
| 291 | 366 | res = ENC_SYM_TRUE; | |
| 292 | } | ||
| 293 | } | ||
| 294 | 405 | return res; | |
| 295 | } | ||
| 296 | |||
| 297 | 588 | lbm_value array_extension_buffer_append_u32(lbm_value *args, lbm_uint argn) { | |
| 298 | |||
| 299 | 588 | lbm_value res = ENC_SYM_EERROR; | |
| 300 | 588 | uint8_t *data = NULL; | |
| 301 | 588 | lbm_uint d_size = 0; | |
| 302 | 588 | bool be = false; | |
| 303 | 588 | lbm_uint index = 0; | |
| 304 | |||
| 305 |
2/2✓ Branch 1 taken 585 times.
✓ Branch 2 taken 3 times.
|
588 | if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 306 |
2/2✓ Branch 2 taken 549 times.
✓ Branch 3 taken 36 times.
|
585 | if (buffer_append_bytes(data, d_size, be, index, 4, (lbm_uint)lbm_dec_as_u32(args[2]))) { |
| 307 | 549 | res = ENC_SYM_TRUE; | |
| 308 | } | ||
| 309 | } | ||
| 310 | 588 | return res; | |
| 311 | } | ||
| 312 | |||
| 313 | 405 | static lbm_uint float_to_u(float number) { | |
| 314 | // Set subnormal numbers to 0 as they are not handled properly | ||
| 315 | // using this method. | ||
| 316 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 390 times.
|
405 | if (fabsf(number) < 1.5e-38) { |
| 317 | 15 | number = 0.0; | |
| 318 | } | ||
| 319 | |||
| 320 | 405 | int e = 0; | |
| 321 | 405 | float sig = frexpf(number, &e); | |
| 322 | 405 | float sig_abs = fabsf(sig); | |
| 323 | 405 | uint32_t sig_i = 0; | |
| 324 | |||
| 325 |
2/2✓ Branch 0 taken 390 times.
✓ Branch 1 taken 15 times.
|
405 | if (sig_abs >= 0.5) { |
| 326 | 390 | sig_i = (uint32_t)((sig_abs - 0.5f) * 2.0f * 8388608.0f); | |
| 327 | 390 | e += 126; | |
| 328 | } | ||
| 329 | |||
| 330 | 405 | uint32_t res = (((uint32_t)e & 0xFFu) << 23) | (uint32_t)(sig_i & 0x7FFFFFu); | |
| 331 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 402 times.
|
405 | if (sig < 0) { |
| 332 | 3 | res |= 1U << 31; | |
| 333 | } | ||
| 334 | |||
| 335 | 405 | return res; | |
| 336 | } | ||
| 337 | |||
| 338 | 372 | static lbm_float u_to_float(uint32_t v) { | |
| 339 | |||
| 340 | 372 | int e = (v >> 23) & 0xFF; | |
| 341 | 372 | uint32_t sig_i = v & 0x7FFFFF; | |
| 342 | 372 | bool neg = v & (1U << 31); | |
| 343 | |||
| 344 | 372 | float sig = 0.0; | |
| 345 |
4/4✓ Branch 0 taken 60 times.
✓ Branch 1 taken 312 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 57 times.
|
372 | if (e != 0 || sig_i != 0) { |
| 346 | 315 | sig = (float)sig_i / (8388608.0f * 2.0f) + 0.5f; | |
| 347 | 315 | e -= 126; | |
| 348 | } | ||
| 349 | |||
| 350 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 366 times.
|
372 | if (neg) { |
| 351 | 6 | sig = -sig; | |
| 352 | } | ||
| 353 | |||
| 354 | 372 | return ldexpf(sig, e); | |
| 355 | } | ||
| 356 | |||
| 357 | 399 | lbm_value array_extension_buffer_append_f32(lbm_value *args, lbm_uint argn) { | |
| 358 | |||
| 359 | 399 | lbm_value res = ENC_SYM_EERROR; | |
| 360 | 399 | uint8_t *data = NULL; | |
| 361 | 399 | lbm_uint d_size = 0; | |
| 362 | 399 | bool be = false; | |
| 363 | 399 | lbm_uint index = 0; | |
| 364 | |||
| 365 |
2/2✓ Branch 1 taken 396 times.
✓ Branch 2 taken 3 times.
|
399 | if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 366 |
2/2✓ Branch 3 taken 360 times.
✓ Branch 4 taken 36 times.
|
396 | if (buffer_append_bytes(data, d_size, be, index, 4, (lbm_uint)float_to_u(lbm_dec_as_float(args[2])))) { |
| 367 | 360 | res = ENC_SYM_TRUE; | |
| 368 | } | ||
| 369 | } | ||
| 370 | 399 | return res; | |
| 371 | } | ||
| 372 | |||
| 373 | /* (buffer-get-i8 buffer index) */ | ||
| 374 | /* (buffer-get-i16 buffer index little-endian) */ | ||
| 375 | |||
| 376 | 7005 | 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) { | |
| 377 | 7005 | bool res = false; | |
| 378 | |||
| 379 | 7005 | *be=true; | |
| 380 | |||
| 381 |
3/3✓ Branch 0 taken 1629 times.
✓ Branch 1 taken 5367 times.
✓ Branch 2 taken 9 times.
|
7005 | switch(argn) { |
| 382 | 1629 | case 3: | |
| 383 |
1/2✓ Branch 1 taken 1629 times.
✗ Branch 2 not taken.
|
1629 | if (lbm_type_of(args[2]) == LBM_TYPE_SYMBOL && |
| 384 |
2/2✓ Branch 1 taken 1623 times.
✓ Branch 2 taken 6 times.
|
1629 | lbm_dec_sym(args[2]) == little_endian) { |
| 385 | 1623 | *be = false; | |
| 386 | } | ||
| 387 | /* fall through */ | ||
| 388 | case 2: { | ||
| 389 | 6996 | lbm_array_header_t *array = lbm_dec_array_r(args[0]); | |
| 390 |
4/4✓ Branch 0 taken 6990 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6978 times.
✓ Branch 3 taken 12 times.
|
13986 | if (array && |
| 391 | 6990 | lbm_is_number(args[1])) { | |
| 392 | 6978 | *a_size = array->size; | |
| 393 | 6978 | *a_data = (uint8_t*)array->data; | |
| 394 | 6978 | *index = lbm_dec_as_u32(args[1]); | |
| 395 | 6978 | res = true; | |
| 396 | } else { | ||
| 397 | 18 | *error = ENC_SYM_TERROR; | |
| 398 | } | ||
| 399 | } | ||
| 400 | } | ||
| 401 | 7005 | return res; | |
| 402 | } | ||
| 403 | |||
| 404 | 6978 | static bool buffer_get_uint(lbm_uint *r_value, uint8_t *data, lbm_uint d_size, bool be, lbm_uint index, lbm_uint nbytes) { | |
| 405 | |||
| 406 | 6978 | bool res = false; | |
| 407 | 6978 | lbm_uint last_index = index + (nbytes - 1); | |
| 408 | |||
| 409 |
2/2✓ Branch 0 taken 6186 times.
✓ Branch 1 taken 792 times.
|
6978 | if (last_index < d_size) { |
| 410 | 6186 | lbm_uint value = 0; | |
| 411 | 6186 | res = true; | |
| 412 |
4/5✓ Branch 0 taken 3387 times.
✓ Branch 1 taken 858 times.
✓ Branch 2 taken 366 times.
✓ Branch 3 taken 1575 times.
✗ Branch 4 not taken.
|
6186 | switch(nbytes) { |
| 413 | 3387 | case 1: | |
| 414 | 3387 | value = (lbm_uint)data[index]; | |
| 415 | 3387 | break; | |
| 416 | 858 | case 2: | |
| 417 |
2/2✓ Branch 0 taken 492 times.
✓ Branch 1 taken 366 times.
|
858 | if (be) { |
| 418 | 492 | value = | |
| 419 | 492 | (lbm_uint) data[index+1] | | |
| 420 | 492 | (lbm_uint) data[index] << 8; | |
| 421 | } else { | ||
| 422 | 366 | value = | |
| 423 | 366 | (lbm_uint) data[index] | | |
| 424 | 366 | (lbm_uint) data[index+1] << 8; | |
| 425 | } | ||
| 426 | 858 | break; | |
| 427 | 366 | case 3: | |
| 428 |
2/2✓ Branch 0 taken 183 times.
✓ Branch 1 taken 183 times.
|
366 | if (be) { |
| 429 | 183 | value = | |
| 430 | 183 | (lbm_uint) data[index+2] | | |
| 431 | 183 | (lbm_uint) data[index+1] << 8 | | |
| 432 | 183 | (lbm_uint) data[index] << 16; | |
| 433 | } else { | ||
| 434 | 183 | value = | |
| 435 | 183 | (lbm_uint) data[index] | | |
| 436 | 183 | (lbm_uint) data[index+1] << 8 | | |
| 437 | 183 | (lbm_uint) data[index+2] << 16; | |
| 438 | } | ||
| 439 | 366 | break; | |
| 440 | 1575 | case 4: | |
| 441 |
2/2✓ Branch 0 taken 1113 times.
✓ Branch 1 taken 462 times.
|
1575 | if (be) { |
| 442 | 1113 | value = | |
| 443 | 1113 | (uint32_t) data[index+3] | | |
| 444 | 1113 | (uint32_t) data[index+2] << 8 | | |
| 445 | 1113 | (uint32_t) data[index+1] << 16 | | |
| 446 | 1113 | (uint32_t) data[index] << 24; | |
| 447 | } else { | ||
| 448 | 462 | value = | |
| 449 | 462 | (uint32_t) data[index] | | |
| 450 | 462 | (uint32_t) data[index+1] << 8 | | |
| 451 | 462 | (uint32_t) data[index+2] << 16 | | |
| 452 | 462 | (uint32_t) data[index+3] << 24; | |
| 453 | } | ||
| 454 | 1575 | break; | |
| 455 | ✗ | default: | |
| 456 | ✗ | res = false; | |
| 457 | } | ||
| 458 | 6186 | *r_value = value; | |
| 459 | } | ||
| 460 | 6978 | return res; | |
| 461 | } | ||
| 462 | |||
| 463 | |||
| 464 | |||
| 465 | 1812 | lbm_value array_extension_buffer_get_i8(lbm_value *args, lbm_uint argn) { | |
| 466 | 1812 | lbm_value res = ENC_SYM_EERROR; | |
| 467 | 1812 | uint8_t *data = NULL; | |
| 468 | 1812 | lbm_uint d_size = 0; | |
| 469 | 1812 | bool be = false; | |
| 470 | 1812 | lbm_uint index = 0; | |
| 471 | 1812 | lbm_uint value = 0; | |
| 472 | |||
| 473 |
2/2✓ Branch 1 taken 1800 times.
✓ Branch 2 taken 12 times.
|
1812 | if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 474 |
2/2✓ Branch 1 taken 1692 times.
✓ Branch 2 taken 108 times.
|
1800 | if (buffer_get_uint(&value, data, d_size, be, index, 1)) { |
| 475 | 1692 | res =lbm_enc_i((int8_t)value); | |
| 476 | } | ||
| 477 | } | ||
| 478 | 1812 | return res; | |
| 479 | } | ||
| 480 | |||
| 481 | 909 | lbm_value array_extension_buffer_get_i16(lbm_value *args, lbm_uint argn) { | |
| 482 | 909 | lbm_value res = ENC_SYM_EERROR; | |
| 483 | 909 | uint8_t *data = NULL; | |
| 484 | 909 | lbm_uint d_size = 0; | |
| 485 | 909 | bool be = false; | |
| 486 | 909 | lbm_uint index = 0; | |
| 487 | 909 | lbm_uint value = 0; | |
| 488 | |||
| 489 |
1/2✓ Branch 1 taken 909 times.
✗ Branch 2 not taken.
|
909 | if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 490 |
2/2✓ Branch 1 taken 729 times.
✓ Branch 2 taken 180 times.
|
909 | if (buffer_get_uint(&value, data, d_size, be, index, 2)) { |
| 491 | 729 | res =lbm_enc_i((int16_t)value); | |
| 492 | } | ||
| 493 | } | ||
| 494 | 909 | return res; | |
| 495 | } | ||
| 496 | |||
| 497 | 657 | lbm_value array_extension_buffer_get_i32(lbm_value *args, lbm_uint argn) { | |
| 498 | 657 | lbm_value res = ENC_SYM_EERROR; | |
| 499 | 657 | uint8_t *data = NULL; | |
| 500 | 657 | lbm_uint d_size = 0; | |
| 501 | 657 | bool be = false; | |
| 502 | 657 | lbm_uint index = 0; | |
| 503 | 657 | lbm_uint value = 0; | |
| 504 | |||
| 505 |
2/2✓ Branch 1 taken 654 times.
✓ Branch 2 taken 3 times.
|
657 | if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 506 |
2/2✓ Branch 1 taken 546 times.
✓ Branch 2 taken 108 times.
|
654 | if (buffer_get_uint(&value, data, d_size, be, index, 4)) { |
| 507 | 546 | res =lbm_enc_i((int32_t)value); | |
| 508 | } | ||
| 509 | } | ||
| 510 | 657 | return res; | |
| 511 | } | ||
| 512 | |||
| 513 | 1806 | lbm_value array_extension_buffer_get_u8(lbm_value *args, lbm_uint argn) { | |
| 514 | 1806 | lbm_value res = ENC_SYM_EERROR; | |
| 515 | 1806 | uint8_t *data = NULL; | |
| 516 | 1806 | lbm_uint d_size = 0; | |
| 517 | 1806 | bool be = false; | |
| 518 | 1806 | lbm_uint index = 0; | |
| 519 | 1806 | lbm_uint value = 0; | |
| 520 | |||
| 521 |
2/2✓ Branch 1 taken 1803 times.
✓ Branch 2 taken 3 times.
|
1806 | if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 522 |
2/2✓ Branch 1 taken 1695 times.
✓ Branch 2 taken 108 times.
|
1803 | if (buffer_get_uint(&value, data, d_size, be, index, 1)) { |
| 523 | 1695 | res = lbm_enc_i((uint8_t)value); | |
| 524 | } | ||
| 525 | } | ||
| 526 | 1806 | return res; | |
| 527 | } | ||
| 528 | |||
| 529 | 168 | lbm_value array_extension_buffer_get_u16(lbm_value *args, lbm_uint argn) { | |
| 530 | 168 | lbm_value res = ENC_SYM_EERROR; | |
| 531 | 168 | uint8_t *data = NULL; | |
| 532 | 168 | lbm_uint d_size = 0; | |
| 533 | 168 | bool be = false; | |
| 534 | 168 | lbm_uint index = 0; | |
| 535 | 168 | lbm_uint value = 0; | |
| 536 | |||
| 537 |
2/2✓ Branch 1 taken 165 times.
✓ Branch 2 taken 3 times.
|
168 | if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 538 |
2/2✓ Branch 1 taken 129 times.
✓ Branch 2 taken 36 times.
|
165 | if (buffer_get_uint(&value, data, d_size, be, index, 2)) { |
| 539 | 129 | res = lbm_enc_i((uint16_t)value); | |
| 540 | } | ||
| 541 | } | ||
| 542 | 168 | return res; | |
| 543 | } | ||
| 544 | |||
| 545 | 477 | lbm_value array_extension_buffer_get_u24(lbm_value *args, lbm_uint argn) { | |
| 546 | 477 | lbm_value res = ENC_SYM_EERROR; | |
| 547 | 477 | uint8_t *data = NULL; | |
| 548 | 477 | lbm_uint d_size = 0; | |
| 549 | 477 | bool be = false; | |
| 550 | 477 | lbm_uint index = 0; | |
| 551 | 477 | lbm_uint value = 0; | |
| 552 | |||
| 553 |
2/2✓ Branch 1 taken 474 times.
✓ Branch 2 taken 3 times.
|
477 | if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 554 |
2/2✓ Branch 1 taken 366 times.
✓ Branch 2 taken 108 times.
|
474 | if (buffer_get_uint(&value, data, d_size, be, index, 3)) { |
| 555 | 366 | res = lbm_enc_i((int32_t)value); | |
| 556 | } | ||
| 557 | } | ||
| 558 | 477 | return res; | |
| 559 | } | ||
| 560 | |||
| 561 | 774 | lbm_value array_extension_buffer_get_u32(lbm_value *args, lbm_uint argn) { | |
| 562 | 774 | lbm_value res = ENC_SYM_EERROR; | |
| 563 | 774 | uint8_t *data = NULL; | |
| 564 | 774 | lbm_uint d_size = 0; | |
| 565 | 774 | bool be = false; | |
| 566 | 774 | lbm_uint index = 0; | |
| 567 | 774 | lbm_uint value = 0; | |
| 568 | |||
| 569 |
1/2✓ Branch 1 taken 774 times.
✗ Branch 2 not taken.
|
774 | if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 570 |
2/2✓ Branch 1 taken 666 times.
✓ Branch 2 taken 108 times.
|
774 | if (buffer_get_uint(&value, data, d_size, be, index, 4)) { |
| 571 | 666 | res = lbm_enc_u32((uint32_t)value); | |
| 572 | } | ||
| 573 | } | ||
| 574 | 774 | return res; | |
| 575 | } | ||
| 576 | |||
| 577 | 402 | lbm_value array_extension_buffer_get_f32(lbm_value *args, lbm_uint argn) { | |
| 578 | 402 | lbm_value res = ENC_SYM_EERROR; | |
| 579 | 402 | uint8_t *data = NULL; | |
| 580 | 402 | lbm_uint d_size = 0; | |
| 581 | 402 | bool be = false; | |
| 582 | 402 | lbm_uint index = 0; | |
| 583 | 402 | lbm_uint value = 0; | |
| 584 | |||
| 585 |
2/2✓ Branch 1 taken 399 times.
✓ Branch 2 taken 3 times.
|
402 | if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) { |
| 586 |
2/2✓ Branch 1 taken 363 times.
✓ Branch 2 taken 36 times.
|
399 | if (buffer_get_uint(&value, data, d_size, be, index, 4)) { |
| 587 | 363 | res = lbm_enc_float(u_to_float((uint32_t)value)); | |
| 588 | } | ||
| 589 | } | ||
| 590 | 402 | return res; | |
| 591 | } | ||
| 592 | |||
| 593 | 390 | lbm_value array_extension_buffer_length(lbm_value *args, lbm_uint argn) { | |
| 594 | 390 | lbm_value res = ENC_SYM_EERROR; | |
| 595 | lbm_array_header_t *array; | ||
| 596 |
4/4✓ Branch 0 taken 387 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 384 times.
✓ Branch 3 taken 3 times.
|
777 | if (argn == 1 && |
| 597 |
1/2✓ Branch 1 taken 384 times.
✗ Branch 2 not taken.
|
771 | (array = lbm_dec_array_r(args[0])) && |
| 598 | 384 | lbm_heap_array_valid(args[0])) { | |
| 599 | 384 | res = lbm_enc_i((lbm_int)array->size); | |
| 600 | } | ||
| 601 | 390 | return res; | |
| 602 | } | ||
| 603 | |||
| 604 | //TODO: Have to think about 32 vs 64 bit here | ||
| 605 | 882 | static lbm_value array_extensions_bufclear(lbm_value *args, lbm_uint argn) { | |
| 606 | 882 | lbm_value res = ENC_SYM_EERROR; | |
| 607 |
4/4✓ Branch 0 taken 843 times.
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 804 times.
✓ Branch 3 taken 39 times.
|
882 | if (argn >= 1 && argn <= 4) { |
| 608 | 804 | res = ENC_SYM_TERROR; | |
| 609 |
2/2✓ Branch 1 taken 798 times.
✓ Branch 2 taken 6 times.
|
804 | if (lbm_is_array_rw(args[0])) { |
| 610 | 798 | lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(args[0]); | |
| 611 | |||
| 612 | 798 | uint8_t clear_byte = 0; | |
| 613 |
2/2✓ Branch 0 taken 690 times.
✓ Branch 1 taken 108 times.
|
798 | if (argn >= 2) { |
| 614 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 687 times.
|
690 | if (!lbm_is_number(args[1])) { |
| 615 | 3 | return res; | |
| 616 | } | ||
| 617 | 687 | clear_byte = (uint8_t)lbm_dec_as_u32(args[1]); | |
| 618 | } | ||
| 619 | |||
| 620 | 795 | uint32_t start = 0; | |
| 621 |
2/2✓ Branch 0 taken 210 times.
✓ Branch 1 taken 585 times.
|
795 | if (argn >= 3) { |
| 622 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 207 times.
|
210 | if (!lbm_is_number(args[2])) { |
| 623 | 3 | return res; | |
| 624 | } | ||
| 625 | 207 | uint32_t start_new = lbm_dec_as_u32(args[2]); | |
| 626 |
2/2✓ Branch 0 taken 204 times.
✓ Branch 1 taken 3 times.
|
207 | if (start_new < array->size) { |
| 627 | 204 | start = start_new; | |
| 628 | } else { | ||
| 629 | 3 | return res; | |
| 630 | } | ||
| 631 | } | ||
| 632 | // Truncates size on 64 bit build | ||
| 633 | 789 | uint32_t len = (uint32_t)array->size - start; | |
| 634 |
2/2✓ Branch 0 taken 168 times.
✓ Branch 1 taken 621 times.
|
789 | if (argn >= 4) { |
| 635 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 165 times.
|
168 | if (!lbm_is_number(args[3])) { |
| 636 | 3 | return res; | |
| 637 | } | ||
| 638 | 165 | uint32_t len_new = lbm_dec_as_u32(args[3]); | |
| 639 |
1/2✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
|
165 | if (len_new <= len) { |
| 640 | 165 | len = len_new; | |
| 641 | } | ||
| 642 | } | ||
| 643 | |||
| 644 | 786 | memset((char*)array->data + start, clear_byte, len); | |
| 645 | 786 | res = ENC_SYM_TRUE; | |
| 646 | } | ||
| 647 | } | ||
| 648 | 870 | return res; | |
| 649 | } | ||
| 650 | |||
| 651 | 192 | static lbm_value array_extensions_bufcpy(lbm_value *args, lbm_uint argn) { | |
| 652 | 192 | lbm_value res = ENC_SYM_EERROR; | |
| 653 | |||
| 654 |
2/2✓ Branch 0 taken 153 times.
✓ Branch 1 taken 39 times.
|
192 | if (argn == 5) { |
| 655 | 153 | res = ENC_SYM_TERROR; | |
| 656 |
4/6✓ Branch 1 taken 81 times.
✓ Branch 2 taken 72 times.
✓ Branch 4 taken 81 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 81 times.
✗ Branch 7 not taken.
|
234 | if (lbm_is_array_rw(args[0]) && lbm_is_number(args[1]) && |
| 657 |
2/4✓ Branch 2 taken 81 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 81 times.
✗ Branch 6 not taken.
|
162 | lbm_is_array_r(args[2]) && lbm_is_number(args[3]) &&lbm_is_number(args[4])) { |
| 658 | 81 | lbm_array_header_t *array1 = (lbm_array_header_t *)lbm_car(args[0]); | |
| 659 | |||
| 660 | 81 | uint32_t start1 = lbm_dec_as_u32(args[1]); | |
| 661 | |||
| 662 | 81 | lbm_array_header_t *array2 = (lbm_array_header_t *)lbm_car(args[2]); | |
| 663 | |||
| 664 | 81 | uint32_t start2 = lbm_dec_as_u32(args[3]); | |
| 665 | 81 | uint32_t len = lbm_dec_as_u32(args[4]); | |
| 666 | |||
| 667 |
2/4✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 81 times.
✗ Branch 3 not taken.
|
81 | if (start1 < array1->size && start2 < array2->size) { |
| 668 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
|
81 | if (len > (array1->size - start1)) { |
| 669 | ✗ | len = ((uint32_t)array1->size - start1); | |
| 670 | } | ||
| 671 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
|
81 | if (len > (array2->size - start2)) { |
| 672 | ✗ | len = ((uint32_t)array2->size - start2); | |
| 673 | } | ||
| 674 | |||
| 675 | 81 | memcpy((char*)array1->data + start1, (char*)array2->data + start2, len); | |
| 676 | } | ||
| 677 | 81 | res = ENC_SYM_TRUE; | |
| 678 | } | ||
| 679 | } | ||
| 680 | 192 | return res; | |
| 681 | } | ||
| 682 | |||
| 683 | 228 | static lbm_value array_extensions_bufset_bit(lbm_value *args, lbm_uint argn) { | |
| 684 | 228 | lbm_value res = ENC_SYM_EERROR; | |
| 685 | |||
| 686 |
2/2✓ Branch 0 taken 117 times.
✓ Branch 1 taken 111 times.
|
228 | if (argn == 3) { |
| 687 | 117 | res = ENC_SYM_TERROR; | |
| 688 |
4/4✓ Branch 1 taken 78 times.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 75 times.
✓ Branch 4 taken 3 times.
|
195 | if (lbm_is_array_rw(args[0]) && |
| 689 |
2/2✓ Branch 2 taken 72 times.
✓ Branch 3 taken 3 times.
|
153 | lbm_is_number(args[1]) && lbm_is_number(args[2])) { |
| 690 | 72 | lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(args[0]); | |
| 691 | |||
| 692 | 72 | unsigned int pos = lbm_dec_as_u32(args[1]); | |
| 693 | 72 | unsigned int bit = lbm_dec_as_u32(args[2]) ? 1 : 0; | |
| 694 | |||
| 695 | 72 | unsigned int bytepos = pos / 8; | |
| 696 | |||
| 697 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | if (bytepos < array->size) { |
| 698 | 72 | unsigned int bitpos = pos % 8; | |
| 699 | 72 | ((uint8_t*)array->data)[bytepos] &= (uint8_t)~(1 << bitpos); | |
| 700 | 72 | ((uint8_t*)array->data)[bytepos] |= (uint8_t)(bit << bitpos); | |
| 701 | } | ||
| 702 | |||
| 703 | 72 | res = ENC_SYM_TRUE; | |
| 704 | } | ||
| 705 | } | ||
| 706 | 228 | return res; | |
| 707 | } | ||
| 708 | |||
| 709 | /* extract/set sequence of bits at arbitrary bit positon | ||
| 710 | within a byte-array. Alternatively allowing DBC bit order | ||
| 711 | with using the 'dbc symbol argument. | ||
| 712 | */ | ||
| 713 | |||
| 714 | 45846 | static lbm_uint dbc_bit_pos(lbm_uint pos) { | |
| 715 | 45846 | return (7 - (pos % 8)) + (pos / 8) * 8; | |
| 716 | } | ||
| 717 | |||
| 718 | 36483 | static int64_t sign_extend64(uint64_t v, lbm_uint len) { | |
| 719 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36483 times.
|
36483 | if (len >= 64) return (int64_t)v; |
| 720 | 36483 | uint64_t sign_bit = (uint64_t)1 << (len - 1); | |
| 721 | 36483 | return (int64_t)((v ^ sign_bit) - sign_bit); | |
| 722 | } | ||
| 723 | |||
| 724 | 145974 | static void decode_bits_flags(lbm_value *args, lbm_uint argn, lbm_uint fixed_argn, bool *be, bool *dbc) { | |
| 725 | 145974 | *be = true; | |
| 726 | 145974 | *dbc = false; | |
| 727 |
2/2✓ Branch 0 taken 139452 times.
✓ Branch 1 taken 145974 times.
|
285426 | for (lbm_uint i = fixed_argn; i < argn; i ++) { |
| 728 |
1/2✓ Branch 1 taken 139452 times.
✗ Branch 2 not taken.
|
139452 | if (lbm_type_of(args[i]) == LBM_TYPE_SYMBOL) { |
| 729 | 139452 | lbm_uint s = lbm_dec_sym(args[i]); | |
| 730 |
2/2✓ Branch 0 taken 37446 times.
✓ Branch 1 taken 102006 times.
|
139452 | if (s == little_endian) *be = false; |
| 731 |
2/2✓ Branch 0 taken 37440 times.
✓ Branch 1 taken 64566 times.
|
102006 | else if (s == big_endian) *be = true; |
| 732 |
1/2✓ Branch 0 taken 64566 times.
✗ Branch 1 not taken.
|
64566 | else if (s == dbc_sym) *dbc = true; |
| 733 | } | ||
| 734 | } | ||
| 735 | 145974 | } | |
| 736 | |||
| 737 | 72981 | static bool decode_bits_get_args(lbm_value *error, lbm_value *args, lbm_uint argn, | |
| 738 | lbm_uint *pos, lbm_uint *len, bool *be, bool *dbc, | ||
| 739 | lbm_uint *a_size, uint8_t **a_data) { | ||
| 740 | 72981 | *error = ENC_SYM_EERROR; | |
| 741 |
2/4✓ Branch 0 taken 72981 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 72981 times.
|
72981 | if (argn < 3 || argn > 5) return false; |
| 742 | 72981 | lbm_array_header_t *array = lbm_dec_array_r(args[0]); | |
| 743 |
3/6✓ Branch 0 taken 72981 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 72981 times.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 72981 times.
|
72981 | if (!(array && lbm_is_number(args[1]) && lbm_is_number(args[2]))) { |
| 744 | ✗ | *error = ENC_SYM_TERROR; | |
| 745 | ✗ | return false; | |
| 746 | } | ||
| 747 | 72981 | *a_size = array->size; | |
| 748 | 72981 | *a_data = (uint8_t*)array->data; | |
| 749 | 72981 | *pos = lbm_dec_as_u32(args[1]); | |
| 750 | 72981 | *len = lbm_dec_as_u32(args[2]); | |
| 751 | 72981 | decode_bits_flags(args, argn, 3, be, dbc); | |
| 752 | 72981 | return true; | |
| 753 | } | ||
| 754 | |||
| 755 | 72975 | static bool decode_bits_set_args(lbm_value *error, lbm_value *args, lbm_uint argn, | |
| 756 | lbm_uint *pos, lbm_uint *len, bool *be, bool *dbc, | ||
| 757 | lbm_uint *a_size, uint8_t **a_data) { | ||
| 758 | 72975 | *error = ENC_SYM_EERROR; | |
| 759 |
2/4✓ Branch 0 taken 72975 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 72975 times.
|
72975 | if (argn < 4 || argn > 6) return false; |
| 760 | 72975 | lbm_array_header_t *array = lbm_dec_array_rw(args[0]); | |
| 761 |
4/8✓ Branch 0 taken 72975 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 72975 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 72975 times.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 72975 times.
|
72975 | if (!(array && lbm_is_number(args[1]) && lbm_is_number(args[2]) && lbm_is_number(args[3]))) { |
| 762 | ✗ | *error = ENC_SYM_TERROR; | |
| 763 | ✗ | return false; | |
| 764 | } | ||
| 765 | 72975 | *a_size = array->size; | |
| 766 | 72975 | *a_data = (uint8_t*)array->data; | |
| 767 | 72975 | *pos = lbm_dec_as_u32(args[1]); | |
| 768 | 72975 | *len = lbm_dec_as_u32(args[2]); | |
| 769 | 72975 | decode_bits_flags(args, argn, 4, be, dbc); | |
| 770 | 72975 | return true; | |
| 771 | } | ||
| 772 | |||
| 773 | 72984 | static bool bits_insert(uint8_t *data, lbm_uint d_size, lbm_uint pos, lbm_uint len, | |
| 774 | bool be, bool dbc, uint64_t number) { | ||
| 775 |
2/4✓ Branch 0 taken 72984 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 72984 times.
|
72984 | if (len == 0 || len > 64) return false; |
| 776 |
4/4✓ Branch 0 taken 32283 times.
✓ Branch 1 taken 40701 times.
✓ Branch 2 taken 22923 times.
✓ Branch 3 taken 9360 times.
|
72984 | if (dbc && be) pos = dbc_bit_pos(pos); |
| 777 |
2/2✓ Branch 0 taken 54261 times.
✓ Branch 1 taken 18723 times.
|
72984 | if (be) number <<= (64 - len); |
| 778 | |||
| 779 | 72984 | lbm_uint bitcnt = 0, remaining = len; | |
| 780 |
2/2✓ Branch 0 taken 237561 times.
✓ Branch 1 taken 72981 times.
|
310542 | while (remaining > 0) { |
| 781 | 237561 | lbm_uint bytepos = (pos + bitcnt) / 8; | |
| 782 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 237558 times.
|
237561 | if (bytepos >= d_size) return false; |
| 783 | 237558 | lbm_uint shift = (pos + bitcnt) % 8; | |
| 784 | 237558 | lbm_uint bits = 8 - shift; | |
| 785 |
2/2✓ Branch 0 taken 62034 times.
✓ Branch 1 taken 175524 times.
|
237558 | if (bits > remaining) bits = remaining; |
| 786 | |||
| 787 | uint8_t bval, mask; | ||
| 788 |
2/2✓ Branch 0 taken 175818 times.
✓ Branch 1 taken 61740 times.
|
237558 | if (be) { |
| 789 | 175818 | bval = (uint8_t)((number >> (64 - bits)) << (8 - bits - shift)); | |
| 790 | 175818 | mask = (uint8_t)(~(0xFFu << (8 - bits - shift)) | (0xFFu << (8 - shift))); | |
| 791 | 175818 | number <<= bits; | |
| 792 | } else { | ||
| 793 | 61740 | bval = (uint8_t)(number << shift); | |
| 794 | 61740 | mask = (uint8_t)(~(0xFFu >> (8 - bits - shift)) | (0xFFu >> (8 - shift))); | |
| 795 | 61740 | number >>= bits; | |
| 796 | } | ||
| 797 | 237558 | data[bytepos] = (uint8_t)((data[bytepos] & mask) | bval); | |
| 798 | 237558 | bitcnt += bits; | |
| 799 | 237558 | remaining -= bits; | |
| 800 | } | ||
| 801 | 72981 | return true; | |
| 802 | } | ||
| 803 | |||
| 804 | 72990 | static bool bits_extract(const uint8_t *data, lbm_uint d_size, lbm_uint pos, lbm_uint len, | |
| 805 | bool be, bool dbc, uint64_t *out) { | ||
| 806 |
2/4✓ Branch 0 taken 72990 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 72990 times.
|
72990 | if (len == 0 || len > 64) return false; |
| 807 |
4/4✓ Branch 0 taken 32283 times.
✓ Branch 1 taken 40707 times.
✓ Branch 2 taken 22923 times.
✓ Branch 3 taken 9360 times.
|
72990 | if (dbc && be) pos = dbc_bit_pos(pos); |
| 808 | |||
| 809 | 72990 | uint64_t res = 0; | |
| 810 | 72990 | lbm_uint bitcnt = 0, remaining = len; | |
| 811 |
2/2✓ Branch 0 taken 237582 times.
✓ Branch 1 taken 72987 times.
|
310569 | while (remaining > 0) { |
| 812 | 237582 | lbm_uint bytepos = (pos + bitcnt) / 8; | |
| 813 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 237579 times.
|
237582 | if (bytepos >= d_size) return false; |
| 814 | 237579 | lbm_uint shift = (pos + bitcnt) % 8; | |
| 815 | 237579 | lbm_uint bits = 8 - shift; | |
| 816 |
2/2✓ Branch 0 taken 62040 times.
✓ Branch 1 taken 175539 times.
|
237579 | if (bits > remaining) bits = remaining; |
| 817 | |||
| 818 |
2/2✓ Branch 0 taken 175839 times.
✓ Branch 1 taken 61740 times.
|
237579 | if (be) { |
| 819 | 175839 | uint8_t bval = (uint8_t)(data[bytepos] & (0xFFu >> shift)); | |
| 820 | 175839 | res = (res + bval) << (bits + shift); | |
| 821 | } else { | ||
| 822 | 61740 | uint8_t mask = (uint8_t)~(0xFFu << bits); | |
| 823 | 61740 | uint8_t bval = (uint8_t)((data[bytepos] >> shift) & mask); | |
| 824 | 61740 | res |= ((uint64_t)bval) << bitcnt; | |
| 825 | } | ||
| 826 | 237579 | bitcnt += bits; | |
| 827 | 237579 | remaining -= bits; | |
| 828 | } | ||
| 829 |
2/2✓ Branch 0 taken 54264 times.
✓ Branch 1 taken 18723 times.
|
72987 | if (be) res >>= 8; |
| 830 | 72987 | *out = res; | |
| 831 | 72987 | return true; | |
| 832 | } | ||
| 833 | |||
| 834 | // Number of bits that fit in a u/i type. | ||
| 835 | #define BITS_IMMEDIATE_SAFE (((lbm_uint)sizeof(lbm_uint) * 8) - LBM_VAL_SHIFT) | ||
| 836 | |||
| 837 | 36495 | static lbm_value array_extensions_bufset_bits_u(lbm_value *args, lbm_uint argn) { | |
| 838 | 36495 | lbm_value res = ENC_SYM_EERROR; | |
| 839 | lbm_uint pos, len, d_size; uint8_t *data; bool be, dbc; | ||
| 840 |
1/2✓ Branch 1 taken 36495 times.
✗ Branch 2 not taken.
|
36495 | if (decode_bits_set_args(&res, args, argn, &pos, &len, &be, &dbc, &d_size, &data)) { |
| 841 | 72990 | res = bits_insert(data, d_size, pos, len, be, dbc, | |
| 842 |
2/2✓ Branch 1 taken 36492 times.
✓ Branch 2 taken 3 times.
|
72990 | lbm_dec_as_u64(args[3])) ? ENC_SYM_TRUE : ENC_SYM_EERROR; |
| 843 | } | ||
| 844 | 36495 | return res; | |
| 845 | } | ||
| 846 | |||
| 847 | 36480 | static lbm_value array_extensions_bufset_bits_i(lbm_value *args, lbm_uint argn) { | |
| 848 | 36480 | lbm_value res = ENC_SYM_EERROR; | |
| 849 | lbm_uint pos, len, d_size; uint8_t *data; bool be, dbc; | ||
| 850 |
1/2✓ Branch 1 taken 36480 times.
✗ Branch 2 not taken.
|
36480 | if (decode_bits_set_args(&res, args, argn, &pos, &len, &be, &dbc, &d_size, &data)) { |
| 851 | 72960 | res = bits_insert(data, d_size, pos, len, be, dbc, | |
| 852 |
1/2✓ Branch 1 taken 36480 times.
✗ Branch 2 not taken.
|
72960 | (uint64_t)lbm_dec_as_i64(args[3])) ? ENC_SYM_TRUE : ENC_SYM_EERROR; |
| 853 | } | ||
| 854 | 36480 | return res; | |
| 855 | } | ||
| 856 | |||
| 857 | 36498 | static lbm_value array_extensions_bufget_bits_u(lbm_value *args, lbm_uint argn) { | |
| 858 | 36498 | lbm_value res = ENC_SYM_EERROR; | |
| 859 | lbm_uint pos, len, d_size; uint8_t *data; bool be, dbc; uint64_t raw; | ||
| 860 |
3/4✓ Branch 1 taken 36498 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36495 times.
✓ Branch 4 taken 3 times.
|
72996 | if (decode_bits_get_args(&res, args, argn, &pos, &len, &be, &dbc, &d_size, &data) && |
| 861 | 36498 | bits_extract(data, d_size, pos, len, be, dbc, &raw)) { | |
| 862 |
2/2✓ Branch 0 taken 26781 times.
✓ Branch 1 taken 9714 times.
|
36495 | if (len <= BITS_IMMEDIATE_SAFE) res = lbm_enc_u((lbm_uint)raw); |
| 863 |
2/2✓ Branch 0 taken 3111 times.
✓ Branch 1 taken 6603 times.
|
9714 | else if (len <= 32) res = lbm_enc_u32((uint32_t)raw); |
| 864 | 6603 | else res = lbm_enc_u64(raw); | |
| 865 | } | ||
| 866 | 36498 | return res; | |
| 867 | } | ||
| 868 | |||
| 869 | 36483 | static lbm_value array_extensions_bufget_bits_i(lbm_value *args, lbm_uint argn) { | |
| 870 | 36483 | lbm_value res = ENC_SYM_EERROR; | |
| 871 | lbm_uint pos, len, d_size; uint8_t *data; bool be, dbc; uint64_t raw; | ||
| 872 |
2/4✓ Branch 1 taken 36483 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36483 times.
✗ Branch 4 not taken.
|
72966 | if (decode_bits_get_args(&res, args, argn, &pos, &len, &be, &dbc, &d_size, &data) && |
| 873 | 36483 | bits_extract(data, d_size, pos, len, be, dbc, &raw)) { | |
| 874 | 36483 | int64_t sv = sign_extend64(raw, len); | |
| 875 |
2/2✓ Branch 0 taken 26775 times.
✓ Branch 1 taken 9708 times.
|
36483 | if (len <= BITS_IMMEDIATE_SAFE) res = lbm_enc_i((lbm_int)sv); |
| 876 |
2/2✓ Branch 0 taken 3108 times.
✓ Branch 1 taken 6600 times.
|
9708 | else if (len <= 32) res = lbm_enc_i32((int32_t)sv); |
| 877 | 6600 | else res = lbm_enc_i64(sv); | |
| 878 | } | ||
| 879 | 36483 | return res; | |
| 880 | } | ||
| 881 | |||
| 882 | 9 | static bool decode_bits_f32_get_args(lbm_value *error, lbm_value *args, lbm_uint argn, | |
| 883 | lbm_uint *pos, bool *be, bool *dbc, | ||
| 884 | lbm_uint *a_size, uint8_t **a_data) { | ||
| 885 | 9 | *error = ENC_SYM_EERROR; | |
| 886 |
2/4✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
|
9 | if (argn < 2 || argn > 4) return false; |
| 887 | 9 | lbm_array_header_t *array = lbm_dec_array_r(args[0]); | |
| 888 |
2/4✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
|
9 | if (!(array && lbm_is_number(args[1]))) { |
| 889 | ✗ | *error = ENC_SYM_TERROR; | |
| 890 | ✗ | return false; | |
| 891 | } | ||
| 892 | 9 | *a_size = array->size; | |
| 893 | 9 | *a_data = (uint8_t*)array->data; | |
| 894 | 9 | *pos = lbm_dec_as_u32(args[1]); | |
| 895 | 9 | decode_bits_flags(args, argn, 2, be, dbc); | |
| 896 | 9 | return true; | |
| 897 | } | ||
| 898 | |||
| 899 | 9 | static bool decode_bits_f32_set_args(lbm_value *error, lbm_value *args, lbm_uint argn, | |
| 900 | lbm_uint *pos, bool *be, bool *dbc, | ||
| 901 | lbm_uint *a_size, uint8_t **a_data) { | ||
| 902 | 9 | *error = ENC_SYM_EERROR; | |
| 903 |
2/4✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
|
9 | if (argn < 3 || argn > 5) return false; |
| 904 | 9 | lbm_array_header_t *array = lbm_dec_array_rw(args[0]); | |
| 905 |
3/6✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 9 times.
|
9 | if (!(array && lbm_is_number(args[1]) && lbm_is_number(args[2]))) { |
| 906 | ✗ | *error = ENC_SYM_TERROR; | |
| 907 | ✗ | return false; | |
| 908 | } | ||
| 909 | 9 | *a_size = array->size; | |
| 910 | 9 | *a_data = (uint8_t*)array->data; | |
| 911 | 9 | *pos = lbm_dec_as_u32(args[1]); | |
| 912 | 9 | decode_bits_flags(args, argn, 3, be, dbc); | |
| 913 | 9 | return true; | |
| 914 | } | ||
| 915 | |||
| 916 | 9 | static lbm_value array_extensions_bufset_bits_f32(lbm_value *args, lbm_uint argn) { | |
| 917 | 9 | lbm_value res = ENC_SYM_EERROR; | |
| 918 | lbm_uint pos, d_size; uint8_t *data; bool be, dbc; | ||
| 919 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | if (decode_bits_f32_set_args(&res, args, argn, &pos, &be, &dbc, &d_size, &data)) { |
| 920 | 18 | res = bits_insert(data, d_size, pos, 32, be, dbc, | |
| 921 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | (uint64_t)float_to_u(lbm_dec_as_float(args[2]))) ? ENC_SYM_TRUE : ENC_SYM_EERROR; |
| 922 | } | ||
| 923 | 9 | return res; | |
| 924 | } | ||
| 925 | |||
| 926 | 9 | static lbm_value array_extensions_bufget_bits_f32(lbm_value *args, lbm_uint argn) { | |
| 927 | 9 | lbm_value res = ENC_SYM_EERROR; | |
| 928 | lbm_uint pos, d_size; uint8_t *data; bool be, dbc; uint64_t raw; | ||
| 929 |
2/4✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
|
18 | if (decode_bits_f32_get_args(&res, args, argn, &pos, &be, &dbc, &d_size, &data) && |
| 930 | 9 | bits_extract(data, d_size, pos, 32, be, dbc, &raw)) { | |
| 931 | 9 | res = lbm_enc_float(u_to_float((uint32_t)raw)); | |
| 932 | } | ||
| 933 | 9 | return res; | |
| 934 | } | ||
| 935 |