Debian Codegraph @

source/shared_lib/include/streflop/softfloat/softfloat-specialize

raw
1 /*============================================================================
2 PROMINENT NOTICE: THIS IS A DERIVATIVE WORK OF THE ORIGINAL SOFTFLOAT CODE
3 CHANGES:
4 This derived work raises REAL system traps, controlled by the value
5 of a global variable.
6
7 Streflop defines the flags controlling traps.
8
9 The following files are now included too
10 #include <unistd.h>
11 #include <signal.h>
12
13 Nicolas Brodu, 2006
14 =============================================================================*/
15 #ifdef STREFLOP_SOFT
16
17 #include <unistd.h>
18 #include <signal.h>
19 #include "../streflop.h"
20
21 namespace streflop {
22 namespace SoftFloat {
23
24 // Here is the variable that controls sending real traps.
25 // Initalized to 0, see FPUSettings.h to check this masks all exceptions
26 int float_exception_realtraps = 0;
27
28 /*============================================================================
29
30 This C source fragment is part of the SoftFloat IEC/IEEE Floating-point
31 Arithmetic Package, Release 2b.
32
33 Written by John R. Hauser. This work was made possible in part by the
34 International Computer Science Institute, located at Suite 600, 1947 Center
35 Street, Berkeley, California 94704. Funding was partially provided by the
36 National Science Foundation under grant MIP-9311980. The original version
37 of this code was written as part of a project to build a fixed-point vector
38 processor in collaboration with the University of California at Berkeley,
39 overseen by Profs. Nelson Morgan and John Wawrzynek. More information
40 is available through the Web page `http://www.cs.berkeley.edu/~jhauser/
41 arithmetic/SoftFloat.html'.
42
43 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort has
44 been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT TIMES
45 RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO PERSONS
46 AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ALL LOSSES,
47 COSTS, OR OTHER PROBLEMS THEY INCUR DUE TO THE SOFTWARE, AND WHO FURTHERMORE
48 EFFECTIVELY INDEMNIFY JOHN HAUSER AND THE INTERNATIONAL COMPUTER SCIENCE
49 INSTITUTE (possibly via similar legal warning) AGAINST ALL LOSSES, COSTS, OR
50 OTHER PROBLEMS INCURRED BY THEIR CUSTOMERS AND CLIENTS DUE TO THE SOFTWARE.
51
52 Derivative works are acceptable, even for commercial purposes, so long as
53 (1) the source code for the derivative work includes prominent notice that
54 the work is derivative, and (2) the source code includes prominent notice with
55 these four paragraphs for those parts of this code that are retained.
56
57 =============================================================================*/
58
59 /*----------------------------------------------------------------------------
60 | Underflow tininess-detection mode, statically initialized to default value.
61 | (The declaration in `softfloat.h' must match the `int8' type here.)
62 *----------------------------------------------------------------------------*/
63 int8 float_detect_tininess = float_tininess_after_rounding;
64
65 /*----------------------------------------------------------------------------
66 | Raises the exceptions specified by `flags'. Floating-point traps can be
67 | defined here if desired. It is currently not possible for such a trap
68 | to substitute a result value. If traps are not implemented, this routine
69 | should be simply `float_exception_flags |= flags;'.
70 *----------------------------------------------------------------------------*/
71
72 void float_raise( int8 flags )
73 {
74
75 float_exception_flags |= flags;
76
77 /* NB060423: Modifications to send real traps
78 Conversion needed between softfloat system and x87 system to check for matches
79 */
80 int trap = 0;
81
82 if ((flags & float_flag_invalid !=0) && (float_exception_realtraps & FE_INVALID !=0)) {
83 trap = 1;
84 }
85 if ((flags & float_flag_divbyzero !=0) && (float_exception_realtraps & FE_DIVBYZERO !=0)) {
86 trap = 1;
87 }
88 if ((flags & float_flag_overflow !=0) && (float_exception_realtraps & FE_OVERFLOW !=0)) {
89 trap = 1;
90 }
91 if ((flags & float_flag_underflow !=0) && (float_exception_realtraps & FE_UNDERFLOW !=0)) {
92 trap = 1;
93 }
94 if ((flags & float_flag_inexact !=0) && (float_exception_realtraps & FE_INEXACT !=0)) {
95 trap = 1;
96 }
97
98 // Send SIGFPE signal to current process
99 if (trap==1) {
100 kill(getpid(), SIGFPE);
101 }
102 }
103
104 /*----------------------------------------------------------------------------
105 | Internal canonical NaN format.
106 *----------------------------------------------------------------------------*/
107 typedef struct {
108 flag sign;
109 bits64 high, low;
110 } commonNaNT;
111
112 /*----------------------------------------------------------------------------
113 | The pattern for a default generated single-precision NaN.
114 *----------------------------------------------------------------------------*/
115 #define float32_default_nan 0xFFC00000
116
117 /*----------------------------------------------------------------------------
118 | Returns 1 if the single-precision floating-point value `a' is a NaN;
119 | otherwise returns 0.
120 *----------------------------------------------------------------------------*/
121
122 flag float32_is_nan( float32 a )
123 {
124
125 return ( 0xFF000000 < (bits32) ( a<<1 ) );
126
127 }
128
129 /*----------------------------------------------------------------------------
130 | Returns 1 if the single-precision floating-point value `a' is a signaling
131 | NaN; otherwise returns 0.
132 *----------------------------------------------------------------------------*/
133
134 flag float32_is_signaling_nan( float32 a )
135 {
136
137 return ( ( ( a>>22 ) & 0x1FF ) == 0x1FE ) && ( a & 0x003FFFFF );
138
139 }
140
141 /*----------------------------------------------------------------------------
142 | Returns the result of converting the single-precision floating-point NaN
143 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
144 | exception is raised.
145 *----------------------------------------------------------------------------*/
146
147 static commonNaNT float32ToCommonNaN( float32 a )
148 {
149 commonNaNT z;
150
151 if ( float32_is_signaling_nan( a ) ) float_raise( float_flag_invalid );
152 z.sign = a>>31;
153 z.low = 0;
154 z.high = ( (bits64) a )<<41;
155 return z;
156
157 }
158
159 /*----------------------------------------------------------------------------
160 | Returns the result of converting the canonical NaN `a' to the single-
161 | precision floating-point format.
162 *----------------------------------------------------------------------------*/
163
164 static float32 commonNaNToFloat32( commonNaNT a )
165 {
166
167 return ( ( (bits32) a.sign )<<31 ) | 0x7FC00000 | ( a.high>>41 );
168
169 }
170
171 /*----------------------------------------------------------------------------
172 | Takes two single-precision floating-point values `a' and `b', one of which
173 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
174 | signaling NaN, the invalid exception is raised.
175 *----------------------------------------------------------------------------*/
176
177 static float32 propagateFloat32NaN( float32 a, float32 b )
178 {
179 flag aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN;
180
181 aIsNaN = float32_is_nan( a );
182 aIsSignalingNaN = float32_is_signaling_nan( a );
183 bIsNaN = float32_is_nan( b );
184 bIsSignalingNaN = float32_is_signaling_nan( b );
185 a |= 0x00400000;
186 b |= 0x00400000;
187 if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_invalid );
188 if ( aIsSignalingNaN ) {
189 if ( bIsSignalingNaN ) goto returnLargerSignificand;
190 return bIsNaN ? b : a;
191 }
192 else if ( aIsNaN ) {
193 if ( bIsSignalingNaN | ! bIsNaN ) return a;
194 returnLargerSignificand:
195 if ( (bits32) ( a<<1 ) < (bits32) ( b<<1 ) ) return b;
196 if ( (bits32) ( b<<1 ) < (bits32) ( a<<1 ) ) return a;
197 return ( a < b ) ? a : b;
198 }
199 else {
200 return b;
201 }
202
203 }
204
205 /*----------------------------------------------------------------------------
206 | The pattern for a default generated double-precision NaN.
207 *----------------------------------------------------------------------------*/
208 #define float64_default_nan LIT64( 0xFFF8000000000000 )
209
210 /*----------------------------------------------------------------------------
211 | Returns 1 if the double-precision floating-point value `a' is a NaN;
212 | otherwise returns 0.
213 *----------------------------------------------------------------------------*/
214
215 flag float64_is_nan( float64 a )
216 {
217
218 return ( LIT64( 0xFFE0000000000000 ) < (bits64) ( a<<1 ) );
219
220 }
221
222 /*----------------------------------------------------------------------------
223 | Returns 1 if the double-precision floating-point value `a' is a signaling
224 | NaN; otherwise returns 0.
225 *----------------------------------------------------------------------------*/
226
227 flag float64_is_signaling_nan( float64 a )
228 {
229
230 return
231 ( ( ( a>>51 ) & 0xFFF ) == 0xFFE )
232 && ( a & LIT64( 0x0007FFFFFFFFFFFF ) );
233
234 }
235
236 /*----------------------------------------------------------------------------
237 | Returns the result of converting the double-precision floating-point NaN
238 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
239 | exception is raised.
240 *----------------------------------------------------------------------------*/
241
242 static commonNaNT float64ToCommonNaN( float64 a )
243 {
244 commonNaNT z;
245
246 if ( float64_is_signaling_nan( a ) ) float_raise( float_flag_invalid );
247 z.sign = a>>63;
248 z.low = 0;
249 z.high = a<<12;
250 return z;
251
252 }
253
254 /*----------------------------------------------------------------------------
255 | Returns the result of converting the canonical NaN `a' to the double-
256 | precision floating-point format.
257 *----------------------------------------------------------------------------*/
258
259 static float64 commonNaNToFloat64( commonNaNT a )
260 {
261
262 return
263 ( ( (bits64) a.sign )<<63 )
264 | LIT64( 0x7FF8000000000000 )
265 | ( a.high>>12 );
266
267 }
268
269 /*----------------------------------------------------------------------------
270 | Takes two double-precision floating-point values `a' and `b', one of which
271 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
272 | signaling NaN, the invalid exception is raised.
273 *----------------------------------------------------------------------------*/
274
275 static float64 propagateFloat64NaN( float64 a, float64 b )
276 {
277 flag aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN;
278
279 aIsNaN = float64_is_nan( a );
280 aIsSignalingNaN = float64_is_signaling_nan( a );
281 bIsNaN = float64_is_nan( b );
282 bIsSignalingNaN = float64_is_signaling_nan( b );
283 a |= LIT64( 0x0008000000000000 );
284 b |= LIT64( 0x0008000000000000 );
285 if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_invalid );
286 if ( aIsSignalingNaN ) {
287 if ( bIsSignalingNaN ) goto returnLargerSignificand;
288 return bIsNaN ? b : a;
289 }
290 else if ( aIsNaN ) {
291 if ( bIsSignalingNaN | ! bIsNaN ) return a;
292 returnLargerSignificand:
293 if ( (bits64) ( a<<1 ) < (bits64) ( b<<1 ) ) return b;
294 if ( (bits64) ( b<<1 ) < (bits64) ( a<<1 ) ) return a;
295 return ( a < b ) ? a : b;
296 }
297 else {
298 return b;
299 }
300
301 }
302
303 #ifdef FLOATX80
304
305 /*----------------------------------------------------------------------------
306 | The pattern for a default generated extended double-precision NaN. The
307 | `high' and `low' values hold the most- and least-significant bits,
308 | respectively.
309 *----------------------------------------------------------------------------*/
310 #define floatx80_default_nan_high 0xFFFF
311 #define floatx80_default_nan_low LIT64( 0xC000000000000000 )
312
313 /*----------------------------------------------------------------------------
314 | Returns 1 if the extended double-precision floating-point value `a' is a
315 | NaN; otherwise returns 0.
316 *----------------------------------------------------------------------------*/
317
318 flag floatx80_is_nan( floatx80 a )
319 {
320
321 return ( ( a.high & 0x7FFF ) == 0x7FFF ) && (bits64) ( a.low<<1 );
322
323 }
324
325 /*----------------------------------------------------------------------------
326 | Returns 1 if the extended double-precision floating-point value `a' is a
327 | signaling NaN; otherwise returns 0.
328 *----------------------------------------------------------------------------*/
329
330 flag floatx80_is_signaling_nan( floatx80 a )
331 {
332 bits64 aLow;
333
334 aLow = a.low & ~ LIT64( 0x4000000000000000 );
335 return
336 ( ( a.high & 0x7FFF ) == 0x7FFF )
337 && (bits64) ( aLow<<1 )
338 && ( a.low == aLow );
339
340 }
341
342 /*----------------------------------------------------------------------------
343 | Returns the result of converting the extended double-precision floating-
344 | point NaN `a' to the canonical NaN format. If `a' is a signaling NaN, the
345 | invalid exception is raised.
346 *----------------------------------------------------------------------------*/
347
348 static commonNaNT floatx80ToCommonNaN( floatx80 a )
349 {
350 commonNaNT z;
351
352 if ( floatx80_is_signaling_nan( a ) ) float_raise( float_flag_invalid );
353 z.sign = a.high>>15;
354 z.low = 0;
355 z.high = a.low<<1;
356 return z;
357
358 }
359
360 /*----------------------------------------------------------------------------
361 | Returns the result of converting the canonical NaN `a' to the extended
362 | double-precision floating-point format.
363 *----------------------------------------------------------------------------*/
364
365 static floatx80 commonNaNToFloatx80( commonNaNT a )
366 {
367 floatx80 z;
368
369 z.low = LIT64( 0xC000000000000000 ) | ( a.high>>1 );
370 z.high = ( ( (bits16) a.sign )<<15 ) | 0x7FFF;
371 return z;
372
373 }
374
375 /*----------------------------------------------------------------------------
376 | Takes two extended double-precision floating-point values `a' and `b', one
377 | of which is a NaN, and returns the appropriate NaN result. If either `a' or
378 | `b' is a signaling NaN, the invalid exception is raised.
379 *----------------------------------------------------------------------------*/
380
381 static floatx80 propagateFloatx80NaN( floatx80 a, floatx80 b )
382 {
383 flag aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN;
384
385 aIsNaN = floatx80_is_nan( a );
386 aIsSignalingNaN = floatx80_is_signaling_nan( a );
387 bIsNaN = floatx80_is_nan( b );
388 bIsSignalingNaN = floatx80_is_signaling_nan( b );
389 a.low |= LIT64( 0xC000000000000000 );
390 b.low |= LIT64( 0xC000000000000000 );
391 if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_invalid );
392 if ( aIsSignalingNaN ) {
393 if ( bIsSignalingNaN ) goto returnLargerSignificand;
394 return bIsNaN ? b : a;
395 }
396 else if ( aIsNaN ) {
397 if ( bIsSignalingNaN | ! bIsNaN ) return a;
398 returnLargerSignificand:
399 if ( a.low < b.low ) return b;
400 if ( b.low < a.low ) return a;
401 return ( a.high < b.high ) ? a : b;
402 }
403 else {
404 return b;
405 }
406
407 }
408
409 #endif
410
411 #ifdef FLOAT128
412
413 /*----------------------------------------------------------------------------
414 | The pattern for a default generated quadruple-precision NaN. The `high' and
415 | `low' values hold the most- and least-significant bits, respectively.
416 *----------------------------------------------------------------------------*/
417 #define float128_default_nan_high LIT64( 0xFFFF800000000000 )
418 #define float128_default_nan_low LIT64( 0x0000000000000000 )
419
420 /*----------------------------------------------------------------------------
421 | Returns 1 if the quadruple-precision floating-point value `a' is a NaN;
422 | otherwise returns 0.
423 *----------------------------------------------------------------------------*/
424
425 flag float128_is_nan( float128 a )
426 {
427
428 return
429 ( LIT64( 0xFFFE000000000000 ) <= (bits64) ( a.high<<1 ) )
430 && ( a.low || ( a.high & LIT64( 0x0000FFFFFFFFFFFF ) ) );
431
432 }
433
434 /*----------------------------------------------------------------------------
435 | Returns 1 if the quadruple-precision floating-point value `a' is a
436 | signaling NaN; otherwise returns 0.
437 *----------------------------------------------------------------------------*/
438
439 flag float128_is_signaling_nan( float128 a )
440 {
441
442 return
443 ( ( ( a.high>>47 ) & 0xFFFF ) == 0xFFFE )
444 && ( a.low || ( a.high & LIT64( 0x00007FFFFFFFFFFF ) ) );
445
446 }
447
448 /*----------------------------------------------------------------------------
449 | Returns the result of converting the quadruple-precision floating-point NaN
450 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
451 | exception is raised.
452 *----------------------------------------------------------------------------*/
453
454 static commonNaNT float128ToCommonNaN( float128 a )
455 {
456 commonNaNT z;
457
458 if ( float128_is_signaling_nan( a ) ) float_raise( float_flag_invalid );
459 z.sign = a.high>>63;
460 shortShift128Left( a.high, a.low, 16, &z.high, &z.low );
461 return z;
462
463 }
464
465 /*----------------------------------------------------------------------------
466 | Returns the result of converting the canonical NaN `a' to the quadruple-
467 | precision floating-point format.
468 *----------------------------------------------------------------------------*/
469
470 static float128 commonNaNToFloat128( commonNaNT a )
471 {
472 float128 z;
473
474 shift128Right( a.high, a.low, 16, &z.high, &z.low );
475 z.high |= ( ( (bits64) a.sign )<<63 ) | LIT64( 0x7FFF800000000000 );
476 return z;
477
478 }
479
480 /*----------------------------------------------------------------------------
481 | Takes two quadruple-precision floating-point values `a' and `b', one of
482 | which is a NaN, and returns the appropriate NaN result. If either `a' or
483 | `b' is a signaling NaN, the invalid exception is raised.
484 *----------------------------------------------------------------------------*/
485
486 static float128 propagateFloat128NaN( float128 a, float128 b )
487 {
488 flag aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN;
489
490 aIsNaN = float128_is_nan( a );
491 aIsSignalingNaN = float128_is_signaling_nan( a );
492 bIsNaN = float128_is_nan( b );
493 bIsSignalingNaN = float128_is_signaling_nan( b );
494 a.high |= LIT64( 0x0000800000000000 );
495 b.high |= LIT64( 0x0000800000000000 );
496 if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_invalid );
497 if ( aIsSignalingNaN ) {
498 if ( bIsSignalingNaN ) goto returnLargerSignificand;
499 return bIsNaN ? b : a;
500 }
501 else if ( aIsNaN ) {
502 if ( bIsSignalingNaN | ! bIsNaN ) return a;
503 returnLargerSignificand:
504 if ( lt128( a.high<<1, a.low, b.high<<1, b.low ) ) return b;
505 if ( lt128( b.high<<1, b.low, a.high<<1, a.low ) ) return a;
506 return ( a.high < b.high ) ? a : b;
507 }
508 else {
509 return b;
510 }
511
512 }
513
514 #endif
515
516
517 // NB060506: close namespaces
518 }
519 }
520 #endif