summaryrefslogtreecommitdiff
path: root/tests/test-float.c
blob: 7614e32e072fe068cda100ad0c34bdd89548072c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/* Test of <float.h> substitute.
   Copyright (C) 2011-2024 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2011.  */

#include <config.h>

#include <float.h>

/* Check that some macros are constant expressions.  */
int a[] =
  {
    FLT_RADIX,
    FLT_EVAL_METHOD /* added in ISO C 99 */
  };

/* ----------------------- Check macros for 'float' ----------------------- */

/* Check that the FLT_* macros expand to constant expressions.  */
int fb[] =
  {
    FLT_MANT_DIG, FLT_MIN_EXP, FLT_MAX_EXP,
    FLT_DIG, FLT_MIN_10_EXP, FLT_MAX_10_EXP,
    FLT_HAS_SUBNORM, /* added in ISO C 11 */
    FLT_DECIMAL_DIG, /* added in ISO C 11 */
    FLT_IS_IEC_60559 /* added in ISO C 23 */
  };
float fc[] =
  {
    FLT_EPSILON, FLT_MIN, FLT_MAX,
    FLT_TRUE_MIN, /* added in ISO C 11 */
    FLT_NORM_MAX /* added in ISO C 23 */
  };
#if 0 /* FLT_SNAN is not a constant expression on some platforms.  */
float fs = FLT_SNAN; /* added in ISO C 23 */
#endif

/* ----------------------- Check macros for 'double' ----------------------- */

/* Check that the DBL_* macros expand to constant expressions.  */
int db[] =
  {
    DBL_MANT_DIG, DBL_MIN_EXP, DBL_MAX_EXP,
    DBL_DIG, DBL_MIN_10_EXP, DBL_MAX_10_EXP,
    DBL_HAS_SUBNORM, /* added in ISO C 11 */
    DBL_DECIMAL_DIG, /* added in ISO C 11 */
    DBL_IS_IEC_60559 /* added in ISO C 23 */
  };
double dc[] =
  {
    DBL_EPSILON, DBL_MIN, DBL_MAX,
    DBL_TRUE_MIN, /* added in ISO C 11 */
    DBL_NORM_MAX /* added in ISO C 23 */
  };
#if 0 /* DBL_SNAN is not a constant expression on some platforms.  */
double ds = DBL_SNAN; /* added in ISO C 23 */
#endif

/* -------------------- Check macros for 'long double' -------------------- */

/* Check that the LDBL_* macros expand to constant expressions.  */
int lb[] =
  {
    LDBL_MANT_DIG, LDBL_MIN_EXP, LDBL_MAX_EXP,
    LDBL_DIG, LDBL_MIN_10_EXP, LDBL_MAX_10_EXP,
    LDBL_HAS_SUBNORM, /* added in ISO C 11 */
    LDBL_DECIMAL_DIG, /* added in ISO C 11 */
    LDBL_IS_IEC_60559 /* added in ISO C 23 */
  };
long double lc1 = LDBL_EPSILON;
long double lc2 = LDBL_MIN;
#if !GNULIB_defined_long_double_union
/* LDBL_MAX is not a constant expression on some platforms.  */
long double lc3 = LDBL_MAX;
#endif
#if !GNULIB_defined_long_double_union
/* LDBL_TRUE_MIN is not a constant expression on FreeBSD/i386.  */
long double lc4 = LDBL_TRUE_MIN; /* added in ISO C 11 */
#endif
#if !GNULIB_defined_long_double_union
/* LDBL_MAX is not a constant expression on some platforms.  */
long double lc5 = LDBL_NORM_MAX; /* added in ISO C 23 */
#endif
#if 0
/* LDBL_SNAN is not a constant expression on some platforms.  */
long double ls = LDBL_SNAN; /* added in ISO C 23 */
#endif

/* ------------------------------------------------------------------------- */

#include "fpucw.h"
#include "isnanf-nolibm.h"
#include "isnand-nolibm.h"
#include "isnanl-nolibm.h"
#include "macros.h"

#if FLT_RADIX == 2

/* Return 2^n.  */
static float
pow2f (int n)
{
  int k = n;
  volatile float x = 1;
  volatile float y = 2;
  /* Invariant: 2^n == x * y^k.  */
  if (k < 0)
    {
      y = 0.5f;
      k = - k;
    }
  while (k > 0)
    {
      if (k != 2 * (k / 2))
        {
          x = x * y;
          k = k - 1;
        }
      if (k == 0)
        break;
      y = y * y;
      k = k / 2;
    }
  /* Now k == 0, hence x == 2^n.  */
  return x;
}

