3 ===============================================================================
\r
5 This C source fragment is part of the SoftFloat IEC/IEEE Floating-point
\r
6 Arithmetic Package, Release 2a.
\r
8 Written by John R. Hauser. This work was made possible in part by the
\r
9 International Computer Science Institute, located at Suite 600, 1947 Center
\r
10 Street, Berkeley, California 94704. Funding was partially provided by the
\r
11 National Science Foundation under grant MIP-9311980. The original version
\r
12 of this code was written as part of a project to build a fixed-point vector
\r
13 processor in collaboration with the University of California at Berkeley,
\r
14 overseen by Profs. Nelson Morgan and John Wawrzynek. More information
\r
15 is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
\r
16 arithmetic/SoftFloat.html'.
\r
18 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
\r
19 has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
\r
20 TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
\r
21 PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
\r
22 AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
\r
24 Derivative works are acceptable, even for commercial purposes, so long as
\r
25 (1) they include prominent notice that the work is derivative, and (2) they
\r
26 include prominent notice akin to these four paragraphs for those parts of
\r
27 this code that are retained.
\r
29 ===============================================================================
\r
33 -------------------------------------------------------------------------------
\r
34 Underflow tininess-detection mode, statically initialized to default value.
\r
35 (The declaration in `softfloat.h' must match the `int8' type here.)
\r
36 -------------------------------------------------------------------------------
\r
38 int8 float_detect_tininess = float_tininess_after_rounding;
\r
41 -------------------------------------------------------------------------------
\r
42 Raises the exceptions specified by `flags'. Floating-point traps can be
\r
43 defined here if desired. It is currently not possible for such a trap to
\r
44 substitute a result value. If traps are not implemented, this routine
\r
45 should be simply `float_exception_flags |= flags;'.
\r
46 -------------------------------------------------------------------------------
\r
48 void float_raise( int8 flags )
\r
51 float_exception_flags |= flags;
\r
56 -------------------------------------------------------------------------------
\r
57 Internal canonical NaN format.
\r
58 -------------------------------------------------------------------------------
\r
66 -------------------------------------------------------------------------------
\r
67 The pattern for a default generated single-precision NaN.
\r
68 -------------------------------------------------------------------------------
\r
70 #define float32_default_nan 0xFFFFFFFF
\r
73 -------------------------------------------------------------------------------
\r
74 Returns 1 if the single-precision floating-point value `a' is a NaN;
\r
75 otherwise returns 0.
\r
76 -------------------------------------------------------------------------------
\r
78 flag float32_is_nan( float32 a )
\r
81 return ( 0xFF000000 < (bits32) ( a<<1 ) );
\r
86 -------------------------------------------------------------------------------
\r
87 Returns 1 if the single-precision floating-point value `a' is a signaling
\r
88 NaN; otherwise returns 0.
\r
89 -------------------------------------------------------------------------------
\r
91 flag float32_is_signaling_nan( float32 a )
\r
94 return ( ( ( a>>22 ) & 0x1FF ) == 0x1FE ) && ( a & 0x003FFFFF );
\r
99 -------------------------------------------------------------------------------
\r
100 Returns the result of converting the single-precision floating-point NaN
\r
101 `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
\r
102 exception is raised.
\r
103 -------------------------------------------------------------------------------
\r
105 static commonNaNT float32ToCommonNaN( float32 a )
\r
109 if ( float32_is_signaling_nan( a ) ) float_raise( float_flag_invalid );
\r
112 z.high = ( (bits64) a )<<41;
\r
118 -------------------------------------------------------------------------------
\r
119 Returns the result of converting the canonical NaN `a' to the single-
\r
120 precision floating-point format.
\r
121 -------------------------------------------------------------------------------
\r
123 static float32 commonNaNToFloat32( commonNaNT a )
\r
126 return ( ( (bits32) a.sign )<<31 ) | 0x7FC00000 | ( a.high>>41 );
\r
131 -------------------------------------------------------------------------------
\r
132 Takes two single-precision floating-point values `a' and `b', one of which
\r
133 is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
\r
134 signaling NaN, the invalid exception is raised.
\r
135 -------------------------------------------------------------------------------
\r
137 static float32 propagateFloat32NaN( float32 a, float32 b )
\r
139 flag aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN;
\r
141 aIsNaN = float32_is_nan( a );
\r
142 aIsSignalingNaN = float32_is_signaling_nan( a );
\r
143 bIsNaN = float32_is_nan( b );
\r
144 bIsSignalingNaN = float32_is_signaling_nan( b );
\r
147 if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_invalid );
\r
149 return ( aIsSignalingNaN & bIsNaN ) ? b : a;
\r
158 -------------------------------------------------------------------------------
\r
159 The pattern for a default generated double-precision NaN.
\r
160 -------------------------------------------------------------------------------
\r
162 #define float64_default_nan LIT64( 0xFFFFFFFFFFFFFFFF )
\r
165 -------------------------------------------------------------------------------
\r
166 Returns 1 if the double-precision floating-point value `a' is a NaN;
\r
167 otherwise returns 0.
\r
168 -------------------------------------------------------------------------------
\r
170 flag float64_is_nan( float64 a )
\r
173 return ( LIT64( 0xFFE0000000000000 ) < (bits64) ( a<<1 ) );
\r
178 -------------------------------------------------------------------------------
\r
179 Returns 1 if the double-precision floating-point value `a' is a signaling
\r
180 NaN; otherwise returns 0.
\r
181 -------------------------------------------------------------------------------
\r
183 flag float64_is_signaling_nan( float64 a )
\r
187 ( ( ( a>>51 ) & 0xFFF ) == 0xFFE )
\r
188 && ( a & LIT64( 0x0007FFFFFFFFFFFF ) );
\r
193 -------------------------------------------------------------------------------
\r
194 Returns the result of converting the double-precision floating-point NaN
\r
195 `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
\r
196 exception is raised.
\r
197 -------------------------------------------------------------------------------
\r
199 static commonNaNT float64ToCommonNaN( float64 a )
\r
203 if ( float64_is_signaling_nan( a ) ) float_raise( float_flag_invalid );
\r
212 -------------------------------------------------------------------------------
\r
213 Returns the result of converting the canonical NaN `a' to the double-
\r
214 precision floating-point format.
\r
215 -------------------------------------------------------------------------------
\r
217 static float64 commonNaNToFloat64( commonNaNT a )
\r
221 ( ( (bits64) a.sign )<<63 )
\r
222 | LIT64( 0x7FF8000000000000 )
\r
228 -------------------------------------------------------------------------------
\r
229 Takes two double-precision floating-point values `a' and `b', one of which
\r
230 is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
\r
231 signaling NaN, the invalid exception is raised.
\r
232 -------------------------------------------------------------------------------
\r
234 static float64 propagateFloat64NaN( float64 a, float64 b )
\r
236 flag aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN;
\r
238 aIsNaN = float64_is_nan( a );
\r
239 aIsSignalingNaN = float64_is_signaling_nan( a );
\r
240 bIsNaN = float64_is_nan( b );
\r
241 bIsSignalingNaN = float64_is_signaling_nan( b );
\r
242 a |= LIT64( 0x0008000000000000 );
\r
243 b |= LIT64( 0x0008000000000000 );
\r
244 if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_invalid );
\r
246 return ( aIsSignalingNaN & bIsNaN ) ? b : a;
\r
257 -------------------------------------------------------------------------------
\r
258 The pattern for a default generated extended double-precision NaN. The
\r
259 `high' and `low' values hold the most- and least-significant bits,
\r
261 -------------------------------------------------------------------------------
\r
263 #define floatx80_default_nan_high 0xFFFF
\r
264 #define floatx80_default_nan_low LIT64( 0xFFFFFFFFFFFFFFFF )
\r
267 -------------------------------------------------------------------------------
\r
268 Returns 1 if the extended double-precision floating-point value `a' is a
\r
269 NaN; otherwise returns 0.
\r
270 -------------------------------------------------------------------------------
\r
272 flag floatx80_is_nan( floatx80 a )
\r
275 return ( ( a.high & 0x7FFF ) == 0x7FFF ) && (bits64) ( a.low<<1 );
\r
280 -------------------------------------------------------------------------------
\r
281 Returns 1 if the extended double-precision floating-point value `a' is a
\r
282 signaling NaN; otherwise returns 0.
\r
283 -------------------------------------------------------------------------------
\r
285 flag floatx80_is_signaling_nan( floatx80 a )
\r
289 aLow = a.low & ~ LIT64( 0x4000000000000000 );
\r
291 ( ( a.high & 0x7FFF ) == 0x7FFF )
\r
292 && (bits64) ( aLow<<1 )
\r
293 && ( a.low == aLow );
\r
298 -------------------------------------------------------------------------------
\r
299 Returns the result of converting the extended double-precision floating-
\r
300 point NaN `a' to the canonical NaN format. If `a' is a signaling NaN, the
\r
301 invalid exception is raised.
\r
302 -------------------------------------------------------------------------------
\r
304 static commonNaNT floatx80ToCommonNaN( floatx80 a )
\r
308 if ( floatx80_is_signaling_nan( a ) ) float_raise( float_flag_invalid );
\r
309 z.sign = a.high>>15;
\r
317 -------------------------------------------------------------------------------
\r
318 Returns the result of converting the canonical NaN `a' to the extended
\r
319 double-precision floating-point format.
\r
320 -------------------------------------------------------------------------------
\r
322 static floatx80 commonNaNToFloatx80( commonNaNT a )
\r
326 z.low = LIT64( 0xC000000000000000 ) | ( a.high>>1 );
\r
327 z.high = ( ( (bits16) a.sign )<<15 ) | 0x7FFF;
\r
333 -------------------------------------------------------------------------------
\r
334 Takes two extended double-precision floating-point values `a' and `b', one
\r
335 of which is a NaN, and returns the appropriate NaN result. If either `a' or
\r
336 `b' is a signaling NaN, the invalid exception is raised.
\r
337 -------------------------------------------------------------------------------
\r
339 static floatx80 propagateFloatx80NaN( floatx80 a, floatx80 b )
\r
341 flag aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN;
\r
343 aIsNaN = floatx80_is_nan( a );
\r
344 aIsSignalingNaN = floatx80_is_signaling_nan( a );
\r
345 bIsNaN = floatx80_is_nan( b );
\r
346 bIsSignalingNaN = floatx80_is_signaling_nan( b );
\r
347 a.low |= LIT64( 0xC000000000000000 );
\r
348 b.low |= LIT64( 0xC000000000000000 );
\r
349 if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_invalid );
\r
351 return ( aIsSignalingNaN & bIsNaN ) ? b : a;
\r
364 -------------------------------------------------------------------------------
\r
365 The pattern for a default generated quadruple-precision NaN. The `high' and
\r
366 `low' values hold the most- and least-significant bits, respectively.
\r
367 -------------------------------------------------------------------------------
\r
369 #define float128_default_nan_high LIT64( 0xFFFFFFFFFFFFFFFF )
\r
370 #define float128_default_nan_low LIT64( 0xFFFFFFFFFFFFFFFF )
\r
373 -------------------------------------------------------------------------------
\r
374 Returns 1 if the quadruple-precision floating-point value `a' is a NaN;
\r
375 otherwise returns 0.
\r
376 -------------------------------------------------------------------------------
\r
378 flag float128_is_nan( float128 a )
\r
382 ( LIT64( 0xFFFE000000000000 ) <= (bits64) ( a.high<<1 ) )
\r
383 && ( a.low || ( a.high & LIT64( 0x0000FFFFFFFFFFFF ) ) );
\r
388 -------------------------------------------------------------------------------
\r
389 Returns 1 if the quadruple-precision floating-point value `a' is a
\r
390 signaling NaN; otherwise returns 0.
\r
391 -------------------------------------------------------------------------------
\r
393 flag float128_is_signaling_nan( float128 a )
\r
397 ( ( ( a.high>>47 ) & 0xFFFF ) == 0xFFFE )
\r
398 && ( a.low || ( a.high & LIT64( 0x00007FFFFFFFFFFF ) ) );
\r
403 -------------------------------------------------------------------------------
\r
404 Returns the result of converting the quadruple-precision floating-point NaN
\r
405 `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
\r
406 exception is raised.
\r
407 -------------------------------------------------------------------------------
\r
409 static commonNaNT float128ToCommonNaN( float128 a )
\r
413 if ( float128_is_signaling_nan( a ) ) float_raise( float_flag_invalid );
\r
414 z.sign = a.high>>63;
\r
415 shortShift128Left( a.high, a.low, 16, &z.high, &z.low );
\r
421 -------------------------------------------------------------------------------
\r
422 Returns the result of converting the canonical NaN `a' to the quadruple-
\r
423 precision floating-point format.
\r
424 -------------------------------------------------------------------------------
\r
426 static float128 commonNaNToFloat128( commonNaNT a )
\r
430 shift128Right( a.high, a.low, 16, &z.high, &z.low );
\r
431 z.high |= ( ( (bits64) a.sign )<<63 ) | LIT64( 0x7FFF800000000000 );
\r
437 -------------------------------------------------------------------------------
\r
438 Takes two quadruple-precision floating-point values `a' and `b', one of
\r
439 which is a NaN, and returns the appropriate NaN result. If either `a' or
\r
440 `b' is a signaling NaN, the invalid exception is raised.
\r
441 -------------------------------------------------------------------------------
\r
443 static float128 propagateFloat128NaN( float128 a, float128 b )
\r
445 flag aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN;
\r
447 aIsNaN = float128_is_nan( a );
\r
448 aIsSignalingNaN = float128_is_signaling_nan( a );
\r
449 bIsNaN = float128_is_nan( b );
\r
450 bIsSignalingNaN = float128_is_signaling_nan( b );
\r
451 a.high |= LIT64( 0x0000800000000000 );
\r
452 b.high |= LIT64( 0x0000800000000000 );
\r
453 if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_invalid );
\r
455 return ( aIsSignalingNaN & bIsNaN ) ? b : a;
\r