GCC Code Coverage Report


Directory: ../src/
File: extensions/math_extensions.c
Date: 2026-10-16 14:49:47
Coverage Exec Excl Total
Lines: 100.0% 124 0 124
Functions: 100.0% 20 0 20
Branches: 100.0% 68 0 68

Line Branch Exec Source
1 /*
2 Copyright 2022, 2023, 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.h"
20 #include "lbm_utils.h"
21
22 #include <math.h>
23
24 #ifdef LBM_OPT_MATH_EXTENSIONS_SIZE
25 #pragma GCC optimize ("-Os")
26 #endif
27 #ifdef LBM_OPT_MATH_EXTENSIONS_SIZE_AGGRESSIVE
28 #pragma GCC optimize ("-Oz")
29 #endif
30
31
32 #ifdef __STRICT_ANSI__
33 #define isnanf isnan
34 #define isinff isinf
35 #endif
36
37 // Math
38
39 81 static lbm_value ext_sin(lbm_value *args, lbm_uint argn) {
40
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 42 times.
81 LBM_CHECK_ARGN_NUMBER(1);
41 42 return lbm_enc_float(sinf(lbm_dec_as_float(args[0])));
42 }
43
44 81 static lbm_value ext_cos(lbm_value *args, lbm_uint argn) {
45
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 42 times.
81 LBM_CHECK_ARGN_NUMBER(1);
46 42 return lbm_enc_float(cosf(lbm_dec_as_float(args[0])));
47 }
48
49 78 static lbm_value ext_tan(lbm_value *args, lbm_uint argn) {
50
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 39 times.
78 LBM_CHECK_ARGN_NUMBER(1);
51 39 return lbm_enc_float(tanf(lbm_dec_as_float(args[0])));
52 }
53
54 81 static lbm_value ext_asin(lbm_value *args, lbm_uint argn) {
55
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 42 times.
81 LBM_CHECK_ARGN_NUMBER(1);
56 42 return lbm_enc_float(asinf(lbm_dec_as_float(args[0])));
57 }
58
59 81 static lbm_value ext_acos(lbm_value *args, lbm_uint argn) {
60
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 42 times.
81 LBM_CHECK_ARGN_NUMBER(1);
61 42 return lbm_enc_float(acosf(lbm_dec_as_float(args[0])));
62 }
63
64 78 static lbm_value ext_atan(lbm_value *args, lbm_uint argn) {
65
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 39 times.
78 LBM_CHECK_ARGN_NUMBER(1);
66 39 return lbm_enc_float(atanf(lbm_dec_as_float(args[0])));
67 }
68
69 84 static lbm_value ext_atan2(lbm_value *args, lbm_uint argn) {
70
2/2
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 42 times.
84 LBM_CHECK_ARGN_NUMBER(2);
71 42 return lbm_enc_float(atan2f(lbm_dec_as_float(args[0]), lbm_dec_as_float(args[1])));
72 }
73
74 84 static lbm_value ext_pow(lbm_value *args, lbm_uint argn) {
75
2/2
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 42 times.
84 LBM_CHECK_ARGN_NUMBER(2);
76 42 return lbm_enc_float(powf(lbm_dec_as_float(args[0]), lbm_dec_as_float(args[1])));
77 }
78
79 92 static lbm_value ext_exp(lbm_value *args, lbm_uint argn) {
80
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 53 times.
92 LBM_CHECK_ARGN_NUMBER(1);
81 53 return lbm_enc_float(expf(lbm_dec_as_float(args[0])));
82 }
83
84 131 static lbm_value ext_sqrt(lbm_value *args, lbm_uint argn) {
85
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 92 times.
131 LBM_CHECK_ARGN_NUMBER(1);
86 92 return lbm_enc_float(sqrtf(lbm_dec_as_float(args[0])));
87 }
88
89 81 static lbm_value ext_log(lbm_value *args, lbm_uint argn) {
90
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 42 times.
81 LBM_CHECK_ARGN_NUMBER(1);
91 42 return lbm_enc_float(logf(lbm_dec_as_float(args[0])));
92 }
93
94 114 static lbm_value ext_log10(lbm_value *args, lbm_uint argn) {
95
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 75 times.
114 LBM_CHECK_ARGN_NUMBER(1);
96 75 return lbm_enc_float(log10f(lbm_dec_as_float(args[0])));
97 }
98
99 81 static lbm_value ext_floor(lbm_value *args, lbm_uint argn) {
100
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 42 times.
81 LBM_CHECK_ARGN_NUMBER(1);
101 42 return lbm_enc_float(floorf(lbm_dec_as_float(args[0])));
102 }
103
104 81 static lbm_value ext_ceil(lbm_value *args, lbm_uint argn) {
105
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 42 times.
81 LBM_CHECK_ARGN_NUMBER(1);
106 42 return lbm_enc_float(ceilf(lbm_dec_as_float(args[0])));
107 }
108
109 81 static lbm_value ext_round(lbm_value *args, lbm_uint argn) {
110
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 42 times.
81 LBM_CHECK_ARGN_NUMBER(1);
111 42 return lbm_enc_float(roundf(lbm_dec_as_float(args[0])));
112 }
113
114 231 static lbm_value ext_deg2rad(lbm_value *args, lbm_uint argn) {
115
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 228 times.
231 LBM_CHECK_NUMBER_ALL();
116
117
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 75 times.
228 if (argn == 1) {
118 153 return lbm_enc_float(DEG2RAD_f(lbm_dec_as_float(args[0])));
119 } else {
120 75 lbm_value out_list = ENC_SYM_NIL;
121
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 75 times.
189 for (int i = (int)(argn - 1);i >= 0;i--) {
122 114 out_list = lbm_cons(lbm_enc_float(DEG2RAD_f(lbm_dec_as_float(args[i]))), out_list);
123 }
124 75 return out_list;
125 }
126 }
127
128 120 static lbm_value ext_rad2deg(lbm_value *args, lbm_uint argn) {
129
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 117 times.
120 LBM_CHECK_NUMBER_ALL();
130
131
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 75 times.
117 if (argn == 1) {
132 42 return lbm_enc_float(RAD2DEG_f(lbm_dec_as_float(args[0])));
133 } else {
134 75 lbm_value out_list = ENC_SYM_NIL;
135
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 75 times.
189 for (int i = (int)(argn - 1);i >= 0;i--) {
136 114 out_list = lbm_cons(lbm_enc_float(RAD2DEG_f(lbm_dec_as_float(args[i]))), out_list);
137 }
138 75 return out_list;
139 }
140 }
141
142
143 254 static lbm_value ext_is_nan(lbm_value *args, lbm_uint argn) {
144 254 lbm_value res = ENC_SYM_EERROR;
145
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 36 times.
254 if (argn == 1) {
146 218 res = ENC_SYM_TERROR;
147
2/2
✓ Branch 1 taken 215 times.
✓ Branch 2 taken 3 times.
218 if (lbm_is_number(args[0])) {
148 215 lbm_uint t = lbm_type_of_functional(args[0]);
149
3/3
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 140 times.
✓ Branch 2 taken 36 times.
215 switch(t) {
150 39 case LBM_TYPE_DOUBLE:
151
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 36 times.
39 if (isnan(lbm_dec_double(args[0]))) {
152 3 res = ENC_SYM_TRUE;
153 } else {
154 36 res = ENC_SYM_NIL;
155 }
156 39 break;
157 140 case LBM_TYPE_FLOAT:
158
2/2
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 84 times.
140 if (isnanf(lbm_dec_float(args[0]))) {
159 56 res = ENC_SYM_TRUE;
160 } else {
161 84 res = ENC_SYM_NIL;
162 }
163 140 break;
164 36 default:
165 36 res = ENC_SYM_NIL;
166 36 break;
167 }
168 }
169 }
170 254 return res;
171 }
172
173 167 static lbm_value ext_is_inf(lbm_value *args, lbm_uint argn) {
174 167 lbm_value res = ENC_SYM_EERROR;
175
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 36 times.
167 if (argn == 1) {
176 131 res = ENC_SYM_TERROR;
177
2/2
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 3 times.
131 if (lbm_is_number(args[0])) {
178 128 lbm_uint t = lbm_type_of_functional(args[0]);
179
3/3
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 53 times.
✓ Branch 2 taken 36 times.
128 switch(t) {
180 39 case LBM_TYPE_DOUBLE:
181
4/4
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 36 times.
39 if (isinf(lbm_dec_double(args[0]))) {
182 3 res = ENC_SYM_TRUE;
183 } else {
184 36 res = ENC_SYM_NIL;
185 }
186 39 break;
187 53 case LBM_TYPE_FLOAT:
188
4/4
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 45 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 45 times.
53 if (isinff(lbm_dec_float(args[0]))) {
189 8 res = ENC_SYM_TRUE;
190 } else {
191 45 res = ENC_SYM_NIL;
192 }
193 53 break;
194 36 default:
195 36 res = ENC_SYM_NIL;
196 36 break;
197 }
198 }
199 }
200 167 return res;
201 }
202
203
204 29167 void lbm_math_extensions_init(void) {
205
206 29167 lbm_add_extension("sin", ext_sin);
207 29167 lbm_add_extension("cos", ext_cos);
208 29167 lbm_add_extension("tan", ext_tan);
209 29167 lbm_add_extension("asin", ext_asin);
210 29167 lbm_add_extension("acos", ext_acos);
211 29167 lbm_add_extension("atan", ext_atan);
212 29167 lbm_add_extension("atan2", ext_atan2);
213 29167 lbm_add_extension("pow", ext_pow);
214 29167 lbm_add_extension("exp", ext_exp);
215 29167 lbm_add_extension("sqrt", ext_sqrt);
216 29167 lbm_add_extension("log", ext_log);
217 29167 lbm_add_extension("log10", ext_log10);
218 29167 lbm_add_extension("floor", ext_floor);
219 29167 lbm_add_extension("ceil", ext_ceil);
220 29167 lbm_add_extension("round", ext_round);
221 29167 lbm_add_extension("deg2rad", ext_deg2rad);
222 29167 lbm_add_extension("rad2deg", ext_rad2deg);
223 29167 lbm_add_extension("is-nan", ext_is_nan);
224 29167 lbm_add_extension("is-inf", ext_is_inf);
225 29167 }
226