/* Return 2^n.  */
static double
pow2d (int n)
{
  int k = n;
  volatile double x = 1;
  volatile double y = 2;
  /* Invariant: 2^n == x * y^k.  */
  if (k < 0)
    {
      y = 0.5;
      k = - k;
    }
  while (k > 0)
    {
      if (k != 2 * (k / 2))
        {
          x = x * y;
          k = k - 1;
        }
      if (k == 0)
        break;
      y = y * y;
      k = k / 2;
    }
  /* Now k == 0, hence x == 2^n.  */
  return x;
}

/* Return 2^n.  */
static long double
pow2l (int n)
{
  int k = n;
  volatile long double x = 1;
  volatile long double y = 2;
  /* Invariant: 2^n == x * y^k.  */
  if (k < 0)
    {
      y = 0.5L;
      k = - k;
    }
  while (k > 0)
    {
      if (k != 2 * (k / 2))
        {
          x = x * y;
          k = k - 1;
        }
      if (k == 0)
        break;
      y = y * y;
      k = k / 2;
    }
  /* Now k == 0, hence x == 2^n.  */
  return x;
}

/* ----------------------- Check macros for 'float' ----------------------- */

static void
test_float (void)
{
  /* Check that the value of FLT_MIN_EXP is well parenthesized.  */
  ASSERT ((FLT_MIN_EXP % 101111) == (FLT_MIN_EXP) % 101111);

  /* Check that the value of DBL_MIN_10_EXP is well parenthesized.  */
  ASSERT ((FLT_MIN_10_EXP % 101111) == (FLT_MIN_10_EXP) % 101111);

  /* Check that 'float' is as specified in IEEE 754.  */
  ASSERT (FLT_MANT_DIG == 24);
  ASSERT (FLT_MIN_EXP == -125);
  ASSERT (FLT_MAX_EXP == 128);

  /* Check the value of FLT_MIN_10_EXP.  */
  ASSERT (FLT_MIN_10_EXP == - (int) (- (FLT_MIN_EXP - 1) * 0.30103));

  /* Check the value of FLT_DIG.  */
  ASSERT (FLT_DIG == (int) ((FLT_MANT_DIG - 1) * 0.30103));

  /* Check the value of FLT_MIN_10_EXP.  */
  ASSERT (FLT_MIN_10_EXP == - (int) (- (FLT_MIN_EXP - 1) * 0.30103));

  /* Check the value of FLT_MAX_10_EXP.  */
  ASSERT (FLT_MAX_10_EXP == (int) (FLT_MAX_EXP * 0.30103));

  /* Check the value of FLT_MAX.  */
  {
    volatile float m = FLT_MAX;
    int n;

    ASSERT (m + m > m);
    for (n = 0; n <= 2 * FLT_MANT_DIG; n++)
      {
        volatile float pow2_n = pow2f (n); /* 2^n */
        volatile float x = m + (m / pow2_n);
        if (x > m)
          ASSERT (x + x == x);
        else
          ASSERT (!(x + x == x));
      }
  }

  /* Check the value of FLT_MIN.  */
  {
    volatile float m = FLT_MIN;
    volatile float x = pow2f (FLT_MIN_EXP - 1);
    ASSERT (m == x);
  }

  /* Check the value of FLT_EPSILON.  */
  {
    volatile float e = FLT_EPSILON;
    volatile float me;
    int n;

    me = 1.0f + e;
    ASSERT (me > 1.0f);
    ASSERT (me - 1.0f == e);
    for (n = 0; n <= 2 * FLT_MANT_DIG; n++)
      {
        volatile float half_n = pow2f (- n); /* 2^-n */
        volatile float x = me - half_n;
        if (x < me)
          ASSERT (x <= 1.0f);
      }
  }

  /* Check the value of FLT_HAS_SUBNORM.  */
  ASSERT (FLT_HAS_SUBNORM == 1);

  /* Check the value of FLT_DECIMAL_DIG.  */
  ASSERT (FLT_DECIMAL_DIG == (int)(FLT_MANT_DIG * 0.3010299956639812 + 2));

  /* Check the value of FLT_TRUE_MIN.  */
  ASSERT (FLT_TRUE_MIN > 0.0f);
  {
    volatile float x = FLT_TRUE_MIN * 0.5f;
    ASSERT (x == 0.0f);
  }

  /* Check the value of FLT_IS_IEC_60559.  */
#if !defined __m68k__
  ASSERT (FLT_IS_IEC_60559);
#elif 0
  /* It is not clear what this macro actually means.  Cf.
     <http://mailman.oakapple.net/pipermail/cfp-interest/2023-April/002760.html> */
  ASSERT (!FLT_IS_IEC_60559);
#endif

  /* Check the value of FLT_NORM_MAX.  */
  ASSERT (FLT_NORM_MAX == FLT_MAX);

  /* Check the value of FLT_SNAN.  */
  ASSERT (isnanf (FLT_SNAN));
}

