[project @ 1997-05-18 04:26:47 by sof]
[ghc-hetmet.git] / ghc / lib / cbits / floatExtreme.lc
1 %
2 %
3 %
4
5 Stubs to check for extremities of (IEEE) floats, 
6 the tests have been (artfully) lifted from the hbc-0.9999.3 (lib/fltcode.c)
7 source.
8
9 \begin{code}
10
11 #include "rtsdefs.h"
12 #include "ieee-flpt.h"
13 #include "floatExtreme.h"
14
15 #ifdef BIGENDIAN
16 #define L 1
17 #define H 0
18 #else
19 #define L 0
20 #define H 1
21 #endif
22
23 #ifdef IEEE_FLOATING_POINT
24
25 StgInt
26 isDoubleNaN(d)
27 StgDouble d;
28 {
29     union { double d; int i[2]; } u;
30     int hx,lx;
31     int r;
32
33     u.d = d;
34     hx = u.i[H];
35     lx = u.i[L];
36     hx &= 0x7fffffff;
37     hx |= (unsigned int)(lx|(-lx))>>31;        
38     hx = 0x7ff00000 - hx;
39     r = (int)((unsigned int)(hx))>>31;
40     return (r);
41 }
42
43 StgInt
44 isDoubleInfinite(d)
45 StgDouble d;
46 {
47     union { double d; int i[2]; } u;
48     int hx,lx;
49
50     u.d = d;
51     hx = u.i[H];
52     lx = u.i[L];
53     hx &= 0x7fffffff;
54     hx ^= 0x7ff00000;
55     hx |= lx;
56     return (hx == 0);
57 }
58
59 StgInt
60 isDoubleDenormalized(d) 
61 StgDouble d;
62 {
63     union { double d; int i[2]; } u;
64     int high, iexp;
65
66     u.d = d;
67     high = u.i[H];
68     iexp = high & (0x7ff << 20);
69     return (iexp == 0);
70 }
71
72 StgInt
73 isDoubleNegativeZero(d) 
74 StgDouble d;
75 {
76     union { double d; int i[2]; } u;
77     int high, iexp;
78
79     u.d = d;
80     return (u.i[H] == 0x80000000 && u.i[L] == 0);
81 }
82
83 StgInt
84 isFloatNaN(f) 
85 StgFloat f;
86 {
87     int ix;
88     int r;
89
90     ix = (int)f;
91     ix &= 0x7fffffff;
92     ix = 0x7f800000 - ix;
93     r = (int)(((unsigned int)(ix))>>31);
94     return (r);
95 }
96
97 StgInt
98 isFloatInfinite(f) 
99 StgFloat f;
100 {
101     int ix;
102
103     ix = (int)f;
104     ix &= 0x7fffffff;
105     ix ^= 0x7f800000;
106     return (ix == 0);
107 }
108
109 StgInt
110 isFloatDenormalized(f) 
111 StgFloat f;
112 {
113     int high, iexp;
114
115     high = (int)f;
116     iexp = high & (0xff << 23);
117     return (iexp == 0);
118 }
119
120 StgInt
121 isFloatNegativeZero(f) 
122 StgFloat f;
123 {
124     int high = (int)f;
125     return (high == 0x80000000);
126 }
127
128
129 #else
130
131 StgInt isDoubleNaN(d) StgDouble d; { return 0; }
132 StgInt isDoubleInfinite(d) StgDouble d; { return 0; }
133 StgInt isDoubleDenormalized(d) StgDouble d; { return 0; }
134 StgInt isDoubleNegativeZero(d) StgDouble d; { return 0; }
135 StgInt isFloatNaN(f) StgFloat f; { return 0; }
136 StgInt isFloatInfinite(f) StgFloat f; { return 0; }
137 StgInt isFloatDenormalized(f) StgFloat f; { return 0; }
138 StgInt isFloatNegativeZero(f) StgFloat f; { return 0; }
139
140 #endif
141
142
143 \end{code}