poly_sin.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*---------------------------------------------------------------------------+
  2. | poly_sin.c |
  3. | |
  4. | Computation of an approximation of the sin function and the cosine |
  5. | function by a polynomial. |
  6. | |
  7. | Copyright (C) 1992,1993,1994,1997,1999 |
  8. | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
  9. | E-mail billm@melbpc.org.au |
  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. #define N_COEFF_P 4
  20. #define N_COEFF_N 4
  21. static const unsigned long long pos_terms_l[N_COEFF_P] = {
  22. 0xaaaaaaaaaaaaaaabLL,
  23. 0x00d00d00d00cf906LL,
  24. 0x000006b99159a8bbLL,
  25. 0x000000000d7392e6LL
  26. };
  27. static const unsigned long long neg_terms_l[N_COEFF_N] = {
  28. 0x2222222222222167LL,
  29. 0x0002e3bc74aab624LL,
  30. 0x0000000b09229062LL,
  31. 0x00000000000c7973LL
  32. };
  33. #define N_COEFF_PH 4
  34. #define N_COEFF_NH 4
  35. static const unsigned long long pos_terms_h[N_COEFF_PH] = {
  36. 0x0000000000000000LL,
  37. 0x05b05b05b05b0406LL,
  38. 0x000049f93edd91a9LL,
  39. 0x00000000c9c9ed62LL
  40. };
  41. static const unsigned long long neg_terms_h[N_COEFF_NH] = {
  42. 0xaaaaaaaaaaaaaa98LL,
  43. 0x001a01a01a019064LL,
  44. 0x0000008f76c68a77LL,
  45. 0x0000000000d58f5eLL
  46. };
  47. /*--- poly_sine() -----------------------------------------------------------+
  48. | |
  49. +---------------------------------------------------------------------------*/
  50. void poly_sine(FPU_REG *st0_ptr)
  51. {
  52. int exponent, echange;
  53. Xsig accumulator, argSqrd, argTo4;
  54. unsigned long fix_up, adj;
  55. unsigned long long fixed_arg;
  56. FPU_REG result;
  57. exponent = exponent(st0_ptr);
  58. accumulator.lsw = accumulator.midw = accumulator.msw = 0;
  59. /* Split into two ranges, for arguments below and above 1.0 */
  60. /* The boundary between upper and lower is approx 0.88309101259 */
  61. if ((exponent < -1)
  62. || ((exponent == -1) && (st0_ptr->sigh <= 0xe21240aa))) {
  63. /* The argument is <= 0.88309101259 */
  64. argSqrd.msw = st0_ptr->sigh;
  65. argSqrd.midw = st0_ptr->sigl;
  66. argSqrd.lsw = 0;
  67. mul64_Xsig(&argSqrd, &significand(st0_ptr));
  68. shr_Xsig(&argSqrd, 2 * (-1 - exponent));
  69. argTo4.msw = argSqrd.msw;
  70. argTo4.midw = argSqrd.midw;
  71. argTo4.lsw = argSqrd.lsw;
  72. mul_Xsig_Xsig(&argTo4, &argTo4);
  73. polynomial_Xsig(&accumulator, &XSIG_LL(argTo4), neg_terms_l,
  74. N_COEFF_N - 1);
  75. mul_Xsig_Xsig(&accumulator, &argSqrd);
  76. negate_Xsig(&accumulator);
  77. polynomial_Xsig(&accumulator, &XSIG_LL(argTo4), pos_terms_l,
  78. N_COEFF_P - 1);
  79. shr_Xsig(&accumulator, 2); /* Divide by four */
  80. accumulator.msw |= 0x80000000; /* Add 1.0 */
  81. mul64_Xsig(&accumulator, &significand(st0_ptr));
  82. mul64_Xsig(&accumulator, &significand(st0_ptr));
  83. mul64_Xsig(&accumulator, &significand(st0_ptr));
  84. /* Divide by four, FPU_REG compatible, etc */
  85. exponent = 3 * exponent;
  86. /* The minimum exponent difference is 3 */
  87. shr_Xsig(&accumulator, exponent(st0_ptr) - exponent);
  88. negate_Xsig(&accumulator);
  89. XSIG_LL(accumulator) += significand(st0_ptr);
  90. echange = round_Xsig(&accumulator);
  91. setexponentpos(&result, exponent(st0_ptr) + echange);
  92. } else {
  93. /* The argument is > 0.88309101259 */
  94. /* We use sin(st(0)) = cos(pi/2-st(0)) */
  95. fixed_arg = significand(st0_ptr);
  96. if (exponent == 0) {
  97. /* The argument is >= 1.0 */
  98. /* Put the binary point at the left. */
  99. fixed_arg <<= 1;
  100. }
  101. /* pi/2 in hex is: 1.921fb54442d18469 898CC51701B839A2 52049C1 */
  102. fixed_arg = 0x921fb54442d18469LL - fixed_arg;
  103. /* There is a special case which arises due to rounding, to fix here. */
  104. if (fixed_arg == 0xffffffffffffffffLL)
  105. fixed_arg = 0;
  106. XSIG_LL(argSqrd) = fixed_arg;
  107. argSqrd.lsw = 0;
  108. mul64_Xsig(&argSqrd, &fixed_arg);
  109. XSIG_LL(argTo4) = XSIG_LL(argSqrd);
  110. argTo4.lsw = argSqrd.lsw;
  111. mul_Xsig_Xsig(&argTo4, &argTo4);
  112. polynomial_Xsig(&accumulator, &XSIG_LL(argTo4), neg_terms_h,
  113. N_COEFF_NH - 1);
  114. mul_Xsig_Xsig(&accumulator, &argSqrd);
  115. negate_Xsig(&accumulator);
  116. polynomial_Xsig(&accumulator, &XSIG_LL(argTo4), pos_terms_h,
  117. N_COEFF_PH - 1);
  118. negate_Xsig(&accumulator);
  119. mul64_Xsig(&accumulator, &fixed_arg);
  120. mul64_Xsig(&accumulator, &fixed_arg);
  121. shr_Xsig(&accumulator, 3);
  122. negate_Xsig(&accumulator);
  123. add_Xsig_Xsig(&accumulator, &argSqrd);
  124. shr_Xsig(&accumulator, 1);
  125. accumulator.lsw |= 1; /* A zero accumulator here would cause problems */
  126. negate_Xsig(&accumulator);
  127. /* The basic computation is complete. Now fix the answer to
  128. compensate for the error due to the approximation used for
  129. pi/2
  130. */
  131. /* This has an exponent of -65 */
  132. fix_up = 0x898cc517;
  133. /* The fix-up needs to be improved for larger args */
  134. if (argSqrd.msw & 0xffc00000) {
  135. /* Get about 32 bit precision in these: */
  136. fix_up -= mul_32_32(0x898cc517, argSqrd.msw) / 6;
  137. }
  138. fix_up = mul_32_32(fix_up, LL_MSW(fixed_arg));
  139. adj = accumulator.lsw; /* temp save */
  140. accumulator.lsw -= fix_up;
  141. if (accumulator.lsw > adj)
  142. XSIG_LL(accumulator)--;
  143. echange = round_Xsig(&accumulator);
  144. setexponentpos(&result, echange - 1);
  145. }
  146. significand(&result) = XSIG_LL(accumulator);
  147. setsign(&result, getsign(st0_ptr));
  148. FPU_copy_to_reg0(&result, TAG_Valid);
  149. #ifdef PARANOID
  150. if ((exponent(&result) >= 0)
  151. && (significand(&result) > 0x8000000000000000LL)) {
  152. EXCEPTION(EX_INTERNAL | 0x150);
  153. }
  154. #endif /* PARANOID */
  155. }
  156. /*--- poly_cos() ------------------------------------------------------------+
  157. | |
  158. +---------------------------------------------------------------------------*/
  159. void poly_cos(FPU_REG *st0_ptr)
  160. {
  161. FPU_REG result;
  162. long int exponent, exp2, echange;
  163. Xsig accumulator, argSqrd, fix_up, argTo4;
  164. unsigned long long fixed_arg;
  165. #ifdef PARANOID
  166. if ((exponent(st0_ptr) > 0)
  167. || ((exponent(st0_ptr) == 0)
  168. && (significand(st0_ptr) > 0xc90fdaa22168c234LL))) {
  169. EXCEPTION(EX_Invalid);
  170. FPU_copy_to_reg0(&CONST_QNaN, TAG_Special);
  171. return;
  172. }
  173. #endif /* PARANOID */
  174. exponent = exponent(st0_ptr);
  175. accumulator.lsw = accumulator.midw = accumulator.msw = 0;
  176. if ((exponent < -1)
  177. || ((exponent == -1) && (st0_ptr->sigh <= 0xb00d6f54))) {
  178. /* arg is < 0.687705 */
  179. argSqrd.msw = st0_ptr->sigh;
  180. argSqrd.midw = st0_ptr->sigl;
  181. argSqrd.lsw = 0;
  182. mul64_Xsig(&argSqrd, &significand(st0_ptr));
  183. if (exponent < -1) {
  184. /* shift the argument right by the required places */
  185. shr_Xsig(&argSqrd, 2 * (-1 - exponent));
  186. }
  187. argTo4.msw = argSqrd.msw;
  188. argTo4.midw = argSqrd.midw;
  189. argTo4.lsw = argSqrd.lsw;
  190. mul_Xsig_Xsig(&argTo4, &argTo4);
  191. polynomial_Xsig(&accumulator, &XSIG_LL(argTo4), neg_terms_h,
  192. N_COEFF_NH - 1);
  193. mul_Xsig_Xsig(&accumulator, &argSqrd);
  194. negate_Xsig(&accumulator);
  195. polynomial_Xsig(&accumulator, &XSIG_LL(argTo4), pos_terms_h,
  196. N_COEFF_PH - 1);
  197. negate_Xsig(&accumulator);
  198. mul64_Xsig(&accumulator, &significand(st0_ptr));
  199. mul64_Xsig(&accumulator, &significand(st0_ptr));
  200. shr_Xsig(&accumulator, -2 * (1 + exponent));
  201. shr_Xsig(&accumulator, 3);
  202. negate_Xsig(&accumulator);
  203. add_Xsig_Xsig(&accumulator, &argSqrd);
  204. shr_Xsig(&accumulator, 1);
  205. /* It doesn't matter if accumulator is all zero here, the
  206. following code will work ok */
  207. negate_Xsig(&accumulator);
  208. if (accumulator.lsw & 0x80000000)
  209. XSIG_LL(accumulator)++;
  210. if (accumulator.msw == 0) {
  211. /* The result is 1.0 */
  212. FPU_copy_to_reg0(&CONST_1, TAG_Valid);
  213. return;
  214. } else {
  215. significand(&result) = XSIG_LL(accumulator);
  216. /* will be a valid positive nr with expon = -1 */
  217. setexponentpos(&result, -1);
  218. }
  219. } else {
  220. fixed_arg = significand(st0_ptr);
  221. if (exponent == 0) {
  222. /* The argument is >= 1.0 */
  223. /* Put the binary point at the left. */
  224. fixed_arg <<= 1;
  225. }
  226. /* pi/2 in hex is: 1.921fb54442d18469 898CC51701B839A2 52049C1 */
  227. fixed_arg = 0x921fb54442d18469LL - fixed_arg;
  228. /* There is a special case which arises due to rounding, to fix here. */
  229. if (fixed_arg == 0xffffffffffffffffLL)
  230. fixed_arg = 0;
  231. exponent = -1;
  232. exp2 = -1;
  233. /* A shift is needed here only for a narrow range of arguments,
  234. i.e. for fixed_arg approx 2^-32, but we pick up more... */
  235. if (!(LL_MSW(fixed_arg) & 0xffff0000)) {
  236. fixed_arg <<= 16;
  237. exponent -= 16;
  238. exp2 -= 16;
  239. }
  240. XSIG_LL(argSqrd) = fixed_arg;
  241. argSqrd.lsw = 0;
  242. mul64_Xsig(&argSqrd, &fixed_arg);
  243. if (exponent < -1) {
  244. /* shift the argument right by the required places */
  245. shr_Xsig(&argSqrd, 2 * (-1 - exponent));
  246. }
  247. argTo4.msw = argSqrd.msw;
  248. argTo4.midw = argSqrd.midw;
  249. argTo4.lsw = argSqrd.lsw;
  250. mul_Xsig_Xsig(&argTo4, &argTo4);
  251. polynomial_Xsig(&accumulator, &XSIG_LL(argTo4), neg_terms_l,
  252. N_COEFF_N - 1);
  253. mul_Xsig_Xsig(&accumulator, &argSqrd);
  254. negate_Xsig(&accumulator);
  255. polynomial_Xsig(&accumulator, &XSIG_LL(argTo4), pos_terms_l,
  256. N_COEFF_P - 1);
  257. shr_Xsig(&accumulator, 2); /* Divide by four */
  258. accumulator.msw |= 0x80000000; /* Add 1.0 */
  259. mul64_Xsig(&accumulator, &fixed_arg);
  260. mul64_Xsig(&accumulator, &fixed_arg);
  261. mul64_Xsig(&accumulator, &fixed_arg);
  262. /* Divide by four, FPU_REG compatible, etc */
  263. exponent = 3 * exponent;
  264. /* The minimum exponent difference is 3 */
  265. shr_Xsig(&accumulator, exp2 - exponent);
  266. negate_Xsig(&accumulator);
  267. XSIG_LL(accumulator) += fixed_arg;
  268. /* The basic computation is complete. Now fix the answer to
  269. compensate for the error due to the approximation used for
  270. pi/2
  271. */
  272. /* This has an exponent of -65 */
  273. XSIG_LL(fix_up) = 0x898cc51701b839a2ll;
  274. fix_up.lsw = 0;
  275. /* The fix-up needs to be improved for larger args */
  276. if (argSqrd.msw & 0xffc00000) {
  277. /* Get about 32 bit precision in these: */
  278. fix_up.msw -= mul_32_32(0x898cc517, argSqrd.msw) / 2;
  279. fix_up.msw += mul_32_32(0x898cc517, argTo4.msw) / 24;
  280. }
  281. exp2 += norm_Xsig(&accumulator);
  282. shr_Xsig(&accumulator, 1); /* Prevent overflow */
  283. exp2++;
  284. shr_Xsig(&fix_up, 65 + exp2);
  285. add_Xsig_Xsig(&accumulator, &fix_up);
  286. echange = round_Xsig(&accumulator);
  287. setexponentpos(&result, exp2 + echange);
  288. significand(&result) = XSIG_LL(accumulator);
  289. }
  290. FPU_copy_to_reg0(&result, TAG_Valid);
  291. #ifdef PARANOID
  292. if ((exponent(&result) >= 0)
  293. && (significand(&result) > 0x8000000000000000LL)) {
  294. EXCEPTION(EX_INTERNAL | 0x151);
  295. }
  296. #endif /* PARANOID */
  297. }