1 /* Test various type-generic builtins by calling "main_tests()". */
2
3 #define FP_NAN 1
4 #define FP_INFINITE 2
5 #define FP_NORMAL 3
6 #define FP_SUBNORMAL 4
7 #define FP_ZERO 5
8 #define fpclassify(X) __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, (X))
9
10 void __attribute__ ((__noinline__))
foo_1(float f,double d,long double ld,int res_unord,int res_isnan,int res_isinf,int res_isinf_sign,int res_isfin,int res_isnorm,int res_signbit,int classification)11 foo_1 (float f, double d, long double ld,
12 int res_unord, int res_isnan, int res_isinf,
13 int res_isinf_sign, int res_isfin, int res_isnorm,
14 int res_signbit, int classification)
15 {
16 if (__builtin_isunordered (f, 0) != res_unord)
17 __builtin_abort ();
18 if (__builtin_isunordered (0, f) != res_unord)
19 __builtin_abort ();
20 if (__builtin_isunordered (d, 0) != res_unord)
21 __builtin_abort ();
22 if (__builtin_isunordered (0, d) != res_unord)
23 __builtin_abort ();
24 if (__builtin_isunordered (ld, 0) != res_unord)
25 __builtin_abort ();
26 if (__builtin_isunordered (0, ld) != res_unord)
27 __builtin_abort ();
28
29 if (__builtin_isnan (f) != res_isnan)
30 __builtin_abort ();
31 if (__builtin_isnan (d) != res_isnan)
32 __builtin_abort ();
33 if (__builtin_isnan (ld) != res_isnan)
34 __builtin_abort ();
35 if (__builtin_isnanf (f) != res_isnan)
36 __builtin_abort ();
37 if (__builtin_isnanl (ld) != res_isnan)
38 __builtin_abort ();
39
40 if (__builtin_isinf (f) != res_isinf)
41 __builtin_abort ();
42 if (__builtin_isinf (d) != res_isinf)
43 __builtin_abort ();
44 if (__builtin_isinf (ld) != res_isinf)
45 __builtin_abort ();
46 if (__builtin_isinff (f) != res_isinf)
47 __builtin_abort ();
48 if (__builtin_isinfl (ld) != res_isinf)
49 __builtin_abort ();
50
51 if (__builtin_isinf_sign (f) != res_isinf_sign)
52 __builtin_abort ();
53 if (__builtin_isinf_sign (d) != res_isinf_sign)
54 __builtin_abort ();
55 if (__builtin_isinf_sign (ld) != res_isinf_sign)
56 __builtin_abort ();
57
58 if (__builtin_isnormal (f) != res_isnorm)
59 __builtin_abort ();
60 if (__builtin_isnormal (d) != res_isnorm)
61 __builtin_abort ();
62 if (__builtin_isnormal (ld) != res_isnorm)
63 __builtin_abort ();
64
65 if (__builtin_isfinite (f) != res_isfin)
66 __builtin_abort ();
67 if (__builtin_isfinite (d) != res_isfin)
68 __builtin_abort ();
69 if (__builtin_isfinite (ld) != res_isfin)
70 __builtin_abort ();
71
72 if (__builtin_finitef (f) != res_isfin)
73 __builtin_abort ();
74 if (__builtin_finite (f) != res_isfin)
75 __builtin_abort ();
76 if (__builtin_finite (d) != res_isfin)
77 __builtin_abort ();
78 if (__builtin_finitel (d) != res_isfin)
79 __builtin_abort ();
80 if (__builtin_finitel (ld) != res_isfin)
81 __builtin_abort ();
82
83 /* Sign bit of zeros and nans is not preserved in unsafe math mode. */
84 #ifdef UNSAFE
85 if (!res_isnan && f != 0 && d != 0 && ld != 0)
86 #endif
87 {
88 if ((__builtin_signbit (f) ? 1 : 0) != res_signbit)
89 __builtin_abort ();
90 if ((__builtin_signbit (d) ? 1 : 0) != res_signbit)
91 __builtin_abort ();
92 if ((__builtin_signbit (ld) ? 1 : 0) != res_signbit)
93 __builtin_abort ();
94 if ((__builtin_signbitf (f) ? 1 : 0) != res_signbit)
95 __builtin_abort ();
96 if ((__builtin_signbitl (ld) ? 1 : 0) != res_signbit)
97 __builtin_abort ();
98 }
99
100 /* Subnormals can abruptly underflow to zero in unsafe math
101 mode, so bypass testing these numbers if necessary. */
102 #ifdef UNSAFE
103 if (classification != FP_SUBNORMAL)
104 #endif
105 {
106 if (fpclassify(f) != classification)
107 __builtin_abort ();
108 if (fpclassify(d) != classification)
109 __builtin_abort ();
110 if (fpclassify(ld) != classification)
111 __builtin_abort ();
112 }
113 }
114
115 void __attribute__ ((__noinline__))
foo(float f,double d,long double ld,int res_unord,int res_isnan,int res_isinf,int res_isfin,int res_isnorm,int classification)116 foo (float f, double d, long double ld,
117 int res_unord, int res_isnan, int res_isinf,
118 int res_isfin, int res_isnorm, int classification)
119 {
120 foo_1 (f, d, ld, res_unord, res_isnan, res_isinf, res_isinf, res_isfin, res_isnorm, 0, classification);
121 /* Try all the values negated as well. All will have the sign bit set,
122 except for the nan. */
123 foo_1 (-f, -d, -ld, res_unord, res_isnan, res_isinf, -res_isinf, res_isfin, res_isnorm, 1, classification);
124 }
125
126 int __attribute__ ((__noinline__))
main_tests(void)127 main_tests (void)
128 {
129 volatile float f;
130 volatile double d;
131 volatile long double ld;
132
133 /* Test NaN. */
134 f = __builtin_nanf(""); d = __builtin_nan(""); ld = __builtin_nanl("");
135 foo(f, d, ld, /*unord=*/ 1, /*isnan=*/ 1, /*isinf=*/ 0, /*isfin=*/ 0, /*isnorm=*/ 0, FP_NAN);
136
137 /* Test infinity. */
138 f = __builtin_inff(); d = __builtin_inf(); ld = __builtin_infl();
139 foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 1, /*isfin=*/ 0, /*isnorm=*/ 0, FP_INFINITE);
140
141 /* Test zero. */
142 f = 0; d = 0; ld = 0;
143 foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 0, /*isfin=*/ 1, /*isnorm=*/ 0, FP_ZERO);
144
145 /* Test one. */
146 f = 1; d = 1; ld = 1;
147 foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 0, /*isfin=*/ 1, /*isnorm=*/ 1, FP_NORMAL);
148
149 /* Test minimum values. */
150 f = __FLT_MIN__; d = __DBL_MIN__; ld = __LDBL_MIN__;
151 foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 0, /*isfin=*/ 1, /*isnorm=*/ 1, FP_NORMAL);
152
153 /* Test subnormal values. */
154 f = __FLT_MIN__/2; d = __DBL_MIN__/2; ld = __LDBL_MIN__/2;
155 foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 0, /*isfin=*/ 1, /*isnorm=*/ 0, FP_SUBNORMAL);
156
157 /* Test maximum values. */
158 f = __FLT_MAX__; d = __DBL_MAX__; ld = __LDBL_MAX__;
159 foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 0, /*isfin=*/ 1, /*isnorm=*/ 1, FP_NORMAL);
160
161 /* Test overflow values. */
162 f = __FLT_MAX__*2; d = __DBL_MAX__*2; ld = __LDBL_MAX__*2;
163 foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 1, /*isfin=*/ 0, /*isnorm=*/ 0, FP_INFINITE);
164
165 return 0;
166 }
167