non-nearest rounding ulp check
[libc-test] / src / common / mtest.h
1 #include <fenv.h>
2 #include <float.h>
3 #include <math.h>
4
5 #undef RN
6 #undef RZ
7 #undef RD
8 #undef RU
9 #ifdef FE_TONEAREST
10 #define RN FE_TONEAREST
11 #else
12 #define RN 0
13 #endif
14 #ifdef FE_TOWARDZERO
15 #define RZ FE_TOWARDZERO
16 #else
17 #define RZ -1
18 #endif
19 #ifdef FE_DOWNWARD
20 #define RD FE_DOWNWARD
21 #else
22 #define RD -1
23 #endif
24 #ifdef FE_UPWARD
25 #define RU FE_UPWARD
26 #else
27 #define RU -1
28 #endif
29
30 #undef INEXACT
31 #undef INVALID
32 #undef DIVBYZERO
33 #undef UNDERFLOW
34 #undef OVERFLOW
35 #ifdef FE_INEXACT
36 #define INEXACT FE_INEXACT
37 #else
38 #define INEXACT 0
39 #endif
40 #ifdef FE_INVALID
41 #define INVALID FE_INVALID
42 #else
43 #define INVALID 0
44 #endif
45 #ifdef FE_DIVBYZERO
46 #define DIVBYZERO FE_DIVBYZERO
47 #else
48 #define DIVBYZERO 0
49 #endif
50 #ifdef FE_UNDERFLOW
51 #define UNDERFLOW FE_UNDERFLOW
52 #else
53 #define UNDERFLOW 0
54 #endif
55 #ifdef FE_OVERFLOW
56 #define OVERFLOW FE_OVERFLOW
57 #else
58 #define OVERFLOW 0
59 #endif
60
61 #undef inf
62 #undef nan
63 #define inf INFINITY
64 #define nan NAN
65
66 #define T(...) {__FILE__, __LINE__, __VA_ARGS__},
67
68 #define POS char *file; int line;
69 struct d_d {POS int r; double x; double y; float dy; int e; };
70 struct f_f {POS int r; float x; float y; float dy; int e; };
71 struct l_l {POS int r; long double x; long double y; float dy; int e; };
72 struct ff_f {POS int r; float x; float x2; float y; float dy; int e; };
73 struct dd_d {POS int r; double x; double x2; double y; float dy; int e; };
74 struct ll_l {POS int r; long double x; long double x2; long double y; float dy; int e; };
75 struct d_di {POS int r; double x; double y; float dy; long long i; int e; };
76 struct f_fi {POS int r; float x; float y; float dy; long long i; int e; };
77 struct l_li {POS int r; long double x; long double y; float dy; long long i; int e; };
78 struct di_d {POS int r; double x; long long i; double y; float dy; int e; };
79 struct fi_f {POS int r; float x; long long i; float y; float dy; int e; };
80 struct li_l {POS int r; long double x; long long i; long double y; float dy; int e; };
81 struct d_i {POS int r; double x; long long i; int e; };
82 struct f_i {POS int r; float x; long long i; int e; };
83 struct l_i {POS int r; long double x; long long i; int e; };
84 struct d_dd {POS int r; double x; double y; float dy; double y2; float dy2; int e; };
85 struct f_ff {POS int r; float x; float y; float dy; float y2; float dy2; int e; };
86 struct l_ll {POS int r; long double x; long double y; float dy; long double y2; float dy2; int e; };
87 struct ff_fi {POS int r; float x; float x2; float y; float dy; long long i; int e; };
88 struct dd_di {POS int r; double x; double x2; double y; float dy; long long i; int e; };
89 struct ll_li {POS int r; long double x; long double x2; long double y; float dy; long long i; int e; };
90 struct fff_f {POS int r; float x; float x2; float x3; float y; float dy; int e; };
91 struct ddd_d {POS int r; double x; double x2; double x3; double y; float dy; int e; };
92 struct lll_l {POS int r; long double x; long double x2; long double x3; long double y; float dy; int e; };
93 #undef POS
94
95 char *estr(int);
96 char *rstr(int);
97
98 float ulperr(double got, double want, float dwant);
99 float ulperrf(float got, float want, float dwant);
100 float ulperrl(long double got, long double want, float dwant);
101
102 static int checkexcept(int got, int want, int r)
103 {
104         if (r == RN)
105 #if defined CHECK_INEXACT
106                 return got == want;
107 #elif defined CHECK_INEXACT_OMISSION
108                 return got == want || got == (want|INEXACT);
109 #else
110                 return (got|INEXACT) == (want|INEXACT);
111 #endif
112         return (got|INEXACT|UNDERFLOW) == (want|INEXACT|UNDERFLOW);
113 }
114
115 static int checkexceptall(int got, int want, int r)
116 {
117         return got == want;
118 }
119
120 static int checkulp(float d, int r)
121 {
122         // TODO: we only care about >=1.5 ulp errors for now, should be 1.0
123         if (r == RN)
124                 return fabsf(d) < 1.5;
125         // accept larger error in non-nearest rounding mode
126         return fabsf(d) < 3.0;
127 }
128
129 static int checkcr(long double y, long double ywant, int r)
130 {
131         if (isnan(ywant))
132                 return isnan(y);
133         return y == ywant && signbit(y) == signbit(ywant);
134 }
135