root/maint/gnulib/tests/test-exp2.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. test_function

   1 /* Test of exp2*() function family.
   2    Copyright (C) 2012-2021 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 static void
  18 test_function (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  19 {
  20   int i;
  21   int j;
  22   const DOUBLE TWO_MANT_DIG =
  23     /* Assume MANT_DIG <= 5 * 31.
  24        Use the identity
  25          n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5).  */
  26     (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
  27     * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
  28     * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
  29     * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
  30     * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
  31 
  32   /* Integral arguments.  */
  33   {
  34     DOUBLE x = L_(0.0);
  35     DOUBLE y = EXP2 (x);
  36     ASSERT (y == L_(1.0));
  37   }
  38   /* <https://sourceware.org/bugzilla/show_bug.cgi?id=13824> */
  39 #if !(defined __linux__ && (defined __sparc__ || defined __powerpc__))
  40   {
  41     int e;
  42     DOUBLE x;
  43     DOUBLE y;
  44     for (e = 0, x = L_(0.0), y = L_(1.0);
  45          e <= MAX_EXP - 1;
  46          e++, x = x + L_(1.0), y = y * L_(2.0))
  47       {
  48         /* Invariant: x = e, y = 2^e.  */
  49         DOUBLE z = EXP2 (x);
  50         ASSERT (z == y);
  51       }
  52   }
  53   {
  54     int e;
  55     DOUBLE x;
  56     DOUBLE y;
  57     for (e = 0, x = L_(0.0), y = L_(1.0);
  58          e >= MIN_EXP - 1;
  59          e--, x = x - L_(1.0), y = y * L_(0.5))
  60       {
  61         /* Invariant: x = e, y = 2^e.  */
  62         DOUBLE z = EXP2 (x);
  63         ASSERT (z == y);
  64       }
  65   }
  66 #endif
  67 
  68   /* Randomized tests.  */
  69   {
  70     /* Error bound, in ulps.  */
  71     const DOUBLE err_bound =
  72       (sizeof (DOUBLE) > sizeof (double) ?
  73 #if defined __i386__ && defined __FreeBSD__
  74        /* On FreeBSD/x86 6.4, the 'long double' type really has only 53 bits of
  75           precision in the compiler but 64 bits of precision at runtime.  See
  76           <https://lists.gnu.org/r/bug-gnulib/2008-07/msg00063.html>.
  77           The compiler has truncated all 'long double' literals in exp2l.c to
  78           53 bits of precision.  */
  79        L_(1350.0)
  80 #else
  81        L_(3.0)
  82 #endif
  83        : L_(3.0));
  84 
  85     for (i = 0; i < SIZEOF (RANDOM); i++)
  86       {
  87         DOUBLE x = L_(16.0) * RANDOM[i]; /* 0.0 <= x <= 16.0 */
  88         DOUBLE y = EXP2 (x);
  89         DOUBLE z = EXP2 (- x);
  90         DOUBLE err = y * z - L_(1.0);
  91         ASSERT (y > L_(0.0));
  92         ASSERT (z > L_(0.0));
  93         ASSERT (err > - err_bound / TWO_MANT_DIG
  94                 && err < err_bound / TWO_MANT_DIG);
  95       }
  96   }
  97 
  98   {
  99     /* Error bound, in ulps.  */
 100     const DOUBLE err_bound =
 101       (sizeof (DOUBLE) > sizeof (double) ?
 102 #if defined __i386__ && defined __FreeBSD__
 103        L_(2300.0)
 104 #else
 105        L_(29.0)
 106 #endif
 107        : L_(11.0));
 108 
 109     for (i = 0; i < SIZEOF (RANDOM) / 5; i++)
 110       for (j = 0; j < SIZEOF (RANDOM) / 5; j++)
 111         {
 112           DOUBLE x = L_(32.0) * RANDOM[i] - L_(16.0); /* -16.0 <= x <= 16.0 */
 113           DOUBLE y = L_(32.0) * RANDOM[j] - L_(16.0); /* -16.0 <= y <= 16.0 */
 114           DOUBLE z = - x - y;
 115           /* Approximately  x + y + z = 0.  */
 116           DOUBLE err = EXP2 (x) * EXP2 (y) * EXP2 (z) - L_(1.0);
 117           ASSERT (err > - err_bound / TWO_MANT_DIG
 118                   && err < err_bound / TWO_MANT_DIG);
 119         }
 120   }
 121 }
 122 
 123 volatile DOUBLE x;
 124 DOUBLE y;

/* [previous][next][first][last][top][bottom][index][help] */