/* ----------------------- Check macros for 'double' ----------------------- */

static void
test_double (void)
{
  /* Check that the value of DBL_MIN_EXP is well parenthesized.  */
  ASSERT ((DBL_MIN_EXP % 101111) == (DBL_MIN_EXP) % 101111);

  /* Check that the value of DBL_MIN_10_EXP is well parenthesized.  */
  ASSERT ((DBL_MIN_10_EXP % 101111) == (DBL_MIN_10_EXP) % 101111);

  /* Check that 'double' is as specified in IEEE 754.  */
  ASSERT (DBL_MANT_DIG == 53);
  ASSERT (DBL_MIN_EXP == -1021);
  ASSERT (DBL_MAX_EXP == 1024);

  /* Check the value of DBL_MIN_10_EXP.  */
  ASSERT (DBL_MIN_10_EXP == - (int) (- (DBL_MIN_EXP - 1) * 0.30103));

  /* Check the value of DBL_DIG.  */
  ASSERT (DBL_DIG == (int) ((DBL_MANT_DIG - 1) * 0.30103));

  /* Check the value of DBL_MIN_10_EXP.  */
  ASSERT (DBL_MIN_10_EXP == - (int) (- (DBL_MIN_EXP - 1) * 0.30103));

  /* Check the value of DBL_MAX_10_EXP.  */
  ASSERT (DBL_MAX_10_EXP == (int) (DBL_MAX_EXP * 0.30103));

  /* Check the value of DBL_MAX.  */
  {
    volatile double m = DBL_MAX;
    int n;

    ASSERT (m + m > m);
    for (n = 0; n <= 2 * DBL_MANT_DIG; n++)
      {
        volatile double pow2_n = pow2d (n); /* 2^n */
        volatile double x = m + (m / pow2_n);
        if (x > m)
          ASSERT (x + x == x);
        else
          ASSERT (!(x + x == x));
      }
  }

  /* Check the value of DBL_MIN.  */
  {
    volatile double m = DBL_MIN;
    volatile double x = pow2d (DBL_MIN_EXP - 1);
    ASSERT (m == x);
  }

  /* Check the value of DBL_EPSILON.  */
  {
    volatile double e = DBL_EPSILON;
    volatile double me;
    int n;

    me = 1.0 + e;
    ASSERT (me > 1.0);
    ASSERT (me - 1.0 == e);
    for (n = 0; n <= 2 * DBL_MANT_DIG; n++)
      {
        volatile double half_n = pow2d (- n); /* 2^-n */
        volatile double x = me - half_n;
        if (x < me)
          ASSERT (x <= 1.0);
      }
  }

  /* Check the value of DBL_HAS_SUBNORM.  */
  ASSERT (DBL_HAS_SUBNORM == 1);

  /* Check the value of DBL_DECIMAL_DIG.  */
  ASSERT (DBL_DECIMAL_DIG == (int)(DBL_MANT_DIG * 0.3010299956639812 + 2));

  /* Check the value of DBL_TRUE_MIN.  */
  ASSERT (DBL_TRUE_MIN > 0.0);
  {
    volatile double x = DBL_TRUE_MIN * 0.5;
    ASSERT (x == 0.0);
  }

  /* Check the value of DBL_IS_IEC_60559.  */
#if !defined __m68k__
  ASSERT (DBL_IS_IEC_60559);
#elif 0
  /* It is not clear what this macro actually means.  Cf.
     <http://mailman.oakapple.net/pipermail/cfp-interest/2023-April/002760.html> */
  ASSERT (!DBL_IS_IEC_60559);
#endif

  /* Check the value of DBL_NORM_MAX.  */
  ASSERT (DBL_NORM_MAX == DBL_MAX);

  /* Check the value of DBL_SNAN.  */
  ASSERT (isnand (DBL_SNAN));
}

