[project @ 1997-03-20 21:54:55 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 #if (defined (sun) && !defined(i386)) || defined(hp300) || defined(_IBMR2) || defined(sgi) || defined(hppa)
12 #define BIGENDIAN
13 #endif
14
15 #include "rtsdefs.h"
16 #include "ieee-flpt.h"
17 #include "floatExtreme.h"
18
19 #ifdef BIGENDIAN
20 #define L 1
21 #define H 0
22 #else
23 #define L 0
24 #define H 1
25 #endif
26
27 #ifdef IEEE_FLOATING_POINT
28
29 StgInt
30 isDoubleNaN(d)
31 StgDouble d;
32 {
33     union { double d; int i[2]; } u;
34     int hx,lx;
35     int r;
36
37     u.d = d;
38     hx = u.i[H];
39     lx = u.i[L];
40     hx &= 0x7fffffff;
41     hx |= (unsigned int)(lx|(-lx))>>31;        
42     hx = 0x7ff00000 - hx;
43     r = (int)((unsigned int)(hx))>>31;
44     return (r);
45 }
46
47 StgInt
48 isDoubleInfinite(d)
49 StgDouble d;
50 {
51     union { double d; int i[2]; } u;
52     int hx,lx;
53
54     u.d = d;
55     hx = u.i[H];
56     lx = u.i[L];
57     hx &= 0x7fffffff;
58     hx ^= 0x7ff00000;
59     hx |= lx;
60     return (hx == 0);
61 }
62
63 StgInt
64 isDoubleDenormalized(d) 
65 StgDouble d;
66 {
67     union { double d; int i[2]; } u;
68     int high, iexp;
69
70     u.d = d;
71     high = u.i[H];
72     iexp = high & (0x7ff << 20);
73     return (iexp == 0);
74 }
75
76 StgInt
77 isDoubleNegativeZero(d) 
78 StgDouble d;
79 {
80     union { double d; int i[2]; } u;
81     int high, iexp;
82
83     u.d = d;
84     return (u.i[H] == 0x80000000 && u.i[L] == 0);
85 }
86
87 StgInt
88 isFloatNaN(f) 
89 StgFloat f;
90 {
91     int ix;
92     int r;
93
94     ix = (int)f;
95     ix &= 0x7fffffff;
96     ix = 0x7f800000 - ix;
97     r = (int)(((unsigned int)(ix))>>31);
98     return (r);
99 }
100
101 StgInt
102 isFloatInfinite(f) 
103 StgFloat f;
104 {
105     int ix;
106
107     ix = (int)f;
108     ix &= 0x7fffffff;
109     ix ^= 0x7f800000;
110     return (ix == 0);
111 }
112
113 StgInt
114 isFloatDenormalized(f) 
115 StgFloat f;
116 {
117     int high, iexp;
118
119     high = (int)f;
120     iexp = high & (0xff << 23);
121     return (iexp == 0);
122 }
123
124 StgInt
125 isFloatNegativeZero(f) 
126 StgFloat f;
127 {
128     int high = (int)f;
129     return (high == 0x80000000);
130 }
131
132
133 #else
134
135 StgInt isDoubleNaN(d) StgDouble d; { return 0; }
136 StgInt isDoubleInfinite(d) StgDouble d; { return 0; }
137 StgInt isDoubleDenormalized(d) StgDouble d; { return 0; }
138 StgInt isDoubleNegativeZero(d) StgDouble d; { return 0; }
139 StgInt isFloatNaN(f) StgFloat f; { return 0; }
140 StgInt isFloatInfinite(f) StgFloat f; { return 0; }
141 StgInt isFloatDenormalized(f) StgFloat f; { return 0; }
142 StgInt isFloatNegativeZero(f) StgFloat f; { return 0; }
143
144 #endif
145
146
147 \end{code}