Contiki 2.5
vfieeefp.h
1 /****************************************************************
2  *
3  * The author of this software is David M. Gay.
4  *
5  * Copyright (c) 1991 by AT&T.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose without fee is hereby granted, provided that this entire notice
9  * is included in all copies of any software which is or includes a copy
10  * or modification of this software and in all copies of the supporting
11  * documentation for such software.
12  *
13  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14  * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
15  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
17  *
18  ***************************************************************/
19 
20 /* Please send bug reports to
21  David M. Gay
22  AT&T Bell Laboratories, Room 2C-463
23  600 Mountain Avenue
24  Murray Hill, NJ 07974-2070
25  U.S.A.
26  dmg@research.att.com or research!dmg
27  */
28 
29 /* This header file is a modification of mprec.h that only contains floating
30  point union code. */
31 
32 #include <ieeefp.h>
33 #include <math.h>
34 #include <float.h>
35 #include <errno.h>
36 #include <sys/config.h>
37 
38 #ifdef __IEEE_LITTLE_ENDIAN
39 #define IEEE_8087
40 #endif
41 
42 #ifdef __IEEE_BIG_ENDIAN
43 #define IEEE_MC68k
44 #endif
45 
46 #ifdef __Z8000__
47 #define Just_16
48 #endif
49 
50 #ifdef Unsigned_Shifts
51 #define Sign_Extend(a,b) if (b < 0) a |= (__uint32_t)0xffff0000;
52 #else
53 #define Sign_Extend(a,b) /*no-op*/
54 #endif
55 
56 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
57 Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
58 #endif
59 
60 #ifdef WANT_IO_LONG_DBL
61 /* If we are going to examine or modify specific bits in a long double using
62  the lword0 or lwordx macros, then we must wrap the long double inside
63  a union. This is necessary to avoid undefined behavior according to
64  the ANSI C spec. */
65 
66 #ifdef IEEE_8087
67 #if LDBL_MANT_DIG == 24
68 struct ldieee
69 {
70  unsigned manh:23;
71  unsigned exp:8;
72  unsigned sign:1;
73 };
74 #elif LDBL_MANT_DIG == 53
75 struct ldieee
76 {
77  unsigned manl:20;
78  unsigned manh:32;
79  unsigned exp:11;
80  unsigned sign:1;
81 };
82 #elif LDBL_MANT_DIG == 64
83 struct ldieee
84 {
85  unsigned manl:32;
86  unsigned manh:32;
87  unsigned exp:15;
88  unsigned sign:1;
89 };
90 #elif LDBL_MANT_DIG > 64
91 struct ldieee
92 {
93  unsigned manl3:16;
94  unsigned manl2:32;
95  unsigned manl:32;
96  unsigned manh:32;
97  unsigned exp:15;
98  unsigned sign:1;
99 };
100 #endif /* LDBL_MANT_DIG */
101 #else /* !IEEE_8087 */
102 #if LDBL_MANT_DIG == 24
103 struct ldieee
104 {
105  unsigned sign:1;
106  unsigned exp:8;
107  unsigned manh:23;
108 };
109 #elif LDBL_MANT_DIG == 53
110 struct ldieee
111 {
112  unsigned sign:1;
113  unsigned exp:11;
114  unsigned manh:32;
115  unsigned manl:20;
116 };
117 #elif LDBL_MANT_DIG == 64
118 struct ldieee
119 {
120  unsigned sign:1;
121  unsigned exp:15;
122  unsigned manh:32;
123  unsigned manl:32;
124 };
125 #elif LDBL_MANT_DIG > 64
126 struct ldieee
127 {
128  unsigned sign:1;
129  unsigned exp:15;
130  unsigned manh:32;
131  unsigned manl:32;
132  unsigned manl2:32;
133  unsigned manl3;16;
134 };
135 #endif /* LDBL_MANT_DIG */
136 #endif /* !IEEE_8087 */
137 #endif /* WANT_IO_LONG_DBL */
138 
139 /* If we are going to examine or modify specific bits in a double using
140  the word0 and/or word1 macros, then we must wrap the double inside
141  a union. This is necessary to avoid undefined behavior according to
142  the ANSI C spec. */
143 union double_union
144 {
145  double d;
146  __uint32_t i[2];
147 };
148 
149 #ifdef IEEE_8087
150 #define word0(x) (x.i[1])
151 #define word1(x) (x.i[0])
152 #else
153 #define word0(x) (x.i[0])
154 #define word1(x) (x.i[1])
155 #endif
156 
157 /* #define P DBL_MANT_DIG */
158 /* Ten_pmax = floor(P*log(2)/log(5)) */
159 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
160 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
161 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
162 
163 #if defined(IEEE_8087) + defined(IEEE_MC68k)
164 #if defined (_DOUBLE_IS_32BITS)
165 #define Exp_shift 23
166 #define Exp_shift1 23
167 #define Exp_msk1 ((__uint32_t)0x00800000L)
168 #define Exp_msk11 ((__uint32_t)0x00800000L)
169 #define Exp_mask ((__uint32_t)0x7f800000L)
170 #define P 24
171 #define Bias 127
172 #if 0
173 #define IEEE_Arith /* it is, but the code doesn't handle IEEE singles yet */
174 #endif
175 #define Emin (-126)
176 #define Exp_1 ((__uint32_t)0x3f800000L)
177 #define Exp_11 ((__uint32_t)0x3f800000L)
178 #define Ebits 8
179 #define Frac_mask ((__uint32_t)0x007fffffL)
180 #define Frac_mask1 ((__uint32_t)0x007fffffL)
181 #define Ten_pmax 10
182 #define Sign_bit ((__uint32_t)0x80000000L)
183 #define Ten_pmax 10
184 #define Bletch 2
185 #define Bndry_mask ((__uint32_t)0x007fffffL)
186 #define Bndry_mask1 ((__uint32_t)0x007fffffL)
187 #define LSB 1
188 #define Sign_bit ((__uint32_t)0x80000000L)
189 #define Log2P 1
190 #define Tiny0 0
191 #define Tiny1 1
192 #define Quick_max 5
193 #define Int_max 6
194 #define Infinite(x) (word0(x) == ((__uint32_t)0x7f800000L))
195 #undef word0
196 #undef word1
197 
198 #define word0(x) (x.i[0])
199 #define word1(x) 0
200 #else
201 
202 #define Exp_shift 20
203 #define Exp_shift1 20
204 #define Exp_msk1 ((__uint32_t)0x100000L)
205 #define Exp_msk11 ((__uint32_t)0x100000L)
206 #define Exp_mask ((__uint32_t)0x7ff00000L)
207 #define P 53
208 #define Bias 1023
209 #define IEEE_Arith
210 #define Emin (-1022)
211 #define Exp_1 ((__uint32_t)0x3ff00000L)
212 #define Exp_11 ((__uint32_t)0x3ff00000L)
213 #define Ebits 11
214 #define Frac_mask ((__uint32_t)0xfffffL)
215 #define Frac_mask1 ((__uint32_t)0xfffffL)
216 #define Ten_pmax 22
217 #define Bletch 0x10
218 #define Bndry_mask ((__uint32_t)0xfffffL)
219 #define Bndry_mask1 ((__uint32_t)0xfffffL)
220 #define LSB 1
221 #define Sign_bit ((__uint32_t)0x80000000L)
222 #define Log2P 1
223 #define Tiny0 0
224 #define Tiny1 1
225 #define Quick_max 14
226 #define Int_max 14
227 #define Infinite(x) (word0(x) == ((__uint32_t)0x7ff00000L)) /* sufficient test for here */
228 #endif
229 
230 #else
231 #undef Sudden_Underflow
232 #define Sudden_Underflow
233 #ifdef IBM
234 #define Exp_shift 24
235 #define Exp_shift1 24
236 #define Exp_msk1 ((__uint32_t)0x1000000L)
237 #define Exp_msk11 ((__uint32_t)0x1000000L)
238 #define Exp_mask ((__uint32_t)0x7f000000L)
239 #define P 14
240 #define Bias 65
241 #define Exp_1 ((__uint32_t)0x41000000L)
242 #define Exp_11 ((__uint32_t)0x41000000L)
243 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
244 #define Frac_mask ((__uint32_t)0xffffffL)
245 #define Frac_mask1 ((__uint32_t)0xffffffL)
246 #define Bletch 4
247 #define Ten_pmax 22
248 #define Bndry_mask ((__uint32_t)0xefffffL)
249 #define Bndry_mask1 ((__uint32_t)0xffffffL)
250 #define LSB 1
251 #define Sign_bit ((__uint32_t)0x80000000L)
252 #define Log2P 4
253 #define Tiny0 ((__uint32_t)0x100000L)
254 #define Tiny1 0
255 #define Quick_max 14
256 #define Int_max 15
257 #else /* VAX */
258 #define Exp_shift 23
259 #define Exp_shift1 7
260 #define Exp_msk1 0x80
261 #define Exp_msk11 ((__uint32_t)0x800000L)
262 #define Exp_mask ((__uint32_t)0x7f80L)
263 #define P 56
264 #define Bias 129
265 #define Exp_1 ((__uint32_t)0x40800000L)
266 #define Exp_11 ((__uint32_t)0x4080L)
267 #define Ebits 8
268 #define Frac_mask ((__uint32_t)0x7fffffL)
269 #define Frac_mask1 ((__uint32_t)0xffff007fL)
270 #define Ten_pmax 24
271 #define Bletch 2
272 #define Bndry_mask ((__uint32_t)0xffff007fL)
273 #define Bndry_mask1 ((__uint32_t)0xffff007fL)
274 #define LSB ((__uint32_t)0x10000L)
275 #define Sign_bit ((__uint32_t)0x8000L)
276 #define Log2P 1
277 #define Tiny0 0x80
278 #define Tiny1 0
279 #define Quick_max 15
280 #define Int_max 15
281 #endif
282 #endif
283 
284