10d9279d4ef5a34555d728b1ea7c39eb98e81107
[libc-test] / src / math / util.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 #undef POS
82
83 char *estr(int);
84 char *rstr(int);
85 int rconv(int *, char *);
86 int econv(int *, char *);
87
88 int eulp(double);
89 int eulpf(float);
90 int eulpl(long double);
91
92 float ulperr(double got, double want, float dwant);
93 float ulperrf(float got, float want, float dwant);
94 float ulperrl(long double got, long double want, float dwant);
95
96 void setupfenv(int);
97 int getexcept(void);
98
99 static int checkexcept(int got, int want, int r)
100 {
101         if (r == RN)
102                 return got == want || got == (want|INEXACT);
103         return 1; //(got|INEXACT|UNDERFLOW) == (want|INEXACT|UNDERFLOW);
104 }
105
106 static int checkulp(float d, int r)
107 {
108         if (r == RN)
109                 return fabsf(d) <= 1.0;
110         return 1; //fabsf(d) <= 2.0;
111 }
112