poly_l2.c 7.1 KB

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