/* -------------------- Check macros for 'long double' -------------------- */

static void
test_long_double (void)
{
  /* Check that the value of LDBL_MIN_EXP is well parenthesized.  */
  ASSERT ((LDBL_MIN_EXP % 101111) == (LDBL_MIN_EXP) % 101111);

  /* Check that the value of LDBL_MIN_10_EXP is well parenthesized.  */
  ASSERT ((LDBL_MIN_10_EXP % 101111) == (LDBL_MIN_10_EXP) % 101111);

  /* Check that 'long double' is at least as wide as 'double'.  */
  ASSERT (LDBL_MANT_DIG >= DBL_MANT_DIG);
  ASSERT (LDBL_MIN_EXP - LDBL_MANT_DIG <= DBL_MIN_EXP - DBL_MANT_DIG);
  ASSERT (LDBL_MAX_EXP >= DBL_MAX_EXP);

  /* Check the value of LDBL_DIG.  */
  ASSERT (LDBL_DIG == (int)((LDBL_MANT_DIG - 1) * 0.30103));

  /* Check the value of LDBL_MIN_10_EXP.  */
  ASSERT (LDBL_MIN_10_EXP == - (int) (- (LDBL_MIN_EXP - 1) * 0.30103));

  /* Check the value of LDBL_MAX_10_EXP.  */
  ASSERT (LDBL_MAX_10_EXP == (int) (LDBL_MAX_EXP * 0.30103));

  /* Check the value of LDBL_MAX.  */
  {
    volatile long double m = LDBL_MAX;
    int n;

    ASSERT (m + m > m);
    for (n = 0; n <= 2 * LDBL_MANT_DIG; n++)
      {
        volatile long double pow2_n = pow2l (n); /* 2^n */
        volatile long double x = m + (m / pow2_n);
        if (x > m)
          ASSERT (x + x == x);
        else
          ASSERT (!(x + x == x));
      }
  }

  /* Check the value of LDBL_MIN.  */
  {
    volatile long double m = LDBL_MIN;
    volatile long double x = pow2l (LDBL_MIN_EXP - 1);
    ASSERT (m == x);
  }

  /* Check the value of LDBL_EPSILON.  */
  {
    volatile long double e = LDBL_EPSILON;
    volatile long double me;
    int n;

    me = 1.0L + e;
    ASSERT (me > 1.0L);
    ASSERT (me - 1.0L == e);
    for (n = 0; n <= 2 * LDBL_MANT_DIG; n++)
      {
        volatile long double half_n = pow2l (- n); /* 2^-n */
        volatile long double x = me - half_n;
        if (x < me)
          ASSERT (x <= 1.0L);
      }
  }

  /* Check the value of LDBL_HAS_SUBNORM.  */
  ASSERT (LDBL_HAS_SUBNORM == 1);

  /* Check the value of LDBL_DECIMAL_DIG.  */
  ASSERT (LDBL_DECIMAL_DIG == (int)(LDBL_MANT_DIG * 0.3010299956639812 + 2));

  /* Check the value of LDBL_TRUE_MIN.  */
  ASSERT (LDBL_TRUE_MIN > 0.0L);
  {
    volatile long double x = LDBL_TRUE_MIN * 0.5L;
    ASSERT (x == 0.0L);
  }

  /* Check the value of LDBL_IS_IEC_60559.  */
#if (LDBL_MANT_DIG == 53 || LDBL_MANT_DIG == 113) && !defined __m68k__
  ASSERT (LDBL_IS_IEC_60559);
#elif 0
  /* It is not clear what this macro actually means.  Cf.
     <http://mailman.oakapple.net/pipermail/cfp-interest/2023-April/002760.html> */
  ASSERT (!LDBL_IS_IEC_60559);
#endif

  /* Check the value of LDBL_NORM_MAX.  */
  ASSERT (LDBL_NORM_MAX == LDBL_MAX);

  /* Check the value of LDBL_SNAN.  */
  ASSERT (isnanl (LDBL_SNAN));
}

int
main ()
{
  test_float ();
  test_double ();

  {
    DECL_LONG_DOUBLE_ROUNDING

    BEGIN_LONG_DOUBLE_ROUNDING ();

    test_long_double ();

    END_LONG_DOUBLE_ROUNDING ();
  }

  return test_exit_status;
}

#else

int
main ()
{
  fprintf (stderr, "Skipping test: FLT_RADIX is not 2.\n");
  return 77;
}

#endif