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
|
/* Test of <float.h> substitute.
Copyright (C) 2011-2017 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 <http://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2011. */
#include <config.h>
#include <float.h>
#include "fpucw.h"
#include "macros.h"
/* Check that FLT_RADIX is a constant expression. */
int a[] = { FLT_RADIX };
#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' ----------------------- */
/* 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
};
float fc[] = { FLT_EPSILON, FLT_MIN, FLT_MAX };
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 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
};
double dc[] = { DBL_EPSILON, DBL_MIN, DBL_MAX };
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 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
};
long double lc1 = LDBL_EPSILON;
long double lc2 = LDBL_MIN;
#if 0 /* LDBL_MAX is not a constant expression on some platforms. */
long double lc3 = LDBL_MAX;
#endif
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);
}
}
}
int
main ()
{
test_float ();
test_double ();
{
DECL_LONG_DOUBLE_ROUNDING
BEGIN_LONG_DOUBLE_ROUNDING ();
test_long_double ();
END_LONG_DOUBLE_ROUNDING ();
}
return 0;
}
#else
int
main ()
{
fprintf (stderr, "Skipping test: FLT_RADIX is not 2.\n");
return 77;
}
#endif
|