poly_tan.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*---------------------------------------------------------------------------+
  2. | poly_tan.c |
  3. | |
  4. | Compute the tan of a FPU_REG, using a polynomial approximation. |
  5. | |
  6. | Copyright (C) 1992,1993,1994,1997,1999 |
  7. | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, |
  8. | Australia. E-mail billm@melbpc.org.au |
  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. #define HiPOWERop 3 /* odd poly, positive terms */
  19. static const unsigned long long oddplterm[HiPOWERop] = {
  20. 0x0000000000000000LL,
  21. 0x0051a1cf08fca228LL,
  22. 0x0000000071284ff7LL
  23. };
  24. #define HiPOWERon 2 /* odd poly, negative terms */
  25. static const unsigned long long oddnegterm[HiPOWERon] = {
  26. 0x1291a9a184244e80LL,
  27. 0x0000583245819c21LL
  28. };
  29. #define HiPOWERep 2 /* even poly, positive terms */
  30. static const unsigned long long evenplterm[HiPOWERep] = {
  31. 0x0e848884b539e888LL,
  32. 0x00003c7f18b887daLL
  33. };
  34. #define HiPOWERen 2 /* even poly, negative terms */
  35. static const unsigned long long evennegterm[HiPOWERen] = {
  36. 0xf1f0200fd51569ccLL,
  37. 0x003afb46105c4432LL
  38. };
  39. static const unsigned long long twothirds = 0xaaaaaaaaaaaaaaabLL;
  40. /*--- poly_tan() ------------------------------------------------------------+
  41. | |
  42. +---------------------------------------------------------------------------*/
  43. void poly_tan(FPU_REG *st0_ptr)
  44. {
  45. long int exponent;
  46. int invert;
  47. Xsig argSq, argSqSq, accumulatoro, accumulatore, accum,
  48. argSignif, fix_up;
  49. unsigned long adj;
  50. exponent = exponent(st0_ptr);
  51. #ifdef PARANOID
  52. if (signnegative(st0_ptr)) { /* Can't hack a number < 0.0 */
  53. arith_invalid(0);
  54. return;
  55. } /* Need a positive number */
  56. #endif /* PARANOID */
  57. /* Split the problem into two domains, smaller and larger than pi/4 */
  58. if ((exponent == 0)
  59. || ((exponent == -1) && (st0_ptr->sigh > 0xc90fdaa2))) {
  60. /* The argument is greater than (approx) pi/4 */
  61. invert = 1;
  62. accum.lsw = 0;
  63. XSIG_LL(accum) = significand(st0_ptr);
  64. if (exponent == 0) {
  65. /* The argument is >= 1.0 */
  66. /* Put the binary point at the left. */
  67. XSIG_LL(accum) <<= 1;
  68. }
  69. /* pi/2 in hex is: 1.921fb54442d18469 898CC51701B839A2 52049C1 */
  70. XSIG_LL(accum) = 0x921fb54442d18469LL - XSIG_LL(accum);
  71. /* This is a special case which arises due to rounding. */
  72. if (XSIG_LL(accum) == 0xffffffffffffffffLL) {
  73. FPU_settag0(TAG_Valid);
  74. significand(st0_ptr) = 0x8a51e04daabda360LL;
  75. setexponent16(st0_ptr,
  76. (0x41 + EXTENDED_Ebias) | SIGN_Negative);
  77. return;
  78. }
  79. argSignif.lsw = accum.lsw;
  80. XSIG_LL(argSignif) = XSIG_LL(accum);
  81. exponent = -1 + norm_Xsig(&argSignif);
  82. } else {
  83. invert = 0;
  84. argSignif.lsw = 0;
  85. XSIG_LL(accum) = XSIG_LL(argSignif) = significand(st0_ptr);
  86. if (exponent < -1) {
  87. /* shift the argument right by the required places */
  88. if (FPU_shrx(&XSIG_LL(accum), -1 - exponent) >=
  89. 0x80000000U)
  90. XSIG_LL(accum)++; /* round up */
  91. }
  92. }
  93. XSIG_LL(argSq) = XSIG_LL(accum);
  94. argSq.lsw = accum.lsw;
  95. mul_Xsig_Xsig(&argSq, &argSq);
  96. XSIG_LL(argSqSq) = XSIG_LL(argSq);
  97. argSqSq.lsw = argSq.lsw;
  98. mul_Xsig_Xsig(&argSqSq, &argSqSq);
  99. /* Compute the negative terms for the numerator polynomial */
  100. accumulatoro.msw = accumulatoro.midw = accumulatoro.lsw = 0;
  101. polynomial_Xsig(&accumulatoro, &XSIG_LL(argSqSq), oddnegterm,
  102. HiPOWERon - 1);
  103. mul_Xsig_Xsig(&accumulatoro, &argSq);
  104. negate_Xsig(&accumulatoro);
  105. /* Add the positive terms */
  106. polynomial_Xsig(&accumulatoro, &XSIG_LL(argSqSq), oddplterm,
  107. HiPOWERop - 1);
  108. /* Compute the positive terms for the denominator polynomial */
  109. accumulatore.msw = accumulatore.midw = accumulatore.lsw = 0;
  110. polynomial_Xsig(&accumulatore, &XSIG_LL(argSqSq), evenplterm,
  111. HiPOWERep - 1);
  112. mul_Xsig_Xsig(&accumulatore, &argSq);
  113. negate_Xsig(&accumulatore);
  114. /* Add the negative terms */
  115. polynomial_Xsig(&accumulatore, &XSIG_LL(argSqSq), evennegterm,
  116. HiPOWERen - 1);
  117. /* Multiply by arg^2 */
  118. mul64_Xsig(&accumulatore, &XSIG_LL(argSignif));
  119. mul64_Xsig(&accumulatore, &XSIG_LL(argSignif));
  120. /* de-normalize and divide by 2 */
  121. shr_Xsig(&accumulatore, -2 * (1 + exponent) + 1);
  122. negate_Xsig(&accumulatore); /* This does 1 - accumulator */
  123. /* Now find the ratio. */
  124. if (accumulatore.msw == 0) {
  125. /* accumulatoro must contain 1.0 here, (actually, 0) but it
  126. really doesn't matter what value we use because it will
  127. have negligible effect in later calculations
  128. */
  129. XSIG_LL(accum) = 0x8000000000000000LL;
  130. accum.lsw = 0;
  131. } else {
  132. div_Xsig(&accumulatoro, &accumulatore, &accum);
  133. }
  134. /* Multiply by 1/3 * arg^3 */
  135. mul64_Xsig(&accum, &XSIG_LL(argSignif));
  136. mul64_Xsig(&accum, &XSIG_LL(argSignif));
  137. mul64_Xsig(&accum, &XSIG_LL(argSignif));
  138. mul64_Xsig(&accum, &twothirds);
  139. shr_Xsig(&accum, -2 * (exponent + 1));
  140. /* tan(arg) = arg + accum */
  141. add_two_Xsig(&accum, &argSignif, &exponent);
  142. if (invert) {
  143. /* We now have the value of tan(pi_2 - arg) where pi_2 is an
  144. approximation for pi/2
  145. */
  146. /* The next step is to fix the answer to compensate for the
  147. error due to the approximation used for pi/2
  148. */
  149. /* This is (approx) delta, the error in our approx for pi/2
  150. (see above). It has an exponent of -65
  151. */
  152. XSIG_LL(fix_up) = 0x898cc51701b839a2LL;
  153. fix_up.lsw = 0;
  154. if (exponent == 0)
  155. adj = 0xffffffff; /* We want approx 1.0 here, but
  156. this is close enough. */
  157. else if (exponent > -30) {
  158. adj = accum.msw >> -(exponent + 1); /* tan */
  159. adj = mul_32_32(adj, adj); /* tan^2 */
  160. } else
  161. adj = 0;
  162. adj = mul_32_32(0x898cc517, adj); /* delta * tan^2 */
  163. fix_up.msw += adj;
  164. if (!(fix_up.msw & 0x80000000)) { /* did fix_up overflow ? */
  165. /* Yes, we need to add an msb */
  166. shr_Xsig(&fix_up, 1);
  167. fix_up.msw |= 0x80000000;
  168. shr_Xsig(&fix_up, 64 + exponent);
  169. } else
  170. shr_Xsig(&fix_up, 65 + exponent);
  171. add_two_Xsig(&accum, &fix_up, &exponent);
  172. /* accum now contains tan(pi/2 - arg).
  173. Use tan(arg) = 1.0 / tan(pi/2 - arg)
  174. */
  175. accumulatoro.lsw = accumulatoro.midw = 0;
  176. accumulatoro.msw = 0x80000000;
  177. div_Xsig(&accumulatoro, &accum, &accum);
  178. exponent = -exponent - 1;
  179. }
  180. /* Transfer the result */
  181. round_Xsig(&accum);
  182. FPU_settag0(TAG_Valid);
  183. significand(st0_ptr) = XSIG_LL(accum);
  184. setexponent16(st0_ptr, exponent + EXTENDED_Ebias); /* Result is positive. */
  185. }