GCC Code Coverage Report


Directory: src/gate/
File: src/gate/mathematics.c
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 162 206 78.6%
Functions: 68 78 87.2%
Branches: 94 118 79.7%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/mathematics.h"
30
31 #if defined(GATE_SYS_EFI)
32 //# define GATE_CORE_MATH_EFI_IMPL 1
33 # define GATE_CORE_MATH_STD_IMPL 1
34 #else
35 # define GATE_CORE_MATH_STD_IMPL 1
36 #endif
37
38 /*****************************
39 * Generic implementations *
40 *****************************/
41
42 #include <stdarg.h>
43 #include <float.h>
44
45 2 void gate_decimal_init(gate_decimal_t* deci, gate_int32_t int_num, gate_uint32_t micro_num)
46 {
47 2 deci->value = (gate_decimal_primitive_t)(int_num * GATE_DECIMAL_FACTOR);
48 2 deci->value += (gate_decimal_primitive_t)(micro_num % GATE_DECIMAL_FACTOR);
49 2 }
50 1 gate_int32_t gate_decimal_get_int(gate_decimal_t const* deci)
51 {
52 1 return (gate_int32_t)(deci->value / GATE_DECIMAL_FACTOR);
53 }
54 1 gate_uint32_t gate_decimal_get_micro(gate_decimal_t const* deci)
55 {
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (deci->value < 0)
57 {
58 ✗ return (gate_uint32_t)((-deci->value) % GATE_DECIMAL_FACTOR);
59 }
60 else
61 {
62 1 return (gate_uint32_t)(deci->value % GATE_DECIMAL_FACTOR);
63 }
64 }
65 ✗ gate_real32_t gate_decimal_get_float(gate_decimal_t const* micro)
66 {
67 ✗ return (gate_real32_t)micro->value / (gate_real32_t)GATE_DECIMAL_FACTOR;
68 }
69
70 5 int gate_decimal_compare(gate_decimal_t const* a, gate_decimal_t const* b)
71 {
72
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 if (a->value < b->value)
73 {
74 2 return -1;
75 }
76
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 else if (a->value > b->value)
77 {
78 3 return 1;
79 }
80 ✗ return 0;
81 }
82
83
84 1 void gate_decimal_add_int(gate_decimal_t* deci, gate_int32_t value)
85 {
86 1 deci->value += (gate_decimal_primitive_t)value * GATE_DECIMAL_FACTOR;
87 1 }
88 1 void gate_decimal_sub_int(gate_decimal_t* deci, gate_int32_t value)
89 {
90 1 deci->value -= (gate_decimal_primitive_t)value * GATE_DECIMAL_FACTOR;
91 1 }
92 1 void gate_decimal_mul_int(gate_decimal_t* deci, gate_int32_t value)
93 {
94 1 deci->value *= (gate_decimal_primitive_t)value;
95 1 }
96 1 void gate_decimal_div_int(gate_decimal_t* deci, gate_int32_t value)
97 {
98 1 deci->value /= (gate_decimal_primitive_t)value;
99 1 }
100
101 1 void gate_decimal_add_dec(gate_decimal_t* deci, gate_decimal_t const* ptr_value)
102 {
103 1 deci->value += ptr_value->value;
104 1 }
105 ✗ void gate_decimal_sub_dec(gate_decimal_t* deci, gate_decimal_t const* ptr_value)
106 {
107 ✗ deci->value -= ptr_value->value;
108 ✗ }
109 ✗ void gate_decimal_mul_dec(gate_decimal_t* deci, gate_decimal_t const* ptr_value)
110 {
111 ✗ deci->value *= ptr_value->value;
112 ✗ deci->value /= (gate_decimal_primitive_t)GATE_DECIMAL_FACTOR;
113 ✗ }
114 ✗ void gate_decimal_div_dec(gate_decimal_t* deci, gate_decimal_t const* ptr_value)
115 {
116 ✗ gate_int64_t r = deci->value % ptr_value->value;
117 ✗ deci->value /= ptr_value->value;
118 ✗ deci->value *= (gate_decimal_primitive_t)GATE_DECIMAL_FACTOR;
119 ✗ deci->value += (gate_decimal_primitive_t)(r * GATE_DECIMAL_FACTOR / ptr_value->value);
120 ✗ }
121
122 ✗ void gate_decimal_add_flt(gate_decimal_t* deci, gate_real32_t value)
123 {
124 ✗ deci->value += (gate_decimal_primitive_t)(value * (gate_real32_t)GATE_DECIMAL_FACTOR);
125 ✗ }
126 ✗ void gate_decimal_sub_flt(gate_decimal_t* deci, gate_real32_t value)
127 {
128 ✗ deci->value -= (gate_decimal_primitive_t)(value * (gate_real32_t)GATE_DECIMAL_FACTOR);
129 ✗ }
130 ✗ void gate_decimal_mul_flt(gate_decimal_t* deci, gate_real32_t value)
131 {
132 ✗ deci->value = (gate_decimal_primitive_t)((gate_real32_t)deci->value * value);
133 ✗ }
134 ✗ void gate_decimal_div_flt(gate_decimal_t* deci, gate_real32_t value)
135 {
136 ✗ deci->value = (gate_decimal_primitive_t)((gate_real32_t)deci->value / value);
137 ✗ }
138 ✗ void gate_decimal_sqrt(gate_decimal_t* deci)
139 {
140 static gate_decimal_t n2 = GATE_DECIMAL_INIT(2, 0);
141 ✗ gate_decimal_t a = GATE_DECIMAL_INIT(1255, 0);
142 gate_decimal_t b;
143 unsigned n;
144
145 ✗ if (gate_decimal_compare(deci, &n2) < 0)
146 {
147 ✗ return;
148 }
149
150 ✗ for (n = 0; n < 8; ++n)
151 {
152 ✗ b.value = (deci->value / a.value) * GATE_DECIMAL_FACTOR;
153 ✗ a.value = (a.value + b.value) / 2;
154 }
155 ✗ deci->value = a.value;
156 }
157
158
159 535 gate_bool_t gate_math_iszero(gate_real64_t num)
160 {
161
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 514 times.
535 if (num < 0.0)
162 {
163 21 return (-num < DBL_EPSILON) ? true : false;
164 }
165 else
166 {
167 514 return (num < DBL_EPSILON) ? true : false;
168 }
169 }
170 2566 gate_bool_t gate_math_iszerof(gate_real32_t num)
171 {
172
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2562 times.
2566 if (num < 0.0f)
173 {
174 4 return (-num < FLT_EPSILON) ? true : false;
175 }
176 else
177 {
178 2562 return (num < FLT_EPSILON) ? true : false;
179 }
180 }
181
182 422 gate_bool_t gate_math_isnan(gate_real64_t num)
183 {
184 422 volatile gate_real64_t tmp = num;
185 422 return (tmp != num) ? true : false;
186 }
187 3 gate_bool_t gate_math_isnanf(gate_real32_t num)
188 {
189 3 volatile gate_real32_t tmp = num;
190 3 return (tmp != num) ? true : false;
191 }
192
193 1 gate_real32_t gate_math_build_nanf(void)
194 {
195 #if !defined(NAN)
196 static gate_real32_t volatile const zero = 0.0f;
197 1 return (zero / zero);
198 #else
199 return NAN;
200 #endif
201 }
202
203 1 gate_real64_t gate_math_build_nan(void)
204 {
205 #if !defined(NAN)
206 static gate_real64_t volatile const zero = 0.0;
207 1 return (zero / zero);
208 #else
209 return NAN;
210 #endif
211 }
212
213
214 422 int gate_math_isinfinite(gate_real64_t num)
215 {
216 #if defined(DBL_MAX)
217
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 421 times.
422 if (num > DBL_MAX)
218 {
219 1 return 1;
220 }
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 else if (num < -DBL_MAX)
222 {
223 ✗ return -1;
224 }
225 421 return 0;
226 #else
227 #define gate_math_infinite_pos (1.0 / 0.0)
228 #define gate_math_infinite_neg (-1.0 / 0.0)
229 if (num == gate_math_infinite_pos)
230 {
231 return 1;
232 }
233 else if (num == gate_math_infinite_neg)
234 {
235 return -1;
236 }
237 return 0;
238 #endif
239 }
240 3 int gate_math_isinfinitef(gate_real32_t num)
241 {
242 #if defined(FLT_MAX)
243
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (num > FLT_MAX)
244 {
245 1 return 1;
246 }
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 else if (num < -FLT_MAX)
248 {
249 ✗ return -1;
250 }
251 2 return 0;
252 #else
253 static gate_real32_t const gate_math_infinite_pos = 1.0f / 0.0f;
254 static gate_real32_t const gate_math_infinite_neg = -1.0f / 0.0f;
255 if (num == gate_math_infinite_pos)
256 {
257 return 1;
258 }
259 else if (num == gate_math_infinite_neg)
260 {
261 return -1;
262 }
263 return 0;
264 #endif
265
266 }
267
268 4 int gate_math_signum_i8(gate_int8_t num)
269 {
270
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 return (num < 0) ? -1 : ((num == 0) ? 0 : 1);
271 }
272 4 int gate_math_signum_i16(gate_int16_t num)
273 {
274
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 return (num < 0) ? -1 : ((num == 0) ? 0 : 1);
275 }
276 4 int gate_math_signum_i32(gate_int32_t num)
277 {
278
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 return (num < 0) ? -1 : ((num == 0) ? 0 : 1);
279 }
280 4 int gate_math_signum_i64(gate_int64_t num)
281 {
282
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 return (num < 0) ? -1 : ((num == 0) ? 0 : 1);
283 }
284 4 int gate_math_signum_r32(gate_real32_t num)
285 {
286
4/4
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
4 return gate_math_iszerof(num) ? 0 : ((num < 0.0f) ? -1 : 1);
287 }
288 4 int gate_math_signum_r64(gate_real64_t num)
289 {
290
4/4
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
4 return gate_math_iszero(num) ? 0 : ((num < 0.0) ? -1 : 1);
291 }
292
293
294 4 gate_int8_t gate_math_abs_i8(gate_int8_t num)
295 {
296 4 return num < 0 ? -num : num;
297 }
298 820 gate_int16_t gate_math_abs_i16(gate_int16_t num)
299 {
300 820 return num < 0 ? -num : num;
301 }
302 164 gate_int32_t gate_math_abs_i32(gate_int32_t num)
303 {
304 164 return num < 0 ? -num : num;
305 }
306 5 gate_int64_t gate_math_abs_i64(gate_int64_t num)
307 {
308 5 return num < 0 ? -num : num;
309 }
310 7 gate_real32_t gate_math_abs_r32(gate_real32_t num)
311 {
312
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
7 return num < 0.0f ? -num : num;
313 }
314 7 gate_real64_t gate_math_abs_r64(gate_real64_t num)
315 {
316
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
7 return num < 0.0 ? -num : num;
317 }
318
319 #define gate_math_min_type(type, va_type, count) \
320 type ret = 0; \
321 type tmp; \
322 va_list vl; \
323 if(count != 0) \
324 { \
325 va_start(vl, count); \
326 ret = (type)va_arg(vl, va_type); \
327 while(--count) \
328 { \
329 tmp = (type)va_arg(vl, va_type); \
330 if(tmp < ret) \
331 { \
332 ret = tmp; \
333 } \
334 } \
335 va_end(vl); \
336 } \
337 return ret
338
339 #define gate_math_max_type(type, va_type, count) \
340 type ret = 0; \
341 type tmp; \
342 va_list vl; \
343 if(count != 0) \
344 { \
345 va_start(vl, count); \
346 ret = (type)va_arg(vl, va_type); \
347 while(--count) \
348 { \
349 tmp = (type)va_arg(vl, va_type); \
350 if(tmp > ret) \
351 { \
352 ret = tmp; \
353 } \
354 } \
355 va_end(vl); \
356 } \
357 return ret
358
359
360 1 gate_int16_t gate_math_min_i16(gate_size_t count, ...)
361 {
362
5/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
3 gate_math_min_type(gate_int16_t, int, count);
363 }
364 1 gate_int32_t gate_math_min_i32(gate_size_t count, ...)
365 {
366
5/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
3 gate_math_min_type(gate_int32_t, gate_int32_t, count);
367 }
368 1 gate_int64_t gate_math_min_i64(gate_size_t count, ...)
369 {
370
5/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
3 gate_math_min_type(gate_int64_t, gate_int64_t, count);
371 }
372 1 gate_real32_t gate_math_min_r32(gate_size_t count, ...)
373 {
374
5/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
3 gate_math_min_type(gate_real32_t, gate_real64_t, count);
375 }
376 1 gate_real64_t gate_math_min_r64(gate_size_t count, ...)
377 {
378
5/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
3 gate_math_min_type(gate_real64_t, gate_real64_t, count);
379 }
380
381 273 gate_int16_t gate_math_max_i16(gate_size_t count, ...)
382 {
383
5/6
✓ Branch 0 taken 273 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 242 times.
✓ Branch 5 taken 304 times.
✓ Branch 6 taken 546 times.
✓ Branch 7 taken 273 times.
819 gate_math_max_type(gate_int16_t, int, count);
384 }
385 1 gate_int32_t gate_math_max_i32(gate_size_t count, ...)
386 {
387
5/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
3 gate_math_max_type(gate_int32_t, gate_int32_t, count);
388 }
389 1 gate_int64_t gate_math_max_i64(gate_size_t count, ...)
390 {
391
5/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
3 gate_math_max_type(gate_int64_t, gate_int64_t, count);
392 }
393 1 gate_real32_t gate_math_max_r32(gate_size_t count, ...)
394 {
395
5/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
3 gate_math_max_type(gate_real32_t, gate_real64_t, count);
396 }
397 1 gate_real64_t gate_math_max_r64(gate_size_t count, ...)
398 {
399
5/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
3 gate_math_max_type(gate_real64_t, gate_real64_t, count);
400 }
401
402 #define GATE_MATH_TYPED_DECIMAL_LENGTH(num_type, value, zero_check_func, max_len) \
403 gate_uint8_t len = 0; \
404 const num_type factor_10 = (num_type)10; \
405 num_type factor = (num_type)1; \
406 num_type test_value; \
407 num_type diff; \
408 if (value < (num_type)0) { \
409 value = -value; \
410 } \
411 while (len < max_len) { \
412 test_value = value * factor; \
413 diff = (test_value - ((num_type)(gate_int64_t)test_value)); \
414 if ( zero_check_func(diff) ) break; \
415 factor *= factor_10; \
416 ++len; \
417 } \
418 return len
419
420
421 1 gate_uint8_t gate_math_decimal_length_r32(gate_real32_t value)
422 {
423
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
4 GATE_MATH_TYPED_DECIMAL_LENGTH(gate_real32_t, value, gate_math_iszerof, 7);
424 }
425
426 7 gate_uint8_t gate_math_decimal_length(gate_real64_t value)
427 {
428
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 23 times.
✓ Branch 5 taken 30 times.
✗ Branch 6 not taken.
30 GATE_MATH_TYPED_DECIMAL_LENGTH(gate_real64_t, value, gate_math_iszero, 16);
429 }
430
431 27 gate_real64_t gate_math_deg2rad(gate_real64_t value)
432 {
433 27 return value * GATE_MATH_CONST_PI / 180.0;
434 }
435 19 gate_real64_t gate_math_rad2deg(gate_real64_t value)
436 {
437 19 return value * 180.0 / GATE_MATH_CONST_PI;
438 }
439
440 3 gate_int64_t gate_math_pow_i64(gate_int64_t base, gate_uint16_t exp)
441 {
442 gate_uint8_t n;
443 3 gate_int64_t ret = 1;
444
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 3 times.
60 for (n = 0; n != exp; ++n)
445 {
446 57 ret *= base;
447 }
448 3 return ret;
449 }
450
451 ✗ gate_real64_t gate_math_pow_iexp(gate_real64_t base, gate_uint16_t exp)
452 {
453 gate_uint16_t n;
454 ✗ gate_real64_t ret = 1.0;
455 ✗ for (n = 0; n != exp; ++n)
456 {
457 ✗ ret *= base;
458 }
459 ✗ return ret;
460 }
461
462 5 gate_real64_t gate_math_acosh(gate_real64_t value)
463 {
464 5 return gate_math_log(value + gate_math_sqrt(value * value - 1.0));
465 }
466 6 gate_real64_t gate_math_asinh(gate_real64_t value)
467 {
468 6 return gate_math_log(value + gate_math_sqrt(value * value + 1.0));
469 }
470 6 gate_real64_t gate_math_atanh(gate_real64_t value)
471 {
472 6 return gate_math_log((1.0 + value) / (1.0 - value)) / 2.0;
473 }
474
475
476
477
478
479
480
481 #if defined(GATE_CORE_MATH_STD_IMPL)
482
483 #include <math.h>
484
485 42 gate_real64_t gate_math_cos(gate_real64_t value)
486 {
487 42 return cos(value);
488 }
489 9 gate_real64_t gate_math_sin(gate_real64_t value)
490 {
491 9 return sin(value);
492 }
493 11 gate_real64_t gate_math_tan(gate_real64_t value)
494 {
495 11 return tan(value);
496 }
497 6 gate_real64_t gate_math_acos(gate_real64_t value)
498 {
499 6 return acos(value);
500 }
501 9 gate_real64_t gate_math_asin(gate_real64_t value)
502 {
503 9 return asin(value);
504 }
505 16 gate_real64_t gate_math_atan(gate_real64_t value)
506 {
507 16 return atan(value);
508 }
509 2 gate_real64_t gate_math_atan2(gate_real64_t y, gate_real64_t x)
510 {
511 2 return atan2(y, x);
512 }
513 6 gate_real64_t gate_math_cosh(gate_real64_t value)
514 {
515 6 return cosh(value);
516 }
517 6 gate_real64_t gate_math_sinh(gate_real64_t value)
518 {
519 6 return sinh(value);
520 }
521 6 gate_real64_t gate_math_tanh(gate_real64_t value)
522 {
523 6 return tanh(value);
524 }
525
526 13 gate_real64_t gate_math_exp(gate_real64_t value)
527 {
528 13 return exp(value);
529 }
530 2 gate_real64_t gate_math_frexp(gate_real64_t value, int* exp)
531 {
532 2 return frexp(value, exp);
533 }
534 3 gate_real64_t gate_math_ldexp(gate_real64_t value, int exp)
535 {
536 3 return ldexp(value, exp);
537 }
538 26 gate_real64_t gate_math_log(gate_real64_t value)
539 {
540 26 return log(value);
541 }
542 6 gate_real64_t gate_math_log2(gate_real64_t value)
543 {
544 static const gate_real64_t log2inverse = 1.4426950408889634;
545 6 return log(value) * log2inverse;
546 }
547 6 gate_real64_t gate_math_log10(gate_real64_t value)
548 {
549 6 return log10(value);
550 }
551 2 gate_real64_t gate_math_modf(gate_real64_t value, double* intpart)
552 {
553 2 return modf(value, intpart);
554 }
555
556 2 gate_real64_t gate_math_pow(gate_real64_t base, gate_real64_t exp)
557 {
558 2 return pow(base, exp);
559 }
560 21 gate_real64_t gate_math_sqrt(gate_real64_t value)
561 {
562 21 return sqrt(value);
563 }
564
565 7 gate_real64_t gate_math_ceil(gate_real64_t value)
566 {
567 7 return ceil(value);
568 }
569 13 gate_real64_t gate_math_floor(gate_real64_t value)
570 {
571 13 return floor(value);
572 }
573
574
575 #endif /* GATE_CORE_MATH_STD_IMPL */
576
577
578
579 #if defined(GATE_CORE_MATH_EFI_IMPL)
580
581 static gate_real64_t normalize_angle(gate_real64_t angle)
582 {
583 while (angle <= -GATE_MATH_CONST_PI)
584 {
585 angle += GATE_MATH_CONST_PI;
586 }
587 while (angle > GATE_MATH_CONST_PI)
588 {
589 angle -= GATE_MATH_CONST_PI;
590 }
591 return angle;
592 }
593
594 gate_real64_t gate_math_cos(gate_real64_t value)
595 {
596 static gate_real64_t const n2 = 2.0;
597 static gate_real64_t const n4 = 24.0;
598 static gate_real64_t const n6 = 720.0;
599 static gate_real64_t const n8 = 40320.0;
600 static gate_real64_t const n10 = 3628800.0;
601 static gate_real64_t const n12 = 479001600.0;
602 gate_real64_t const x = normalize_angle(value);
603 gate_real64_t const x2 = x * x;
604 gate_real64_t const x4 = x2 * x * x;
605 gate_real64_t const x6 = x4 * x * x;
606 gate_real64_t const x8 = x6 * x * x;
607 gate_real64_t const x10 = x8 * x * x;
608 gate_real64_t const x12 = x10 * x * x;
609
610 return 1 - x2 / n2 + x4 / n4 - x6 / n6 + x8 / n8 - x10 / n10 + x12 / n12;
611 }
612
613
614
615 gate_real64_t gate_math_sin(gate_real64_t value)
616 {
617 static gate_real64_t const n3 = 6.0;
618 static gate_real64_t const n5 = 120.0;
619 static gate_real64_t const n7 = 5040.0;
620 static gate_real64_t const n9 = 362880.0;
621 static gate_real64_t const n11 = 39916800.0;
622 static gate_real64_t const n13 = 6227020800.0;
623 gate_real64_t const x = normalize_angle(value);
624 gate_real64_t const x3 = x * x * x;
625 gate_real64_t const x5 = x3 * x * x;
626 gate_real64_t const x7 = x5 * x * x;
627 gate_real64_t const x9 = x7 * x * x;
628 gate_real64_t const x11 = x9 * x * x;
629 gate_real64_t const x13 = x11 * x * x;
630
631 return x - x3 / n3 + x5 / n5 - x7 / n7 + x9 / n9 - x11 / n11 + x13 / n13;
632 }
633 gate_real64_t gate_math_tan(gate_real64_t value)
634 {
635 return gate_math_sin(value) / gate_math_cos(value);
636 }
637
638 gate_real64_t gate_math_asin(gate_real64_t value)
639 {
640 /* https://de.wikipedia.org/wiki/Arkussinus_und_Arkuskosinus#Reihenentwicklungen */
641
642 static double const ret_0_96 = 1.2870022176;
643 /*static double const ret_1_00 = GATE_MATH_CONST_PI * 0.5;*/
644 static double const diff_1_00 = GATE_MATH_CONST_PI * 0.5 - 1.2870022176;
645 double ret = value;
646
647 if (value > 0.96)
648 {
649 /* iterative solution becomes unstable for value > 0.96
650 just apply a quick and dirty linear patch */
651 double factor = (value - 0.96) / (1.00 - 0.96);
652 ret = ret_0_96 + diff_1_00 * factor;
653 }
654 else
655 {
656 static unsigned const n_max = 107; /* quite OK up to 0.96 */
657 unsigned n;
658 double x = value;
659 double a = 1;
660 double b = 1;
661 for (n = 3; n != n_max; n += 2)
662 {
663 x *= value * value;
664 a *= ((double)n - 2.0);
665 b *= ((double)n - 1.0);
666 ret += a * x / (b * (double)n);
667 }
668 }
669
670 return ret;
671 }
672 gate_real64_t gate_math_acos(gate_real64_t value)
673 {
674 /* https://stackoverflow.com/questions/3380628/fast-arc-cos-algorithm/20914630 */
675 /* return (-0.69813170079773212 * value * value - 0.87266462599716477) * value + 1.5707963267948966; */
676 return GATE_MATH_CONST_PI * 0.5 - gate_math_asin(value);
677 }
678 gate_real64_t gate_math_atan(gate_real64_t value)
679 {
680 gate_real64_t ret;
681 ret = gate_math_asin(value / gate_math_sqrt(1 + value * value));
682 return ret;
683 }
684 gate_real64_t gate_math_atan2(gate_real64_t y, gate_real64_t x)
685 {
686 /* TODO */
687 return 0.0;
688 }
689 gate_real64_t gate_math_sinh(gate_real64_t value)
690 {
691 return 0.5 * (gate_math_exp(value) - gate_math_exp(-value));
692 }
693 gate_real64_t gate_math_cosh(gate_real64_t value)
694 {
695 return 0.5 * (gate_math_exp(value) + gate_math_exp(-value));
696 }
697 gate_real64_t gate_math_tanh(gate_real64_t value)
698 {
699 return gate_math_sinh(value) / gate_math_cosh(value);
700 }
701
702 gate_real64_t gate_math_exp(gate_real64_t value)
703 {
704 /* https://www.pseudorandom.com/implementing-exp */
705 gate_real64_t x0, tn;
706 gate_int64_t i, n;
707
708 if (gate_math_iszero(value))
709 {
710 return 1.0;
711 }
712 x0 = gate_math_abs_r64(value);
713 tn = 1.0;
714 n = 12;
715 n *= (gate_int64_t)gate_math_ceil(x0 * GATE_MATH_CONST_E);
716 for (i = n; i > 0; i--)
717 {
718 tn = tn * (x0 / i) + 1.0;
719 }
720
721 if (value < 0.0)
722 {
723 tn = 1.0 / tn;
724 }
725 return tn;
726 }
727 gate_real64_t gate_math_frexp(gate_real64_t value, int* exp)
728 {
729 /* TODO */
730 return 0.0;
731 }
732 gate_real64_t gate_math_ldexp(gate_real64_t value, int exp)
733 {
734 /* TODO */
735 return 0.0;
736 }
737
738 static unsigned msb(unsigned v)
739 {
740 unsigned ret = 0;
741 while (v >>= 1)
742 {
743 ret++;
744 }
745 return ret;
746 }
747
748 static float log_n(float value)
749 {
750 /* https://stackoverflow.com/questions/9799041/efficient-implementation-of-natural-logarithm-ln-and-exponentiation */
751 int log2;
752 float divisor, x, result;
753
754 log2 = msb((int)value); // See: https://stackoverflow.com/a/4970859/6630230
755 divisor = (float)(1 << log2);
756 x = value / divisor;
757 result = -1.7417939f + (2.8212026f + (-1.4699568f + (0.44717955f - 0.056570851f * x) * x) * x) * x;
758 result += ((float)log2) * 0.69314718f;
759
760 return result;
761 }
762
763
764 gate_real64_t gate_math_log(gate_real64_t value)
765 {
766 return (gate_real64_t)log_n((float)value);
767 }
768 gate_real64_t gate_math_log10(gate_real64_t value)
769 {
770 return gate_math_log(value) / gate_math_log(10.0);
771 }
772 gate_real64_t gate_math_modf(gate_real64_t value, gate_real64_t* intpart)
773 {
774 gate_real64_t ret;
775 gate_uint64_t ui;
776 if (value < 0.0)
777 {
778 ui = (gate_uint64_t)-value;
779 ret = -value - (gate_real64_t)ui;
780 if (intpart)
781 {
782 *intpart = -((gate_real64_t)ui);
783 }
784 }
785 else
786 {
787 ui = (gate_uint64_t)value;
788 ret = value - (gate_real64_t)ui;
789 if (intpart)
790 {
791 *intpart = (gate_real64_t)ui;
792 }
793 }
794 return ret;
795 }
796
797 gate_real64_t gate_math_pow(gate_real64_t base, gate_real64_t exp)
798 {
799 if (exp < 0.0)
800 {
801 return 1.0 / gate_math_pow_iexp(base, (gate_uint16_t)-exp);
802 }
803 else
804 {
805 return gate_math_pow_iexp(base, (gate_uint16_t)exp);
806 }
807 return 0.0;
808 }
809
810 union f_rsqrt_helper
811 {
812 float f;
813 gate_uint32_t i;
814 };
815
816
817 union d_rsqrt_helper
818 {
819 double d;
820 gate_uint64_t i;
821 };
822
823 /* https://en.wikipedia.org/wiki/Fast_inverse_square_root */
824 /* https://stackoverflow.com/questions/11644441/fast-inverse-square-root-on-x64/11644533 */
825 static float f_rsqrt(float number)
826 {
827 static const float threehalfs = 1.5F;
828 const float x2 = number * 0.5F;
829
830 union f_rsqrt_helper conv;
831 conv.f = number;
832 conv.i = 0x5f3759df - (conv.i >> 1);
833 conv.f *= threehalfs - (x2 * conv.f * conv.f);
834 return conv.f;
835 }
836
837 static double d_rsqrt(double number)
838 {
839 double y = number;
840 double x2 = y * 0.5;
841
842 union d_rsqrt_helper conv;
843 conv.d = y;
844 conv.i = 0x5fe6eb50c7b537a9 - (conv.i >> 1);
845 y = conv.d;
846 y = y * (1.5 - (x2 * y * y));
847 y = y * (1.5 - (x2 * y * y));
848 return y;
849 }
850
851
852 gate_real64_t gate_math_sqrt(gate_real64_t value)
853 {
854 double num = gate_math_abs_r64((double)value);
855 return (gate_real64_t)(1.0 / d_rsqrt(num));
856 }
857
858 gate_real64_t gate_math_ceil(gate_real64_t value)
859 {
860 gate_int64_t i = (gate_int64_t)value;
861 gate_real64_t diff = value - (gate_real64_t)i;
862 if (!gate_math_iszero(diff))
863 {
864 if (value >= 0.0)
865 {
866 i = (gate_int64_t)(value + 1.0);
867 }
868 else
869 {
870 i = -(gate_int64_t)(-value);
871 }
872 }
873 return (gate_real64_t)i;
874 }
875 gate_real64_t gate_math_floor(gate_real64_t value)
876 {
877 gate_int64_t i = (gate_int64_t)value;
878 gate_real64_t diff = value - (gate_real64_t)i;
879 if (!gate_math_iszero(diff))
880 {
881 if (value >= 0.0)
882 {
883 i = (gate_int64_t)(value);
884 }
885 else
886 {
887 i = -(gate_int64_t)(-value + 1.0);
888 }
889 }
890 return (gate_real64_t)i;
891 }
892
893 #endif /* GATE_CORE_MATH_EFI_IMPL */
894
895