Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /*---------------------------------------------------------------------------+ |
| 2 | | poly_l2.c | |
| 3 | | | |
| 4 | | Compute the base 2 log of a FPU_REG, using a polynomial approximation. | |
| 5 | | | |
| 6 | | Copyright (C) 1992,1993,1994,1997 | |
| 7 | | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia | |
| 8 | | E-mail billm@suburbia.net | |
| 9 | | | |
| 10 | | | |
| 11 | +---------------------------------------------------------------------------*/ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "exception.h" |
| 14 | #include "reg_constant.h" |
| 15 | #include "fpu_emu.h" |
| 16 | #include "fpu_system.h" |
| 17 | #include "control_w.h" |
| 18 | #include "poly.h" |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | static void log2_kernel(FPU_REG const *arg, u_char argsign, |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 21 | Xsig * accum_result, long int *expon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | /*--- poly_l2() -------------------------------------------------------------+ |
| 24 | | Base 2 logarithm by a polynomial approximation. | |
| 25 | +---------------------------------------------------------------------------*/ |
Ingo Molnar | e8d591d | 2008-01-30 13:30:12 +0100 | [diff] [blame] | 26 | void poly_l2(FPU_REG *st0_ptr, FPU_REG *st1_ptr, u_char st1_sign) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 28 | long int exponent, expon, expon_expon; |
| 29 | Xsig accumulator, expon_accum, yaccum; |
| 30 | u_char sign, argsign; |
| 31 | FPU_REG x; |
| 32 | int tag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 34 | exponent = exponent16(st0_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 36 | /* From st0_ptr, make a number > sqrt(2)/2 and < sqrt(2) */ |
| 37 | if (st0_ptr->sigh > (unsigned)0xb504f334) { |
| 38 | /* Treat as sqrt(2)/2 < st0_ptr < 1 */ |
| 39 | significand(&x) = -significand(st0_ptr); |
| 40 | setexponent16(&x, -1); |
| 41 | exponent++; |
| 42 | argsign = SIGN_NEG; |
| 43 | } else { |
| 44 | /* Treat as 1 <= st0_ptr < sqrt(2) */ |
| 45 | x.sigh = st0_ptr->sigh - 0x80000000; |
| 46 | x.sigl = st0_ptr->sigl; |
| 47 | setexponent16(&x, 0); |
| 48 | argsign = SIGN_POS; |
| 49 | } |
| 50 | tag = FPU_normalize_nuo(&x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 52 | if (tag == TAG_Zero) { |
| 53 | expon = 0; |
| 54 | accumulator.msw = accumulator.midw = accumulator.lsw = 0; |
| 55 | } else { |
| 56 | log2_kernel(&x, argsign, &accumulator, &expon); |
| 57 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 59 | if (exponent < 0) { |
| 60 | sign = SIGN_NEG; |
| 61 | exponent = -exponent; |
| 62 | } else |
| 63 | sign = SIGN_POS; |
| 64 | expon_accum.msw = exponent; |
| 65 | expon_accum.midw = expon_accum.lsw = 0; |
| 66 | if (exponent) { |
| 67 | expon_expon = 31 + norm_Xsig(&expon_accum); |
| 68 | shr_Xsig(&accumulator, expon_expon - expon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 70 | if (sign ^ argsign) |
| 71 | negate_Xsig(&accumulator); |
| 72 | add_Xsig_Xsig(&accumulator, &expon_accum); |
| 73 | } else { |
| 74 | expon_expon = expon; |
| 75 | sign = argsign; |
| 76 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 78 | yaccum.lsw = 0; |
| 79 | XSIG_LL(yaccum) = significand(st1_ptr); |
| 80 | mul_Xsig_Xsig(&accumulator, &yaccum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 82 | expon_expon += round_Xsig(&accumulator); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 84 | if (accumulator.msw == 0) { |
| 85 | FPU_copy_to_reg1(&CONST_Z, TAG_Zero); |
| 86 | return; |
| 87 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 89 | significand(st1_ptr) = XSIG_LL(accumulator); |
| 90 | setexponent16(st1_ptr, expon_expon + exponent16(st1_ptr) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 92 | tag = FPU_round(st1_ptr, 1, 0, FULL_PRECISION, sign ^ st1_sign); |
| 93 | FPU_settagi(1, tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 95 | set_precision_flag_up(); /* 80486 appears to always do this */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 97 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | } |
| 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | /*--- poly_l2p1() -----------------------------------------------------------+ |
| 102 | | Base 2 logarithm by a polynomial approximation. | |
| 103 | | log2(x+1) | |
| 104 | +---------------------------------------------------------------------------*/ |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 105 | int poly_l2p1(u_char sign0, u_char sign1, |
| 106 | FPU_REG * st0_ptr, FPU_REG * st1_ptr, FPU_REG * dest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 108 | u_char tag; |
| 109 | long int exponent; |
| 110 | Xsig accumulator, yaccum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 112 | if (exponent16(st0_ptr) < 0) { |
| 113 | log2_kernel(st0_ptr, sign0, &accumulator, &exponent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 115 | yaccum.lsw = 0; |
| 116 | XSIG_LL(yaccum) = significand(st1_ptr); |
| 117 | mul_Xsig_Xsig(&accumulator, &yaccum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 119 | exponent += round_Xsig(&accumulator); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 121 | exponent += exponent16(st1_ptr) + 1; |
| 122 | if (exponent < EXP_WAY_UNDER) |
| 123 | exponent = EXP_WAY_UNDER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 125 | significand(dest) = XSIG_LL(accumulator); |
| 126 | setexponent16(dest, exponent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 128 | tag = FPU_round(dest, 1, 0, FULL_PRECISION, sign0 ^ sign1); |
| 129 | FPU_settagi(1, tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 131 | if (tag == TAG_Valid) |
| 132 | set_precision_flag_up(); /* 80486 appears to always do this */ |
| 133 | } else { |
| 134 | /* The magnitude of st0_ptr is far too large. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 136 | if (sign0 != SIGN_POS) { |
| 137 | /* Trying to get the log of a negative number. */ |
| 138 | #ifdef PECULIAR_486 /* Stupid 80486 doesn't worry about log(negative). */ |
| 139 | changesign(st1_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | #else |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 141 | if (arith_invalid(1) < 0) |
| 142 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | #endif /* PECULIAR_486 */ |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* 80486 appears to do this */ |
| 147 | if (sign0 == SIGN_NEG) |
| 148 | set_precision_flag_down(); |
| 149 | else |
| 150 | set_precision_flag_up(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 153 | if (exponent(dest) <= EXP_UNDER) |
| 154 | EXCEPTION(EX_Underflow); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 156 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | } |
| 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | #undef HIPOWER |
| 161 | #define HIPOWER 10 |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 162 | static const unsigned long long logterms[HIPOWER] = { |
| 163 | 0x2a8eca5705fc2ef0LL, |
| 164 | 0xf6384ee1d01febceLL, |
| 165 | 0x093bb62877cdf642LL, |
| 166 | 0x006985d8a9ec439bLL, |
| 167 | 0x0005212c4f55a9c8LL, |
| 168 | 0x00004326a16927f0LL, |
| 169 | 0x0000038d1d80a0e7LL, |
| 170 | 0x0000003141cc80c6LL, |
| 171 | 0x00000002b1668c9fLL, |
| 172 | 0x000000002c7a46aaLL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | static const unsigned long leadterm = 0xb8000000; |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | /*--- log2_kernel() ---------------------------------------------------------+ |
| 178 | | Base 2 logarithm by a polynomial approximation. | |
| 179 | | log2(x+1) | |
| 180 | +---------------------------------------------------------------------------*/ |
Ingo Molnar | e8d591d | 2008-01-30 13:30:12 +0100 | [diff] [blame] | 181 | static void log2_kernel(FPU_REG const *arg, u_char argsign, Xsig *accum_result, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | long int *expon) |
| 183 | { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 184 | long int exponent, adj; |
| 185 | unsigned long long Xsq; |
| 186 | Xsig accumulator, Numer, Denom, argSignif, arg_signif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 188 | exponent = exponent16(arg); |
| 189 | Numer.lsw = Denom.lsw = 0; |
| 190 | XSIG_LL(Numer) = XSIG_LL(Denom) = significand(arg); |
| 191 | if (argsign == SIGN_POS) { |
| 192 | shr_Xsig(&Denom, 2 - (1 + exponent)); |
| 193 | Denom.msw |= 0x80000000; |
| 194 | div_Xsig(&Numer, &Denom, &argSignif); |
| 195 | } else { |
| 196 | shr_Xsig(&Denom, 1 - (1 + exponent)); |
| 197 | negate_Xsig(&Denom); |
| 198 | if (Denom.msw & 0x80000000) { |
| 199 | div_Xsig(&Numer, &Denom, &argSignif); |
| 200 | exponent++; |
| 201 | } else { |
| 202 | /* Denom must be 1.0 */ |
| 203 | argSignif.lsw = Numer.lsw; |
| 204 | argSignif.midw = Numer.midw; |
| 205 | argSignif.msw = Numer.msw; |
| 206 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | #ifndef PECULIAR_486 |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 210 | /* Should check here that |local_arg| is within the valid range */ |
| 211 | if (exponent >= -2) { |
| 212 | if ((exponent > -2) || (argSignif.msw > (unsigned)0xafb0ccc0)) { |
| 213 | /* The argument is too large */ |
| 214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | #endif /* PECULIAR_486 */ |
| 217 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 218 | arg_signif.lsw = argSignif.lsw; |
| 219 | XSIG_LL(arg_signif) = XSIG_LL(argSignif); |
| 220 | adj = norm_Xsig(&argSignif); |
| 221 | accumulator.lsw = argSignif.lsw; |
| 222 | XSIG_LL(accumulator) = XSIG_LL(argSignif); |
| 223 | mul_Xsig_Xsig(&accumulator, &accumulator); |
| 224 | shr_Xsig(&accumulator, 2 * (-1 - (1 + exponent + adj))); |
| 225 | Xsq = XSIG_LL(accumulator); |
| 226 | if (accumulator.lsw & 0x80000000) |
| 227 | Xsq++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 229 | accumulator.msw = accumulator.midw = accumulator.lsw = 0; |
| 230 | /* Do the basic fixed point polynomial evaluation */ |
| 231 | polynomial_Xsig(&accumulator, &Xsq, logterms, HIPOWER - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 233 | mul_Xsig_Xsig(&accumulator, &argSignif); |
| 234 | shr_Xsig(&accumulator, 6 - adj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 236 | mul32_Xsig(&arg_signif, leadterm); |
| 237 | add_two_Xsig(&accumulator, &arg_signif, &exponent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 239 | *expon = exponent + 1; |
| 240 | accum_result->lsw = accumulator.lsw; |
| 241 | accum_result->midw = accumulator.midw; |
| 242 | accum_result->msw = accumulator.msw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | } |