poly_sin.c 11 KB

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