GCC Code Coverage Report


Directory: ../src/
File: extensions/dsp_extensions.c
Date: 2026-10-16 14:49:47
Coverage Exec Excl Total
Lines: 94.1% 305 0 324
Functions: 100.0% 14 0 14
Branches: 67.8% 137 0 202

Line Branch Exec Source
1 /*
2 Copyright 2025 Joel Svensson svenssonjoel@yahoo.se
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "extensions.h"
19 #include "lbm_utils.h"
20
21 #include <math.h>
22 #include <string.h>
23 #include <stdio.h>
24
25 #ifdef LBM_OPT_DSP_EXTENSIONS_SIZE
26 #pragma GCC optimize ("-Os")
27 #endif
28 #ifdef LBM_OPT_DSP_EXTENSIONS_SIZE_AGGRESSIVE
29 #pragma GCC optimize ("-Oz")
30 #endif
31
32 #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
33 #define LBM_SYSTEM_LITTLE_ENDIAN true
34 #else
35 #define LBM_SYSTEM_LITTLE_ENDIAN false
36 #endif
37
38
39 static lbm_uint sym_inverse = 0;
40 static lbm_uint sym_big_endian = 0;
41 static lbm_uint sym_little_endian = 0;
42
43 1278 static inline uint32_t byte_order_swap(uint32_t w) {
44 1278 uint32_t r = w;
45 1278 uint8_t *bytes = (uint8_t*)&r;
46 1278 uint8_t tmp = bytes[0];
47 1278 bytes[0] = bytes[3];
48 1278 bytes[3] = tmp;
49 1278 tmp = bytes[1];
50 1278 bytes[1] = bytes[2];
51 1278 bytes[2] = tmp;
52
53 1278 return r;
54 }
55
56 486 static inline float read_float(float *ptr, bool swap) {
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 486 times.
486 if (swap) {
58 union { uint32_t u; float f; } pun;
59 ✗ pun.f = *ptr;
60 ✗ uint32_t swapped = byte_order_swap(pun.u);
61 ✗ pun.u = swapped;
62 ✗ return pun.f;
63 }
64 486 return *ptr;
65 }
66
67
68 126 static inline void write_float(float *ptr, float value, bool swap) {
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if (swap) {
70 union { uint32_t u; float f; } pun;
71 ✗ pun.f = value;
72 ✗ uint32_t swapped = byte_order_swap(pun.u);
73 ✗ pun.u = swapped;
74 ✗ *ptr = pun.f;
75 } else {
76 126 *ptr = value;
77 }
78 126 }
79
80
81 3 static void lbm_corr(float *s1,
82 lbm_uint s1_len,
83 float *s2,
84 lbm_uint s2_len,
85 float *output,
86 lbm_uint output_len,
87 bool swap_byte_order) {
88
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 3 times.
24 for (int n = 0; n < (int)output_len; n++) {
89 21 float sum = 0.0f;
90
91
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 21 times.
84 for (int k = 0; k < (int)s2_len; k++) {
92 63 int j = n + k;
93
3/4
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 27 times.
63 if (j >= 0 && j < (int)s1_len) {
94 36 sum +=
95 36 read_float(&s1[j], swap_byte_order) *
96 36 read_float(&s2[k], swap_byte_order);
97 }
98 }
99 21 write_float(&output[n], sum, swap_byte_order);
100 }
101 3 }
102
103
104 3 static void lbm_complex_corr(float *s1_re,
105 float *s1_im,
106 lbm_uint s1_len,
107 float *s2_re,
108 float *s2_im,
109 lbm_uint s2_len,
110 float *output_re,
111 float *output_im,
112 lbm_uint output_len,
113 bool swap_byte_order) {
114
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 3 times.
24 for (int n = 0; n < (int)output_len; n++) {
115 21 float sum_re = 0.0f;
116 21 float sum_im = 0.0f;
117
118
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 21 times.
84 for (int k = 0; k < (int)s2_len; k++) {
119 63 int j = n + k;
120
3/4
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 27 times.
63 if (j >= 0 && j < (int)s1_len) {
121 36 float x_r = read_float(&s1_re[j], swap_byte_order);
122 36 float x_i = read_float(&s1_im[j], swap_byte_order);
123 36 float h_r = read_float(&s2_re[k], swap_byte_order);
124 36 float h_i = read_float(&s2_im[k], swap_byte_order);
125 // Note conjugate
126 36 sum_re += x_r * h_r + x_i * h_i; // ac + bd
127 36 sum_im += x_i * h_r - x_r * h_i; // bc - ad
128 }
129 }
130 21 write_float(&output_re[n], sum_re, swap_byte_order);
131 21 write_float(&output_im[n], sum_im, swap_byte_order);
132 }
133 3 }
134
135 3 static lbm_value ext_correlate(lbm_value *args, lbm_uint argn) {
136 3 lbm_value r = ENC_SYM_TERROR;
137
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
6 if ((argn == 2 || argn == 3) &&
138
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 lbm_is_array_r(args[0]) &&
139 3 lbm_is_array_r(args[1])) {
140
141 3 bool be = true;
142
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6 if (argn == 3 &&
143 3 lbm_is_symbol(args[2])) {
144
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (lbm_dec_sym(args[2]) == sym_little_endian) {
145 3 be = false;
146 }
147 }
148 3 bool swap_byte_order =
149 (be && LBM_SYSTEM_LITTLE_ENDIAN) ||
150 (!be && !LBM_SYSTEM_LITTLE_ENDIAN);
151
152 3 lbm_array_header_t *s1_arr = lbm_dec_array_r(args[0]);
153 3 lbm_array_header_t *s2_arr = lbm_dec_array_r(args[1]);
154 // The input arrays have been checked with lbm_is_array_r.
155 // If there is no way to create a byte array from a null pointer,
156 // then there is no way that these arrays can be null.
157 #ifdef __INFER__
158 __infer_assume(s1_arr != NULL);
159 __infer_assume(s2_arr != NULL);
160 #endif
161
162 3 lbm_uint s1_len = s1_arr->size / 4;
163 3 lbm_uint s2_len = s2_arr->size / 4;
164
165 // cppcheck-suppress invalidPointerCast
166 3 float *s1_data = (float*)s1_arr->data;
167 // cppcheck-suppress invalidPointerCast
168 3 float *s2_data = (float*)s2_arr->data;
169
170 lbm_value output;
171
172 3 lbm_uint out_len = s1_len + s2_len - 1;
173
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (lbm_heap_allocate_array(&output, out_len * sizeof(float))) {
174 3 lbm_array_header_t *out_arr = lbm_dec_array_r(output);
175 // cppcheck-suppress invalidPointerCast
176 3 float *out_data = (float*)out_arr->data;
177
178 3 lbm_corr(s1_data,
179 s1_len,
180 s2_data,
181 s2_len,
182 out_data,
183 out_len,
184 swap_byte_order);
185 3 r = output;
186 } else {
187 ✗ r = ENC_SYM_MERROR;
188 }
189 }
190 3 return r;
191 }
192
193 3 static lbm_value ext_complex_correlate(lbm_value *args, lbm_uint argn) {
194 3 lbm_value r = ENC_SYM_TERROR;
195
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
6 if ((argn == 4 || argn == 5) && // Allow only 4 or 5 args
196
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 lbm_is_array_r(args[0]) &&
197
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 lbm_is_array_r(args[1]) &&
198
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 lbm_is_array_r(args[2]) &&
199 3 lbm_is_array_r(args[3])) {
200
201 3 bool be = true;
202
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6 if (argn == 5 &&
203 3 lbm_is_symbol(args[4])) {
204
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (lbm_dec_sym(args[4]) == sym_little_endian) {
205 3 be = false;
206 }
207 }
208 3 bool swap_byte_order =
209 (be && LBM_SYSTEM_LITTLE_ENDIAN) ||
210 (!be && !LBM_SYSTEM_LITTLE_ENDIAN);
211
212 3 lbm_array_header_t *s1_re_arr = lbm_dec_array_r(args[0]);
213 3 lbm_array_header_t *s1_im_arr = lbm_dec_array_r(args[1]);
214 3 lbm_array_header_t *s2_re_arr = lbm_dec_array_r(args[2]);
215 3 lbm_array_header_t *s2_im_arr = lbm_dec_array_r(args[3]);
216
217
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 3 times.
3 if (!s1_re_arr || !s1_im_arr || !s2_re_arr || !s2_im_arr) {
218 ✗ return ENC_SYM_TERROR;
219 }
220
221
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (s1_re_arr->size != s1_im_arr->size ||
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 s2_re_arr->size != s2_im_arr->size) {
223 ✗ return ENC_SYM_TERROR;
224 }
225
226 3 lbm_uint s1_len = s1_re_arr->size / sizeof(float);
227 3 lbm_uint s2_len = s2_re_arr->size / sizeof(float);
228
229 // cppcheck-suppress invalidPointerCast
230 3 float *s1_re_data = (float*)s1_re_arr->data;
231 // cppcheck-suppress invalidPointerCast
232 3 float *s1_im_data = (float*)s1_im_arr->data;
233 // cppcheck-suppress invalidPointerCast
234 3 float *s2_re_data = (float*)s2_re_arr->data;
235 // cppcheck-suppress invalidPointerCast
236 3 float *s2_im_data = (float*)s2_im_arr->data;
237
238 lbm_value output_re;
239 lbm_value output_im;
240
241 3 lbm_uint out_len = s1_len + s2_len - 1;
242 3 lbm_value r_cons = lbm_cons(ENC_SYM_NIL, ENC_SYM_NIL);
243
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6 if (r_cons != ENC_SYM_MERROR &&
244
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 lbm_heap_allocate_array(&output_re, out_len * sizeof(float)) &&
245 3 lbm_heap_allocate_array(&output_im, out_len * sizeof(float))) {
246 3 lbm_array_header_t *out_re_arr = lbm_dec_array_r(output_re);
247 3 lbm_array_header_t *out_im_arr = lbm_dec_array_r(output_im);
248 // cppcheck-suppress invalidPointerCast
249 3 float *out_re_data = (float*)out_re_arr->data;
250 // cppcheck-suppress invalidPointerCast
251 3 float *out_im_data = (float*)out_im_arr->data;
252
253 3 lbm_complex_corr(s1_re_data,
254 s1_im_data,
255 s1_len,
256 s2_re_data,
257 s2_im_data,
258 s2_len,
259 out_re_data,
260 out_im_data,
261 out_len,
262 swap_byte_order);
263 3 lbm_set_car_and_cdr(r_cons, output_re, output_im);
264 3 r = r_cons;
265 } else {
266 ✗ r = ENC_SYM_MERROR;
267 }
268 }
269 3 return r;
270 }
271
272
273 3 static void lbm_convolve(float *signal,
274 lbm_uint signal_len,
275 float *filter,
276 lbm_uint filter_len,
277 float *output,
278 lbm_uint output_len,
279 bool swap_byte_order) {
280
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 3 times.
24 for (int n = 0; n < (int)output_len; n++) {
281 21 float sum = 0.0f;
282
283
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 21 times.
84 for (int k = 0; k < (int)filter_len; k++) {
284 63 int j = n - k;
285
4/4
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 45 times.
✓ Branch 3 taken 9 times.
63 if (j >= 0 && j < (int)signal_len) {
286 45 sum +=
287 45 read_float(&signal[j], swap_byte_order) *
288 45 read_float(&filter[k], swap_byte_order);
289 }
290 }
291 21 write_float(&output[n], sum, swap_byte_order);
292 }
293 3 }
294
295
296 3 void lbm_complex_convolve(float *signal_re,
297 float *signal_im,
298 unsigned int signal_len,
299 float *filter_re,
300 float *filter_im,
301 unsigned int filter_len,
302 float *output_re,
303 float *output_im,
304 unsigned int output_len,
305 bool swap_byte_order) {
306
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 3 times.
24 for (int n = 0; n < (int)output_len; n++) {
307 21 float real_sum = 0.0f;
308 21 float imag_sum = 0.0f;
309
310
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 21 times.
84 for (int k = 0; k < (int)filter_len; k++) {
311 63 int j = n - k;
312
4/4
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 45 times.
✓ Branch 3 taken 9 times.
63 if (j >= 0 && j < (int)signal_len) {
313 45 float x_r = read_float(&signal_re[j], swap_byte_order);
314 45 float x_i = read_float(&signal_im[j], swap_byte_order);
315 45 float h_r = read_float(&filter_re[k], swap_byte_order);
316 45 float h_i = read_float(&filter_im[k], swap_byte_order);
317
318 45 real_sum += x_r * h_r - x_i * h_i;
319 45 imag_sum += x_r * h_i + x_i * h_r;
320 }
321 }
322 21 write_float(&output_re[n], real_sum, swap_byte_order);
323 21 write_float(&output_im[n], imag_sum, swap_byte_order);
324 }
325 3 }
326
327 3 static lbm_value ext_convolve(lbm_value *args, lbm_uint argn) {
328 3 lbm_value r = ENC_SYM_TERROR;
329
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
6 if ((argn == 2 || argn == 3) &&
330
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 lbm_is_array_r(args[0]) &&
331 3 lbm_is_array_r(args[1])) {
332
333 3 bool be = true;
334
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6 if (argn == 3 &&
335 3 lbm_is_symbol(args[2])) {
336
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (lbm_dec_sym(args[2]) == sym_little_endian) {
337 3 be = false;
338 }
339 }
340 3 bool swap_byte_order =
341 (be && LBM_SYSTEM_LITTLE_ENDIAN) ||
342 (!be && !LBM_SYSTEM_LITTLE_ENDIAN);
343
344 3 lbm_array_header_t *sig_arr = lbm_dec_array_r(args[0]);
345 3 lbm_array_header_t *fil_arr = lbm_dec_array_r(args[1]);
346 // The input arrays have been checked with lbm_is_array_r.
347 // If there is no way to create a byte array from a null pointer,
348 // then there is no way that these arrays can be null.
349 #ifdef __INFER__
350 __infer_assume(sig_arr != NULL);
351 __infer_assume(fil_arr != NULL);
352 #endif
353
354 3 lbm_uint sig_len = sig_arr->size / 4;
355 3 lbm_uint fil_len = fil_arr->size / 4;
356
357 // cppcheck-suppress invalidPointerCast
358 3 float *sig_data = (float*)sig_arr->data;
359 // cppcheck-suppress invalidPointerCast
360 3 float *fil_data = (float*)fil_arr->data;
361
362 lbm_value output;
363
364 3 lbm_uint out_len = sig_len + fil_len - 1;
365
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (lbm_heap_allocate_array(&output, out_len * sizeof(float))) {
366 3 lbm_array_header_t *out_arr = lbm_dec_array_r(output);
367 // cppcheck-suppress invalidPointerCast
368 3 float *out_data = (float*)out_arr->data;
369
370 3 lbm_convolve(sig_data,
371 sig_len,
372 fil_data,
373 fil_len,
374 out_data,
375 out_len,
376 swap_byte_order);
377 3 r = output;
378 } else {
379 ✗ r = ENC_SYM_MERROR;
380 }
381 }
382 3 return r;
383 }
384
385 3 static lbm_value ext_complex_convolve(lbm_value *args, lbm_uint argn) {
386 3 lbm_value r = ENC_SYM_TERROR;
387
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
6 if ((argn == 4 || argn == 5) &&
388
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 lbm_is_array_r(args[0]) &&
389
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 lbm_is_array_r(args[1]) &&
390
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 lbm_is_array_r(args[2]) &&
391 3 lbm_is_array_r(args[3])) {
392
393 3 bool be = true;
394
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6 if (argn == 5 &&
395 3 lbm_is_symbol(args[4])) {
396
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (lbm_dec_sym(args[4]) == sym_little_endian) {
397 3 be = false;
398 }
399 }
400 3 bool swap_byte_order =
401 (be && LBM_SYSTEM_LITTLE_ENDIAN) ||
402 (!be && !LBM_SYSTEM_LITTLE_ENDIAN);
403
404 3 lbm_array_header_t *sig_re_arr = lbm_dec_array_r(args[0]);
405 3 lbm_array_header_t *sig_im_arr = lbm_dec_array_r(args[1]);
406 3 lbm_array_header_t *fil_re_arr = lbm_dec_array_r(args[2]);
407 3 lbm_array_header_t *fil_im_arr = lbm_dec_array_r(args[3]);
408
409
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 3 times.
3 if (!sig_re_arr || !sig_im_arr || !fil_re_arr || !fil_im_arr) {
410 ✗ return ENC_SYM_TERROR;
411 }
412
413 // Check that real and imaginary parts have matching sizes
414
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (sig_re_arr->size != sig_im_arr->size ||
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 fil_re_arr->size != fil_im_arr->size) {
416 ✗ return ENC_SYM_TERROR;
417 }
418
419 3 lbm_uint sig_len = sig_re_arr->size / sizeof(float);
420 3 lbm_uint fil_len = fil_re_arr->size / sizeof(float);
421
422 // cppcheck-suppress invalidPointerCast
423 3 float *sig_re_data = (float*)sig_re_arr->data;
424 // cppcheck-suppress invalidPointerCast
425 3 float *sig_im_data = (float*)sig_im_arr->data;
426 // cppcheck-suppress invalidPointerCast
427 3 float *fil_re_data = (float*)fil_re_arr->data;
428 // cppcheck-suppress invalidPointerCast
429 3 float *fil_im_data = (float*)fil_im_arr->data;
430
431 lbm_value output_re;
432 lbm_value output_im;
433
434 3 lbm_uint out_len = sig_len + fil_len - 1;
435 3 lbm_value r_cons = lbm_cons(ENC_SYM_NIL, ENC_SYM_NIL);
436
2/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
6 if (lbm_heap_allocate_array(&output_re, out_len * sizeof(float)) &&
437 3 lbm_heap_allocate_array(&output_im, out_len * sizeof(float))) {
438 3 lbm_array_header_t *out_re_arr = lbm_dec_array_r(output_re);
439 3 lbm_array_header_t *out_im_arr = lbm_dec_array_r(output_im);
440 // cppcheck-suppress invalidPointerCast
441 3 float *out_re_data = (float*)out_re_arr->data;
442 // cppcheck-suppress invalidPointerCast
443 3 float *out_im_data = (float*)out_im_arr->data;
444
445 3 lbm_complex_convolve(sig_re_data,
446 sig_im_data,
447 (unsigned int)sig_len,
448 fil_re_data,
449 fil_im_data,
450 (unsigned int)fil_len,
451 out_re_data,
452 out_im_data,
453 (unsigned int)out_len,
454 swap_byte_order);
455 3 lbm_set_car_and_cdr(r_cons, output_re, output_im);
456 3 r = r_cons;
457 } else {
458 ✗ r = ENC_SYM_MERROR;
459 }
460 }
461 3 return r;
462 }
463
464
465 69 static void lbm_fft(float *real, float *imag, int n, int inverse) {
466 69 int k = 0;
467
2/2
✓ Branch 0 taken 321 times.
✓ Branch 1 taken 69 times.
390 for (int i = 1; i < n; i++) {
468 321 int bit = n >> 1;
469
2/2
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 321 times.
483 while (k & bit) {
470 162 k ^= bit;
471 162 bit >>= 1;
472 }
473 321 k ^= bit;
474
475
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 213 times.
321 if (i < k) {
476 108 float temp = real[i];
477 108 real[i] = real[k];
478 108 real[k] = temp;
479
480 108 temp = imag[i];
481 108 imag[i] = imag[k];
482 108 imag[k] = temp;
483 }
484 }
485
486
2/2
✓ Branch 0 taken 159 times.
✓ Branch 1 taken 69 times.
228 for (int len = 2; len <= n; len <<= 1) {
487
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 129 times.
159 float angle = (inverse ? 2.0f : -2.0f) * (float)M_PI / (float)len;
488 159 float wlen_r = cosf(angle);
489 159 float wlen_i = sinf(angle);
490
491
2/2
✓ Branch 0 taken 321 times.
✓ Branch 1 taken 159 times.
480 for (int i = 0; i < n; i += len) {
492 321 float w_r = 1.0f;
493 321 float w_i = 0.0f;
494
495
2/2
✓ Branch 0 taken 531 times.
✓ Branch 1 taken 321 times.
852 for (int j = 0; j < len / 2; j++) {
496 531 float u_r = real[i + j];
497 531 float u_i = imag[i + j];
498 531 float v_r = real[i + j + len / 2];
499 531 float v_i = imag[i + j + len / 2];
500
501 531 float t_r = w_r * v_r - w_i * v_i;
502 531 float t_i = w_r * v_i + w_i * v_r;
503
504 531 real[i + j] = u_r + t_r;
505 531 imag[i + j] = u_i + t_i;
506 531 real[i + j + len / 2] = u_r - t_r;
507 531 imag[i + j + len / 2] = u_i - t_i;
508
509 531 float next_w_r = w_r * wlen_r - w_i * wlen_i;
510 531 float next_w_i = w_r * wlen_i + w_i * wlen_r;
511 531 w_r = next_w_r;
512 531 w_i = next_w_i;
513 }
514 }
515 }
516
517
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 54 times.
69 if (inverse) {
518
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 15 times.
75 for (int i = 0; i < n; i++) {
519 60 real[i] /= (float)n;
520 60 imag[i] /= (float)n;
521 }
522 }
523 69 }
524
525 81 static lbm_value ext_fft_f32(lbm_value *args, lbm_uint argn) {
526 81 lbm_value r = ENC_SYM_TERROR;
527
4/4
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 6 times.
159 if (argn >= 2 &&
528
2/2
✓ Branch 1 taken 69 times.
✓ Branch 2 taken 3 times.
150 lbm_is_array_r(args[0]) &&
529 72 lbm_is_array_r(args[1])) {
530
531 69 int inverse = 0;
532 69 bool be = true;
533
534
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
93 if (argn > 2 &&
535 24 lbm_is_symbol(args[2])) {
536 24 lbm_uint sym = lbm_dec_sym(args[2]);
537
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 9 times.
24 if (sym == sym_inverse) {
538 15 inverse = 1;
539
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 } else if (sym == sym_little_endian) {
540 6 be = false;
541 }
542 }
543
544
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 63 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
75 if (argn > 3 &&
545 6 lbm_is_symbol(args[3])) {
546
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 if (lbm_dec_sym(args[3]) == sym_little_endian) {
547 6 be = false;
548 }
549 }
550
551 69 bool swap_byte_order =
552 (be && LBM_SYSTEM_LITTLE_ENDIAN) ||
553 (!be && !LBM_SYSTEM_LITTLE_ENDIAN);
554
555 69 lbm_array_header_t *real_arr = lbm_dec_array_r(args[0]);
556 69 lbm_array_header_t *imag_arr = lbm_dec_array_r(args[1]);
557
558
2/4
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 69 times.
69 if (!real_arr || !imag_arr) {
559 ✗ return ENC_SYM_TERROR;
560 }
561
562
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
69 if (real_arr->size == imag_arr->size &&
563
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
69 real_arr->size > sizeof(float) && // need more than 4 bytes for a float
564
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
138 (real_arr->size % sizeof(float) == 0)) {
565 69 lbm_uint n = real_arr->size / sizeof(float);
566 69 lbm_uint padded_size = n;
567
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 51 times.
69 if ((n & (n - 1)) != 0) { // not a power of two. needs padding.
568 18 padded_size = 1;
569
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 18 times.
69 while (padded_size < n) padded_size <<= 1;
570 }
571
572 lbm_value real_copy;
573 lbm_value imag_copy;
574
2/4
✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 69 times.
✗ Branch 4 not taken.
138 if (lbm_heap_allocate_array(&real_copy, padded_size * sizeof(float)) &&
575 138 lbm_heap_allocate_array(&imag_copy, padded_size * sizeof(float))) {
576 // cppcheck-suppress invalidPointerCast
577 69 float *real_data = (float*)real_arr->data;
578 // cppcheck-suppress invalidPointerCast
579 69 float *imag_data = (float*)imag_arr->data;
580
581 69 lbm_array_header_t *real_copy_arr = lbm_dec_array_r(real_copy);
582 69 lbm_array_header_t *imag_copy_arr = lbm_dec_array_r(imag_copy);
583 // cppcheck-suppress invalidPointerCast
584 69 float *real_copy_data = (float*)real_copy_arr->data;
585 // cppcheck-suppress invalidPointerCast
586 69 float *imag_copy_data = (float*)imag_copy_arr->data;
587 69 memset(real_copy_data, 0, padded_size * sizeof(float));
588 69 memset(imag_copy_data, 0, padded_size * sizeof(float));
589
590
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 12 times.
69 if (swap_byte_order) {
591
2/2
✓ Branch 0 taken 297 times.
✓ Branch 1 taken 57 times.
354 for (lbm_uint w = 0; w < n; w ++) {
592 // cppcheck-suppress invalidPointerCast
593 297 uint32_t swapped = byte_order_swap(((uint32_t*)real_data)[w]);
594 // cppcheck-suppress invalidPointerCast
595 297 ((uint32_t*)real_copy_data)[w] = swapped;
596 // cppcheck-suppress invalidPointerCast
597 297 swapped = byte_order_swap(((uint32_t*)imag_data)[w]);
598 // cppcheck-suppress invalidPointerCast
599 297 ((uint32_t*)imag_copy_data)[w] = swapped;
600 }
601 } else {
602 12 memcpy(real_copy_data, real_data, real_arr->size);
603 12 memcpy(imag_copy_data, imag_data, real_arr->size);
604 }
605
606 69 lbm_fft(real_copy_data, imag_copy_data, (int)padded_size, inverse);
607
608
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 12 times.
69 if (swap_byte_order) {
609
2/2
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 57 times.
399 for (lbm_uint w = 0; w < padded_size; w ++) {
610 // cppcheck-suppress invalidPointerCast
611 342 uint32_t swapped = byte_order_swap(((uint32_t*)real_copy_data)[w]);
612 // cppcheck-suppress invalidPointerCast
613 342 ((uint32_t*)real_copy_data)[w] = swapped;
614 // cppcheck-suppress invalidPointerCast
615 342 swapped = byte_order_swap(((uint32_t*)imag_copy_data)[w]);
616 // cppcheck-suppress invalidPointerCast
617 342 ((uint32_t*)imag_copy_data)[w] = swapped;
618 }
619 }
620 69 r = lbm_cons(real_copy, imag_copy);
621 } else {
622 ✗ r = ENC_SYM_MERROR;
623 }
624 } else {
625 ✗ r = ENC_SYM_NIL;
626 }
627 }
628 81 return r;
629 }
630
631 727 void lbm_dsp_extensions_init(void) {
632 727 lbm_add_symbol("inverse", &sym_inverse);
633 727 lbm_add_symbol("little-endian", &sym_little_endian);
634 727 lbm_add_symbol("big-endian", &sym_big_endian);
635
636 727 lbm_add_extension("correlate", ext_correlate);
637 727 lbm_add_extension("complex-correlate", ext_complex_correlate);
638 727 lbm_add_extension("convolve", ext_convolve);
639 727 lbm_add_extension("complex-convolve", ext_complex_convolve);
640 727 lbm_add_extension("fft", ext_fft_f32);
641 727 }
642