GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/extensions/math_extensions.c
Date: 2024-08-06 17:32:21
Exec Total Coverage
Lines: 36 123 29.3%
Functions: 3 20 15.0%
Branches: 44 144 30.6%

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