add math/modf benchmark
[libc-test] / src / math / classify.c
1 #include <stdio.h>
2 #include <math.h>
3 #include "test.h"
4
5 static struct {
6         float f;
7         int class;
8 } tf[] = {
9         0.0/0.0, FP_NAN,
10         -0.0/0.0, FP_NAN,
11         1/0.0, FP_INFINITE,
12         -1/0.0, FP_INFINITE,
13         0x1.ffffp127, FP_NORMAL,
14         -0x1.ffffp127, FP_NORMAL,
15         0x1p-127, FP_SUBNORMAL,
16         -0x1p-127, FP_SUBNORMAL,
17         0.0, FP_ZERO,
18         -0.0, FP_ZERO,
19         3.14, FP_NORMAL,
20         -42, FP_NORMAL,
21 };
22 static int ntf = sizeof tf / sizeof *tf;
23
24 static struct {
25         double f;
26         int class;
27 } td[] = {
28         0.0/0.0, FP_NAN,
29         -0.0/0.0, FP_NAN,
30         1/0.0, FP_INFINITE,
31         -1/0.0, FP_INFINITE,
32         0x1.ffffp1023, FP_NORMAL,
33         -0x1.ffffp1023, FP_NORMAL,
34         0x1p-1023, FP_SUBNORMAL,
35         -0x1p-1023, FP_SUBNORMAL,
36         0.0, FP_ZERO,
37         -0.0, FP_ZERO,
38         3.14, FP_NORMAL,
39         -42, FP_NORMAL,
40 };
41 static int ntd = sizeof td / sizeof *td;
42
43 static struct {
44         long double f;
45         int class;
46 } tl[] = {
47         0.0/0.0, FP_NAN,
48         -0.0/0.0, FP_NAN,
49         1/0.0, FP_INFINITE,
50         -1/0.0, FP_INFINITE,
51         0x1.ffffp16383L, FP_NORMAL,
52         -0x1.ffffp16383L, FP_NORMAL,
53         0x1p-16383L, FP_SUBNORMAL,
54         -0x1p-16383L, FP_SUBNORMAL,
55         0.0, FP_ZERO,
56         -0.0, FP_ZERO,
57         3.14, FP_NORMAL,
58         -42, FP_NORMAL,
59 };
60 static int ntl = sizeof tl / sizeof *tl;
61
62
63 void test_fpclassify()
64 {
65         int i;
66
67         for (i = 0; i < ntf; i++) {
68                 if (fpclassify(tf[i].f) != tf[i].class)
69                         error("%a want class %d got %d\n", tf[i].f, tf[i].class, fpclassify(tf[i].f));
70                 if (!!isinf(tf[i].f) != (tf[i].class == FP_INFINITE))
71                         error("%a want class %d got isinf %d\n", tf[i].f, tf[i].class, isinf(tf[i].f));
72                 if (!!isnan(tf[i].f) != (tf[i].class == FP_NAN))
73                         error("%a want class %d got isnan %d\n", tf[i].f, tf[i].class, isnan(tf[i].f));
74                 if (!!isnormal(tf[i].f) != (tf[i].class == FP_NORMAL))
75                         error("%a want class %d got isnormal %d\n", tf[i].f, tf[i].class, isnormal(tf[i].f));
76                 if (!!isfinite(tf[i].f) != (tf[i].class > FP_INFINITE))
77                         error("%a want class %d got isfinite %d\n", tf[i].f, tf[i].class, isfinite(tf[i].f));
78         }
79
80         for (i = 0; i < ntd; i++) {
81                 if (fpclassify(td[i].f) != td[i].class)
82                         error("%a want class %d got %d\n", td[i].f, td[i].class, fpclassify(td[i].f));
83                 if (!!isinf(td[i].f) != (td[i].class == FP_INFINITE))
84                         error("%a want class %d got isinf %d\n", td[i].f, td[i].class, isinf(td[i].f));
85                 if (!!isnan(td[i].f) != (td[i].class == FP_NAN))
86                         error("%a want class %d got isnan %d\n", td[i].f, td[i].class, isnan(td[i].f));
87                 if (!!isnormal(td[i].f) != (td[i].class == FP_NORMAL))
88                         error("%a want class %d got isnormal %d\n", td[i].f, td[i].class, isnormal(td[i].f));
89                 if (!!isfinite(td[i].f) != (td[i].class > FP_INFINITE))
90                         error("%a want class %d got isfinite %d\n", td[i].f, td[i].class, isfinite(td[i].f));
91         }
92
93         for (i = 0; i < ntl; i++) {
94                 if (fpclassify(tl[i].f) != tl[i].class)
95                         error("%La want class %d got %d\n", tl[i].f, tl[i].class, fpclassify(tl[i].f));
96                 if (!!isinf(tl[i].f) != (tl[i].class == FP_INFINITE))
97                         error("%La want class %d got isinf %d\n", tl[i].f, tl[i].class, isinf(tl[i].f));
98                 if (!!isnan(tl[i].f) != (tl[i].class == FP_NAN))
99                         error("%La want class %d got isnan %d\n", tl[i].f, tl[i].class, isnan(tl[i].f));
100                 if (!!isnormal(tl[i].f) != (tl[i].class == FP_NORMAL))
101                         error("%La want class %d got isnormal %d\n", tl[i].f, tl[i].class, isnormal(tl[i].f));
102                 if (!!isfinite(tl[i].f) != (tl[i].class > FP_INFINITE))
103                         error("%La want class %d got isfinite %d\n", tl[i].f, tl[i].class, isfinite(tl[i].f));
104         }
105 }