Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / regress / lib / libc / ieeefp / subnormal / subnormal.c
blob99d3075baab57548e60e74db81fc29af70f2399e
1 /* $NetBSD$ */
2 #include <math.h>
3 #include <float.h>
4 #include <assert.h>
6 #ifdef USE_FLOAT
7 #define FPTYPE float
8 #define FPTYPE_MIN FLT_MIN
9 #define FPTYPE_MIN_EXP FLT_MIN_EXP
10 #define FPTYPE_MANT_DIG FLT_MANT_DIG
11 #define LDEXP(d,e) ldexpf(d,e)
12 #define MODF(d,ip) modff(d,ip)
13 #define FREXP(d,e) frexpf(d,e)
14 #else
15 #ifdef USE_LONGDOUBLE
16 #define FPTYPE long double
17 #define FPTYPE_MIN LDBL_MIN
18 #define FPTYPE_MIN_EXP LDBL_MIN_EXP
19 #define FPTYPE_MANT_DIG LDBL_MANT_DIG
20 #define LDEXP(d,e) ldexpl(d,e)
21 #define MODF(d,ip) modfl(d,ip)
22 #define FREXP(d,e) frexpl(d,e)
23 #else
24 #define FPTYPE double
25 #define FPTYPE_MIN DBL_MIN
26 #define FPTYPE_MIN_EXP DBL_MIN_EXP
27 #define FPTYPE_MANT_DIG DBL_MANT_DIG
28 #define LDEXP(d,e) ldexp(d,e)
29 #define MODF(d,ip) modf(d,ip)
30 #define FREXP(d,e) frexp(d,e)
31 #endif
32 #endif
34 int
35 main()
37 FPTYPE d0, d1, d2, f, ip;
38 int e, i;
40 d0 = FPTYPE_MIN;
41 assert(fpclassify(d0) == FP_NORMAL);
42 f = FREXP(d0, &e);
43 assert(e == FPTYPE_MIN_EXP);
44 assert(f == 0.5);
45 d1 = d0;
47 /* shift a "1" bit through the mantissa (skip the implicit bit) */
48 for (i = 1; i < FPTYPE_MANT_DIG; i++) {
49 d1 /= 2;
50 assert(fpclassify(d1) == FP_SUBNORMAL);
51 assert(d1 > 0 && d1 < d0);
53 d2 = LDEXP(d0, -i);
54 assert(d2 == d1);
55 d2 = MODF(d1, &ip);
56 assert(d2 == d1);
57 assert(ip == 0);
59 f = FREXP(d1, &e);
60 assert(e == FPTYPE_MIN_EXP - i);
61 assert(f == 0.5);
64 d1 /= 2;
65 assert(fpclassify(d1) == FP_ZERO);
66 f = FREXP(d1, &e);
67 assert(e == 0);
68 assert(f == 0);
70 return (0);