1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354 | #include "test_math.h"
#include "gate/tests.h"
#include "gate/mathematics.h"
#include <math.h>
#if !defined(NAN) || !defined(INFINITY)
static gate_real32_t zero_float = 0.0f;
static gate_real64_t zero_double = 0.0;
#endif
#if defined(NAN)
static gate_real32_t create_float_nan() { return NAN; }<--- Skipping configuration 'INFINITY;NAN' since the value of 'NAN' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'NAN' since the value of 'NAN' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
static gate_real64_t create_double_nan() { return NAN; }<--- Skipping configuration 'INFINITY;NAN' since the value of 'NAN' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'NAN' since the value of 'NAN' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#else
static gate_real32_t create_float_nan() { return zero_float / zero_float; }
static gate_real64_t create_double_nan() { return zero_double / zero_double; }
#endif
#if defined(INFINITY)
static gate_real32_t create_float_inf() { return INFINITY; }<--- Skipping configuration 'INFINITY' since the value of 'INFINITY' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'INFINITY;NAN' since the value of 'INFINITY' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
static gate_real64_t create_double_inf() { return INFINITY; }<--- Skipping configuration 'INFINITY' since the value of 'INFINITY' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'INFINITY;NAN' since the value of 'INFINITY' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#else
static gate_real32_t create_float_inf() { return 1.0f / zero_float; }
static gate_real64_t create_double_inf() { return 1.0 / zero_double; }
#endif
typedef struct
{
gate_real64_t parameter;
gate_real64_t expected;
} math_value_table_t;
static gate_bool_t test_math_table(gate_real64_t(*func)(gate_real64_t), math_value_table_t const* table, gate_size_t table_count)
{
gate_size_t ndx;
gate_real64_t result;
gate_bool_t test_succeeded;
gate_bool_t succeeded = true;
for (ndx = 0; ndx != table_count; ++ndx)
{
result = func(table[ndx].parameter);
test_succeeded = true;
test_succeeded &= (result > table[ndx].expected - 0.0001);
test_succeeded &= (result < table[ndx].expected + 0.0001);
GATE_TEST_TRACE_MESSAGE(test_succeeded ? "test_math_table SUCCEEDED" : "test_math_table FAILED");
succeeded &= test_succeeded;
}
return succeeded;
}
static math_value_table_t sin_table[] =
{
{ 0.0, 0.0 },
{ 1.0, 0.84147098 },
{ 2.0, 0.90929742 },
{ 3.0, 0.14112000 },
{ -1.0, -0.84147098 },
{ -2.0, -0.90929742 },
{ -3.0, -0.14112000 }
};
static math_value_table_t cos_table[] =
{
{ 0.0, 1.0 },
{ 1.0, 0.54030230 },
{ 2.0, -0.41614683 },
{ 3.0, -0.98999249 },
{ -1.0, 0.54030230 },
{ -2.0, -0.41614683 },
{ -3.0, -0.98999249 }
};
static math_value_table_t tan_table[] =
{
{ 0.0, 0.0 },
{ 1.0, 1.5574077246549 },
{ 2.0, -2.1850398632615 },
{ 3.0, -0.1425465431 },
{ 4.0, 1.15782128235 },
};
static math_value_table_t asin_table[] =
{
{ 0.0, 0.0 },
{ 0.3, 0.304692654 },
{ 0.6, 0.643501109 },
{ 0.9, 1.119769515 },
{ 0.95, 1.2532358975 },
{ 0.96, 1.2870022176 },
{ 1.0, 1.570796326794 }
};
static math_value_table_t acos_table[] =
{
{ 0.0, 1.5707963268 },
{ 0.3, 1.26610367278 },
{ 0.6, 0.927295218 },
{ 1.0, 0.0 }
};
static math_value_table_t atan_table[] =
{
{ 0.0, 0.0 },
{ 0.1, 0.09966865249 },
{ 0.5, 0.463647609 },
{ 0.9, 0.73281510179 },
{ 0.98, 0.7752974968 },
{ 1.0, 0.785398163397 },
{ 2.0, 1.107148717794 },
{ 3.0, 1.249045772398 }
};
static math_value_table_t sinh_table[] =
{
{ 0.0, 0.0 },
{ 1.0, 1.1752011936438 },
{ 2.0, 3.626860407847 },
{ 3.0, 10.0178749274 }
};
static math_value_table_t cosh_table[] =
{
{ 0.0, 1.0 },
{ 1.0, 1.5430806348 },
{ 2.0, 3.762195691 },
{ 3.0, 10.0676619958 }
};
static math_value_table_t tanh_table[] =
{
{ 0.0, 0.0 },
{ 1.0, 0.761594155956 },
{ 2.0, 0.96402758 },
{ 3.0, 0.995054753687 },
};
static math_value_table_t asinh_table[] =
{
{ 0.0, 0.0 },
{ 1.0, 0.881373587 },
{ 2.0, 1.443635475 },
{ 3.0, 1.818446459 }
};
static math_value_table_t acosh_table[] =
{
{ 1.0, 0.0 },
{ 2.0, 1.3169578969 },
{ 3.0, 1.762747174039 }
};
static math_value_table_t atanh_table[] =
{
{ 0.0, 0.0 },
{ 0.5, 0.549306144334 },
{ 0.9, 1.472219489583 },
{ 0.1, 0.100335347731 }
};
static math_value_table_t exp_table[] =
{
{ 0.0, 1.0 },
{ 1.0, GATE_MATH_CONST_E },
{ 5.0, 148.4131591 },
{ 12.0, 162754.791419 },
{ 24.0, 26489122129.843472 }
};
static math_value_table_t log_table[] =
{
{ 1.0, 0.0 },
{ 10.0, 2.302585092994 },
{ 20000.0, 9.903487552536 },
{ 4000000.0, 15.2018049191 }
};
static math_value_table_t log10_table[] =
{
{ 1.0, 0.0 },
{ 2.0, 0.30103 },
{ 22.0, 1.342422681 },
{ 2222.0, 3.3467440546 }
};
static math_value_table_t sqrt_table[] =
{
{ 4.0, 2.0 },
{ 25.0, 5.0 },
{ 10.0, 3.162277 },
{ 40.0, 6.324555 },
{ 32767.0, 181.016573 }
};
static math_value_table_t ceil_table[] =
{
{ 0.0, 0.0 },
{ 42.0, 42.0 },
{ 42.1, 43.0 },
{ -42.0, -42.0 },
{ -42.1, -42.0 }
};
static math_value_table_t floor_table[] =
{
{ 0.0, 0.0 },
{ 42.0, 42.0 },
{ 42.1, 42.0 },
{ -42.0, -42.0 },
{ -42.1, -43.0 }
};
static math_value_table_t deg2rad_table[] =
{
{ 0.0, 0.0 },
{ 90.0, GATE_MATH_CONST_PI / 2.0 },
{ 180.0, GATE_MATH_CONST_PI },
{ 270.0, GATE_MATH_CONST_PI * 1.5 },
{ 360.0, GATE_MATH_CONST_PI * 2.0 }
};
static math_value_table_t rad2deg_table[] =
{
{ 0.0, 0.0 },
{ GATE_MATH_CONST_PI / 2.0, 90.0 },
{ GATE_MATH_CONST_PI, 180.0 },
{ -GATE_MATH_CONST_PI / 2.0, -90.0 },
{ -GATE_MATH_CONST_PI, -180.0 }
};
GATE_TEST_FUNCTION(test_math)
{
GATE_TEST_UNIT_BEGIN(test_math);
GATE_TEST_CHECK_EQUAL(gate_math_iszero(0.0), true);
GATE_TEST_CHECK_EQUAL(gate_math_iszero(0.1), false);
GATE_TEST_CHECK_EQUAL(gate_math_iszero(-0.1), false);
GATE_TEST_CHECK_EQUAL(gate_math_iszero(0.0f), true);
GATE_TEST_CHECK_EQUAL(gate_math_iszero(0.1f), false);
GATE_TEST_CHECK_EQUAL(gate_math_iszero(-0.1f), false);
GATE_TEST_CHECK_EQUAL(gate_math_isnan(1.0), false);
GATE_TEST_CHECK_EQUAL(gate_math_isnan(create_double_nan()), true);
GATE_TEST_CHECK_EQUAL(gate_math_isnanf(1.0f), false);
GATE_TEST_CHECK_EQUAL(gate_math_isnanf(create_float_nan()), true);
GATE_TEST_CHECK_EQUAL(gate_math_isinfinite(1.0), false);
GATE_TEST_CHECK_EQUAL(gate_math_isinfinite(create_double_inf()), true);
GATE_TEST_CHECK_EQUAL(gate_math_isinfinitef(1.0f), false);
GATE_TEST_CHECK_EQUAL(gate_math_isinfinitef(create_float_inf()), true);
GATE_TEST_CHECK(gate_math_signum_i8(0) == 0);
GATE_TEST_CHECK(gate_math_signum_i8(1) > 0);
GATE_TEST_CHECK(gate_math_signum_i8(-1) < 0);
GATE_TEST_CHECK(gate_math_signum_i16(0) == 0);
GATE_TEST_CHECK(gate_math_signum_i16(1) > 0);
GATE_TEST_CHECK(gate_math_signum_i16(-1) < 0);
GATE_TEST_CHECK(gate_math_signum_i32(0) == 0);
GATE_TEST_CHECK(gate_math_signum_i32(1) > 0);
GATE_TEST_CHECK(gate_math_signum_i32(-1) < 0);
GATE_TEST_CHECK(gate_math_signum_i64(0) == 0);
GATE_TEST_CHECK(gate_math_signum_i64(1) > 0);
GATE_TEST_CHECK(gate_math_signum_i64(-1) < 0);
GATE_TEST_CHECK(gate_math_signum_r32(0.0f) == 0);
GATE_TEST_CHECK(gate_math_signum_r32(1.0f) > 0);
GATE_TEST_CHECK(gate_math_signum_r32(-1.0f) < 0);
GATE_TEST_CHECK(gate_math_signum_r64(0.0) == 0);
GATE_TEST_CHECK(gate_math_signum_r64(1.0) > 0);
GATE_TEST_CHECK(gate_math_signum_r64(-1.0) < 0);
GATE_TEST_CHECK(gate_math_abs_i8(0) == 0);
GATE_TEST_CHECK(gate_math_abs_i8(42) == 42);
GATE_TEST_CHECK(gate_math_abs_i8(-42) == 42);
GATE_TEST_CHECK(gate_math_abs_i16(0) == 0);
GATE_TEST_CHECK(gate_math_abs_i16(42) == 42);
GATE_TEST_CHECK(gate_math_abs_i16(-42) == 42);
GATE_TEST_CHECK(gate_math_abs_i32(0) == 0);
GATE_TEST_CHECK(gate_math_abs_i32(42) == 42);
GATE_TEST_CHECK(gate_math_abs_i32(-42) == 42);
GATE_TEST_CHECK(gate_math_abs_i64(0) == 0);
GATE_TEST_CHECK(gate_math_abs_i64(42) == 42);
GATE_TEST_CHECK(gate_math_abs_i64(-42) == 42);
GATE_TEST_CHECK(gate_math_iszero(gate_math_abs_r32(0.0f)));
GATE_TEST_CHECK(gate_math_iszero(gate_math_abs_r32(42.0f) - 42.0f));
GATE_TEST_CHECK(gate_math_iszero(gate_math_abs_r32(-42.0f) - 42.0f));
GATE_TEST_CHECK(gate_math_iszero(gate_math_abs_r32(0.0)));
GATE_TEST_CHECK(gate_math_iszero(gate_math_abs_r32(42.0) - 42.0));
GATE_TEST_CHECK(gate_math_iszero(gate_math_abs_r32(-42.0) - 42.0));
GATE_TEST_CHECK_EQUAL(gate_math_min_i16(3, (gate_int16_t)2, (gate_int16_t)1, (gate_int16_t)3), 1);
GATE_TEST_CHECK_EQUAL(gate_math_min_i32(3, (gate_int32_t)2, (gate_int32_t)1, (gate_int32_t)3), 1);
GATE_TEST_CHECK_EQUAL(gate_math_min_i64(3, (gate_int64_t)2, (gate_int64_t)1, (gate_int64_t)3), 1);
GATE_TEST_CHECK(gate_math_iszerof(gate_math_min_r32(3, 2.0f, 1.0f, 3.0f) - 1.0f));
GATE_TEST_CHECK(gate_math_iszero(gate_math_min_r64(3, 2.0, 1.0, 3.0) - 1.0));
GATE_TEST_CHECK_EQUAL(gate_math_max_i16(3, (gate_int16_t)2, (gate_int16_t)1, (gate_int16_t)3), 3);
GATE_TEST_CHECK_EQUAL(gate_math_max_i32(3, (gate_int32_t)2, (gate_int32_t)1, (gate_int32_t)3), 3);
GATE_TEST_CHECK_EQUAL(gate_math_max_i64(3, (gate_int64_t)2, (gate_int64_t)1, (gate_int64_t)3), 3);
GATE_TEST_CHECK(gate_math_iszerof(gate_math_max_r32(3, 2.0f, 1.0f, 3.0f) - 3.0f));
GATE_TEST_CHECK(gate_math_iszero(gate_math_max_r64(3, 2.0, 1.0, 3.0) - 3.0));
GATE_TEST_CHECK_EQUAL(gate_math_decimal_length(0.12345), 5);
GATE_TEST_CHECK(test_math_table(&gate_math_sin, sin_table, sizeof(sin_table) / sizeof(sin_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_cos, cos_table, sizeof(cos_table) / sizeof(cos_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_tan, tan_table, sizeof(tan_table) / sizeof(tan_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_asin, asin_table, sizeof(asin_table) / sizeof(asin_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_acos, acos_table, sizeof(acos_table) / sizeof(acos_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_atan, atan_table, sizeof(atan_table) / sizeof(atan_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_sinh, sinh_table, sizeof(sinh_table) / sizeof(sinh_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_cosh, cosh_table, sizeof(cosh_table) / sizeof(cosh_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_tanh, tanh_table, sizeof(tanh_table) / sizeof(tanh_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_asinh, asinh_table, sizeof(asinh_table) / sizeof(asinh_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_acosh, acosh_table, sizeof(acosh_table) / sizeof(acosh_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_atanh, atanh_table, sizeof(atanh_table) / sizeof(atanh_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_exp, exp_table, sizeof(exp_table) / sizeof(exp_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_log, log_table, sizeof(log_table) / sizeof(log_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_log10, log10_table, sizeof(log10_table) / sizeof(log10_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_sqrt, sqrt_table, sizeof(sqrt_table) / sizeof(sqrt_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_ceil, ceil_table, sizeof(ceil_table) / sizeof(ceil_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_floor, floor_table, sizeof(floor_table) / sizeof(floor_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_deg2rad, deg2rad_table, sizeof(deg2rad_table) / sizeof(deg2rad_table[0])));
GATE_TEST_CHECK(test_math_table(&gate_math_rad2deg, rad2deg_table, sizeof(rad2deg_table) / sizeof(rad2deg_table[0])));
GATE_TEST_UNIT_END;
//GATE_CORE_API gate_real64_t gate_math_atan2(gate_real64_t y, gate_real64_t x);
//GATE_CORE_API gate_real64_t gate_math_frexp(gate_real64_t value, int* exp);
//GATE_CORE_API gate_real64_t gate_math_ldexp(gate_real64_t value, int exp);
//GATE_CORE_API gate_real64_t gate_math_modf(gate_real64_t value, gate_real64_t* intpart);
//GATE_CORE_API gate_int64_t gate_math_pow_i64(gate_int64_t base, gate_uint16_t exp);
//GATE_CORE_API gate_real64_t gate_math_pow_iexp(gate_real64_t base, gate_uint16_t exp);
//GATE_CORE_API gate_real64_t gate_math_pow(gate_real64_t base, gate_real64_t exp);
}
|