Debian Codegraph @

gnulib-tests/test-float-h.c

raw
1 /* Test of <float.h> substitute.
2 Copyright (C) 2011-2025 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Bruno Haible <[email protected]>, 2011. */
18
19 #include <config.h>
20
21 #include <float.h>
22
23 /* Check that some macros are constant expressions. */
24 int a[] =
25 {
26 FLT_RADIX,
27 FLT_EVAL_METHOD /* added in ISO C 99 */
28 };
29
30 /* ----------------------- Check macros for 'float' ----------------------- */
31
32 /* Check that the FLT_* macros expand to constant expressions. */
33 int fb[] =
34 {
35 FLT_MANT_DIG, FLT_MIN_EXP, FLT_MAX_EXP,
36 FLT_DIG, FLT_MIN_10_EXP, FLT_MAX_10_EXP,
37 FLT_HAS_SUBNORM, /* added in ISO C 11 */
38 FLT_DECIMAL_DIG, /* added in ISO C 11 */
39 FLT_IS_IEC_60559 /* added in ISO C 23 */
40 };
41 float fc[] =
42 {
43 FLT_EPSILON, FLT_MIN, FLT_MAX,
44 FLT_TRUE_MIN, /* added in ISO C 11 */
45 FLT_NORM_MAX /* added in ISO C 23 */
46 };
47 #if 0 /* FLT_SNAN is not a constant expression on some platforms. */
48 float fs = FLT_SNAN; /* added in ISO C 23 */
49 #endif
50
51 /* ----------------------- Check macros for 'double' ----------------------- */
52
53 /* Check that the DBL_* macros expand to constant expressions. */
54 int db[] =
55 {
56 DBL_MANT_DIG, DBL_MIN_EXP, DBL_MAX_EXP,
57 DBL_DIG, DBL_MIN_10_EXP, DBL_MAX_10_EXP,
58 DBL_HAS_SUBNORM, /* added in ISO C 11 */
59 DBL_DECIMAL_DIG, /* added in ISO C 11 */
60 DBL_IS_IEC_60559 /* added in ISO C 23 */
61 };
62 double dc[] =
63 {
64 DBL_EPSILON, DBL_MIN, DBL_MAX,
65 DBL_TRUE_MIN, /* added in ISO C 11 */
66 DBL_NORM_MAX /* added in ISO C 23 */
67 };
68 #if 0 /* DBL_SNAN is not a constant expression on some platforms. */
69 double ds = DBL_SNAN; /* added in ISO C 23 */
70 #endif
71
72 /* -------------------- Check macros for 'long double' -------------------- */
73
74 /* Check that the LDBL_* macros expand to constant expressions. */
75 int lb[] =
76 {
77 LDBL_MANT_DIG, LDBL_MIN_EXP, LDBL_MAX_EXP,
78 LDBL_DIG, LDBL_MIN_10_EXP, LDBL_MAX_10_EXP,
79 LDBL_HAS_SUBNORM, /* added in ISO C 11 */
80 LDBL_DECIMAL_DIG, /* added in ISO C 11 */
81 LDBL_IS_IEC_60559 /* added in ISO C 23 */
82 };
83 long double lc1 = LDBL_EPSILON;
84 long double lc2 = LDBL_MIN;
85 #if !GNULIB_defined_long_double_union
86 /* LDBL_MAX is not a constant expression on some platforms. */
87 long double lc3 = LDBL_MAX;
88 #endif
89 #if !GNULIB_defined_long_double_union
90 /* LDBL_TRUE_MIN is not a constant expression on FreeBSD/i386. */
91 long double lc4 = LDBL_TRUE_MIN; /* added in ISO C 11 */
92 #endif
93 #if !GNULIB_defined_long_double_union
94 /* LDBL_MAX is not a constant expression on some platforms. */
95 long double lc5 = LDBL_NORM_MAX; /* added in ISO C 23 */
96 #endif
97 #if 0
98 /* LDBL_SNAN is not a constant expression on some platforms. */
99 long double ls = LDBL_SNAN; /* added in ISO C 23 */
100 #endif
101
102 /* ------------------------------------------------------------------------- */
103
104 #include "fpucw.h"
105 #include "isnanf-nolibm.h"
106 #include "isnand-nolibm.h"
107 #include "isnanl-nolibm.h"
108 #include "macros.h"
109
110 #if FLT_RADIX == 2
111
112 /* Return 2^n. */
113 static float
114 pow2f (int n)
115 {
116 int k = n;
117 volatile float x = 1;
118 volatile float y = 2;
119 /* Invariant: 2^n == x * y^k. */
120 if (k < 0)
121 {
122 y = 0.5f;
123 k = - k;
124 }
125 while (k > 0)
126 {
127 if (k != 2 * (k / 2))
128 {
129 x = x * y;
130 k = k - 1;
131 }
132 if (k == 0)
133 break;
134 y = y * y;
135 k = k / 2;
136 }
137 /* Now k == 0, hence x == 2^n. */
138 return x;
139 }
140
141 /* Return 2^n. */
142 static double
143 pow2d (int n)
144 {
145 int k = n;
146 volatile double x = 1;
147 volatile double y = 2;
148 /* Invariant: 2^n == x * y^k. */
149 if (k < 0)
150 {
151 y = 0.5;
152 k = - k;
153 }
154 while (k > 0)
155 {
156 if (k != 2 * (k / 2))
157 {
158 x = x * y;
159 k = k - 1;
160 }
161 if (k == 0)
162 break;
163 y = y * y;
164 k = k / 2;
165 }
166 /* Now k == 0, hence x == 2^n. */
167 return x;
168 }
169
170 /* Return 2^n. */
171 static long double
172 pow2l (int n)
173 {
174 int k = n;
175 volatile long double x = 1;
176 volatile long double y = 2;
177 /* Invariant: 2^n == x * y^k. */
178 if (k < 0)
179 {
180 y = 0.5L;
181 k = - k;
182 }
183 while (k > 0)
184 {
185 if (k != 2 * (k / 2))
186 {
187 x = x * y;
188 k = k - 1;
189 }
190 if (k == 0)
191 break;
192 y = y * y;
193 k = k / 2;
194 }
195 /* Now k == 0, hence x == 2^n. */
196 return x;
197 }
198
199 /* ----------------------- Check macros for 'float' ----------------------- */
200
201 static void
202 test_float (void)
203 {
204 /* Check that the value of FLT_MIN_EXP is well parenthesized. */
205 ASSERT ((FLT_MIN_EXP % 101111) == (FLT_MIN_EXP) % 101111);
206
207 /* Check that the value of DBL_MIN_10_EXP is well parenthesized. */
208 ASSERT ((FLT_MIN_10_EXP % 101111) == (FLT_MIN_10_EXP) % 101111);
209
210 /* Check that 'float' is as specified in IEEE 754. */
211 ASSERT (FLT_MANT_DIG == 24);
212 ASSERT (FLT_MIN_EXP == -125);
213 ASSERT (FLT_MAX_EXP == 128);
214
215 /* Check the value of FLT_MIN_10_EXP. */
216 ASSERT (FLT_MIN_10_EXP == - (int) (- (FLT_MIN_EXP - 1) * 0.30103));
217
218 /* Check the value of FLT_DIG. */
219 ASSERT (FLT_DIG == (int) ((FLT_MANT_DIG - 1) * 0.30103));
220
221 /* Check the value of FLT_MIN_10_EXP. */
222 ASSERT (FLT_MIN_10_EXP == - (int) (- (FLT_MIN_EXP - 1) * 0.30103));
223
224 /* Check the value of FLT_MAX_10_EXP. */
225 ASSERT (FLT_MAX_10_EXP == (int) (FLT_MAX_EXP * 0.30103));
226
227 /* Check the value of FLT_MAX. */
228 {
229 volatile float m = FLT_MAX;
230 int n;
231
232 ASSERT (m + m > m);
233 for (n = 0; n <= 2 * FLT_MANT_DIG; n++)
234 {
235 volatile float pow2_n = pow2f (n); /* 2^n */
236 volatile float x = m + (m / pow2_n);
237 if (x > m)
238 ASSERT (x + x == x);
239 else
240 ASSERT (!(x + x == x));
241 }
242 }
243
244 /* Check the value of FLT_MIN. */
245 {
246 volatile float m = FLT_MIN;
247 volatile float x = pow2f (FLT_MIN_EXP - 1);
248 ASSERT (m == x);
249 }
250
251 /* Check the value of FLT_EPSILON. */
252 {
253 volatile float e = FLT_EPSILON;
254 volatile float me;
255 int n;
256
257 me = 1.0f + e;
258 ASSERT (me > 1.0f);
259 ASSERT (me - 1.0f == e);
260 for (n = 0; n <= 2 * FLT_MANT_DIG; n++)
261 {
262 volatile float half_n = pow2f (- n); /* 2^-n */
263 volatile float x = me - half_n;
264 if (x < me)
265 ASSERT (x <= 1.0f);
266 }
267 }
268
269 /* Check the value of FLT_HAS_SUBNORM. */
270 ASSERT (FLT_HAS_SUBNORM == 1);
271
272 /* Check the value of FLT_DECIMAL_DIG. */
273 ASSERT (FLT_DECIMAL_DIG == (int)(FLT_MANT_DIG * 0.3010299956639812 + 2));
274
275 /* Check the value of FLT_TRUE_MIN. */
276 ASSERT (FLT_TRUE_MIN > 0.0f);
277 {
278 volatile float x = FLT_TRUE_MIN * 0.5f;
279 ASSERT (x == 0.0f);
280 }
281
282 /* Check the value of FLT_IS_IEC_60559. */
283 #if !defined __m68k__
284 ASSERT (FLT_IS_IEC_60559);
285 #elif 0
286 /* It is not clear what this macro actually means. Cf.
287 <https://mailman.oakapple.net/pipermail/cfp-interest/2023-April/002760.html> */
288 ASSERT (!FLT_IS_IEC_60559);
289 #endif
290
291 /* Check the value of FLT_NORM_MAX. */
292 ASSERT (FLT_NORM_MAX == FLT_MAX);
293
294 /* Check the value of FLT_SNAN. */
295 ASSERT (isnanf (FLT_SNAN));
296 }
297
298 /* ----------------------- Check macros for 'double' ----------------------- */
299
300 static void
301 test_double (void)
302 {
303 /* Check that the value of DBL_MIN_EXP is well parenthesized. */
304 ASSERT ((DBL_MIN_EXP % 101111) == (DBL_MIN_EXP) % 101111);
305
306 /* Check that the value of DBL_MIN_10_EXP is well parenthesized. */
307 ASSERT ((DBL_MIN_10_EXP % 101111) == (DBL_MIN_10_EXP) % 101111);
308
309 /* Check that 'double' is as specified in IEEE 754. */
310 ASSERT (DBL_MANT_DIG == 53);
311 ASSERT (DBL_MIN_EXP == -1021);
312 ASSERT (DBL_MAX_EXP == 1024);
313
314 /* Check the value of DBL_MIN_10_EXP. */
315 ASSERT (DBL_MIN_10_EXP == - (int) (- (DBL_MIN_EXP - 1) * 0.30103));
316
317 /* Check the value of DBL_DIG. */
318 ASSERT (DBL_DIG == (int) ((DBL_MANT_DIG - 1) * 0.30103));
319
320 /* Check the value of DBL_MIN_10_EXP. */
321 ASSERT (DBL_MIN_10_EXP == - (int) (- (DBL_MIN_EXP - 1) * 0.30103));
322
323 /* Check the value of DBL_MAX_10_EXP. */
324 ASSERT (DBL_MAX_10_EXP == (int) (DBL_MAX_EXP * 0.30103));
325
326 /* Check the value of DBL_MAX. */
327 {
328 volatile double m = DBL_MAX;
329 int n;
330
331 ASSERT (m + m > m);
332 for (n = 0; n <= 2 * DBL_MANT_DIG; n++)
333 {
334 volatile double pow2_n = pow2d (n); /* 2^n */
335 volatile double x = m + (m / pow2_n);
336 if (x > m)
337 ASSERT (x + x == x);
338 else
339 ASSERT (!(x + x == x));
340 }
341 }
342
343 /* Check the value of DBL_MIN. */
344 {
345 volatile double m = DBL_MIN;
346 volatile double x = pow2d (DBL_MIN_EXP - 1);
347 ASSERT (m == x);
348 }
349
350 /* Check the value of DBL_EPSILON. */
351 {
352 volatile double e = DBL_EPSILON;
353 volatile double me;
354 int n;
355
356 me = 1.0 + e;
357 ASSERT (me > 1.0);
358 ASSERT (me - 1.0 == e);
359 for (n = 0; n <= 2 * DBL_MANT_DIG; n++)
360 {
361 volatile double half_n = pow2d (- n); /* 2^-n */
362 volatile double x = me - half_n;
363 if (x < me)
364 ASSERT (x <= 1.0);
365 }
366 }
367
368 /* Check the value of DBL_HAS_SUBNORM. */
369 ASSERT (DBL_HAS_SUBNORM == 1);
370
371 /* Check the value of DBL_DECIMAL_DIG. */
372 ASSERT (DBL_DECIMAL_DIG == (int)(DBL_MANT_DIG * 0.3010299956639812 + 2));
373
374 /* Check the value of DBL_TRUE_MIN. */
375 ASSERT (DBL_TRUE_MIN > 0.0);
376 {
377 volatile double x = DBL_TRUE_MIN * 0.5;
378 ASSERT (x == 0.0);
379 }
380
381 /* Check the value of DBL_IS_IEC_60559. */
382 #if !defined __m68k__
383 ASSERT (DBL_IS_IEC_60559);
384 #elif 0
385 /* It is not clear what this macro actually means. Cf.
386 <https://mailman.oakapple.net/pipermail/cfp-interest/2023-April/002760.html> */
387 ASSERT (!DBL_IS_IEC_60559);
388 #endif
389
390 /* Check the value of DBL_NORM_MAX. */
391 ASSERT (DBL_NORM_MAX == DBL_MAX);
392
393 /* Check the value of DBL_SNAN. */
394 ASSERT (isnand (DBL_SNAN));
395 }
396
397 /* -------------------- Check macros for 'long double' -------------------- */
398
399 static void
400 test_long_double (void)
401 {
402 /* Check that the value of LDBL_MIN_EXP is well parenthesized. */
403 ASSERT ((LDBL_MIN_EXP % 101111) == (LDBL_MIN_EXP) % 101111);
404
405 /* Check that the value of LDBL_MIN_10_EXP is well parenthesized. */
406 ASSERT ((LDBL_MIN_10_EXP % 101111) == (LDBL_MIN_10_EXP) % 101111);
407
408 /* Check that 'long double' is at least as wide as 'double'. */
409 ASSERT (LDBL_MANT_DIG >= DBL_MANT_DIG);
410 ASSERT (LDBL_MIN_EXP - LDBL_MANT_DIG <= DBL_MIN_EXP - DBL_MANT_DIG);
411 ASSERT (LDBL_MAX_EXP >= DBL_MAX_EXP);
412
413 /* Check the value of LDBL_DIG. */
414 ASSERT (LDBL_DIG == (int)((LDBL_MANT_DIG - 1) * 0.30103));
415
416 /* Check the value of LDBL_MIN_10_EXP. */
417 ASSERT (LDBL_MIN_10_EXP == - (int) (- (LDBL_MIN_EXP - 1) * 0.30103));
418
419 /* Check the value of LDBL_MAX_10_EXP. */
420 ASSERT (LDBL_MAX_10_EXP == (int) (LDBL_MAX_EXP * 0.30103));
421
422 /* Check the value of LDBL_MAX. */
423 {
424 volatile long double m = LDBL_MAX;
425 int n;
426
427 ASSERT (m + m > m);
428 for (n = 0; n <= 2 * LDBL_MANT_DIG; n++)
429 {
430 volatile long double pow2_n = pow2l (n); /* 2^n */
431 volatile long double x = m + (m / pow2_n);
432 if (x > m)
433 ASSERT (x + x == x);
434 else
435 ASSERT (!(x + x == x));
436 }
437 }
438
439 /* Check the value of LDBL_MIN. */
440 {
441 volatile long double m = LDBL_MIN;
442 volatile long double x = pow2l (LDBL_MIN_EXP - 1);
443 ASSERT (m == x);
444 }
445
446 /* Check the value of LDBL_EPSILON. */
447 {
448 volatile long double e = LDBL_EPSILON;
449 volatile long double me;
450 int n;
451
452 me = 1.0L + e;
453 ASSERT (me > 1.0L);
454 ASSERT (me - 1.0L == e);
455 for (n = 0; n <= 2 * LDBL_MANT_DIG; n++)
456 {
457 volatile long double half_n = pow2l (- n); /* 2^-n */
458 volatile long double x = me - half_n;
459 if (x < me)
460 ASSERT (x <= 1.0L);
461 }
462 }
463
464 /* Check the value of LDBL_HAS_SUBNORM. */
465 ASSERT (LDBL_HAS_SUBNORM == 1);
466
467 /* Check the value of LDBL_DECIMAL_DIG. */
468 ASSERT (LDBL_DECIMAL_DIG == (int)(LDBL_MANT_DIG * 0.3010299956639812 + 2));
469
470 /* Check the value of LDBL_TRUE_MIN. */
471 ASSERT (LDBL_TRUE_MIN > 0.0L);
472 {
473 volatile long double x = LDBL_TRUE_MIN * 0.5L;
474 ASSERT (x == 0.0L);
475 }
476
477 /* Check the value of LDBL_IS_IEC_60559. */
478 #if (LDBL_MANT_DIG == 53 || LDBL_MANT_DIG == 113) && !defined __m68k__
479 ASSERT (LDBL_IS_IEC_60559);
480 #elif 0
481 /* It is not clear what this macro actually means. Cf.
482 <https://mailman.oakapple.net/pipermail/cfp-interest/2023-April/002760.html> */
483 ASSERT (!LDBL_IS_IEC_60559);
484 #endif
485
486 /* Check the value of LDBL_NORM_MAX. */
487 ASSERT (LDBL_NORM_MAX == LDBL_MAX);
488
489 /* Check the value of LDBL_SNAN. */
490 ASSERT (isnanl (LDBL_SNAN));
491 }
492
493 int
494 main ()
495 {
496 test_float ();
497 test_double ();
498
499 {
500 DECL_LONG_DOUBLE_ROUNDING
501
502 BEGIN_LONG_DOUBLE_ROUNDING ();
503
504 test_long_double ();
505
506 END_LONG_DOUBLE_ROUNDING ();
507 }
508
509 return test_exit_status;
510 }
511
512 #else
513
514 int
515 main ()
516 {
517 fprintf (stderr, "Skipping test: FLT_RADIX is not 2.\n");
518 return 77;
519 }
520
521 #endif