poly_sin.c 11 KB

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