|
|
1.1 root 1:
2: /*============================================================================
3:
4: This C source fragment is part of the SoftFloat IEC/IEEE Floating-point
5: Arithmetic Package, Release 2b.
6:
7: Written by John R. Hauser. This work was made possible in part by the
8: International Computer Science Institute, located at Suite 600, 1947 Center
9: Street, Berkeley, California 94704. Funding was partially provided by the
10: National Science Foundation under grant MIP-9311980. The original version
11: of this code was written as part of a project to build a fixed-point vector
12: processor in collaboration with the University of California at Berkeley,
13: overseen by Profs. Nelson Morgan and John Wawrzynek. More information
14: is available through the Web page `http://www.cs.berkeley.edu/~jhauser/
15: arithmetic/SoftFloat.html'.
16:
17: THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort has
18: been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT TIMES
19: RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO PERSONS
20: AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ALL LOSSES,
21: COSTS, OR OTHER PROBLEMS THEY INCUR DUE TO THE SOFTWARE, AND WHO FURTHERMORE
22: EFFECTIVELY INDEMNIFY JOHN HAUSER AND THE INTERNATIONAL COMPUTER SCIENCE
23: INSTITUTE (possibly via similar legal warning) AGAINST ALL LOSSES, COSTS, OR
24: OTHER PROBLEMS INCURRED BY THEIR CUSTOMERS AND CLIENTS DUE TO THE SOFTWARE.
25:
26: Derivative works are acceptable, even for commercial purposes, so long as
27: (1) the source code for the derivative work includes prominent notice that
28: the work is derivative, and (2) the source code includes prominent notice with
29: these four paragraphs for those parts of this code that are retained.
30:
31: =============================================================================*/
32:
33: /*----------------------------------------------------------------------------
34: | Underflow tininess-detection mode, statically initialized to default value.
35: | (The declaration in `softfloat.h' must match the `int8' type here.)
36: *----------------------------------------------------------------------------*/
1.1.1.2 ! root 37: #if 0
1.1 root 38: int8 float_detect_tininess = float_tininess_before_rounding;
1.1.1.2 ! root 39: #endif
1.1 root 40:
41: /*----------------------------------------------------------------------------
42: | Raises the exceptions specified by `flags'. Floating-point traps can be
43: | defined here if desired. It is currently not possible for such a trap to
44: | substitute a result value. If traps are not implemented, this routine
45: | should be simply `float_exception_flags |= flags;'.
46: *----------------------------------------------------------------------------*/
47:
1.1.1.2 ! root 48: void float_raise( int8 flags, float_ctrl* c )
1.1 root 49: {
50:
1.1.1.2 ! root 51: c->float_exception_flags |= flags;
1.1 root 52:
53: }
1.1.1.2 ! root 54: #if 0
1.1 root 55: #ifdef SOFTFLOAT_I860
56: void float_raise2( int8 flags )
57: {
58:
59: float_exception_flags2 |= flags;
60:
61: }
62: #endif
1.1.1.2 ! root 63: #endif
1.1 root 64:
65: /*----------------------------------------------------------------------------
66: | Internal canonical NaN format.
67: *----------------------------------------------------------------------------*/
68: typedef struct {
69: flag sign;
70: bits64 high, low;
71: } commonNaNT;
72:
73: /*----------------------------------------------------------------------------
74: | Returns 1 if the single-precision floating-point value `a' is a NaN;
75: | otherwise returns 0.
76: *----------------------------------------------------------------------------*/
77:
1.1.1.2 ! root 78: static flag float32_is_nan( float32 a )
1.1 root 79: {
80:
81: return ( 0xFF000000 < (bits32) ( a<<1 ) );
82:
83: }
84:
85: /*----------------------------------------------------------------------------
86: | Returns 1 if the single-precision floating-point value `a' is a signaling
87: | NaN; otherwise returns 0.
88: *----------------------------------------------------------------------------*/
89:
90: flag float32_is_signaling_nan( float32 a )
91: {
92:
93: return ( ( ( a>>22 ) & 0x1FF ) == 0x1FE ) && ( a & 0x003FFFFF );
94:
95: }
96:
97: /*----------------------------------------------------------------------------
98: | Returns the result of converting the single-precision floating-point NaN
99: | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
100: | exception is raised.
101: *----------------------------------------------------------------------------*/
102:
1.1.1.2 ! root 103: static commonNaNT float32ToCommonNaN( float32 a, float_ctrl* c )
1.1 root 104: {
105: commonNaNT z;
106:
107: #ifdef SOFTFLOAT_I860
1.1.1.2 ! root 108: if ( float32_is_signaling_nan( a ) ) float_raise( float_flag_signaling, c );
1.1 root 109: #else
110: if ( float32_is_signaling_nan( a ) ) float_raise( float_flag_signaling );
111: #endif
112: z.sign = a>>31;
113: z.low = 0;
114: z.high = ( (bits64) a )<<41;
115: return z;
116:
117: }
118:
119: /*----------------------------------------------------------------------------
120: | Returns the result of converting the canonical NaN `a' to the single-
121: | precision floating-point format.
122: *----------------------------------------------------------------------------*/
123:
124: static float32 commonNaNToFloat32( commonNaNT a )
125: {
126:
127: return ( ( (bits32) a.sign )<<31 ) | 0x7FC00000 | ( a.high>>41 );
128:
129: }
130:
131: /*----------------------------------------------------------------------------
132: | Takes two single-precision floating-point values `a' and `b', one of which
133: | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
134: | signaling NaN, the invalid exception is raised.
135: *----------------------------------------------------------------------------*/
136:
1.1.1.2 ! root 137: static float32 propagateFloat32NaN( float32 a, float32 b, float_ctrl* c )
1.1 root 138: {
139: flag aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN;
140:
141: aIsNaN = float32_is_nan( a );
142: aIsSignalingNaN = float32_is_signaling_nan( a );
143: bIsNaN = float32_is_nan( b );
144: bIsSignalingNaN = float32_is_signaling_nan( b );
145: a |= 0x00400000;
146: b |= 0x00400000;
147: #ifdef SOFTFLOAT_I860
1.1.1.2 ! root 148: if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_signaling, c );
1.1 root 149: #else
150: if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_signaling );
151: #endif
152: if ( aIsNaN ) {
153: return ( aIsSignalingNaN & bIsNaN ) ? b : a;
154: }
155: else {
156: return b;
157: }
158:
159: }
160:
161: /*----------------------------------------------------------------------------
162: | Returns 1 if the double-precision floating-point value `a' is a NaN;
163: | otherwise returns 0.
164: *----------------------------------------------------------------------------*/
165:
1.1.1.2 ! root 166: static flag float64_is_nan( float64 a )
1.1 root 167: {
168:
169: return ( LIT64( 0xFFE0000000000000 ) < (bits64) ( a<<1 ) );
170:
171: }
172:
173: /*----------------------------------------------------------------------------
174: | Returns 1 if the double-precision floating-point value `a' is a signaling
175: | NaN; otherwise returns 0.
176: *----------------------------------------------------------------------------*/
177:
178: flag float64_is_signaling_nan( float64 a )
179: {
180:
181: return
182: ( ( ( a>>51 ) & 0xFFF ) == 0xFFE )
183: && ( a & LIT64( 0x0007FFFFFFFFFFFF ) );
184:
185: }
186:
187: /*----------------------------------------------------------------------------
188: | Returns the result of converting the double-precision floating-point NaN
189: | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
190: | exception is raised.
191: *----------------------------------------------------------------------------*/
192:
1.1.1.2 ! root 193: static commonNaNT float64ToCommonNaN( float64 a, float_ctrl* c )
1.1 root 194: {
195: commonNaNT z;
196:
197: #ifdef SOFTFLOAT_I860
1.1.1.2 ! root 198: if ( float64_is_signaling_nan( a ) ) float_raise( float_flag_signaling, c );
1.1 root 199: #else
200: if ( float64_is_signaling_nan( a ) ) float_raise( float_flag_signaling );
201: #endif
202: z.sign = a>>63;
203: z.low = 0;
204: z.high = a<<12;
205: return z;
206:
207: }
208:
209: /*----------------------------------------------------------------------------
210: | Returns the result of converting the canonical NaN `a' to the double-
211: | precision floating-point format.
212: *----------------------------------------------------------------------------*/
213:
214: static float64 commonNaNToFloat64( commonNaNT a )
215: {
216:
217: return
218: ( ( (bits64) a.sign )<<63 )
219: | LIT64( 0x7FF8000000000000 )
220: | ( a.high>>12 );
221:
222: }
223:
224: /*----------------------------------------------------------------------------
225: | Takes two double-precision floating-point values `a' and `b', one of which
226: | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
227: | signaling NaN, the invalid exception is raised.
228: *----------------------------------------------------------------------------*/
229:
1.1.1.2 ! root 230: static float64 propagateFloat64NaN( float64 a, float64 b, float_ctrl* c )
1.1 root 231: {
232: flag aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN;
233:
234: aIsNaN = float64_is_nan( a );
235: aIsSignalingNaN = float64_is_signaling_nan( a );
236: bIsNaN = float64_is_nan( b );
237: bIsSignalingNaN = float64_is_signaling_nan( b );
238: a |= LIT64( 0x0008000000000000 );
239: b |= LIT64( 0x0008000000000000 );
240: #ifdef SOFTFLOAT_I860
1.1.1.2 ! root 241: if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_signaling, c );
1.1 root 242: #else
243: if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_signaling );
244: #endif
245: if ( aIsNaN ) {
246: return ( aIsSignalingNaN & bIsNaN ) ? b : a;
247: }
248: else {
249: return b;
250: }
251:
252: }
253:
254: #ifdef FLOATX80
255:
256: /*----------------------------------------------------------------------------
257: | Returns 1 if the extended double-precision floating-point value `a' is a
258: | NaN; otherwise returns 0.
259: *----------------------------------------------------------------------------*/
260:
261: flag floatx80_is_nan( floatx80 a )
262: {
263:
264: return ( ( a.high & 0x7FFF ) == 0x7FFF ) && (bits64) ( a.low<<1 );
265:
266: }
267:
268: /*----------------------------------------------------------------------------
269: | Returns 1 if the extended double-precision floating-point value `a' is a
270: | signaling NaN; otherwise returns 0.
271: *----------------------------------------------------------------------------*/
272:
273: flag floatx80_is_signaling_nan( floatx80 a )
274: {
275: bits64 aLow;
276:
277: aLow = a.low & ~ LIT64( 0x4000000000000000 );
278: return
279: ( ( a.high & 0x7FFF ) == 0x7FFF )
280: && (bits64) ( aLow<<1 )
281: && ( a.low == aLow );
282:
283: }
284:
285: #ifdef SOFTFLOAT_68K // 28-12-2016: Added for Previous:
286:
287: /*----------------------------------------------------------------------------
288: | Returns 1 if the extended double-precision floating-point value `a' is
289: | zero; otherwise returns 0.
290: *----------------------------------------------------------------------------*/
291:
292: flag floatx80_is_zero( floatx80 a )
293: {
294:
295: return ( ( a.high & 0x7FFF ) < 0x7FFF ) && ( a.low == 0 );
296:
297: }
298:
299: /*----------------------------------------------------------------------------
300: | Returns 1 if the extended double-precision floating-point value `a' is
301: | infinity; otherwise returns 0.
302: *----------------------------------------------------------------------------*/
303:
304: flag floatx80_is_infinity( floatx80 a )
305: {
306:
307: return ( ( a.high & 0x7FFF ) == 0x7FFF ) && ( (bits64) ( a.low<<1 ) == 0 );
308:
309: }
310:
311: /*----------------------------------------------------------------------------
312: | Returns 1 if the extended double-precision floating-point value `a' is
313: | negative; otherwise returns 0.
314: *----------------------------------------------------------------------------*/
315:
316: flag floatx80_is_negative( floatx80 a )
317: {
318:
319: return ( ( a.high & 0x8000 ) == 0x8000 );
320:
321: }
322:
323: /*----------------------------------------------------------------------------
324: | Returns 1 if the extended double-precision floating-point value `a' is
325: | denormal; otherwise returns 0.
326: *----------------------------------------------------------------------------*/
327:
328: flag floatx80_is_denormal( floatx80 a )
329: {
330:
331: return
332: ( ( a.high & 0x7FFF ) == 0 )
333: && ( (bits64) ( a.low & LIT64( 0x8000000000000000 ) ) == LIT64( 0x0000000000000000 ) )
334: && (bits64) ( a.low<<1 );
335:
336: }
337:
338: /*----------------------------------------------------------------------------
339: | Returns 1 if the extended double-precision floating-point value `a' is
340: | unnormal; otherwise returns 0.
341: *----------------------------------------------------------------------------*/
342:
343: flag floatx80_is_unnormal( floatx80 a )
344: {
345:
346: return
347: ( ( a.high & 0x7FFF ) > 0 )
348: && ( ( a.high & 0x7FFF ) < 0x7FFF)
349: && ( (bits64) ( a.low & LIT64( 0x8000000000000000 ) ) == LIT64( 0x0000000000000000 ) );
350:
351: }
352:
353: /*----------------------------------------------------------------------------
354: | Returns 1 if the extended double-precision floating-point value `a' is
355: | normal; otherwise returns 0.
356: *----------------------------------------------------------------------------*/
357:
358: flag floatx80_is_normal( floatx80 a )
359: {
360:
361: return
362: ( ( a.high & 0x7FFF ) < 0x7FFF )
363: && ( (bits64) ( a.low & LIT64( 0x8000000000000000 ) ) == LIT64( 0x8000000000000000 ) );
364:
365: }
366:
367: #endif // End of addition for Previous
368:
369: /*----------------------------------------------------------------------------
370: | Returns the result of converting the extended double-precision floating-
371: | point NaN `a' to the canonical NaN format. If `a' is a signaling NaN, the
372: | invalid exception is raised.
373: *----------------------------------------------------------------------------*/
374:
1.1.1.2 ! root 375: static commonNaNT floatx80ToCommonNaN( floatx80 a, float_ctrl* c )
1.1 root 376: {
377: commonNaNT z;
378:
1.1.1.2 ! root 379: if ( floatx80_is_signaling_nan( a ) ) float_raise( float_flag_signaling, c );
1.1 root 380: z.sign = a.high>>15;
381: z.low = 0;
382: z.high = a.low<<1;
383: return z;
384:
385: }
386:
387: /*----------------------------------------------------------------------------
388: | Returns the result of converting the canonical NaN `a' to the extended
389: | double-precision floating-point format.
390: *----------------------------------------------------------------------------*/
391:
392: static floatx80 commonNaNToFloatx80( commonNaNT a )
393: {
394: floatx80 z;
395: #ifdef SOFTFLOAT_68K
396: z.low = LIT64( 0x4000000000000000 ) | ( a.high>>1 );
397: #else
398: z.low = LIT64( 0xC000000000000000 ) | ( a.high>>1 );
399: #endif
400: z.high = ( ( (bits16) a.sign )<<15 ) | 0x7FFF;
401: return z;
402:
403: }
404:
405: /*----------------------------------------------------------------------------
406: | Takes two extended double-precision floating-point values `a' and `b', one
407: | of which is a NaN, and returns the appropriate NaN result. If either `a' or
408: | `b' is a signaling NaN, the invalid exception is raised.
409: *----------------------------------------------------------------------------*/
410:
1.1.1.2 ! root 411: floatx80 propagateFloatx80NaN( floatx80 a, floatx80 b, float_ctrl* c )
1.1 root 412: {
1.1.1.2 ! root 413: flag aIsNaN, aIsSignalingNaN, bIsSignalingNaN;
! 414: #ifndef SOFTFLOAT_68K
! 415: flag bIsNaN;
! 416:
! 417: bIsNaN = floatx80_is_nan( b );
! 418: #endif
1.1 root 419: aIsNaN = floatx80_is_nan( a );
420: aIsSignalingNaN = floatx80_is_signaling_nan( a );
421: bIsSignalingNaN = floatx80_is_signaling_nan( b );
422: #ifdef SOFTFLOAT_68K
423: a.low |= LIT64( 0x4000000000000000 );
424: b.low |= LIT64( 0x4000000000000000 );
1.1.1.2 ! root 425: if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_signaling, c );
1.1 root 426: return aIsNaN ? a : b;
427: #else
428: a.low |= LIT64( 0xC000000000000000 );
429: b.low |= LIT64( 0xC000000000000000 );
1.1.1.2 ! root 430: if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_signaling, c );
1.1 root 431: if ( aIsNaN ) {
432: return ( aIsSignalingNaN & bIsNaN ) ? b : a;
433: }
434: else {
435: return b;
436: }
437: #endif
438:
439: }
440:
441: #ifdef SOFTFLOAT_68K
442: /*----------------------------------------------------------------------------
443: | Takes extended double-precision floating-point NaN `a' and returns the
444: | appropriate NaN result. If `a' is a signaling NaN, the invalid exception
445: | is raised.
446: *----------------------------------------------------------------------------*/
447:
1.1.1.2 ! root 448: floatx80 propagateFloatx80NaNOneArg( floatx80 a, float_ctrl* c )
1.1 root 449: {
450: if ( floatx80_is_signaling_nan( a ) )
1.1.1.2 ! root 451: float_raise( float_flag_signaling, c );
1.1 root 452:
453: a.low |= LIT64( 0x4000000000000000 );
454:
455: return a;
456: }
457: #endif
458:
459: #define EXP_BIAS 0x3FFF
460:
461: /*----------------------------------------------------------------------------
462: | Returns the fraction bits of the extended double-precision floating-point
463: | value `a'.
464: *----------------------------------------------------------------------------*/
465:
466: bits64 extractFloatx80Frac( floatx80 a )
467: {
468:
469: return a.low;
470:
471: }
472:
473: /*----------------------------------------------------------------------------
474: | Returns the exponent bits of the extended double-precision floating-point
475: | value `a'.
476: *----------------------------------------------------------------------------*/
477:
478: int32 extractFloatx80Exp( floatx80 a )
479: {
480:
481: return a.high & 0x7FFF;
482:
483: }
484:
485: /*----------------------------------------------------------------------------
486: | Returns the sign bit of the extended double-precision floating-point value
487: | `a'.
488: *----------------------------------------------------------------------------*/
489:
490: flag extractFloatx80Sign( floatx80 a )
491: {
492:
493: return a.high>>15;
494:
495: }
496:
497: #endif
498:
499: #ifdef FLOAT128
500:
501: /*----------------------------------------------------------------------------
502: | Returns 1 if the quadruple-precision floating-point value `a' is a NaN;
503: | otherwise returns 0.
504: *----------------------------------------------------------------------------*/
505:
1.1.1.2 ! root 506: static flag float128_is_nan( float128 a )
1.1 root 507: {
508:
509: return
510: ( LIT64( 0xFFFE000000000000 ) <= (bits64) ( a.high<<1 ) )
511: && ( a.low || ( a.high & LIT64( 0x0000FFFFFFFFFFFF ) ) );
512:
513: }
514:
515: /*----------------------------------------------------------------------------
516: | Returns 1 if the quadruple-precision floating-point value `a' is a
517: | signaling NaN; otherwise returns 0.
518: *----------------------------------------------------------------------------*/
519:
520: flag float128_is_signaling_nan( float128 a )
521: {
522:
523: return
524: ( ( ( a.high>>47 ) & 0xFFFF ) == 0xFFFE )
525: && ( a.low || ( a.high & LIT64( 0x00007FFFFFFFFFFF ) ) );
526:
527: }
528:
529: /*----------------------------------------------------------------------------
530: | Returns the result of converting the quadruple-precision floating-point NaN
531: | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
532: | exception is raised.
533: *----------------------------------------------------------------------------*/
534:
1.1.1.2 ! root 535: static commonNaNT float128ToCommonNaN( float128 a, float_ctrl* c )
1.1 root 536: {
537: commonNaNT z;
538:
1.1.1.2 ! root 539: if ( float128_is_signaling_nan( a ) ) float_raise( float_flag_signaling, c );
1.1 root 540: z.sign = a.high>>63;
541: shortShift128Left( a.high, a.low, 16, &z.high, &z.low );
542: return z;
543:
544: }
545:
546: /*----------------------------------------------------------------------------
547: | Returns the result of converting the canonical NaN `a' to the quadruple-
548: | precision floating-point format.
549: *----------------------------------------------------------------------------*/
550:
551: static float128 commonNaNToFloat128( commonNaNT a )
552: {
553: float128 z;
554:
555: shift128Right( a.high, a.low, 16, &z.high, &z.low );
556: z.high |= ( ( (bits64) a.sign )<<63 ) | LIT64( 0x7FFF800000000000 );
557: return z;
558:
559: }
560:
561: /*----------------------------------------------------------------------------
562: | Takes two quadruple-precision floating-point values `a' and `b', one of
563: | which is a NaN, and returns the appropriate NaN result. If either `a' or
564: | `b' is a signaling NaN, the invalid exception is raised.
565: *----------------------------------------------------------------------------*/
566:
1.1.1.2 ! root 567: static float128 propagateFloat128NaN( float128 a, float128 b, float_ctrl* c )
1.1 root 568: {
569: flag aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN;
570:
571: aIsNaN = float128_is_nan( a );
572: aIsSignalingNaN = float128_is_signaling_nan( a );
573: bIsNaN = float128_is_nan( b );
574: bIsSignalingNaN = float128_is_signaling_nan( b );
575: a.high |= LIT64( 0x0000800000000000 );
576: b.high |= LIT64( 0x0000800000000000 );
1.1.1.2 ! root 577: if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_signaling, c );
1.1 root 578: if ( aIsNaN ) {
579: return ( aIsSignalingNaN & bIsNaN ) ? b : a;
580: }
581: else {
582: return b;
583: }
584:
585: }
586:
587: #endif
588:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.