1 /*---------------------------------------------------------------------------+
4 | Function to compute 2^x-1 by a polynomial approximation. |
6 | Copyright (C) 1992,1993,1994,1997 |
7 | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
8 | E-mail billm@suburbia.net |
11 +---------------------------------------------------------------------------*/
13 #include "exception.h"
14 #include "reg_constant.h"
16 #include "fpu_system.h"
17 #include "control_w.h"
22 static const unsigned long long lterms[HIPOWER] =
24 0x0000000000000000LL, /* This term done separately as 12 bytes */
37 static const Xsig hiterm = MK_XSIG(0xb17217f7, 0xd1cf79ab, 0xc8a39194);
39 /* Four slices: 0.0 : 0.25 : 0.50 : 0.75 : 1.0,
40 These numbers are 2^(1/4), 2^(1/2), and 2^(3/4)
42 static const Xsig shiftterm0 = MK_XSIG(0, 0, 0);
43 static const Xsig shiftterm1 = MK_XSIG(0x9837f051, 0x8db8a96f, 0x46ad2318);
44 static const Xsig shiftterm2 = MK_XSIG(0xb504f333, 0xf9de6484, 0x597d89b3);
45 static const Xsig shiftterm3 = MK_XSIG(0xd744fcca, 0xd69d6af4, 0x39a68bb9);
47 static const Xsig *shiftterm[] = { &shiftterm0, &shiftterm1,
48 &shiftterm2, &shiftterm3 };
51 /*--- poly_2xm1() -----------------------------------------------------------+
52 | Requires st(0) which is TAG_Valid and < 1. |
53 +---------------------------------------------------------------------------*/
54 int poly_2xm1(u_char sign, FPU_REG *arg, FPU_REG *result)
56 long int exponent, shift;
57 unsigned long long Xll;
58 Xsig accumulator, Denom, argSignif;
61 exponent = exponent16(arg);
64 if ( exponent >= 0 ) /* Don't want a |number| >= 1.0 */
66 /* Number negative, too large, or not Valid. */
67 EXCEPTION(EX_INTERNAL|0x127);
73 XSIG_LL(argSignif) = Xll = significand(arg);
77 shift = (argSignif.msw & 0x40000000) ? 3 : 2;
78 /* subtract 0.5 or 0.75 */
80 XSIG_LL(argSignif) <<= 2;
83 else if ( exponent == -2 )
88 XSIG_LL(argSignif) <<= 1;
96 /* Shift the argument right by the required places. */
97 if ( FPU_shrx(&Xll, -2-exponent) >= 0x80000000U )
101 accumulator.lsw = accumulator.midw = accumulator.msw = 0;
102 polynomial_Xsig(&accumulator, &Xll, lterms, HIPOWER-1);
103 mul_Xsig_Xsig(&accumulator, &argSignif);
104 shr_Xsig(&accumulator, 3);
106 mul_Xsig_Xsig(&argSignif, &hiterm); /* The leading term */
107 add_two_Xsig(&accumulator, &argSignif, &exponent);
111 /* The argument is large, use the identity:
112 f(x+a) = f(a) * (f(x) + 1) - 1;
114 shr_Xsig(&accumulator, - exponent);
115 accumulator.msw |= 0x80000000; /* add 1.0 */
116 mul_Xsig_Xsig(&accumulator, shiftterm[shift]);
117 accumulator.msw &= 0x3fffffff; /* subtract 1.0 */
121 if ( sign != SIGN_POS )
123 /* The argument is negative, use the identity:
124 f(-x) = -f(x) / (1 + f(x))
126 Denom.lsw = accumulator.lsw;
127 XSIG_LL(Denom) = XSIG_LL(accumulator);
129 shr_Xsig(&Denom, - exponent);
130 else if ( exponent > 0 )
132 /* exponent must be 1 here */
133 XSIG_LL(Denom) <<= 1;
134 if ( Denom.lsw & 0x80000000 )
138 Denom.msw |= 0x80000000; /* add 1.0 */
139 div_Xsig(&accumulator, &Denom, &accumulator);
142 /* Convert to 64 bit signed-compatible */
143 exponent += round_Xsig(&accumulator);
146 significand(result) = XSIG_LL(accumulator);
147 setexponent16(result, exponent);
149 tag = FPU_round(result, 1, 0, FULL_PRECISION, sign);
151 setsign(result, sign);