poly_l2.c 7.1 KB

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