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"
21 static const unsigned long long lterms[HIPOWER] = {
22 0x0000000000000000LL, /* This term done separately as 12 bytes */
35 static const Xsig hiterm = MK_XSIG(0xb17217f7, 0xd1cf79ab, 0xc8a39194);
37 /* Four slices: 0.0 : 0.25 : 0.50 : 0.75 : 1.0,
38 These numbers are 2^(1/4), 2^(1/2), and 2^(3/4)
40 static const Xsig shiftterm0 = MK_XSIG(0, 0, 0);
41 static const Xsig shiftterm1 = MK_XSIG(0x9837f051, 0x8db8a96f, 0x46ad2318);
42 static const Xsig shiftterm2 = MK_XSIG(0xb504f333, 0xf9de6484, 0x597d89b3);
43 static const Xsig shiftterm3 = MK_XSIG(0xd744fcca, 0xd69d6af4, 0x39a68bb9);
45 static const Xsig *shiftterm[] = { &shiftterm0, &shiftterm1,
46 &shiftterm2, &shiftterm3
49 /*--- poly_2xm1() -----------------------------------------------------------+
50 | Requires st(0) which is TAG_Valid and < 1. |
51 +---------------------------------------------------------------------------*/
52 int poly_2xm1(u_char sign, FPU_REG *arg, FPU_REG *result)
54 long int exponent, shift;
55 unsigned long long Xll;
56 Xsig accumulator, Denom, argSignif;
59 exponent = exponent16(arg);
62 if (exponent >= 0) { /* Don't want a |number| >= 1.0 */
63 /* Number negative, too large, or not Valid. */
64 EXCEPTION(EX_INTERNAL | 0x127);
70 XSIG_LL(argSignif) = Xll = significand(arg);
73 shift = (argSignif.msw & 0x40000000) ? 3 : 2;
74 /* subtract 0.5 or 0.75 */
76 XSIG_LL(argSignif) <<= 2;
78 } else if (exponent == -2) {
82 XSIG_LL(argSignif) <<= 1;
88 /* Shift the argument right by the required places. */
89 if (FPU_shrx(&Xll, -2 - exponent) >= 0x80000000U)
93 accumulator.lsw = accumulator.midw = accumulator.msw = 0;
94 polynomial_Xsig(&accumulator, &Xll, lterms, HIPOWER - 1);
95 mul_Xsig_Xsig(&accumulator, &argSignif);
96 shr_Xsig(&accumulator, 3);
98 mul_Xsig_Xsig(&argSignif, &hiterm); /* The leading term */
99 add_two_Xsig(&accumulator, &argSignif, &exponent);
102 /* The argument is large, use the identity:
103 f(x+a) = f(a) * (f(x) + 1) - 1;
105 shr_Xsig(&accumulator, -exponent);
106 accumulator.msw |= 0x80000000; /* add 1.0 */
107 mul_Xsig_Xsig(&accumulator, shiftterm[shift]);
108 accumulator.msw &= 0x3fffffff; /* subtract 1.0 */
112 if (sign != SIGN_POS) {
113 /* The argument is negative, use the identity:
114 f(-x) = -f(x) / (1 + f(x))
116 Denom.lsw = accumulator.lsw;
117 XSIG_LL(Denom) = XSIG_LL(accumulator);
119 shr_Xsig(&Denom, -exponent);
120 else if (exponent > 0) {
121 /* exponent must be 1 here */
122 XSIG_LL(Denom) <<= 1;
123 if (Denom.lsw & 0x80000000)
127 Denom.msw |= 0x80000000; /* add 1.0 */
128 div_Xsig(&accumulator, &Denom, &accumulator);
131 /* Convert to 64 bit signed-compatible */
132 exponent += round_Xsig(&accumulator);
135 significand(result) = XSIG_LL(accumulator);
136 setexponent16(result, exponent);
138 tag = FPU_round(result, 1, 0, FULL_PRECISION, sign);
140 setsign(result, sign);