1029 lines
25 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstring>
  14. #include <cstdio>
  15. #include "core.h"
  16. using namespace std;
  17. namespace lol
  18. {
  19. real::real(float f) { *this = (double)f; }
  20. real::real(int i) { *this = (double)i; }
  21. real::real(unsigned int i) { *this = (double)i; }
  22. real::real(double d)
  23. {
  24. union { double d; uint64_t x; } u = { d };
  25. uint32_t sign = (u.x >> 63) << 31;
  26. uint32_t exponent = (u.x << 1) >> 53;
  27. switch (exponent)
  28. {
  29. case 0x00:
  30. m_signexp = sign;
  31. break;
  32. case 0x7ff:
  33. m_signexp = sign | 0x7fffffffu;
  34. break;
  35. default:
  36. m_signexp = sign | (exponent + (1 << 30) - (1 << 10));
  37. break;
  38. }
  39. m_mantissa[0] = u.x >> 20;
  40. m_mantissa[1] = u.x << 12;
  41. memset(m_mantissa + 2, 0, (BIGITS - 2) * sizeof(m_mantissa[0]));
  42. }
  43. real::operator float() const { return (float)(double)(*this); }
  44. real::operator int() const { return (int)(double)(*this); }
  45. real::operator unsigned int() const { return (unsigned int)(double)(*this); }
  46. real::operator double() const
  47. {
  48. union { double d; uint64_t x; } u;
  49. /* Get sign */
  50. u.x = m_signexp >> 31;
  51. u.x <<= 11;
  52. /* Compute new exponent */
  53. uint32_t exponent = (m_signexp << 1) >> 1;
  54. int e = (int)exponent - (1 << 30) + (1 << 10);
  55. if (e < 0)
  56. u.x <<= 52;
  57. else if (e >= 0x7ff)
  58. {
  59. u.x |= 0x7ff;
  60. u.x <<= 52;
  61. }
  62. else
  63. {
  64. u.x |= e;
  65. /* Store mantissa if necessary */
  66. u.x <<= 32;
  67. u.x |= m_mantissa[0];
  68. u.x <<= 20;
  69. u.x |= m_mantissa[1] >> 12;
  70. /* Rounding */
  71. u.x += (m_mantissa[1] >> 11) & 1;
  72. }
  73. return u.d;
  74. }
  75. real real::operator +() const
  76. {
  77. return *this;
  78. }
  79. real real::operator -() const
  80. {
  81. real ret = *this;
  82. ret.m_signexp ^= 0x80000000u;
  83. return ret;
  84. }
  85. real real::operator +(real const &x) const
  86. {
  87. if (x.m_signexp << 1 == 0)
  88. return *this;
  89. /* Ensure both arguments are positive. Otherwise, switch signs,
  90. * or replace + with -. */
  91. if (m_signexp >> 31)
  92. return -(-*this + -x);
  93. if (x.m_signexp >> 31)
  94. return *this - (-x);
  95. /* Ensure *this has the larger exponent (no need for the mantissa to
  96. * be larger, as in subtraction). Otherwise, switch. */
  97. if ((m_signexp << 1) < (x.m_signexp << 1))
  98. return x + *this;
  99. real ret;
  100. int e1 = m_signexp - (1 << 30) + 1;
  101. int e2 = x.m_signexp - (1 << 30) + 1;
  102. int bigoff = (e1 - e2) / BIGIT_BITS;
  103. int off = e1 - e2 - bigoff * BIGIT_BITS;
  104. if (bigoff > BIGITS)
  105. return *this;
  106. ret.m_signexp = m_signexp;
  107. uint64_t carry = 0;
  108. for (int i = BIGITS; i--; )
  109. {
  110. carry += m_mantissa[i];
  111. if (i - bigoff >= 0)
  112. carry += x.m_mantissa[i - bigoff] >> off;
  113. if (off && i - bigoff > 0)
  114. carry += (x.m_mantissa[i - bigoff - 1] << (BIGIT_BITS - off)) & 0xffffffffu;
  115. else if (i - bigoff == 0)
  116. carry += (uint64_t)1 << (BIGIT_BITS - off);
  117. ret.m_mantissa[i] = carry;
  118. carry >>= BIGIT_BITS;
  119. }
  120. /* Renormalise in case we overflowed the mantissa */
  121. if (carry)
  122. {
  123. carry--;
  124. for (int i = 0; i < BIGITS; i++)
  125. {
  126. uint32_t tmp = ret.m_mantissa[i];
  127. ret.m_mantissa[i] = (carry << (BIGIT_BITS - 1)) | (tmp >> 1);
  128. carry = tmp & 1u;
  129. }
  130. ret.m_signexp++;
  131. }
  132. return ret;
  133. }
  134. real real::operator -(real const &x) const
  135. {
  136. if (x.m_signexp << 1 == 0)
  137. return *this;
  138. /* Ensure both arguments are positive. Otherwise, switch signs,
  139. * or replace - with +. */
  140. if (m_signexp >> 31)
  141. return -(-*this + x);
  142. if (x.m_signexp >> 31)
  143. return (*this) + (-x);
  144. /* Ensure *this is larger than x */
  145. if (*this < x)
  146. return -(x - *this);
  147. real ret;
  148. int e1 = m_signexp - (1 << 30) + 1;
  149. int e2 = x.m_signexp - (1 << 30) + 1;
  150. int bigoff = (e1 - e2) / BIGIT_BITS;
  151. int off = e1 - e2 - bigoff * BIGIT_BITS;
  152. if (bigoff > BIGITS)
  153. return *this;
  154. ret.m_signexp = m_signexp;
  155. int64_t carry = 0;
  156. for (int i = 0; i < bigoff; i++)
  157. {
  158. carry -= x.m_mantissa[BIGITS - i];
  159. /* Emulates a signed shift */
  160. carry >>= BIGIT_BITS;
  161. carry |= carry << BIGIT_BITS;
  162. }
  163. carry -= x.m_mantissa[BIGITS - 1 - bigoff] & (((int64_t)1 << off) - 1);
  164. carry /= (int64_t)1 << off;
  165. for (int i = BIGITS; i--; )
  166. {
  167. carry += m_mantissa[i];
  168. if (i - bigoff >= 0)
  169. carry -= x.m_mantissa[i - bigoff] >> off;
  170. if (off && i - bigoff > 0)
  171. carry -= (x.m_mantissa[i - bigoff - 1] << (BIGIT_BITS - off)) & 0xffffffffu;
  172. else if (i - bigoff == 0)
  173. carry -= (uint64_t)1 << (BIGIT_BITS - off);
  174. ret.m_mantissa[i] = carry;
  175. carry >>= BIGIT_BITS;
  176. carry |= carry << BIGIT_BITS;
  177. }
  178. carry += 1;
  179. /* Renormalise if we underflowed the mantissa */
  180. if (carry == 0)
  181. {
  182. /* How much do we need to shift the mantissa? FIXME: this could
  183. * be computed above */
  184. off = 0;
  185. for (int i = 0; i < BIGITS; i++)
  186. {
  187. if (!ret.m_mantissa[i])
  188. {
  189. off += BIGIT_BITS;
  190. continue;
  191. }
  192. for (uint32_t tmp = ret.m_mantissa[i]; tmp < 0x80000000u; tmp <<= 1)
  193. off++;
  194. break;
  195. }
  196. if (off == BIGITS * BIGIT_BITS)
  197. ret.m_signexp &= 0x80000000u;
  198. else
  199. {
  200. off++; /* Shift one more to get rid of the leading one */
  201. ret.m_signexp -= off;
  202. bigoff = off / BIGIT_BITS;
  203. off -= bigoff * BIGIT_BITS;
  204. for (int i = 0; i < BIGITS; i++)
  205. {
  206. uint32_t tmp = 0;
  207. if (i + bigoff < BIGITS)
  208. tmp |= ret.m_mantissa[i + bigoff] << off;
  209. if (off && i + bigoff + 1 < BIGITS)
  210. tmp |= ret.m_mantissa[i + bigoff + 1] >> (BIGIT_BITS - off);
  211. ret.m_mantissa[i] = tmp;
  212. }
  213. }
  214. }
  215. return ret;
  216. }
  217. real real::operator *(real const &x) const
  218. {
  219. real ret;
  220. if (m_signexp << 1 == 0 || x.m_signexp << 1 == 0)
  221. {
  222. ret = (m_signexp << 1 == 0) ? *this : x;
  223. ret.m_signexp ^= x.m_signexp & 0x80000000u;
  224. return ret;
  225. }
  226. ret.m_signexp = (m_signexp ^ x.m_signexp) & 0x80000000u;
  227. int e = (m_signexp & 0x7fffffffu) - (1 << 30) + 1
  228. + (x.m_signexp & 0x7fffffffu) - (1 << 30) + 1;
  229. /* Accumulate low order product; no need to store it, we just
  230. * want the carry value */
  231. uint64_t carry = 0, hicarry = 0, prev;
  232. for (int i = 0; i < BIGITS; i++)
  233. {
  234. for (int j = 0; j < i + 1; j++)
  235. {
  236. prev = carry;
  237. carry += (uint64_t)m_mantissa[BIGITS - 1 - j]
  238. * (uint64_t)x.m_mantissa[BIGITS - 1 + j - i];
  239. if (carry < prev)
  240. hicarry++;
  241. }
  242. carry >>= BIGIT_BITS;
  243. carry |= hicarry << BIGIT_BITS;
  244. hicarry >>= BIGIT_BITS;
  245. }
  246. for (int i = 0; i < BIGITS; i++)
  247. {
  248. for (int j = i + 1; j < BIGITS; j++)
  249. {
  250. prev = carry;
  251. carry += (uint64_t)m_mantissa[BIGITS - 1 - j]
  252. * (uint64_t)x.m_mantissa[j - 1 - i];
  253. if (carry < prev)
  254. hicarry++;
  255. }
  256. prev = carry;
  257. carry += m_mantissa[BIGITS - 1 - i];
  258. carry += x.m_mantissa[BIGITS - 1 - i];
  259. if (carry < prev)
  260. hicarry++;
  261. ret.m_mantissa[BIGITS - 1 - i] = carry & 0xffffffffu;
  262. carry >>= BIGIT_BITS;
  263. carry |= hicarry << BIGIT_BITS;
  264. hicarry >>= BIGIT_BITS;
  265. }
  266. /* Renormalise in case we overflowed the mantissa */
  267. if (carry)
  268. {
  269. carry--;
  270. for (int i = 0; i < BIGITS; i++)
  271. {
  272. uint32_t tmp = ret.m_mantissa[i];
  273. ret.m_mantissa[i] = (carry << (BIGIT_BITS - 1)) | (tmp >> 1);
  274. carry = tmp & 1u;
  275. }
  276. e++;
  277. }
  278. ret.m_signexp |= e + (1 << 30) - 1;
  279. return ret;
  280. }
  281. real real::operator /(real const &x) const
  282. {
  283. return *this * re(x);
  284. }
  285. real &real::operator +=(real const &x)
  286. {
  287. real tmp = *this;
  288. return *this = tmp + x;
  289. }
  290. real &real::operator -=(real const &x)
  291. {
  292. real tmp = *this;
  293. return *this = tmp - x;
  294. }
  295. real &real::operator *=(real const &x)
  296. {
  297. real tmp = *this;
  298. return *this = tmp * x;
  299. }
  300. real &real::operator /=(real const &x)
  301. {
  302. real tmp = *this;
  303. return *this = tmp / x;
  304. }
  305. real real::operator <<(int x) const
  306. {
  307. real tmp = *this;
  308. return tmp <<= x;
  309. }
  310. real real::operator >>(int x) const
  311. {
  312. real tmp = *this;
  313. return tmp >>= x;
  314. }
  315. real &real::operator <<=(int x)
  316. {
  317. if (m_signexp << 1)
  318. m_signexp += x;
  319. return *this;
  320. }
  321. real &real::operator >>=(int x)
  322. {
  323. if (m_signexp << 1)
  324. m_signexp -= x;
  325. return *this;
  326. }
  327. bool real::operator ==(real const &x) const
  328. {
  329. if ((m_signexp << 1) == 0 && (x.m_signexp << 1) == 0)
  330. return true;
  331. if (m_signexp != x.m_signexp)
  332. return false;
  333. return memcmp(m_mantissa, x.m_mantissa, sizeof(m_mantissa)) == 0;
  334. }
  335. bool real::operator !=(real const &x) const
  336. {
  337. return !(*this == x);
  338. }
  339. bool real::operator <(real const &x) const
  340. {
  341. /* Ensure both numbers are positive */
  342. if (m_signexp >> 31)
  343. return (x.m_signexp >> 31) ? -*this > -x : true;
  344. if (x.m_signexp >> 31)
  345. return false;
  346. /* Compare all relevant bits */
  347. if (m_signexp != x.m_signexp)
  348. return m_signexp < x.m_signexp;
  349. for (int i = 0; i < BIGITS; i++)
  350. if (m_mantissa[i] != x.m_mantissa[i])
  351. return m_mantissa[i] < x.m_mantissa[i];
  352. return false;
  353. }
  354. bool real::operator <=(real const &x) const
  355. {
  356. return !(*this > x);
  357. }
  358. bool real::operator >(real const &x) const
  359. {
  360. /* Ensure both numbers are positive */
  361. if (m_signexp >> 31)
  362. return (x.m_signexp >> 31) ? -*this < -x : false;
  363. if (x.m_signexp >> 31)
  364. return true;
  365. /* Compare all relevant bits */
  366. if (m_signexp != x.m_signexp)
  367. return m_signexp > x.m_signexp;
  368. for (int i = 0; i < BIGITS; i++)
  369. if (m_mantissa[i] != x.m_mantissa[i])
  370. return m_mantissa[i] > x.m_mantissa[i];
  371. return false;
  372. }
  373. bool real::operator >=(real const &x) const
  374. {
  375. return !(*this < x);
  376. }
  377. bool real::operator !() const
  378. {
  379. return !(bool)*this;
  380. }
  381. real::operator bool() const
  382. {
  383. /* A real is "true" if it is non-zero (exponent is non-zero) AND
  384. * not NaN (exponent is not full bits OR higher order mantissa is zero) */
  385. uint32_t exponent = m_signexp << 1;
  386. return exponent && (~exponent || m_mantissa[0] == 0);
  387. }
  388. real re(real const &x)
  389. {
  390. if (!(x.m_signexp << 1))
  391. {
  392. real ret = x;
  393. ret.m_signexp = x.m_signexp | 0x7fffffffu;
  394. ret.m_mantissa[0] = 0;
  395. return ret;
  396. }
  397. /* Use the system's float inversion to approximate 1/x */
  398. union { float f; uint32_t x; } u = { 1.0f }, v = { 1.0f };
  399. v.x |= x.m_mantissa[0] >> 9;
  400. v.f = 1.0 / v.f;
  401. real ret;
  402. ret.m_mantissa[0] = v.x << 9;
  403. uint32_t sign = x.m_signexp & 0x80000000u;
  404. ret.m_signexp = sign;
  405. int exponent = (x.m_signexp & 0x7fffffffu) + 1;
  406. exponent = -exponent + (v.x >> 23) - (u.x >> 23);
  407. ret.m_signexp |= (exponent - 1) & 0x7fffffffu;
  408. /* FIXME: 1+log2(BIGITS) steps of Newton-Raphson seems to be enough for
  409. * convergence, but this hasn't been checked seriously. */
  410. for (int i = 1; i <= real::BIGITS; i *= 2)
  411. ret = ret * (real::R_2 - ret * x);
  412. return ret;
  413. }
  414. real sqrt(real const &x)
  415. {
  416. /* if zero, return x */
  417. if (!(x.m_signexp << 1))
  418. return x;
  419. /* if negative, return NaN */
  420. if (x.m_signexp >> 31)
  421. {
  422. real ret;
  423. ret.m_signexp = 0x7fffffffu;
  424. ret.m_mantissa[0] = 0xffffu;
  425. return ret;
  426. }
  427. /* Use the system's float inversion to approximate 1/sqrt(x). First
  428. * we construct a float in the [1..4[ range that has roughly the same
  429. * mantissa as our real. Its exponent is 0 or 1, depending on the
  430. * partity of x. The final exponent is 0, -1 or -2. We use the final
  431. * exponent and final mantissa to pre-fill the result. */
  432. union { float f; uint32_t x; } u = { 1.0f }, v = { 2.0f };
  433. v.x -= ((x.m_signexp & 1) << 23);
  434. v.x |= x.m_mantissa[0] >> 9;
  435. v.f = 1.0 / sqrtf(v.f);
  436. real ret;
  437. ret.m_mantissa[0] = v.x << 9;
  438. uint32_t sign = x.m_signexp & 0x80000000u;
  439. ret.m_signexp = sign;
  440. uint32_t exponent = (x.m_signexp & 0x7fffffffu);
  441. exponent = ((1 << 30) + (1 << 29) - 1) - (exponent + 1) / 2;
  442. exponent = exponent + (v.x >> 23) - (u.x >> 23);
  443. ret.m_signexp |= exponent & 0x7fffffffu;
  444. /* FIXME: 1+log2(BIGITS) steps of Newton-Raphson seems to be enough for
  445. * convergence, but this hasn't been checked seriously. */
  446. for (int i = 1; i <= real::BIGITS; i *= 2)
  447. {
  448. ret = ret * (real::R_3 - ret * ret * x);
  449. ret.m_signexp--;
  450. }
  451. return ret * x;
  452. }
  453. real fabs(real const &x)
  454. {
  455. real ret = x;
  456. ret.m_signexp &= 0x7fffffffu;
  457. return ret;
  458. }
  459. static real fast_log(real const &x)
  460. {
  461. /* This fast log method is tuned to work on the [1..2] range and
  462. * no effort whatsoever was made to improve convergence outside this
  463. * domain of validity. It can converge pretty fast, provided we use
  464. * the following variable substitutions:
  465. * y = sqrt(x)
  466. * z = (y - 1) / (y + 1)
  467. *
  468. * And the following identities:
  469. * ln(x) = 2 ln(y)
  470. * = 2 ln((1 + z) / (1 - z))
  471. * = 4 z (1 + z^2 / 3 + z^4 / 5 + z^6 / 7...)
  472. *
  473. * Any additional sqrt() call would halve the convergence time, but
  474. * would also impact the final precision. For now we stick with one
  475. * sqrt() call. */
  476. real y = sqrt(x);
  477. real z = (y - real::R_1) / (y + real::R_1), z2 = z * z, zn = z2;
  478. real sum = real::R_1;
  479. for (int i = 3; ; i += 2)
  480. {
  481. real newsum = sum + zn / (real)i;
  482. if (newsum == sum)
  483. break;
  484. sum = newsum;
  485. zn *= z2;
  486. }
  487. return z * (sum << 2);
  488. }
  489. real log(real const &x)
  490. {
  491. /* Strategy for log(x): if x = 2^E*M then log(x) = E log(2) + log(M),
  492. * with the property that M is in [1..2[, so fast_log() applies here. */
  493. real tmp = x;
  494. if (x.m_signexp >> 31 || x.m_signexp == 0)
  495. {
  496. tmp.m_signexp = 0xffffffffu;
  497. tmp.m_mantissa[0] = 0xffffffffu;
  498. return tmp;
  499. }
  500. tmp.m_signexp = (1 << 30) - 1;
  501. return (real)(x.m_signexp - (1 << 30) + 1) * real::R_LN2 + fast_log(tmp);
  502. }
  503. real exp(real const &x)
  504. {
  505. /* Strategy for exp(x): the Taylor series does not converge very fast
  506. * with large positive or negative values.
  507. *
  508. * However, we know that the result is going to be in the form M*2^E,
  509. * where M is the mantissa and E the exponent. We first try to predict
  510. * a value for E, which is approximately log2(exp(x)) = x / log(2).
  511. *
  512. * Let E0 be an integer close to x / log(2). We need to find a value x0
  513. * such that exp(x) = 2^E0 * exp(x0). We get x0 = x - E0 log(2).
  514. *
  515. * Thus the final algorithm:
  516. * int E0 = x / log(2)
  517. * real x0 = x - E0 log(2)
  518. * real x1 = exp(x0)
  519. * return x1 * 2^E0
  520. */
  521. int e0 = x / real::R_LN2;
  522. real x0 = x - (real)e0 * real::R_LN2;
  523. real x1 = real::R_1, fact = real::R_1, xn = x0;
  524. for (int i = 1; ; i++)
  525. {
  526. fact *= (real)i;
  527. real newx1 = x1 + xn / fact;
  528. if (newx1 == x1)
  529. break;
  530. x1 = newx1;
  531. xn *= x0;
  532. }
  533. x1.m_signexp += e0;
  534. return x1;
  535. }
  536. real floor(real const &x)
  537. {
  538. /* Strategy for floor(x):
  539. * - if negative, return -ceil(-x)
  540. * - if zero or negative zero, return x
  541. * - if less than one, return zero
  542. * - otherwise, if e is the exponent, clear all bits except the
  543. * first e. */
  544. if (x < -real::R_0)
  545. return -ceil(-x);
  546. if (!x)
  547. return x;
  548. if (x < real::R_1)
  549. return real::R_0;
  550. real ret = x;
  551. int exponent = x.m_signexp - (1 << 30) + 1;
  552. for (int i = 0; i < real::BIGITS; i++)
  553. {
  554. if (exponent <= 0)
  555. ret.m_mantissa[i] = 0;
  556. else if (exponent < real::BIGIT_BITS)
  557. ret.m_mantissa[i] &= ~((1 << (real::BIGIT_BITS - exponent)) - 1);
  558. exponent -= real::BIGIT_BITS;
  559. }
  560. return ret;
  561. }
  562. real ceil(real const &x)
  563. {
  564. /* Strategy for ceil(x):
  565. * - if negative, return -floor(-x)
  566. * - if x == floor(x), return x
  567. * - otherwise, return floor(x) + 1 */
  568. if (x < -real::R_0)
  569. return -floor(-x);
  570. real ret = floor(x);
  571. if (x == ret)
  572. return ret;
  573. else
  574. return ret + real::R_1;
  575. }
  576. real round(real const &x)
  577. {
  578. if (x < real::R_0)
  579. return -round(-x);
  580. return floor(x + (real::R_1 >> 1));
  581. }
  582. real fmod(real const &x, real const &y)
  583. {
  584. if (!y)
  585. return real::R_0; /* FIXME: return NaN */
  586. if (!x)
  587. return x;
  588. real tmp = round(x / y);
  589. return x - tmp * y;
  590. }
  591. real sin(real const &x)
  592. {
  593. bool switch_sign = x.m_signexp & 0x80000000u;
  594. real absx = fmod(fabs(x), real::R_PI << 1);
  595. if (absx > real::R_PI)
  596. {
  597. absx -= real::R_PI;
  598. switch_sign = !switch_sign;
  599. }
  600. if (absx > real::R_PI_2)
  601. absx = real::R_PI - absx;
  602. real ret = real::R_0, fact = real::R_1, xn = absx, x2 = absx * absx;
  603. for (int i = 1; ; i += 2)
  604. {
  605. real newret = ret + xn / fact;
  606. if (newret == ret)
  607. break;
  608. ret = newret;
  609. xn *= x2;
  610. fact *= (real)(-(i + 1) * (i + 2));
  611. }
  612. /* Propagate sign */
  613. if (switch_sign)
  614. ret.m_signexp ^= 0x80000000u;
  615. return ret;
  616. }
  617. real cos(real const &x)
  618. {
  619. return sin(real::R_PI_2 - x);
  620. }
  621. real tan(real const &x)
  622. {
  623. /* Constrain input to [-π,π] */
  624. real y = fmod(x, real::R_PI);
  625. /* Constrain input to [-π/2,π/2] */
  626. if (y < -real::R_PI_2)
  627. y += real::R_PI;
  628. else if (y > real::R_PI_2)
  629. y -= real::R_PI;
  630. /* In [-π/4,π/4] return sin/cos */
  631. if (fabs(y) <= real::R_PI_4)
  632. return sin(y) / cos(y);
  633. /* Otherwise, return cos/sin */
  634. if (y > real::R_0)
  635. y = real::R_PI_2 - y;
  636. else
  637. y = -real::R_PI_2 - y;
  638. return cos(y) / sin(y);
  639. }
  640. static real asinacos(real const &x, bool is_asin, bool is_negative)
  641. {
  642. /* Strategy for asin(): in [-0.5..0.5], use a Taylor series around
  643. * zero. In [0.5..1], use asin(x) = π/2 - 2*asin(sqrt((1-x)/2)), and
  644. * in [-1..-0.5] just revert the sign.
  645. * Strategy for acos(): use acos(x) = π/2 - asin(x) and try not to
  646. * lose the precision around x=1. */
  647. real absx = fabs(x);
  648. bool around_zero = (absx < (real::R_1 >> 1));
  649. if (!around_zero)
  650. absx = sqrt((real::R_1 - absx) >> 1);
  651. real ret = absx, xn = absx, x2 = absx * absx, fact1 = 2, fact2 = 1;
  652. for (int i = 1; ; i++)
  653. {
  654. xn *= x2;
  655. real mul = (real)(2 * i + 1);
  656. real newret = ret + ((fact1 * xn / (mul * fact2)) >> (i * 2));
  657. if (newret == ret)
  658. break;
  659. ret = newret;
  660. fact1 *= (real)((2 * i + 1) * (2 * i + 2));
  661. fact2 *= (real)((i + 1) * (i + 1));
  662. }
  663. if (is_negative)
  664. ret = -ret;
  665. if (around_zero)
  666. ret = is_asin ? ret : real::R_PI_2 - ret;
  667. else
  668. {
  669. real adjust = is_negative ? real::R_PI : real::R_0;
  670. if (is_asin)
  671. ret = real::R_PI_2 - adjust - (ret << 1);
  672. else
  673. ret = adjust + (ret << 1);
  674. }
  675. return ret;
  676. }
  677. real asin(real const &x)
  678. {
  679. return asinacos(x, true, x.m_signexp >> 31);
  680. }
  681. real acos(real const &x)
  682. {
  683. return asinacos(x, false, x.m_signexp >> 31);
  684. }
  685. real atan(real const &x)
  686. {
  687. /* Computing atan(x): we choose a different Taylor series depending on
  688. * the value of x to help with convergence.
  689. *
  690. * If |x| < 0.5 we evaluate atan(y) near 0:
  691. * atan(y) = y - y^3/3 + y^5/5 - y^7/7 + y^9/9 ...
  692. *
  693. * If 0.5 <= |x| < 1.5 we evaluate atan(1+y) near 0:
  694. * atan(1+y) = π/4 + y/(1*2^1) - y^2/(2*2^1) + y^3/(3*2^2)
  695. * - y^5/(5*2^3) + y^6/(6*2^3) - y^7/(7*2^4)
  696. * + y^9/(9*2^5) - y^10/(10*2^5) + y^11/(11*2^6) ...
  697. *
  698. * If 1.5 <= |x| < 2 we evaluate atan(sqrt(3)+2y) near 0:
  699. * atan(sqrt(3)+2y) = π/3 + 1/2 y - sqrt(3)/2 y^2/2
  700. * + y^3/3 - sqrt(3)/2 y^4/4 + 1/2 y^5/5
  701. * - 1/2 y^7/7 + sqrt(3)/2 y^8/8
  702. * - y^9/9 + sqrt(3)/2 y^10/10 - 1/2 y^11/11
  703. * + 1/2 y^13/13 - sqrt(3)/2 y^14/14
  704. * + y^15/15 - sqrt(3)/2 y^16/16 + 1/2 y^17/17 ...
  705. *
  706. * If |x| >= 2 we evaluate atan(y) near +∞:
  707. * atan(y) = π/2 - y^-1 + y^-3/3 - y^-5/5 + y^-7/7 - y^-9/9 ...
  708. */
  709. real absx = fabs(x);
  710. if (absx < (real::R_1 >> 1))
  711. {
  712. real ret = x, xn = x, mx2 = -x * x;
  713. for (int i = 3; ; i += 2)
  714. {
  715. xn *= mx2;
  716. real newret = ret + xn / (real)i;
  717. if (newret == ret)
  718. break;
  719. ret = newret;
  720. }
  721. return ret;
  722. }
  723. real ret = 0;
  724. if (absx < (real::R_3 >> 1))
  725. {
  726. real y = real::R_1 - absx;
  727. real yn = y, my2 = -y * y;
  728. for (int i = 0; ; i += 2)
  729. {
  730. real newret = ret + ((yn / (real)(2 * i + 1)) >> (i + 1));
  731. yn *= y;
  732. newret += (yn / (real)(2 * i + 2)) >> (i + 1);
  733. yn *= y;
  734. newret += (yn / (real)(2 * i + 3)) >> (i + 2);
  735. if (newret == ret)
  736. break;
  737. ret = newret;
  738. yn *= my2;
  739. }
  740. ret = real::R_PI_4 - ret;
  741. }
  742. else if (absx < real::R_2)
  743. {
  744. real y = (absx - real::R_SQRT3) >> 1;
  745. real yn = y, my2 = -y * y;
  746. for (int i = 1; ; i += 6)
  747. {
  748. real newret = ret + ((yn / (real)i) >> 1);
  749. yn *= y;
  750. newret -= (real::R_SQRT3 >> 1) * yn / (real)(i + 1);
  751. yn *= y;
  752. newret += yn / (real)(i + 2);
  753. yn *= y;
  754. newret -= (real::R_SQRT3 >> 1) * yn / (real)(i + 3);
  755. yn *= y;
  756. newret += (yn / (real)(i + 4)) >> 1;
  757. if (newret == ret)
  758. break;
  759. ret = newret;
  760. yn *= my2;
  761. }
  762. ret = real::R_PI_3 + ret;
  763. }
  764. else
  765. {
  766. real y = re(absx);
  767. real yn = y, my2 = -y * y;
  768. ret = y;
  769. for (int i = 3; ; i += 2)
  770. {
  771. yn *= my2;
  772. real newret = ret + yn / (real)i;
  773. if (newret == ret)
  774. break;
  775. ret = newret;
  776. }
  777. ret = real::R_PI_2 - ret;
  778. }
  779. /* Propagate sign */
  780. ret.m_signexp |= (x.m_signexp & 0x80000000u);
  781. return ret;
  782. }
  783. void real::print(int ndigits) const
  784. {
  785. real const r1 = 1, r10 = 10;
  786. real x = *this;
  787. if (x.m_signexp >> 31)
  788. {
  789. printf("-");
  790. x = -x;
  791. }
  792. /* Normalise x so that mantissa is in [1..9.999] */
  793. int exponent = 0;
  794. if (x.m_signexp)
  795. {
  796. for (real div = r1, newdiv; true; div = newdiv)
  797. {
  798. newdiv = div * r10;
  799. if (x < newdiv)
  800. {
  801. x /= div;
  802. break;
  803. }
  804. exponent++;
  805. }
  806. for (real mul = 1, newx; true; mul *= r10)
  807. {
  808. newx = x * mul;
  809. if (newx >= r1)
  810. {
  811. x = newx;
  812. break;
  813. }
  814. exponent--;
  815. }
  816. }
  817. /* Print digits */
  818. for (int i = 0; i < ndigits; i++)
  819. {
  820. int digit = (int)x;
  821. printf("%i", digit);
  822. if (i == 0)
  823. printf(".");
  824. x -= real(digit);
  825. x *= r10;
  826. }
  827. /* Print exponent information */
  828. if (exponent < 0)
  829. printf("e-%i", -exponent);
  830. else if (exponent > 0)
  831. printf("e+%i", exponent);
  832. printf("\n");
  833. }
  834. static real fast_pi()
  835. {
  836. /* Approximate Pi using Machin's formula: 16*atan(1/5)-4*atan(1/239) */
  837. real ret = 0.0, x0 = 5.0, x1 = 239.0;
  838. real const m0 = -x0 * x0, m1 = -x1 * x1, r16 = 16.0, r4 = 4.0;
  839. for (int i = 1; ; i += 2)
  840. {
  841. real newret = ret + r16 / (x0 * (real)i) - r4 / (x1 * (real)i);
  842. if (newret == ret)
  843. break;
  844. ret = newret;
  845. x0 *= m0;
  846. x1 *= m1;
  847. }
  848. return ret;
  849. }
  850. real const real::R_0 = (real)0.0;
  851. real const real::R_1 = (real)1.0;
  852. real const real::R_2 = (real)2.0;
  853. real const real::R_3 = (real)3.0;
  854. real const real::R_10 = (real)10.0;
  855. real const real::R_LN2 = fast_log(R_2);
  856. real const real::R_LN10 = log(R_10);
  857. real const real::R_LOG2E = re(R_LN2);
  858. real const real::R_LOG10E = re(R_LN10);
  859. real const real::R_E = exp(R_1);
  860. real const real::R_PI = fast_pi();
  861. real const real::R_PI_2 = R_PI >> 1;
  862. real const real::R_PI_3 = R_PI / R_3;
  863. real const real::R_PI_4 = R_PI >> 2;
  864. real const real::R_1_PI = re(R_PI);
  865. real const real::R_2_PI = R_1_PI << 1;
  866. real const real::R_2_SQRTPI = re(sqrt(R_PI)) << 1;
  867. real const real::R_SQRT2 = sqrt(R_2);
  868. real const real::R_SQRT3 = sqrt(R_3);
  869. real const real::R_SQRT1_2 = R_SQRT2 >> 1;
  870. } /* namespace lol */