source: diffutils/vendor/current/gnulib-tests/test-float.c@ 530

Last change on this file since 530 was 530, checked in by Yuri Dario, 12 years ago

diffutils: initial vendor import of diffutils 3.2.0.

File size: 9.2 KB
Line 
1/* -*- buffer-read-only: t -*- vi: set ro: */
2/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3/* Test of <float.h> substitute.
4 Copyright (C) 2011 Free Software Foundation, Inc.
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19/* Written by Bruno Haible <bruno@clisp.org>, 2011. */
20
21#include <config.h>
22
23#include <float.h>
24
25#include "fpucw.h"
26#include "macros.h"
27
28/* Check that FLT_RADIX is a constant expression. */
29int a[] = { FLT_RADIX };
30
31#if FLT_RADIX == 2
32
33/* Return 2^n. */
34static float
35pow2f (int n)
36{
37 int k = n;
38 volatile float x = 1;
39 volatile float y = 2;
40 /* Invariant: 2^n == x * y^k. */
41 if (k < 0)
42 {
43 y = 0.5f;
44 k = - k;
45 }
46 while (k > 0)
47 {
48 if (k != 2 * (k / 2))
49 {
50 x = x * y;
51 k = k - 1;
52 }
53 if (k == 0)
54 break;
55 y = y * y;
56 k = k / 2;
57 }
58 /* Now k == 0, hence x == 2^n. */
59 return x;
60}
61
62/* Return 2^n. */
63static double
64pow2d (int n)
65{
66 int k = n;
67 volatile double x = 1;
68 volatile double y = 2;
69 /* Invariant: 2^n == x * y^k. */
70 if (k < 0)
71 {
72 y = 0.5;
73 k = - k;
74 }
75 while (k > 0)
76 {
77 if (k != 2 * (k / 2))
78 {
79 x = x * y;
80 k = k - 1;
81 }
82 if (k == 0)
83 break;
84 y = y * y;
85 k = k / 2;
86 }
87 /* Now k == 0, hence x == 2^n. */
88 return x;
89}
90
91/* Return 2^n. */
92static long double
93pow2l (int n)
94{
95 int k = n;
96 volatile long double x = 1;
97 volatile long double y = 2;
98 /* Invariant: 2^n == x * y^k. */
99 if (k < 0)
100 {
101 y = 0.5L;
102 k = - k;
103 }
104 while (k > 0)
105 {
106 if (k != 2 * (k / 2))
107 {
108 x = x * y;
109 k = k - 1;
110 }
111 if (k == 0)
112 break;
113 y = y * y;
114 k = k / 2;
115 }
116 /* Now k == 0, hence x == 2^n. */
117 return x;
118}
119
120/* ----------------------- Check macros for 'float' ----------------------- */
121
122/* Check that the FLT_* macros expand to constant expressions. */
123int fb[] =
124 {
125 FLT_MANT_DIG, FLT_MIN_EXP, FLT_MAX_EXP,
126 FLT_DIG, FLT_MIN_10_EXP, FLT_MAX_10_EXP
127 };
128float fc[] = { FLT_EPSILON, FLT_MIN, FLT_MAX };
129
130static void
131test_float (void)
132{
133 /* Check that the value of FLT_MIN_EXP is well parenthesized. */
134 ASSERT ((FLT_MIN_EXP % 101111) == (FLT_MIN_EXP) % 101111);
135
136 /* Check that the value of DBL_MIN_10_EXP is well parenthesized. */
137 ASSERT ((FLT_MIN_10_EXP % 101111) == (FLT_MIN_10_EXP) % 101111);
138
139 /* Check that 'float' is as specified in IEEE 754. */
140 ASSERT (FLT_MANT_DIG == 24);
141 ASSERT (FLT_MIN_EXP == -125);
142 ASSERT (FLT_MAX_EXP == 128);
143
144 /* Check the value of FLT_MIN_10_EXP. */
145 ASSERT (FLT_MIN_10_EXP == - (int) (- (FLT_MIN_EXP - 1) * 0.30103));
146
147 /* Check the value of FLT_DIG. */
148 ASSERT (FLT_DIG == (int) ((FLT_MANT_DIG - 1) * 0.30103));
149
150 /* Check the value of FLT_MIN_10_EXP. */
151 ASSERT (FLT_MIN_10_EXP == - (int) (- (FLT_MIN_EXP - 1) * 0.30103));
152
153 /* Check the value of FLT_MAX_10_EXP. */
154 ASSERT (FLT_MAX_10_EXP == (int) (FLT_MAX_EXP * 0.30103));
155
156 /* Check the value of FLT_MAX. */
157 {
158 volatile float m = FLT_MAX;
159 int n;
160
161 ASSERT (m + m > m);
162 for (n = 0; n <= 2 * FLT_MANT_DIG; n++)
163 {
164 volatile float pow2_n = pow2f (n); /* 2^n */
165 volatile float x = m + (m / pow2_n);
166 if (x > m)
167 ASSERT (x + x == x);
168 else
169 ASSERT (!(x + x == x));
170 }
171 }
172
173 /* Check the value of FLT_MIN. */
174 {
175 volatile float m = FLT_MIN;
176 volatile float x = pow2f (FLT_MIN_EXP - 1);
177 ASSERT (m == x);
178 }
179
180 /* Check the value of FLT_EPSILON. */
181 {
182 volatile float e = FLT_EPSILON;
183 volatile float me;
184 int n;
185
186 me = 1.0f + e;
187 ASSERT (me > 1.0f);
188 ASSERT (me - 1.0f == e);
189 for (n = 0; n <= 2 * FLT_MANT_DIG; n++)
190 {
191 volatile float half_n = pow2f (- n); /* 2^-n */
192 volatile float x = me - half_n;
193 if (x < me)
194 ASSERT (x <= 1.0f);
195 }
196 }
197}
198
199/* ----------------------- Check macros for 'double' ----------------------- */
200
201/* Check that the DBL_* macros expand to constant expressions. */
202int db[] =
203 {
204 DBL_MANT_DIG, DBL_MIN_EXP, DBL_MAX_EXP,
205 DBL_DIG, DBL_MIN_10_EXP, DBL_MAX_10_EXP
206 };
207double dc[] = { DBL_EPSILON, DBL_MIN, DBL_MAX };
208
209static void
210test_double (void)
211{
212 /* Check that the value of DBL_MIN_EXP is well parenthesized. */
213 ASSERT ((DBL_MIN_EXP % 101111) == (DBL_MIN_EXP) % 101111);
214
215 /* Check that the value of DBL_MIN_10_EXP is well parenthesized. */
216 ASSERT ((DBL_MIN_10_EXP % 101111) == (DBL_MIN_10_EXP) % 101111);
217
218 /* Check that 'double' is as specified in IEEE 754. */
219 ASSERT (DBL_MANT_DIG == 53);
220 ASSERT (DBL_MIN_EXP == -1021);
221 ASSERT (DBL_MAX_EXP == 1024);
222
223 /* Check the value of DBL_MIN_10_EXP. */
224 ASSERT (DBL_MIN_10_EXP == - (int) (- (DBL_MIN_EXP - 1) * 0.30103));
225
226 /* Check the value of DBL_DIG. */
227 ASSERT (DBL_DIG == (int) ((DBL_MANT_DIG - 1) * 0.30103));
228
229 /* Check the value of DBL_MIN_10_EXP. */
230 ASSERT (DBL_MIN_10_EXP == - (int) (- (DBL_MIN_EXP - 1) * 0.30103));
231
232 /* Check the value of DBL_MAX_10_EXP. */
233 ASSERT (DBL_MAX_10_EXP == (int) (DBL_MAX_EXP * 0.30103));
234
235 /* Check the value of DBL_MAX. */
236 {
237 volatile double m = DBL_MAX;
238 int n;
239
240 ASSERT (m + m > m);
241 for (n = 0; n <= 2 * DBL_MANT_DIG; n++)
242 {
243 volatile double pow2_n = pow2d (n); /* 2^n */
244 volatile double x = m + (m / pow2_n);
245 if (x > m)
246 ASSERT (x + x == x);
247 else
248 ASSERT (!(x + x == x));
249 }
250 }
251
252 /* Check the value of DBL_MIN. */
253 {
254 volatile double m = DBL_MIN;
255 volatile double x = pow2d (DBL_MIN_EXP - 1);
256 ASSERT (m == x);
257 }
258
259 /* Check the value of DBL_EPSILON. */
260 {
261 volatile double e = DBL_EPSILON;
262 volatile double me;
263 int n;
264
265 me = 1.0 + e;
266 ASSERT (me > 1.0);
267 ASSERT (me - 1.0 == e);
268 for (n = 0; n <= 2 * DBL_MANT_DIG; n++)
269 {
270 volatile double half_n = pow2d (- n); /* 2^-n */
271 volatile double x = me - half_n;
272 if (x < me)
273 ASSERT (x <= 1.0);
274 }
275 }
276}
277
278/* -------------------- Check macros for 'long double' -------------------- */
279
280/* Check that the LDBL_* macros expand to constant expressions. */
281int lb[] =
282 {
283 LDBL_MANT_DIG, LDBL_MIN_EXP, LDBL_MAX_EXP,
284 LDBL_DIG, LDBL_MIN_10_EXP, LDBL_MAX_10_EXP
285 };
286long double lc1 = LDBL_EPSILON;
287long double lc2 = LDBL_MIN;
288#if 0 /* LDBL_MAX is not a constant expression on some platforms. */
289long double lc3 = LDBL_MAX;
290#endif
291
292static void
293test_long_double (void)
294{
295 /* Check that the value of LDBL_MIN_EXP is well parenthesized. */
296 ASSERT ((LDBL_MIN_EXP % 101111) == (LDBL_MIN_EXP) % 101111);
297
298 /* Check that the value of LDBL_MIN_10_EXP is well parenthesized. */
299 ASSERT ((LDBL_MIN_10_EXP % 101111) == (LDBL_MIN_10_EXP) % 101111);
300
301 /* Check that 'long double' is at least as wide as 'double'. */
302 ASSERT (LDBL_MANT_DIG >= DBL_MANT_DIG);
303
304 /* Normally, we would also assert this:
305 ASSERT (LDBL_MIN_EXP <= DBL_MIN_EXP);
306 but at least on powerpc64 with gcc-4.4.4, it would fail:
307 $ :|gcc -dD -E -include stddef.h -|grep -E 'L?DBL_MIN_EXP'
308 #define __DBL_MIN_EXP__ (-1021)
309 #define __LDBL_MIN_EXP__ (-968)
310 */
311 ASSERT (LDBL_MAX_EXP >= DBL_MAX_EXP);
312
313 /* Check the value of LDBL_DIG. */
314 ASSERT (LDBL_DIG == (int)((LDBL_MANT_DIG - 1) * 0.30103));
315
316 /* Check the value of LDBL_MIN_10_EXP. */
317 ASSERT (LDBL_MIN_10_EXP == - (int) (- (LDBL_MIN_EXP - 1) * 0.30103));
318
319 /* Check the value of LDBL_MAX_10_EXP. */
320 ASSERT (LDBL_MAX_10_EXP == (int) (LDBL_MAX_EXP * 0.30103));
321
322 /* Check the value of LDBL_MAX. */
323 {
324 volatile long double m = LDBL_MAX;
325 int n;
326
327 ASSERT (m + m > m);
328 for (n = 0; n <= 2 * LDBL_MANT_DIG; n++)
329 {
330 volatile long double pow2_n = pow2l (n); /* 2^n */
331 volatile long double x = m + (m / pow2_n);
332 if (x > m)
333 ASSERT (x + x == x);
334 else
335 ASSERT (!(x + x == x));
336 }
337 }
338
339 /* Check the value of LDBL_MIN. */
340 {
341 volatile long double m = LDBL_MIN;
342 volatile long double x = pow2l (LDBL_MIN_EXP - 1);
343 ASSERT (m == x);
344 }
345
346 /* Check the value of LDBL_EPSILON. */
347 {
348 volatile long double e = LDBL_EPSILON;
349 volatile long double me;
350 int n;
351
352 me = 1.0L + e;
353 ASSERT (me > 1.0L);
354 ASSERT (me - 1.0L == e);
355 for (n = 0; n <= 2 * LDBL_MANT_DIG; n++)
356 {
357 volatile long double half_n = pow2l (- n); /* 2^-n */
358 volatile long double x = me - half_n;
359 if (x < me)
360 ASSERT (x <= 1.0L);
361 }
362 }
363}
364
365int
366main ()
367{
368 test_float ();
369 test_double ();
370
371 {
372 DECL_LONG_DOUBLE_ROUNDING
373
374 BEGIN_LONG_DOUBLE_ROUNDING ();
375
376 test_long_double ();
377
378 END_LONG_DOUBLE_ROUNDING ();
379 }
380
381 return 0;
382}
383
384#else
385
386int
387main ()
388{
389 fprintf (stderr, "Skipping test: FLT_RADIX is not 2.\n");
390 return 77;
391}
392
393#endif
Note: See TracBrowser for help on using the repository browser.