GCC Code Coverage Report


Directory: ../src/
File: extensions/dsp_extensions.c
Date: 2026-02-08 10:07:39
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 426 static inline uint32_t byte_order_swap(uint32_t w) {
44 426 uint32_t r = w;
45 426 uint8_t *bytes = (uint8_t*)&r;
46 426 uint8_t tmp = bytes[0];
47 426 bytes[0] = bytes[3];
48 426 bytes[3] = tmp;
49 426 tmp = bytes[1];
50 426 bytes[1] = bytes[2];
51 426 bytes[2] = tmp;
52
53 426 return r;
54 }
55
56 162 static inline float read_float(float *ptr, bool swap) {
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 162 times.
162 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 162 return *ptr;
65 }
66
67
68 42 static inline void write_float(float *ptr, float value, bool swap) {
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 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 42 *ptr = value;
77 }
78 42 }
79
80
81 1 static void lbm_corr(float *s1,
82 unsigned int s1_len,
83 float *s2,
84 unsigned int s2_len,
85 float *output,
86 unsigned int output_len,
87 bool swap_byte_order) {
88
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 time.
8 for (int n = 0; n < (int)output_len; n++) {
89 7 float sum = 0.0f;
90
91
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7 times.
28 for (int k = 0; k < (int)s2_len; k++) {
92 21 int j = n + k;
93
3/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 9 times.
21 if (j >= 0 && j < (int)s1_len) {
94 12 sum +=
95 12 read_float(&s1[j], swap_byte_order) *
96 12 read_float(&s2[k], swap_byte_order);
97 }
98 }
99 7 write_float(&output[n], sum, swap_byte_order);
100 }
101 1 }
102
103
104 1 static void lbm_complex_corr(float *s1_re,
105 float *s1_im,
106 unsigned int s1_len,
107 float *s2_re,
108 float *s2_im,
109 unsigned int s2_len,
110 float *output_re,
111 float *output_im,
112 unsigned int output_len,
113 bool swap_byte_order) {
114
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 time.
8 for (int n = 0; n < (int)output_len; n++) {
115 7 float sum_re = 0.0f;
116 7 float sum_im = 0.0f;
117
118
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7 times.
28 for (int k = 0; k < (int)s2_len; k++) {
119 21 int j = n + k;
120
3/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 9 times.
21 if (j >= 0 && j < (int)s1_len) {
121 12 float x_r = read_float(&s1_re[j], swap_byte_order);
122 12 float x_i = read_float(&s1_im[j], swap_byte_order);
123 12 float h_r = read_float(&s2_re[k], swap_byte_order);
124 12 float h_i = read_float(&s2_im[k], swap_byte_order);
125 // Note conjugate
126 12 sum_re += x_r * h_r + x_i * h_i; // ac + bd
127 12 sum_im += x_i * h_r - x_r * h_i; // bc - ad
128 }
129 }
130 7 write_float(&output_re[n], sum_re, swap_byte_order);
131 7 write_float(&output_im[n], sum_im, swap_byte_order);
132 }
133 1 }
134
135 1 static lbm_value ext_correlate(lbm_value *args, lbm_uint argn) {
136 1 lbm_value r = ENC_SYM_TERROR;
137
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 if ((argn == 2 || argn == 3) &&
138
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_array_r(args[0]) &&
139 1 lbm_is_array_r(args[1])) {
140
141 1 bool be = true;
142
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 if (argn == 3 &&
143 1 lbm_is_symbol(args[2])) {
144
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 if (lbm_dec_sym(args[2]) == sym_little_endian) {
145 1 be = false;
146 }
147 }
148 1 bool swap_byte_order =
149 (be && LBM_SYSTEM_LITTLE_ENDIAN) ||
150 (!be && !LBM_SYSTEM_LITTLE_ENDIAN);
151
152 1 lbm_array_header_t *s1_arr = lbm_dec_array_r(args[0]);
153 1 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 1 unsigned int s1_len = s1_arr->size / 4;
163 1 unsigned int s2_len = s2_arr->size / 4;
164
165 // cppcheck-suppress invalidPointerCast
166 1 float *s1_data = (float*)s1_arr->data;
167 // cppcheck-suppress invalidPointerCast
168 1 float *s2_data = (float*)s2_arr->data;
169
170 lbm_value output;
171
172 1 unsigned int out_len = s1_len + s2_len - 1;
173
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 if (lbm_heap_allocate_array(&output, out_len * sizeof(float))) {
174 1 lbm_array_header_t *out_arr = lbm_dec_array_r(output);
175 // cppcheck-suppress invalidPointerCast
176 1 float *out_data = (float*)out_arr->data;
177
178 1 lbm_corr(s1_data,
179 s1_len,
180 s2_data,
181 s2_len,
182 out_data,
183 out_len,
184 swap_byte_order);
185 1 r = output;
186 } else {
187 ✗ r = ENC_SYM_MERROR;
188 }
189 }
190 1 return r;
191 }
192
193 1 static lbm_value ext_complex_correlate(lbm_value *args, lbm_uint argn) {
194 1 lbm_value r = ENC_SYM_TERROR;
195
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 if ((argn == 4 || argn == 5) && // Allow only 4 or 5 args
196
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_array_r(args[0]) &&
197
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_array_r(args[1]) &&
198
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_array_r(args[2]) &&
199 1 lbm_is_array_r(args[3])) {
200
201 1 bool be = true;
202
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 if (argn == 5 &&
203 1 lbm_is_symbol(args[4])) {
204
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 if (lbm_dec_sym(args[4]) == sym_little_endian) {
205 1 be = false;
206 }
207 }
208 1 bool swap_byte_order =
209 (be && LBM_SYSTEM_LITTLE_ENDIAN) ||
210 (!be && !LBM_SYSTEM_LITTLE_ENDIAN);
211
212 1 lbm_array_header_t *s1_re_arr = lbm_dec_array_r(args[0]);
213 1 lbm_array_header_t *s1_im_arr = lbm_dec_array_r(args[1]);
214 1 lbm_array_header_t *s2_re_arr = lbm_dec_array_r(args[2]);
215 1 lbm_array_header_t *s2_im_arr = lbm_dec_array_r(args[3]);
216
217
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 time.
1 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 1 time.
✗ Branch 1 not taken.
1 if (s1_re_arr->size != s1_im_arr->size ||
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 s2_re_arr->size != s2_im_arr->size) {
223 ✗ return ENC_SYM_TERROR;
224 }
225
226 1 unsigned int s1_len = s1_re_arr->size / sizeof(float);
227 1 unsigned int s2_len = s2_re_arr->size / sizeof(float);
228
229 // cppcheck-suppress invalidPointerCast
230 1 float *s1_re_data = (float*)s1_re_arr->data;
231 // cppcheck-suppress invalidPointerCast
232 1 float *s1_im_data = (float*)s1_im_arr->data;
233 // cppcheck-suppress invalidPointerCast
234 1 float *s2_re_data = (float*)s2_re_arr->data;
235 // cppcheck-suppress invalidPointerCast
236 1 float *s2_im_data = (float*)s2_im_arr->data;
237
238 lbm_value output_re;
239 lbm_value output_im;
240
241 1 unsigned int out_len = s1_len + s2_len - 1;
242 1 lbm_value r_cons = lbm_cons(ENC_SYM_NIL, ENC_SYM_NIL);
243
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 if (r_cons != ENC_SYM_MERROR &&
244
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_heap_allocate_array(&output_re, out_len * sizeof(float)) &&
245 1 lbm_heap_allocate_array(&output_im, out_len * sizeof(float))) {
246 1 lbm_array_header_t *out_re_arr = lbm_dec_array_r(output_re);
247 1 lbm_array_header_t *out_im_arr = lbm_dec_array_r(output_im);
248 // cppcheck-suppress invalidPointerCast
249 1 float *out_re_data = (float*)out_re_arr->data;
250 // cppcheck-suppress invalidPointerCast
251 1 float *out_im_data = (float*)out_im_arr->data;
252
253 1 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 1 lbm_set_car_and_cdr(r_cons, output_re, output_im);
264 1 r = r_cons;
265 } else {
266 ✗ r = ENC_SYM_MERROR;
267 }
268 }
269 1 return r;
270 }
271
272
273 1 static void lbm_convolve(float *signal,
274 unsigned int signal_len,
275 float *filter,
276 unsigned int filter_len,
277 float *output,
278 unsigned int output_len,
279 bool swap_byte_order) {
280
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 time.
8 for (int n = 0; n < (int)output_len; n++) {
281 7 float sum = 0.0f;
282
283
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7 times.
28 for (int k = 0; k < (int)filter_len; k++) {
284 21 int j = n - k;
285
4/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 3 times.
21 if (j >= 0 && j < (int)signal_len) {
286 15 sum +=
287 15 read_float(&signal[j], swap_byte_order) *
288 15 read_float(&filter[k], swap_byte_order);
289 }
290 }
291 7 write_float(&output[n], sum, swap_byte_order);
292 }
293 1 }
294
295
296 1 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 7 times.
✓ Branch 1 taken 1 time.
8 for (int n = 0; n < (int)output_len; n++) {
307 7 float real_sum = 0.0f;
308 7 float imag_sum = 0.0f;
309
310
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7 times.
28 for (int k = 0; k < (int)filter_len; k++) {
311 21 int j = n - k;
312
4/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 3 times.
21 if (j >= 0 && j < (int)signal_len) {
313 15 float x_r = read_float(&signal_re[j], swap_byte_order);
314 15 float x_i = read_float(&signal_im[j], swap_byte_order);
315 15 float h_r = read_float(&filter_re[k], swap_byte_order);
316 15 float h_i = read_float(&filter_im[k], swap_byte_order);
317
318 15 real_sum += x_r * h_r - x_i * h_i;
319 15 imag_sum += x_r * h_i + x_i * h_r;
320 }
321 }
322 7 write_float(&output_re[n], real_sum, swap_byte_order);
323 7 write_float(&output_im[n], imag_sum, swap_byte_order);
324 }
325 1 }
326
327 1 static lbm_value ext_convolve(lbm_value *args, lbm_uint argn) {
328 1 lbm_value r = ENC_SYM_TERROR;
329
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 if ((argn == 2 || argn == 3) &&
330
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_array_r(args[0]) &&
331 1 lbm_is_array_r(args[1])) {
332
333 1 bool be = true;
334
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 if (argn == 3 &&
335 1 lbm_is_symbol(args[2])) {
336
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 if (lbm_dec_sym(args[2]) == sym_little_endian) {
337 1 be = false;
338 }
339 }
340 1 bool swap_byte_order =
341 (be && LBM_SYSTEM_LITTLE_ENDIAN) ||
342 (!be && !LBM_SYSTEM_LITTLE_ENDIAN);
343
344 1 lbm_array_header_t *sig_arr = lbm_dec_array_r(args[0]);
345 1 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 1 unsigned int sig_len = sig_arr->size / 4;
355 1 unsigned int fil_len = fil_arr->size / 4;
356
357 // cppcheck-suppress invalidPointerCast
358 1 float *sig_data = (float*)sig_arr->data;
359 // cppcheck-suppress invalidPointerCast
360 1 float *fil_data = (float*)fil_arr->data;
361
362 lbm_value output;
363
364 1 unsigned int out_len = sig_len + fil_len - 1;
365
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 if (lbm_heap_allocate_array(&output, out_len * sizeof(float))) {
366 1 lbm_array_header_t *out_arr = lbm_dec_array_r(output);
367 // cppcheck-suppress invalidPointerCast
368 1 float *out_data = (float*)out_arr->data;
369
370 1 lbm_convolve(sig_data,
371 sig_len,
372 fil_data,
373 fil_len,
374 out_data,
375 out_len,
376 swap_byte_order);
377 1 r = output;
378 } else {
379 ✗ r = ENC_SYM_MERROR;
380 }
381 }
382 1 return r;
383 }
384
385 1 static lbm_value ext_complex_convolve(lbm_value *args, lbm_uint argn) {
386 1 lbm_value r = ENC_SYM_TERROR;
387
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 if ((argn == 4 || argn == 5) &&
388
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_array_r(args[0]) &&
389
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_array_r(args[1]) &&
390
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_array_r(args[2]) &&
391 1 lbm_is_array_r(args[3])) {
392
393 1 bool be = true;
394
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 if (argn == 5 &&
395 1 lbm_is_symbol(args[4])) {
396
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 if (lbm_dec_sym(args[4]) == sym_little_endian) {
397 1 be = false;
398 }
399 }
400 1 bool swap_byte_order =
401 (be && LBM_SYSTEM_LITTLE_ENDIAN) ||
402 (!be && !LBM_SYSTEM_LITTLE_ENDIAN);
403
404 1 lbm_array_header_t *sig_re_arr = lbm_dec_array_r(args[0]);
405 1 lbm_array_header_t *sig_im_arr = lbm_dec_array_r(args[1]);
406 1 lbm_array_header_t *fil_re_arr = lbm_dec_array_r(args[2]);
407 1 lbm_array_header_t *fil_im_arr = lbm_dec_array_r(args[3]);
408
409
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 time.
1 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 1 time.
✗ Branch 1 not taken.
1 if (sig_re_arr->size != sig_im_arr->size ||
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 fil_re_arr->size != fil_im_arr->size) {
416 ✗ return ENC_SYM_TERROR;
417 }
418
419 1 unsigned int sig_len = sig_re_arr->size / sizeof(float);
420 1 unsigned int fil_len = fil_re_arr->size / sizeof(float);
421
422 // cppcheck-suppress invalidPointerCast
423 1 float *sig_re_data = (float*)sig_re_arr->data;
424 // cppcheck-suppress invalidPointerCast
425 1 float *sig_im_data = (float*)sig_im_arr->data;
426 // cppcheck-suppress invalidPointerCast
427 1 float *fil_re_data = (float*)fil_re_arr->data;
428 // cppcheck-suppress invalidPointerCast
429 1 float *fil_im_data = (float*)fil_im_arr->data;
430
431 lbm_value output_re;
432 lbm_value output_im;
433
434 1 unsigned int out_len = sig_len + fil_len - 1;
435 1 lbm_value r_cons = lbm_cons(ENC_SYM_NIL, ENC_SYM_NIL);
436
2/4
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
2 if (lbm_heap_allocate_array(&output_re, out_len * sizeof(float)) &&
437 1 lbm_heap_allocate_array(&output_im, out_len * sizeof(float))) {
438 1 lbm_array_header_t *out_re_arr = lbm_dec_array_r(output_re);
439 1 lbm_array_header_t *out_im_arr = lbm_dec_array_r(output_im);
440 // cppcheck-suppress invalidPointerCast
441 1 float *out_re_data = (float*)out_re_arr->data;
442 // cppcheck-suppress invalidPointerCast
443 1 float *out_im_data = (float*)out_im_arr->data;
444
445 1 lbm_complex_convolve(sig_re_data,
446 sig_im_data,
447 sig_len,
448 fil_re_data,
449 fil_im_data,
450 fil_len,
451 out_re_data,
452 out_im_data,
453 out_len,
454 swap_byte_order);
455 1 lbm_set_car_and_cdr(r_cons, output_re, output_im);
456 1 r = r_cons;
457 } else {
458 ✗ r = ENC_SYM_MERROR;
459 }
460 }
461 1 return r;
462 }
463
464
465 23 static void lbm_fft(float *real, float *imag, int n, int inverse) {
466 23 int k = 0;
467
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 23 times.
130 for (int i = 1; i < n; i++) {
468 107 int bit = n >> 1;
469
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 107 times.
161 while (k & bit) {
470 54 k ^= bit;
471 54 bit >>= 1;
472 }
473 107 k ^= bit;
474
475
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 71 times.
107 if (i < k) {
476 36 float temp = real[i];
477 36 real[i] = real[k];
478 36 real[k] = temp;
479
480 36 temp = imag[i];
481 36 imag[i] = imag[k];
482 36 imag[k] = temp;
483 }
484 }
485
486
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 23 times.
76 for (int len = 2; len <= n; len <<= 1) {
487
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 43 times.
53 float angle = (inverse ? 2.0f : -2.0f) * (float)M_PI / (float)len;
488 53 float wlen_r = cosf(angle);
489 53 float wlen_i = sinf(angle);
490
491
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 53 times.
160 for (int i = 0; i < n; i += len) {
492 107 float w_r = 1.0f;
493 107 float w_i = 0.0f;
494
495
2/2
✓ Branch 0 taken 177 times.
✓ Branch 1 taken 107 times.
284 for (int j = 0; j < len / 2; j++) {
496 177 float u_r = real[i + j];
497 177 float u_i = imag[i + j];
498 177 float v_r = real[i + j + len / 2];
499 177 float v_i = imag[i + j + len / 2];
500
501 177 float t_r = w_r * v_r - w_i * v_i;
502 177 float t_i = w_r * v_i + w_i * v_r;
503
504 177 real[i + j] = u_r + t_r;
505 177 imag[i + j] = u_i + t_i;
506 177 real[i + j + len / 2] = u_r - t_r;
507 177 imag[i + j + len / 2] = u_i - t_i;
508
509 177 float next_w_r = w_r * wlen_r - w_i * wlen_i;
510 177 float next_w_i = w_r * wlen_i + w_i * wlen_r;
511 177 w_r = next_w_r;
512 177 w_i = next_w_i;
513 }
514 }
515 }
516
517
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 18 times.
23 if (inverse) {
518
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 5 times.
25 for (int i = 0; i < n; i++) {
519 20 real[i] /= (float)n;
520 20 imag[i] /= (float)n;
521 }
522 }
523 23 }
524
525 27 static lbm_value ext_fft_f32(lbm_value *args, lbm_uint argn) {
526 27 lbm_value r = ENC_SYM_TERROR;
527
4/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 2 times.
53 if (argn >= 2 &&
528
2/2
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 1 time.
50 lbm_is_array_r(args[0]) &&
529 24 lbm_is_array_r(args[1])) {
530
531 23 int inverse = 0;
532 23 bool be = true;
533
534
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
31 if (argn > 2 &&
535 8 lbm_is_symbol(args[2])) {
536 8 lbm_uint sym = lbm_dec_sym(args[2]);
537
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
8 if (sym == sym_inverse) {
538 5 inverse = 1;
539
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 } else if (sym == sym_little_endian) {
540 2 be = false;
541 }
542 }
543
544
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
25 if (argn > 3 &&
545 2 lbm_is_symbol(args[3])) {
546
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (lbm_dec_sym(args[3]) == sym_little_endian) {
547 2 be = false;
548 }
549 }
550
551 23 bool swap_byte_order =
552 (be && LBM_SYSTEM_LITTLE_ENDIAN) ||
553 (!be && !LBM_SYSTEM_LITTLE_ENDIAN);
554
555 23 lbm_array_header_t *real_arr = lbm_dec_array_r(args[0]);
556 23 lbm_array_header_t *imag_arr = lbm_dec_array_r(args[1]);
557
558
2/4
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 23 times.
23 if (!real_arr || !imag_arr) {
559 ✗ return ENC_SYM_TERROR;
560 }
561
562
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 if (real_arr->size == imag_arr->size &&
563
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 real_arr->size > sizeof(float) && // need more than 4 bytes for a float
564
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
46 (real_arr->size % sizeof(float) == 0)) {
565 23 lbm_uint n = real_arr->size / sizeof(float);
566 23 lbm_uint padded_size = n;
567
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 17 times.
23 if ((n & (n - 1)) != 0) { // not a power of two. needs padding.
568 6 padded_size = 1;
569
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 6 times.
23 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 23 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 23 times.
✗ Branch 4 not taken.
46 if (lbm_heap_allocate_array(&real_copy, padded_size * sizeof(float)) &&
575 46 lbm_heap_allocate_array(&imag_copy, padded_size * sizeof(float))) {
576 // cppcheck-suppress invalidPointerCast
577 23 float *real_data = (float*)real_arr->data;
578 // cppcheck-suppress invalidPointerCast
579 23 float *imag_data = (float*)imag_arr->data;
580
581 23 lbm_array_header_t *real_copy_arr = lbm_dec_array_r(real_copy);
582 23 lbm_array_header_t *imag_copy_arr = lbm_dec_array_r(imag_copy);
583 // cppcheck-suppress invalidPointerCast
584 23 float *real_copy_data = (float*)real_copy_arr->data;
585 // cppcheck-suppress invalidPointerCast
586 23 float *imag_copy_data = (float*)imag_copy_arr->data;
587 23 memset(real_copy_data, 0, padded_size * sizeof(float));
588 23 memset(imag_copy_data, 0, padded_size * sizeof(float));
589
590
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 4 times.
23 if (swap_byte_order) {
591
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 19 times.
118 for (lbm_uint w = 0; w < n; w ++) {
592 // cppcheck-suppress invalidPointerCast
593 99 uint32_t swapped = byte_order_swap(((uint32_t*)real_data)[w]);
594 // cppcheck-suppress invalidPointerCast
595 99 ((uint32_t*)real_copy_data)[w] = swapped;
596 // cppcheck-suppress invalidPointerCast
597 99 swapped = byte_order_swap(((uint32_t*)imag_data)[w]);
598 // cppcheck-suppress invalidPointerCast
599 99 ((uint32_t*)imag_copy_data)[w] = swapped;
600 }
601 } else {
602 4 memcpy(real_copy_data, real_data, real_arr->size);
603 4 memcpy(imag_copy_data, imag_data, real_arr->size);
604 }
605
606 23 lbm_fft(real_copy_data, imag_copy_data, (int)padded_size, inverse);
607
608
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 4 times.
23 if (swap_byte_order) {
609
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 19 times.
133 for (lbm_uint w = 0; w < padded_size; w ++) {
610 // cppcheck-suppress invalidPointerCast
611 114 uint32_t swapped = byte_order_swap(((uint32_t*)real_copy_data)[w]);
612 // cppcheck-suppress invalidPointerCast
613 114 ((uint32_t*)real_copy_data)[w] = swapped;
614 // cppcheck-suppress invalidPointerCast
615 114 swapped = byte_order_swap(((uint32_t*)imag_copy_data)[w]);
616 // cppcheck-suppress invalidPointerCast
617 114 ((uint32_t*)imag_copy_data)[w] = swapped;
618 }
619 }
620 23 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 27 return r;
629 }
630
631 294 void lbm_dsp_extensions_init(void) {
632 294 lbm_add_symbol("inverse", &sym_inverse);
633 294 lbm_add_symbol("little-endian", &sym_little_endian);
634 294 lbm_add_symbol("big-endian", &sym_big_endian);
635
636 294 lbm_add_extension("correlate", ext_correlate);
637 294 lbm_add_extension("complex-correlate", ext_complex_correlate);
638 294 lbm_add_extension("convolve", ext_convolve);
639 294 lbm_add_extension("complex-convolve", ext_complex_convolve);
640 294 lbm_add_extension("fft", ext_fft_f32);
641 294 }
642