disable warning C5250: _BitScanReverse: intrinsic function not declared
[liba.git] / test / math.h
blob0ba3d5feb48a48757acb0ce1269c52bb5b28292f
1 #define MAIN_(x) A_CAST_2(x, _math)
2 #include "test.h"
3 #include "a/math.h"
5 #if defined(__MINGW32__) && A_PREREQ_GNUC(3, 0)
6 #pragma GCC diagnostic ignored "-Wfloat-conversion"
7 #endif /* __MINGW32__ */
8 #if A_PREREQ_GNUC(4, 6) || __has_warning("-Wdouble-promotion")
9 #pragma GCC diagnostic ignored "-Wdouble-promotion"
10 #endif /* -Wdouble-promotion */
12 static void test_u32_sqrt(void)
14 TEST_BUG(a_u32_sqrt(A_U32_C(~0)) == A_U16_C(0xFFFF));
15 TEST_BUG(a_u32_sqrt(A_U32_C(0x10000)) == A_U16_C(0x100));
16 TEST_BUG(a_u32_sqrt(A_U32_C(0xFFFFFFFF)) == A_U16_C(0xFFFF));
19 static void test_u64_sqrt(void)
21 TEST_BUG(a_u64_sqrt(A_U64_C(~0)) == A_U32_C(0xFFFFFFFF));
22 TEST_BUG(a_u64_sqrt(A_U64_C(0x10000)) == A_U32_C(0x100));
23 TEST_BUG(a_u64_sqrt(A_U64_C(0xFFFFFFFF)) == A_U32_C(0xFFFF));
24 TEST_BUG(a_u64_sqrt(A_U64_C(0x100000000)) == A_U32_C(0x10000));
25 TEST_BUG(a_u64_sqrt(A_U64_C(0xFFFFFFFFFFFFFFFF)) == A_U32_C(0xFFFFFFFF));
28 static void test_f32_rsqrt(void)
30 a_f32 data[] = {
31 A_F32_C(-1.0),
32 A_F32_C(-0.0),
33 A_F32_C(1e-10),
34 A_F32_C(1.0),
35 A_F32_C(4.0),
36 A_F32_C(2.5) * A_F32_C(2.5),
38 for (unsigned int i = 0; i != sizeof(data) / sizeof(a_f32); ++i)
40 debug("1/sqrt(%g):\t%-10g%-10g\n", data[i], 1 / a_f32_sqrt(data[i]), a_f32_rsqrt(data[i]));
44 static void test_f64_rsqrt(void)
46 a_f64 data[] = {
47 A_F64_C(-1.0),
48 A_F64_C(-0.0),
49 A_F64_C(1e-10),
50 A_F64_C(1.0),
51 A_F64_C(4.0),
52 A_F64_C(2.5) * A_F64_C(2.5),
54 for (unsigned int i = 0; i != sizeof(data) / sizeof(a_f64); ++i)
56 debug("1/sqrt(%g):\t%-10g%-10g\n", data[i], 1 / a_f64_sqrt(data[i]), a_f64_rsqrt(data[i]));
60 int MAIN(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
62 (void)(argc);
63 (void)(argv);
64 test_u32_sqrt();
65 test_u64_sqrt();
66 test_f32_rsqrt();
67 test_f64_rsqrt();
69 a_f64 min = A_F64_MIN;
70 a_f64 max = A_F64_MAX;
71 a_f64 inf = A_F64_INF;
72 a_f64 nan = A_F64_NAN;
73 debug("64 min = %-12g max = %g\n", min, max);
74 debug("64 inf = %-12g nan = %g\n", inf, nan);
77 a_f32 min = A_F32_MIN;
78 a_f32 max = A_F32_MAX;
79 a_f32 inf = A_F32_INF;
80 a_f32 nan = A_F32_NAN;
81 debug("32 min = %-12g max = %g\n", min, max);
82 debug("32 inf = %-12g nan = %g\n", inf, nan);
85 a_float min = A_FLOAT_MIN;
86 a_float max = A_FLOAT_MAX;
87 a_float inf = A_FLOAT_INF;
88 a_float nan = A_FLOAT_NAN;
89 debug("min = " A_FLOAT_PRI("-12", "g ") "max = " A_FLOAT_PRI("", "g\n"), min, max);
90 debug("inf = " A_FLOAT_PRI("-12", "g ") "nan = " A_FLOAT_PRI("", "g\n"), inf, nan);
93 #undef a_float_expm1
94 a_float x = A_FLOAT_EPSILON / 2;
95 TEST_BUG(isinf(a_float_expm1(A_FLOAT_INF)));
96 TEST_BUG(isnan(a_float_expm1(A_FLOAT_NAN)));
97 debug("expm1(" A_FLOAT_PRI(".15", "g") ")=" A_FLOAT_PRI(".15", "g\n"), x, a_float_expm1(x));
98 debug("exp(" A_FLOAT_PRI(".15", "g") ")-1=" A_FLOAT_PRI(".15", "g\n"), x, a_float_exp(x) - 1);
101 #undef a_float_log1p
102 a_float x = A_FLOAT_EPSILON / 2;
103 TEST_BUG(isinf(a_float_log1p(A_FLOAT_INF)));
104 TEST_BUG(isnan(a_float_log1p(A_FLOAT_NAN)));
105 debug("log1p(" A_FLOAT_PRI(".15", "g") ")=" A_FLOAT_PRI(".15", "g\n"), x, a_float_log1p(x));
106 debug("log(1+" A_FLOAT_PRI(".15", "g") ")=" A_FLOAT_PRI(".15", "g\n"), x, a_float_log(x + 1));
109 #undef a_float_hypot
110 a_float x = A_FLOAT_MAX / A_FLOAT_SQRT2;
111 a_float y = A_FLOAT_MAX / A_FLOAT_SQRT2;
112 TEST_BUG(!isinf(a_float_hypot(x, y)));
113 TEST_BUG(isinf(a_float_hypot(A_FLOAT_INF, A_FLOAT_NAN)));
114 TEST_BUG(isinf(a_float_hypot(A_FLOAT_NAN, A_FLOAT_INF)));
115 TEST_BUG(isnan(a_float_hypot(A_FLOAT_NAN, A_FLOAT_NAN)));
118 a_float x = A_FLOAT_MAX / A_FLOAT_SQRT3;
119 a_float y = A_FLOAT_MAX / A_FLOAT_SQRT3;
120 a_float z = A_FLOAT_MAX / A_FLOAT_SQRT3;
121 TEST_BUG(!isinf(a_float_hypot3(x, y, z)));
122 TEST_BUG(isinf(a_float_hypot3(A_FLOAT_INF, A_FLOAT_NAN, A_FLOAT_NAN)));
123 TEST_BUG(isinf(a_float_hypot3(A_FLOAT_NAN, A_FLOAT_INF, A_FLOAT_NAN)));
124 TEST_BUG(isinf(a_float_hypot3(A_FLOAT_NAN, A_FLOAT_NAN, A_FLOAT_INF)));
125 TEST_BUG(isnan(a_float_hypot3(A_FLOAT_NAN, A_FLOAT_NAN, A_FLOAT_NAN)));
127 return 0;