00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include "tconfig.h"
00047 #include "fp-bit.h"
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 #ifdef DECLARE_LIBRARY_RENAMES
00078 DECLARE_LIBRARY_RENAMES
00079 #endif
00080
00081 #ifdef EXTENDED_FLOAT_STUBS
00082 extern void abort (void);
00083 void __extendsfxf2 (void) { abort(); }
00084 void __extenddfxf2 (void) { abort(); }
00085 void __truncxfdf2 (void) { abort(); }
00086 void __truncxfsf2 (void) { abort(); }
00087 void __fixxfsi (void) { abort(); }
00088 void __floatsixf (void) { abort(); }
00089 void __addxf3 (void) { abort(); }
00090 void __subxf3 (void) { abort(); }
00091 void __mulxf3 (void) { abort(); }
00092 void __divxf3 (void) { abort(); }
00093 void __negxf2 (void) { abort(); }
00094 void __eqxf2 (void) { abort(); }
00095 void __nexf2 (void) { abort(); }
00096 void __gtxf2 (void) { abort(); }
00097 void __gexf2 (void) { abort(); }
00098 void __lexf2 (void) { abort(); }
00099 void __ltxf2 (void) { abort(); }
00100
00101 void __extendsftf2 (void) { abort(); }
00102 void __extenddftf2 (void) { abort(); }
00103 void __trunctfdf2 (void) { abort(); }
00104 void __trunctfsf2 (void) { abort(); }
00105 void __fixtfsi (void) { abort(); }
00106 void __floatsitf (void) { abort(); }
00107 void __addtf3 (void) { abort(); }
00108 void __subtf3 (void) { abort(); }
00109 void __multf3 (void) { abort(); }
00110 void __divtf3 (void) { abort(); }
00111 void __negtf2 (void) { abort(); }
00112 void __eqtf2 (void) { abort(); }
00113 void __netf2 (void) { abort(); }
00114 void __gttf2 (void) { abort(); }
00115 void __getf2 (void) { abort(); }
00116 void __letf2 (void) { abort(); }
00117 void __lttf2 (void) { abort(); }
00118 #else
00119
00120
00121
00122 #ifdef NO_NANS
00123
00124 #define nan() 0
00125 #define isnan(x) 0
00126 #define isinf(x) 0
00127 #else
00128
00129 #if defined L_thenan_sf
00130 const fp_number_type __thenan_sf = { CLASS_SNAN, 0, 0, {(fractype) 0} };
00131 #elif defined L_thenan_df
00132 const fp_number_type __thenan_df = { CLASS_SNAN, 0, 0, {(fractype) 0} };
00133 #elif defined L_thenan_tf
00134 const fp_number_type __thenan_tf = { CLASS_SNAN, 0, 0, {(fractype) 0} };
00135 #elif defined TFLOAT
00136 extern const fp_number_type __thenan_tf;
00137 #elif defined FLOAT
00138 extern const fp_number_type __thenan_sf;
00139 #else
00140 extern const fp_number_type __thenan_df;
00141 #endif
00142
00143 INLINE
00144 static fp_number_type *
00145 nan (void)
00146 {
00147
00148 #ifdef TFLOAT
00149 return (fp_number_type *) (& __thenan_tf);
00150 #elif defined FLOAT
00151 return (fp_number_type *) (& __thenan_sf);
00152 #else
00153 return (fp_number_type *) (& __thenan_df);
00154 #endif
00155 }
00156
00157 INLINE
00158 static int
00159 isnan ( fp_number_type * x)
00160 {
00161 return x->class == CLASS_SNAN || x->class == CLASS_QNAN;
00162 }
00163
00164 INLINE
00165 static int
00166 isinf ( fp_number_type * x)
00167 {
00168 return x->class == CLASS_INFINITY;
00169 }
00170
00171 #endif
00172
00173 INLINE
00174 static int
00175 iszero ( fp_number_type * x)
00176 {
00177 return x->class == CLASS_ZERO;
00178 }
00179
00180 INLINE
00181 static void
00182 flip_sign ( fp_number_type * x)
00183 {
00184 x->sign = !x->sign;
00185 }
00186
00187 extern FLO_type pack_d ( fp_number_type * );
00188
00189 #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
00190 FLO_type
00191 pack_d ( fp_number_type * src)
00192 {
00193 FLO_union_type dst;
00194 fractype fraction = src->fraction.ll;
00195 int sign = src->sign;
00196 int exp = 0;
00197
00198 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && (isnan (src) || isinf (src)))
00199 {
00200
00201
00202
00203 exp = EXPMAX;
00204 fraction = ((fractype) 1 << FRACBITS) - 1;
00205 }
00206 else if (isnan (src))
00207 {
00208 exp = EXPMAX;
00209 if (src->class == CLASS_QNAN || 1)
00210 {
00211 fraction |= QUIET_NAN;
00212 }
00213 }
00214 else if (isinf (src))
00215 {
00216 exp = EXPMAX;
00217 fraction = 0;
00218 }
00219 else if (iszero (src))
00220 {
00221 exp = 0;
00222 fraction = 0;
00223 }
00224 else if (fraction == 0)
00225 {
00226 exp = 0;
00227 }
00228 else
00229 {
00230 if (src->normal_exp < NORMAL_EXPMIN)
00231 {
00232 #ifdef NO_DENORMALS
00233
00234
00235
00236 exp = 0;
00237 fraction = 0;
00238 #else
00239
00240
00241
00242
00243 int shift = NORMAL_EXPMIN - src->normal_exp;
00244
00245 exp = 0;
00246
00247 if (shift > FRAC_NBITS - NGARDS)
00248 {
00249
00250 fraction = 0;
00251 }
00252 else
00253 {
00254 int lowbit = (fraction & (((fractype)1 << shift) - 1)) ? 1 : 0;
00255 fraction = (fraction >> shift) | lowbit;
00256 }
00257 if ((fraction & GARDMASK) == GARDMSB)
00258 {
00259 if ((fraction & (1 << NGARDS)))
00260 fraction += GARDROUND + 1;
00261 }
00262 else
00263 {
00264
00265 fraction += GARDROUND;
00266 }
00267
00268
00269 if (fraction >= IMPLICIT_1)
00270 {
00271 exp += 1;
00272 }
00273 fraction >>= NGARDS;
00274 #endif
00275 }
00276 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS)
00277 && src->normal_exp > EXPBIAS)
00278 {
00279 exp = EXPMAX;
00280 fraction = 0;
00281 }
00282 else
00283 {
00284 exp = src->normal_exp + EXPBIAS;
00285 if (!ROUND_TOWARDS_ZERO)
00286 {
00287
00288
00289
00290 if ((fraction & GARDMASK) == GARDMSB)
00291 {
00292 if (fraction & (1 << NGARDS))
00293 fraction += GARDROUND + 1;
00294 }
00295 else
00296 {
00297
00298 fraction += GARDROUND;
00299 }
00300 if (fraction >= IMPLICIT_2)
00301 {
00302 fraction >>= 1;
00303 exp += 1;
00304 }
00305 }
00306 fraction >>= NGARDS;
00307
00308 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp > EXPMAX)
00309 {
00310
00311 exp = EXPMAX;
00312 fraction = ((fractype) 1 << FRACBITS) - 1;
00313 }
00314 }
00315 }
00316
00317
00318
00319
00320 #ifdef FLOAT_BIT_ORDER_MISMATCH
00321 dst.bits.fraction = fraction;
00322 dst.bits.exp = exp;
00323 dst.bits.sign = sign;
00324 #else
00325 # if defined TFLOAT && defined HALFFRACBITS
00326 {
00327 halffractype high, low;
00328
00329 high = (fraction >> (FRACBITS - HALFFRACBITS));
00330 high &= (((fractype)1) << HALFFRACBITS) - 1;
00331 high |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << HALFFRACBITS;
00332 high |= ((fractype) (sign & 1)) << (HALFFRACBITS | EXPBITS);
00333
00334 low = (halffractype)fraction &
00335 ((((halffractype)1) << (FRACBITS - HALFFRACBITS)) - 1);
00336
00337 if (exp == EXPMAX || exp == 0 || low == 0)
00338 low = 0;
00339 else
00340 {
00341 exp -= HALFFRACBITS + 1;
00342
00343 while (exp > 0
00344 && low < ((halffractype)1 << HALFFRACBITS))
00345 {
00346 low <<= 1;
00347 exp--;
00348 }
00349
00350 if (exp <= 0)
00351 {
00352 halffractype roundmsb, round;
00353
00354 exp = -exp + 1;
00355
00356 roundmsb = (1 << (exp - 1));
00357 round = low & ((roundmsb << 1) - 1);
00358
00359 low >>= exp;
00360 exp = 0;
00361
00362 if (round > roundmsb || (round == roundmsb && (low & 1)))
00363 {
00364 low++;
00365 if (low >= ((halffractype)1 << HALFFRACBITS))
00366
00367
00368
00369 exp++;
00370 }
00371 }
00372
00373 low &= ((halffractype)1 << HALFFRACBITS) - 1;
00374 low |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << HALFFRACBITS;
00375 low |= ((fractype) (sign & 1)) << (HALFFRACBITS | EXPBITS);
00376 }
00377
00378 dst.value_raw = (((fractype) high) << HALFSHIFT) | low;
00379 }
00380 # else
00381 dst.value_raw = fraction & ((((fractype)1) << FRACBITS) - (fractype)1);
00382 dst.value_raw |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << FRACBITS;
00383 dst.value_raw |= ((fractype) (sign & 1)) << (FRACBITS | EXPBITS);
00384 # endif
00385 #endif
00386
00387 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
00388 #ifdef TFLOAT
00389 {
00390 qrtrfractype tmp1 = dst.words[0];
00391 qrtrfractype tmp2 = dst.words[1];
00392 dst.words[0] = dst.words[3];
00393 dst.words[1] = dst.words[2];
00394 dst.words[2] = tmp2;
00395 dst.words[3] = tmp1;
00396 }
00397 #else
00398 {
00399 halffractype tmp = dst.words[0];
00400 dst.words[0] = dst.words[1];
00401 dst.words[1] = tmp;
00402 }
00403 #endif
00404 #endif
00405
00406 return dst.value;
00407 }
00408 #endif
00409
00410 #if defined(L_unpack_df) || defined(L_unpack_sf) || defined(L_unpack_tf)
00411 void
00412 unpack_d (FLO_union_type * src, fp_number_type * dst)
00413 {
00414
00415
00416
00417 fractype fraction;
00418 int exp;
00419 int sign;
00420
00421 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
00422 FLO_union_type swapped;
00423
00424 #ifdef TFLOAT
00425 swapped.words[0] = src->words[3];
00426 swapped.words[1] = src->words[2];
00427 swapped.words[2] = src->words[1];
00428 swapped.words[3] = src->words[0];
00429 #else
00430 swapped.words[0] = src->words[1];
00431 swapped.words[1] = src->words[0];
00432 #endif
00433 src = &swapped;
00434 #endif
00435
00436 #ifdef FLOAT_BIT_ORDER_MISMATCH
00437 fraction = src->bits.fraction;
00438 exp = src->bits.exp;
00439 sign = src->bits.sign;
00440 #else
00441 # if defined TFLOAT && defined HALFFRACBITS
00442 {
00443 halffractype high, low;
00444
00445 high = src->value_raw >> HALFSHIFT;
00446 low = src->value_raw & (((fractype)1 << HALFSHIFT) - 1);
00447
00448 fraction = high & ((((fractype)1) << HALFFRACBITS) - 1);
00449 fraction <<= FRACBITS - HALFFRACBITS;
00450 exp = ((int)(high >> HALFFRACBITS)) & ((1 << EXPBITS) - 1);
00451 sign = ((int)(high >> (((HALFFRACBITS + EXPBITS))))) & 1;
00452
00453 if (exp != EXPMAX && exp != 0 && low != 0)
00454 {
00455 int lowexp = ((int)(low >> HALFFRACBITS)) & ((1 << EXPBITS) - 1);
00456 int lowsign = ((int)(low >> (((HALFFRACBITS + EXPBITS))))) & 1;
00457 int shift;
00458 fractype xlow;
00459
00460 xlow = low & ((((fractype)1) << HALFFRACBITS) - 1);
00461 if (lowexp)
00462 xlow |= (((halffractype)1) << HALFFRACBITS);
00463 else
00464 lowexp = 1;
00465 shift = (FRACBITS - HALFFRACBITS) - (exp - lowexp);
00466 if (shift > 0)
00467 xlow <<= shift;
00468 else if (shift < 0)
00469 xlow >>= -shift;
00470 if (sign == lowsign)
00471 fraction += xlow;
00472 else
00473 fraction -= xlow;
00474 }
00475 }
00476 # else
00477 fraction = src->value_raw & ((((fractype)1) << FRACBITS) - 1);
00478 exp = ((int)(src->value_raw >> FRACBITS)) & ((1 << EXPBITS) - 1);
00479 sign = ((int)(src->value_raw >> (FRACBITS + EXPBITS))) & 1;
00480 # endif
00481 #endif
00482
00483 dst->sign = sign;
00484 if (exp == 0)
00485 {
00486
00487 if (fraction == 0
00488 #ifdef NO_DENORMALS
00489 || 1
00490 #endif
00491 )
00492 {
00493
00494 dst->class = CLASS_ZERO;
00495 }
00496 else
00497 {
00498
00499
00500
00501 dst->normal_exp = exp - EXPBIAS + 1;
00502 fraction <<= NGARDS;
00503
00504 dst->class = CLASS_NUMBER;
00505 #if 1
00506 while (fraction < IMPLICIT_1)
00507 {
00508 fraction <<= 1;
00509 dst->normal_exp--;
00510 }
00511 #endif
00512 dst->fraction.ll = fraction;
00513 }
00514 }
00515 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp == EXPMAX)
00516 {
00517
00518 if (fraction == 0)
00519 {
00520
00521 dst->class = CLASS_INFINITY;
00522 }
00523 else
00524 {
00525
00526 if (fraction & QUIET_NAN)
00527 {
00528 dst->class = CLASS_QNAN;
00529 }
00530 else
00531 {
00532 dst->class = CLASS_SNAN;
00533 }
00534
00535 dst->fraction.ll = fraction;
00536 }
00537 }
00538 else
00539 {
00540
00541 dst->normal_exp = exp - EXPBIAS;
00542 dst->class = CLASS_NUMBER;
00543 dst->fraction.ll = (fraction << NGARDS) | IMPLICIT_1;
00544 }
00545 }
00546 #endif
00547
00548 #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
00549 static fp_number_type *
00550 _fpadd_parts (fp_number_type * a,
00551 fp_number_type * b,
00552 fp_number_type * tmp)
00553 {
00554 intfrac tfraction;
00555
00556
00557 int a_normal_exp;
00558 int b_normal_exp;
00559 fractype a_fraction;
00560 fractype b_fraction;
00561
00562 if (isnan (a))
00563 {
00564 return a;
00565 }
00566 if (isnan (b))
00567 {
00568 return b;
00569 }
00570 if (isinf (a))
00571 {
00572
00573 if (isinf (b) && a->sign != b->sign)
00574 return nan ();
00575 return a;
00576 }
00577 if (isinf (b))
00578 {
00579 return b;
00580 }
00581 if (iszero (b))
00582 {
00583 if (iszero (a))
00584 {
00585 *tmp = *a;
00586 tmp->sign = a->sign & b->sign;
00587 return tmp;
00588 }
00589 return a;
00590 }
00591 if (iszero (a))
00592 {
00593 return b;
00594 }
00595
00596
00597
00598 {
00599 int diff;
00600
00601 a_normal_exp = a->normal_exp;
00602 b_normal_exp = b->normal_exp;
00603 a_fraction = a->fraction.ll;
00604 b_fraction = b->fraction.ll;
00605
00606 diff = a_normal_exp - b_normal_exp;
00607
00608 if (diff < 0)
00609 diff = -diff;
00610 if (diff < FRAC_NBITS)
00611 {
00612
00613 while (a_normal_exp > b_normal_exp)
00614 {
00615 b_normal_exp++;
00616 LSHIFT (b_fraction);
00617 }
00618 while (b_normal_exp > a_normal_exp)
00619 {
00620 a_normal_exp++;
00621 LSHIFT (a_fraction);
00622 }
00623 }
00624 else
00625 {
00626
00627 if (a_normal_exp > b_normal_exp)
00628 {
00629 b_normal_exp = a_normal_exp;
00630 b_fraction = 0;
00631 }
00632 else
00633 {
00634 a_normal_exp = b_normal_exp;
00635 a_fraction = 0;
00636 }
00637 }
00638 }
00639
00640 if (a->sign != b->sign)
00641 {
00642 if (a->sign)
00643 {
00644 tfraction = -a_fraction + b_fraction;
00645 }
00646 else
00647 {
00648 tfraction = a_fraction - b_fraction;
00649 }
00650 if (tfraction >= 0)
00651 {
00652 tmp->sign = 0;
00653 tmp->normal_exp = a_normal_exp;
00654 tmp->fraction.ll = tfraction;
00655 }
00656 else
00657 {
00658 tmp->sign = 1;
00659 tmp->normal_exp = a_normal_exp;
00660 tmp->fraction.ll = -tfraction;
00661 }
00662
00663
00664 while (tmp->fraction.ll < IMPLICIT_1 && tmp->fraction.ll)
00665 {
00666 tmp->fraction.ll <<= 1;
00667 tmp->normal_exp--;
00668 }
00669 }
00670 else
00671 {
00672 tmp->sign = a->sign;
00673 tmp->normal_exp = a_normal_exp;
00674 tmp->fraction.ll = a_fraction + b_fraction;
00675 }
00676 tmp->class = CLASS_NUMBER;
00677
00678
00679
00680 if (tmp->fraction.ll >= IMPLICIT_2)
00681 {
00682 LSHIFT (tmp->fraction.ll);
00683 tmp->normal_exp++;
00684 }
00685 return tmp;
00686
00687 }
00688
00689 FLO_type
00690 add (FLO_type arg_a, FLO_type arg_b)
00691 {
00692 fp_number_type a;
00693 fp_number_type b;
00694 fp_number_type tmp;
00695 fp_number_type *res;
00696 FLO_union_type au, bu;
00697
00698 au.value = arg_a;
00699 bu.value = arg_b;
00700
00701 unpack_d (&au, &a);
00702 unpack_d (&bu, &b);
00703
00704 res = _fpadd_parts (&a, &b, &tmp);
00705
00706 return pack_d (res);
00707 }
00708
00709 FLO_type
00710 sub (FLO_type arg_a, FLO_type arg_b)
00711 {
00712 fp_number_type a;
00713 fp_number_type b;
00714 fp_number_type tmp;
00715 fp_number_type *res;
00716 FLO_union_type au, bu;
00717
00718 au.value = arg_a;
00719 bu.value = arg_b;
00720
00721 unpack_d (&au, &a);
00722 unpack_d (&bu, &b);
00723
00724 b.sign ^= 1;
00725
00726 res = _fpadd_parts (&a, &b, &tmp);
00727
00728 return pack_d (res);
00729 }
00730 #endif
00731
00732 #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
00733 static inline __attribute__ ((__always_inline__)) fp_number_type *
00734 _fpmul_parts ( fp_number_type * a,
00735 fp_number_type * b,
00736 fp_number_type * tmp)
00737 {
00738 fractype low = 0;
00739 fractype high = 0;
00740
00741 if (isnan (a))
00742 {
00743 a->sign = a->sign != b->sign;
00744 return a;
00745 }
00746 if (isnan (b))
00747 {
00748 b->sign = a->sign != b->sign;
00749 return b;
00750 }
00751 if (isinf (a))
00752 {
00753 if (iszero (b))
00754 return nan ();
00755 a->sign = a->sign != b->sign;
00756 return a;
00757 }
00758 if (isinf (b))
00759 {
00760 if (iszero (a))
00761 {
00762 return nan ();
00763 }
00764 b->sign = a->sign != b->sign;
00765 return b;
00766 }
00767 if (iszero (a))
00768 {
00769 a->sign = a->sign != b->sign;
00770 return a;
00771 }
00772 if (iszero (b))
00773 {
00774 b->sign = a->sign != b->sign;
00775 return b;
00776 }
00777
00778
00779
00780 {
00781 #if defined(NO_DI_MODE) || defined(TFLOAT)
00782 {
00783 fractype x = a->fraction.ll;
00784 fractype ylow = b->fraction.ll;
00785 fractype yhigh = 0;
00786 int bit;
00787
00788
00789 for (bit = 0; bit < FRAC_NBITS; bit++)
00790 {
00791 int carry;
00792
00793 if (x & 1)
00794 {
00795 carry = (low += ylow) < ylow;
00796 high += yhigh + carry;
00797 }
00798 yhigh <<= 1;
00799 if (ylow & FRACHIGH)
00800 {
00801 yhigh |= 1;
00802 }
00803 ylow <<= 1;
00804 x >>= 1;
00805 }
00806 }
00807 #elif defined(FLOAT)
00808
00809 {
00810 UDItype answer = (UDItype)a->fraction.ll * (UDItype)b->fraction.ll;
00811
00812 high = answer >> BITS_PER_SI;
00813 low = answer;
00814 }
00815 #else
00816
00817
00818
00819 {
00820 USItype nl = a->fraction.ll;
00821 USItype nh = a->fraction.ll >> BITS_PER_SI;
00822 USItype ml = b->fraction.ll;
00823 USItype mh = b->fraction.ll >> BITS_PER_SI;
00824 UDItype pp_ll = (UDItype) ml * nl;
00825 UDItype pp_hl = (UDItype) mh * nl;
00826 UDItype pp_lh = (UDItype) ml * nh;
00827 UDItype pp_hh = (UDItype) mh * nh;
00828 UDItype res2 = 0;
00829 UDItype res0 = 0;
00830 UDItype ps_hh__ = pp_hl + pp_lh;
00831 if (ps_hh__ < pp_hl)
00832 res2 += (UDItype)1 << BITS_PER_SI;
00833 pp_hl = (UDItype)(USItype)ps_hh__ << BITS_PER_SI;
00834 res0 = pp_ll + pp_hl;
00835 if (res0 < pp_ll)
00836 res2++;
00837 res2 += (ps_hh__ >> BITS_PER_SI) + pp_hh;
00838 high = res2;
00839 low = res0;
00840 }
00841 #endif
00842 }
00843
00844 tmp->normal_exp = a->normal_exp + b->normal_exp
00845 + FRAC_NBITS - (FRACBITS + NGARDS);
00846 tmp->sign = a->sign != b->sign;
00847 while (high >= IMPLICIT_2)
00848 {
00849 tmp->normal_exp++;
00850 if (high & 1)
00851 {
00852 low >>= 1;
00853 low |= FRACHIGH;
00854 }
00855 high >>= 1;
00856 }
00857 while (high < IMPLICIT_1)
00858 {
00859 tmp->normal_exp--;
00860
00861 high <<= 1;
00862 if (low & FRACHIGH)
00863 high |= 1;
00864 low <<= 1;
00865 }
00866
00867 #if 0
00868 if (low & FRACHIGH2)
00869 {
00870 if (((high & GARDMASK) != GARDMSB)
00871 && (((high + 1) & GARDMASK) == GARDMSB))
00872 {
00873
00874 }
00875 else
00876 {
00877 high++;
00878 }
00879 }
00880 #endif
00881 if (!ROUND_TOWARDS_ZERO && (high & GARDMASK) == GARDMSB)
00882 {
00883 if (high & (1 << NGARDS))
00884 {
00885
00886 high += GARDROUND + 1;
00887 }
00888 else if (low)
00889 {
00890
00891 high += GARDROUND + 1;
00892 }
00893 }
00894 tmp->fraction.ll = high;
00895 tmp->class = CLASS_NUMBER;
00896 return tmp;
00897 }
00898
00899 FLO_type
00900 multiply (FLO_type arg_a, FLO_type arg_b)
00901 {
00902 fp_number_type a;
00903 fp_number_type b;
00904 fp_number_type tmp;
00905 fp_number_type *res;
00906 FLO_union_type au, bu;
00907
00908 au.value = arg_a;
00909 bu.value = arg_b;
00910
00911 unpack_d (&au, &a);
00912 unpack_d (&bu, &b);
00913
00914 res = _fpmul_parts (&a, &b, &tmp);
00915
00916 return pack_d (res);
00917 }
00918 #endif
00919
00920 #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
00921 static inline __attribute__ ((__always_inline__)) fp_number_type *
00922 _fpdiv_parts (fp_number_type * a,
00923 fp_number_type * b)
00924 {
00925 fractype bit;
00926 fractype numerator;
00927 fractype denominator;
00928 fractype quotient;
00929
00930 if (isnan (a))
00931 {
00932 return a;
00933 }
00934 if (isnan (b))
00935 {
00936 return b;
00937 }
00938
00939 a->sign = a->sign ^ b->sign;
00940
00941 if (isinf (a) || iszero (a))
00942 {
00943 if (a->class == b->class)
00944 return nan ();
00945 return a;
00946 }
00947
00948 if (isinf (b))
00949 {
00950 a->fraction.ll = 0;
00951 a->normal_exp = 0;
00952 return a;
00953 }
00954 if (iszero (b))
00955 {
00956 a->class = CLASS_INFINITY;
00957 return a;
00958 }
00959
00960
00961
00962 {
00963
00964
00965
00966
00967 a->normal_exp = a->normal_exp - b->normal_exp;
00968 numerator = a->fraction.ll;
00969 denominator = b->fraction.ll;
00970
00971 if (numerator < denominator)
00972 {
00973
00974 numerator *= 2;
00975 a->normal_exp--;
00976 }
00977 bit = IMPLICIT_1;
00978 quotient = 0;
00979
00980 while (bit)
00981 {
00982 if (numerator >= denominator)
00983 {
00984 quotient |= bit;
00985 numerator -= denominator;
00986 }
00987 bit >>= 1;
00988 numerator *= 2;
00989 }
00990
00991 if (!ROUND_TOWARDS_ZERO && (quotient & GARDMASK) == GARDMSB)
00992 {
00993 if (quotient & (1 << NGARDS))
00994 {
00995
00996 quotient += GARDROUND + 1;
00997 }
00998 else if (numerator)
00999 {
01000
01001 quotient += GARDROUND + 1;
01002 }
01003 }
01004
01005 a->fraction.ll = quotient;
01006 return (a);
01007 }
01008 }
01009
01010 FLO_type
01011 divide (FLO_type arg_a, FLO_type arg_b)
01012 {
01013 fp_number_type a;
01014 fp_number_type b;
01015 fp_number_type *res;
01016 FLO_union_type au, bu;
01017
01018 au.value = arg_a;
01019 bu.value = arg_b;
01020
01021 unpack_d (&au, &a);
01022 unpack_d (&bu, &b);
01023
01024 res = _fpdiv_parts (&a, &b);
01025
01026 return pack_d (res);
01027 }
01028 #endif
01029
01030 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df) \
01031 || defined(L_fpcmp_parts_tf)
01032
01033
01034
01035
01036
01037
01038 int
01039 __fpcmp_parts (fp_number_type * a, fp_number_type * b)
01040 {
01041 #if 0
01042
01043 if (isnan (a) && isnan (b))
01044 {
01045 return 1;
01046 }
01047 #endif
01048
01049 if (isnan (a) || isnan (b))
01050 {
01051 return 1;
01052 }
01053 if (isinf (a) && isinf (b))
01054 {
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064 return b->sign - a->sign;
01065 }
01066
01067 if (isinf (a))
01068 {
01069 return a->sign ? -1 : 1;
01070 }
01071 if (isinf (b))
01072 {
01073 return b->sign ? 1 : -1;
01074 }
01075 if (iszero (a) && iszero (b))
01076 {
01077 return 0;
01078 }
01079 if (iszero (a))
01080 {
01081 return b->sign ? 1 : -1;
01082 }
01083 if (iszero (b))
01084 {
01085 return a->sign ? -1 : 1;
01086 }
01087
01088 if (a->sign != b->sign)
01089 {
01090
01091 return a->sign ? -1 : 1;
01092 }
01093
01094 if (a->normal_exp > b->normal_exp)
01095 {
01096 return a->sign ? -1 : 1;
01097 }
01098 if (a->normal_exp < b->normal_exp)
01099 {
01100 return a->sign ? 1 : -1;
01101 }
01102
01103 if (a->fraction.ll > b->fraction.ll)
01104 {
01105 return a->sign ? -1 : 1;
01106 }
01107 if (a->fraction.ll < b->fraction.ll)
01108 {
01109 return a->sign ? 1 : -1;
01110 }
01111
01112 return 0;
01113 }
01114 #endif
01115
01116 #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compoare_tf)
01117 CMPtype
01118 compare (FLO_type arg_a, FLO_type arg_b)
01119 {
01120 fp_number_type a;
01121 fp_number_type b;
01122 FLO_union_type au, bu;
01123
01124 au.value = arg_a;
01125 bu.value = arg_b;
01126
01127 unpack_d (&au, &a);
01128 unpack_d (&bu, &b);
01129
01130 return __fpcmp_parts (&a, &b);
01131 }
01132 #endif
01133
01134 #ifndef US_SOFTWARE_GOFAST
01135
01136
01137
01138 #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
01139 CMPtype
01140 _eq_f2 (FLO_type arg_a, FLO_type arg_b)
01141 {
01142 fp_number_type a;
01143 fp_number_type b;
01144 FLO_union_type au, bu;
01145
01146 au.value = arg_a;
01147 bu.value = arg_b;
01148
01149 unpack_d (&au, &a);
01150 unpack_d (&bu, &b);
01151
01152 if (isnan (&a) || isnan (&b))
01153 return 1;
01154
01155 return __fpcmp_parts (&a, &b) ;
01156 }
01157 #endif
01158
01159 #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
01160 CMPtype
01161 _ne_f2 (FLO_type arg_a, FLO_type arg_b)
01162 {
01163 fp_number_type a;
01164 fp_number_type b;
01165 FLO_union_type au, bu;
01166
01167 au.value = arg_a;
01168 bu.value = arg_b;
01169
01170 unpack_d (&au, &a);
01171 unpack_d (&bu, &b);
01172
01173 if (isnan (&a) || isnan (&b))
01174 return 1;
01175
01176 return __fpcmp_parts (&a, &b) ;
01177 }
01178 #endif
01179
01180 #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
01181 CMPtype
01182 _gt_f2 (FLO_type arg_a, FLO_type arg_b)
01183 {
01184 fp_number_type a;
01185 fp_number_type b;
01186 FLO_union_type au, bu;
01187
01188 au.value = arg_a;
01189 bu.value = arg_b;
01190
01191 unpack_d (&au, &a);
01192 unpack_d (&bu, &b);
01193
01194 if (isnan (&a) || isnan (&b))
01195 return -1;
01196
01197 return __fpcmp_parts (&a, &b);
01198 }
01199 #endif
01200
01201 #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
01202 CMPtype
01203 _ge_f2 (FLO_type arg_a, FLO_type arg_b)
01204 {
01205 fp_number_type a;
01206 fp_number_type b;
01207 FLO_union_type au, bu;
01208
01209 au.value = arg_a;
01210 bu.value = arg_b;
01211
01212 unpack_d (&au, &a);
01213 unpack_d (&bu, &b);
01214
01215 if (isnan (&a) || isnan (&b))
01216 return -1;
01217 return __fpcmp_parts (&a, &b) ;
01218 }
01219 #endif
01220
01221 #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
01222 CMPtype
01223 _lt_f2 (FLO_type arg_a, FLO_type arg_b)
01224 {
01225 fp_number_type a;
01226 fp_number_type b;
01227 FLO_union_type au, bu;
01228
01229 au.value = arg_a;
01230 bu.value = arg_b;
01231
01232 unpack_d (&au, &a);
01233 unpack_d (&bu, &b);
01234
01235 if (isnan (&a) || isnan (&b))
01236 return 1;
01237
01238 return __fpcmp_parts (&a, &b);
01239 }
01240 #endif
01241
01242 #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
01243 CMPtype
01244 _le_f2 (FLO_type arg_a, FLO_type arg_b)
01245 {
01246 fp_number_type a;
01247 fp_number_type b;
01248 FLO_union_type au, bu;
01249
01250 au.value = arg_a;
01251 bu.value = arg_b;
01252
01253 unpack_d (&au, &a);
01254 unpack_d (&bu, &b);
01255
01256 if (isnan (&a) || isnan (&b))
01257 return 1;
01258
01259 return __fpcmp_parts (&a, &b) ;
01260 }
01261 #endif
01262
01263 #endif
01264
01265 #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
01266 CMPtype
01267 _unord_f2 (FLO_type arg_a, FLO_type arg_b)
01268 {
01269 fp_number_type a;
01270 fp_number_type b;
01271 FLO_union_type au, bu;
01272
01273 au.value = arg_a;
01274 bu.value = arg_b;
01275
01276 unpack_d (&au, &a);
01277 unpack_d (&bu, &b);
01278
01279 return (isnan (&a) || isnan (&b));
01280 }
01281 #endif
01282
01283 #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
01284 FLO_type
01285 si_to_float (SItype arg_a)
01286 {
01287 fp_number_type in;
01288
01289 in.class = CLASS_NUMBER;
01290 in.sign = arg_a < 0;
01291 if (!arg_a)
01292 {
01293 in.class = CLASS_ZERO;
01294 }
01295 else
01296 {
01297 in.normal_exp = FRACBITS + NGARDS;
01298 if (in.sign)
01299 {
01300
01301
01302 if (arg_a == (- MAX_SI_INT - 1))
01303 {
01304 return (FLO_type)(- MAX_SI_INT - 1);
01305 }
01306 in.fraction.ll = (-arg_a);
01307 }
01308 else
01309 in.fraction.ll = arg_a;
01310
01311 while (in.fraction.ll < ((fractype)1 << (FRACBITS + NGARDS)))
01312 {
01313 in.fraction.ll <<= 1;
01314 in.normal_exp -= 1;
01315 }
01316 }
01317 return pack_d (&in);
01318 }
01319 #endif
01320
01321 #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
01322 FLO_type
01323 usi_to_float (USItype arg_a)
01324 {
01325 fp_number_type in;
01326
01327 in.sign = 0;
01328 if (!arg_a)
01329 {
01330 in.class = CLASS_ZERO;
01331 }
01332 else
01333 {
01334 in.class = CLASS_NUMBER;
01335 in.normal_exp = FRACBITS + NGARDS;
01336 in.fraction.ll = arg_a;
01337
01338 while (in.fraction.ll > ((fractype)1 << (FRACBITS + NGARDS)))
01339 {
01340 in.fraction.ll >>= 1;
01341 in.normal_exp += 1;
01342 }
01343 while (in.fraction.ll < ((fractype)1 << (FRACBITS + NGARDS)))
01344 {
01345 in.fraction.ll <<= 1;
01346 in.normal_exp -= 1;
01347 }
01348 }
01349 return pack_d (&in);
01350 }
01351 #endif
01352
01353 #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
01354 SItype
01355 float_to_si (FLO_type arg_a)
01356 {
01357 fp_number_type a;
01358 SItype tmp;
01359 FLO_union_type au;
01360
01361 au.value = arg_a;
01362 unpack_d (&au, &a);
01363
01364 if (iszero (&a))
01365 return 0;
01366 if (isnan (&a))
01367 return 0;
01368
01369 if (isinf (&a))
01370 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
01371
01372 if (a.normal_exp < 0)
01373 return 0;
01374 if (a.normal_exp > BITS_PER_SI - 2)
01375 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
01376 tmp = a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
01377 return a.sign ? (-tmp) : (tmp);
01378 }
01379 #endif
01380
01381 #if defined(L_sf_to_usi) || defined(L_df_to_usi) || defined(L_tf_to_usi)
01382 #if defined US_SOFTWARE_GOFAST || defined(L_tf_to_usi)
01383
01384
01385
01386
01387
01388
01389 USItype
01390 float_to_usi (FLO_type arg_a)
01391 {
01392 fp_number_type a;
01393 FLO_union_type au;
01394
01395 au.value = arg_a;
01396 unpack_d (&au, &a);
01397
01398 if (iszero (&a))
01399 return 0;
01400 if (isnan (&a))
01401 return 0;
01402
01403 if (a.sign)
01404 return 0;
01405
01406 if (isinf (&a))
01407 return MAX_USI_INT;
01408
01409 if (a.normal_exp < 0)
01410 return 0;
01411 if (a.normal_exp > BITS_PER_SI - 1)
01412 return MAX_USI_INT;
01413 else if (a.normal_exp > (FRACBITS + NGARDS))
01414 return a.fraction.ll << (a.normal_exp - (FRACBITS + NGARDS));
01415 else
01416 return a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
01417 }
01418 #endif
01419 #endif
01420
01421 #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
01422 FLO_type
01423 negate (FLO_type arg_a)
01424 {
01425 fp_number_type a;
01426 FLO_union_type au;
01427
01428 au.value = arg_a;
01429 unpack_d (&au, &a);
01430
01431 flip_sign (&a);
01432 return pack_d (&a);
01433 }
01434 #endif
01435
01436 #ifdef FLOAT
01437
01438 #if defined(L_make_sf)
01439 SFtype
01440 __make_fp(fp_class_type class,
01441 unsigned int sign,
01442 int exp,
01443 USItype frac)
01444 {
01445 fp_number_type in;
01446
01447 in.class = class;
01448 in.sign = sign;
01449 in.normal_exp = exp;
01450 in.fraction.ll = frac;
01451 return pack_d (&in);
01452 }
01453 #endif
01454
01455 #ifndef FLOAT_ONLY
01456
01457
01458
01459
01460
01461
01462 #if defined(L_sf_to_df)
01463 DFtype
01464 sf_to_df (SFtype arg_a)
01465 {
01466 fp_number_type in;
01467 FLO_union_type au;
01468
01469 au.value = arg_a;
01470 unpack_d (&au, &in);
01471
01472 return __make_dp (in.class, in.sign, in.normal_exp,
01473 ((UDItype) in.fraction.ll) << F_D_BITOFF);
01474 }
01475 #endif
01476
01477 #if defined(L_sf_to_tf) && defined(TMODES)
01478 TFtype
01479 sf_to_tf (SFtype arg_a)
01480 {
01481 fp_number_type in;
01482 FLO_union_type au;
01483
01484 au.value = arg_a;
01485 unpack_d (&au, &in);
01486
01487 return __make_tp (in.class, in.sign, in.normal_exp,
01488 ((UTItype) in.fraction.ll) << F_T_BITOFF);
01489 }
01490 #endif
01491
01492 #endif
01493 #endif
01494
01495 #ifndef FLOAT
01496
01497 extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
01498
01499 #if defined(L_make_df)
01500 DFtype
01501 __make_dp (fp_class_type class, unsigned int sign, int exp, UDItype frac)
01502 {
01503 fp_number_type in;
01504
01505 in.class = class;
01506 in.sign = sign;
01507 in.normal_exp = exp;
01508 in.fraction.ll = frac;
01509 return pack_d (&in);
01510 }
01511 #endif
01512
01513 #if defined(L_df_to_sf)
01514 SFtype
01515 df_to_sf (DFtype arg_a)
01516 {
01517 fp_number_type in;
01518 USItype sffrac;
01519 FLO_union_type au;
01520
01521 au.value = arg_a;
01522 unpack_d (&au, &in);
01523
01524 sffrac = in.fraction.ll >> F_D_BITOFF;
01525
01526
01527
01528 if ((in.fraction.ll & (((USItype) 1 << F_D_BITOFF) - 1)) != 0)
01529 sffrac |= 1;
01530
01531 return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
01532 }
01533 #endif
01534
01535 #if defined(L_df_to_tf) && defined(TMODES) \
01536 && !defined(FLOAT) && !defined(TFLOAT)
01537 TFtype
01538 df_to_tf (DFtype arg_a)
01539 {
01540 fp_number_type in;
01541 FLO_union_type au;
01542
01543 au.value = arg_a;
01544 unpack_d (&au, &in);
01545
01546 return __make_tp (in.class, in.sign, in.normal_exp,
01547 ((UTItype) in.fraction.ll) << D_T_BITOFF);
01548 }
01549 #endif
01550
01551 #ifdef TFLOAT
01552 #if defined(L_make_tf)
01553 TFtype
01554 __make_tp(fp_class_type class,
01555 unsigned int sign,
01556 int exp,
01557 UTItype frac)
01558 {
01559 fp_number_type in;
01560
01561 in.class = class;
01562 in.sign = sign;
01563 in.normal_exp = exp;
01564 in.fraction.ll = frac;
01565 return pack_d (&in);
01566 }
01567 #endif
01568
01569 #if defined(L_tf_to_df)
01570 DFtype
01571 tf_to_df (TFtype arg_a)
01572 {
01573 fp_number_type in;
01574 UDItype sffrac;
01575 FLO_union_type au;
01576
01577 au.value = arg_a;
01578 unpack_d (&au, &in);
01579
01580 sffrac = in.fraction.ll >> D_T_BITOFF;
01581
01582
01583
01584 if ((in.fraction.ll & (((UTItype) 1 << D_T_BITOFF) - 1)) != 0)
01585 sffrac |= 1;
01586
01587 return __make_dp (in.class, in.sign, in.normal_exp, sffrac);
01588 }
01589 #endif
01590
01591 #if defined(L_tf_to_sf)
01592 SFtype
01593 tf_to_sf (TFtype arg_a)
01594 {
01595 fp_number_type in;
01596 USItype sffrac;
01597 FLO_union_type au;
01598
01599 au.value = arg_a;
01600 unpack_d (&au, &in);
01601
01602 sffrac = in.fraction.ll >> F_T_BITOFF;
01603
01604
01605
01606 if ((in.fraction.ll & (((UTItype) 1 << F_T_BITOFF) - 1)) != 0)
01607 sffrac |= 1;
01608
01609 return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
01610 }
01611 #endif
01612 #endif
01613
01614 #endif
01615 #endif