GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/extensions/math_extensions.c
Date: 2026-07-11 23:13:58
Exec Total Coverage
Lines: 124 124 100.0%
Functions: 20 20 100.0%
Branches: 68 68 100.0%

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