math.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Mițca Dumitru <dumitru0mitca@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/Platform.h>
  28. #include <AK/StdLibExtras.h>
  29. #include <LibC/assert.h>
  30. #include <fenv.h>
  31. #include <math.h>
  32. #include <stdint.h>
  33. #include <stdlib.h>
  34. template<size_t>
  35. constexpr double e_to_power();
  36. template<>
  37. constexpr double e_to_power<0>() { return 1; }
  38. template<size_t exponent>
  39. constexpr double e_to_power() { return M_E * e_to_power<exponent - 1>(); }
  40. template<size_t>
  41. constexpr size_t factorial();
  42. template<>
  43. constexpr size_t factorial<0>() { return 1; }
  44. template<size_t value>
  45. constexpr size_t factorial() { return value * factorial<value - 1>(); }
  46. template<size_t>
  47. constexpr size_t product_even();
  48. template<>
  49. constexpr size_t product_even<2>() { return 2; }
  50. template<size_t value>
  51. constexpr size_t product_even() { return value * product_even<value - 2>(); }
  52. template<size_t>
  53. constexpr size_t product_odd();
  54. template<>
  55. constexpr size_t product_odd<1>() { return 1; }
  56. template<size_t value>
  57. constexpr size_t product_odd() { return value * product_odd<value - 2>(); }
  58. enum class RoundingMode {
  59. ToZero = FE_TOWARDZERO,
  60. Up = FE_UPWARD,
  61. Down = FE_DOWNWARD,
  62. ToEven = FE_TONEAREST
  63. };
  64. template<typename T>
  65. union FloatExtractor;
  66. #if ARCH(I386) || ARCH(X86_64)
  67. // This assumes long double is 80 bits, which is true with GCC on Intel platforms
  68. template<>
  69. union FloatExtractor<long double> {
  70. static const int mantissa_bits = 64;
  71. static const unsigned long long mantissa_max = ~0u;
  72. static const int exponent_bias = 16383;
  73. static const int exponent_bits = 15;
  74. static const unsigned exponent_max = 32767;
  75. struct {
  76. unsigned long long mantissa;
  77. unsigned exponent : 15;
  78. unsigned sign : 1;
  79. };
  80. long double d;
  81. };
  82. #endif
  83. template<>
  84. union FloatExtractor<double> {
  85. static const int mantissa_bits = 52;
  86. static const unsigned long long mantissa_max = (1ull << 52) - 1;
  87. static const int exponent_bias = 1023;
  88. static const int exponent_bits = 11;
  89. static const unsigned exponent_max = 2047;
  90. struct {
  91. unsigned long long mantissa : 52;
  92. unsigned exponent : 11;
  93. unsigned sign : 1;
  94. };
  95. double d;
  96. };
  97. template<>
  98. union FloatExtractor<float> {
  99. static const int mantissa_bits = 23;
  100. static const unsigned mantissa_max = (1 << 23) - 1;
  101. static const int exponent_bias = 127;
  102. static const int exponent_bits = 8;
  103. static const unsigned exponent_max = 255;
  104. struct {
  105. unsigned long long mantissa : 23;
  106. unsigned exponent : 8;
  107. unsigned sign : 1;
  108. };
  109. float d;
  110. };
  111. // This is much branchier than it really needs to be
  112. template<typename FloatType>
  113. static FloatType internal_to_integer(FloatType x, RoundingMode rounding_mode)
  114. {
  115. if (!isfinite(x))
  116. return x;
  117. using Extractor = FloatExtractor<decltype(x)>;
  118. Extractor extractor;
  119. extractor.d = x;
  120. auto unbiased_exponent = extractor.exponent - Extractor::exponent_bias;
  121. bool round = false;
  122. bool guard = false;
  123. if (unbiased_exponent < 0) {
  124. // it was easier to special case [0..1) as it saves us from
  125. // handling subnormals, underflows, etc
  126. if (unbiased_exponent == -1) {
  127. round = true;
  128. }
  129. guard = extractor.mantissa != 0;
  130. extractor.mantissa = 0;
  131. extractor.exponent = 0;
  132. } else {
  133. if (unbiased_exponent >= Extractor::mantissa_bits)
  134. return x;
  135. auto dead_bitcount = Extractor::mantissa_bits - unbiased_exponent;
  136. auto dead_mask = (1ull << dead_bitcount) - 1;
  137. auto dead_bits = extractor.mantissa & dead_mask;
  138. extractor.mantissa &= ~dead_mask;
  139. auto guard_mask = dead_mask >> 1;
  140. guard = (dead_bits & guard_mask) != 0;
  141. round = (dead_bits & ~guard_mask) != 0;
  142. }
  143. bool should_round = false;
  144. switch (rounding_mode) {
  145. case RoundingMode::ToEven:
  146. should_round = round;
  147. break;
  148. case RoundingMode::Up:
  149. if (!extractor.sign)
  150. should_round = guard || round;
  151. break;
  152. case RoundingMode::Down:
  153. if (extractor.sign)
  154. should_round = guard || round;
  155. break;
  156. case RoundingMode::ToZero:
  157. break;
  158. }
  159. if (should_round) {
  160. // We could do this ourselves, but this saves us from manually
  161. // handling overflow.
  162. if (extractor.sign)
  163. extractor.d -= 1.0;
  164. else
  165. extractor.d += 1.0;
  166. }
  167. return extractor.d;
  168. }
  169. // This is much branchier than it really needs to be
  170. template<typename FloatType>
  171. static FloatType internal_nextafter(FloatType x, bool up)
  172. {
  173. if (!isfinite(x))
  174. return x;
  175. using Extractor = FloatExtractor<decltype(x)>;
  176. Extractor extractor;
  177. extractor.d = x;
  178. if (x == 0) {
  179. if (!extractor.sign) {
  180. extractor.mantissa = 1;
  181. extractor.sign = !up;
  182. return extractor.d;
  183. }
  184. if (up) {
  185. extractor.sign = false;
  186. extractor.mantissa = 1;
  187. return extractor.d;
  188. }
  189. extractor.mantissa = 1;
  190. extractor.sign = up != extractor.sign;
  191. return extractor.d;
  192. }
  193. if (up != extractor.sign) {
  194. extractor.mantissa++;
  195. if (!extractor.mantissa) {
  196. // no need to normalize the mantissa as we just hit a power
  197. // of two.
  198. extractor.exponent++;
  199. if (extractor.exponent == Extractor::exponent_max) {
  200. extractor.exponent = Extractor::exponent_max - 1;
  201. extractor.mantissa = Extractor::mantissa_max;
  202. }
  203. }
  204. return extractor.d;
  205. }
  206. if (!extractor.mantissa) {
  207. if (extractor.exponent) {
  208. extractor.exponent--;
  209. extractor.mantissa = Extractor::mantissa_max;
  210. } else {
  211. extractor.d = 0;
  212. }
  213. return extractor.d;
  214. }
  215. extractor.mantissa--;
  216. if (extractor.mantissa != Extractor::mantissa_max)
  217. return extractor.d;
  218. if (extractor.exponent) {
  219. extractor.exponent--;
  220. // normalize
  221. extractor.mantissa <<= 1;
  222. } else {
  223. if (extractor.sign) {
  224. // Negative infinity
  225. extractor.mantissa = 0;
  226. extractor.exponent = Extractor::exponent_max;
  227. }
  228. }
  229. return extractor.d;
  230. }
  231. template<typename FloatT>
  232. static int internal_ilogb(FloatT x) NOEXCEPT
  233. {
  234. if (x == 0)
  235. return FP_ILOGB0;
  236. if (isnan(x))
  237. return FP_ILOGNAN;
  238. if (!isfinite(x))
  239. return INT_MAX;
  240. using Extractor = FloatExtractor<FloatT>;
  241. Extractor extractor;
  242. extractor.d = x;
  243. return (int)extractor.exponent - Extractor::exponent_bias;
  244. }
  245. template<typename FloatT>
  246. static FloatT internal_scalbn(FloatT x, int exponent) NOEXCEPT
  247. {
  248. if (x == 0 || !isfinite(x) || isnan(x) || exponent == 0)
  249. return x;
  250. using Extractor = FloatExtractor<FloatT>;
  251. Extractor extractor;
  252. extractor.d = x;
  253. if (extractor.exponent != 0) {
  254. extractor.exponent = clamp((int)extractor.exponent + exponent, 0, (int)Extractor::exponent_max);
  255. return extractor.d;
  256. }
  257. unsigned leading_mantissa_zeroes = extractor.mantissa == 0 ? 32 : __builtin_clz(extractor.mantissa);
  258. int shift = min((int)leading_mantissa_zeroes, exponent);
  259. exponent = max(exponent - shift, 0);
  260. extractor.exponent <<= shift;
  261. extractor.exponent = exponent + 1;
  262. return extractor.d;
  263. }
  264. template<typename FloatT>
  265. static FloatT internal_copysign(FloatT x, FloatT y) NOEXCEPT
  266. {
  267. using Extractor = FloatExtractor<FloatT>;
  268. Extractor ex, ey;
  269. ex.d = x;
  270. ey.d = y;
  271. ex.sign = ey.sign;
  272. return ex.d;
  273. }
  274. extern "C" {
  275. float nanf(const char* s) NOEXCEPT
  276. {
  277. return __builtin_nanf(s);
  278. }
  279. double nan(const char* s) NOEXCEPT
  280. {
  281. return __builtin_nan(s);
  282. }
  283. long double nanl(const char* s) NOEXCEPT
  284. {
  285. return __builtin_nanl(s);
  286. }
  287. double trunc(double x) NOEXCEPT
  288. {
  289. return internal_to_integer(x, RoundingMode::ToZero);
  290. }
  291. float truncf(float x) NOEXCEPT
  292. {
  293. return internal_to_integer(x, RoundingMode::ToZero);
  294. }
  295. long double truncl(long double x) NOEXCEPT
  296. {
  297. return internal_to_integer(x, RoundingMode::ToZero);
  298. }
  299. double cos(double angle) NOEXCEPT
  300. {
  301. return sin(angle + M_PI_2);
  302. }
  303. float cosf(float angle) NOEXCEPT
  304. {
  305. return sinf(angle + M_PI_2);
  306. }
  307. // This can also be done with a taylor expansion, but for
  308. // now this works pretty well (and doesn't mess anything up
  309. // in quake in particular, which is very Floating-Point precision
  310. // heavy)
  311. double sin(double angle) NOEXCEPT
  312. {
  313. double ret = 0.0;
  314. __asm__(
  315. "fsin"
  316. : "=t"(ret)
  317. : "0"(angle));
  318. return ret;
  319. }
  320. float sinf(float angle) NOEXCEPT
  321. {
  322. float ret = 0.0f;
  323. __asm__(
  324. "fsin"
  325. : "=t"(ret)
  326. : "0"(angle));
  327. return ret;
  328. }
  329. double pow(double x, double y) NOEXCEPT
  330. {
  331. // FIXME: Please fix me. I am naive.
  332. if (isnan(y))
  333. return y;
  334. if (y == 0)
  335. return 1;
  336. if (x == 0)
  337. return 0;
  338. if (y == 1)
  339. return x;
  340. int y_as_int = (int)y;
  341. if (y == (double)y_as_int) {
  342. double result = x;
  343. for (int i = 0; i < fabs(y) - 1; ++i)
  344. result *= x;
  345. if (y < 0)
  346. result = 1.0 / result;
  347. return result;
  348. }
  349. return exp2(y * log2(x));
  350. }
  351. float powf(float x, float y) NOEXCEPT
  352. {
  353. return (float)pow(x, y);
  354. }
  355. // On systems where FLT_RADIX == 2, ldexp is equivalent to scalbn
  356. long double ldexpl(long double x, int exp) NOEXCEPT
  357. {
  358. return internal_scalbn(x, exp);
  359. }
  360. double ldexp(double x, int exp) NOEXCEPT
  361. {
  362. return internal_scalbn(x, exp);
  363. }
  364. float ldexpf(float x, int exp) NOEXCEPT
  365. {
  366. return internal_scalbn(x, exp);
  367. }
  368. double tanh(double x) NOEXCEPT
  369. {
  370. if (x > 0) {
  371. double exponentiated = exp(2 * x);
  372. return (exponentiated - 1) / (exponentiated + 1);
  373. }
  374. double plusX = exp(x);
  375. double minusX = 1 / plusX;
  376. return (plusX - minusX) / (plusX + minusX);
  377. }
  378. static double ampsin(double angle) NOEXCEPT
  379. {
  380. double looped_angle = fmod(M_PI + angle, M_TAU) - M_PI;
  381. double looped_angle_squared = looped_angle * looped_angle;
  382. double quadratic_term;
  383. if (looped_angle > 0) {
  384. quadratic_term = -looped_angle_squared;
  385. } else {
  386. quadratic_term = looped_angle_squared;
  387. }
  388. double linear_term = M_PI * looped_angle;
  389. return quadratic_term + linear_term;
  390. }
  391. double tan(double angle) NOEXCEPT
  392. {
  393. return ampsin(angle) / ampsin(M_PI_2 + angle);
  394. }
  395. float tanf(float angle) NOEXCEPT
  396. {
  397. return (float)tan((double)angle);
  398. }
  399. double sqrt(double x) NOEXCEPT
  400. {
  401. double res;
  402. __asm__("fsqrt"
  403. : "=t"(res)
  404. : "0"(x));
  405. return res;
  406. }
  407. float sqrtf(float x) NOEXCEPT
  408. {
  409. float res;
  410. __asm__("fsqrt"
  411. : "=t"(res)
  412. : "0"(x));
  413. return res;
  414. }
  415. double sinh(double x) NOEXCEPT
  416. {
  417. double exponentiated = exp(x);
  418. if (x > 0)
  419. return (exponentiated * exponentiated - 1) / 2 / exponentiated;
  420. return (exponentiated - 1 / exponentiated) / 2;
  421. }
  422. double log10(double x) NOEXCEPT
  423. {
  424. double ret = 0.0;
  425. __asm__(
  426. "fldlg2\n"
  427. "fld %%st(1)\n"
  428. "fyl2x\n"
  429. "fstp %%st(1)"
  430. : "=t"(ret)
  431. : "0"(x));
  432. return ret;
  433. }
  434. double log(double x) NOEXCEPT
  435. {
  436. double ret = 0.0;
  437. __asm__(
  438. "fldln2\n"
  439. "fld %%st(1)\n"
  440. "fyl2x\n"
  441. "fstp %%st(1)"
  442. : "=t"(ret)
  443. : "0"(x));
  444. return ret;
  445. }
  446. float logf(float x) NOEXCEPT
  447. {
  448. return (float)log(x);
  449. }
  450. double fmod(double index, double period) NOEXCEPT
  451. {
  452. return index - trunc(index / period) * period;
  453. }
  454. float fmodf(float index, float period) NOEXCEPT
  455. {
  456. return index - trunc(index / period) * period;
  457. }
  458. double exp(double exponent) NOEXCEPT
  459. {
  460. double res = 0;
  461. __asm__("fldl2e\n"
  462. "fmulp\n"
  463. "fld1\n"
  464. "fld %%st(1)\n"
  465. "fprem\n"
  466. "f2xm1\n"
  467. "faddp\n"
  468. "fscale\n"
  469. "fstp %%st(1)"
  470. : "=t"(res)
  471. : "0"(exponent));
  472. return res;
  473. }
  474. float expf(float exponent) NOEXCEPT
  475. {
  476. return (float)exp(exponent);
  477. }
  478. double exp2(double exponent) NOEXCEPT
  479. {
  480. double res = 0;
  481. __asm__("fld1\n"
  482. "fld %%st(1)\n"
  483. "fprem\n"
  484. "f2xm1\n"
  485. "faddp\n"
  486. "fscale\n"
  487. "fstp %%st(1)"
  488. : "=t"(res)
  489. : "0"(exponent));
  490. return res;
  491. }
  492. float exp2f(float exponent) NOEXCEPT
  493. {
  494. return (float)exp2(exponent);
  495. }
  496. double cosh(double x) NOEXCEPT
  497. {
  498. double exponentiated = exp(-x);
  499. if (x < 0)
  500. return (1 + exponentiated * exponentiated) / 2 / exponentiated;
  501. return (1 / exponentiated + exponentiated) / 2;
  502. }
  503. double atan2(double y, double x) NOEXCEPT
  504. {
  505. if (x > 0)
  506. return atan(y / x);
  507. if (x == 0) {
  508. if (y > 0)
  509. return M_PI_2;
  510. if (y < 0)
  511. return -M_PI_2;
  512. return 0;
  513. }
  514. if (y >= 0)
  515. return atan(y / x) + M_PI;
  516. return atan(y / x) - M_PI;
  517. }
  518. float atan2f(float y, float x) NOEXCEPT
  519. {
  520. return (float)atan2(y, x);
  521. }
  522. double atan(double x) NOEXCEPT
  523. {
  524. if (x < 0)
  525. return -atan(-x);
  526. if (x > 1)
  527. return M_PI_2 - atan(1 / x);
  528. double squared = x * x;
  529. return x / (1 + 1 * 1 * squared / (3 + 2 * 2 * squared / (5 + 3 * 3 * squared / (7 + 4 * 4 * squared / (9 + 5 * 5 * squared / (11 + 6 * 6 * squared / (13 + 7 * 7 * squared)))))));
  530. }
  531. double asin(double x) NOEXCEPT
  532. {
  533. if (x > 1 || x < -1)
  534. return NAN;
  535. if (x > 0.5 || x < -0.5)
  536. return 2 * atan(x / (1 + sqrt(1 - x * x)));
  537. double squared = x * x;
  538. double value = x;
  539. double i = x * squared;
  540. value += i * product_odd<1>() / product_even<2>() / 3;
  541. i *= squared;
  542. value += i * product_odd<3>() / product_even<4>() / 5;
  543. i *= squared;
  544. value += i * product_odd<5>() / product_even<6>() / 7;
  545. i *= squared;
  546. value += i * product_odd<7>() / product_even<8>() / 9;
  547. i *= squared;
  548. value += i * product_odd<9>() / product_even<10>() / 11;
  549. i *= squared;
  550. value += i * product_odd<11>() / product_even<12>() / 13;
  551. return value;
  552. }
  553. float asinf(float x) NOEXCEPT
  554. {
  555. return (float)asin(x);
  556. }
  557. double acos(double x) NOEXCEPT
  558. {
  559. return M_PI_2 - asin(x);
  560. }
  561. float acosf(float x) NOEXCEPT
  562. {
  563. return M_PI_2 - asinf(x);
  564. }
  565. double fabs(double value) NOEXCEPT
  566. {
  567. return value < 0 ? -value : value;
  568. }
  569. int ilogbl(long double x) NOEXCEPT
  570. {
  571. return internal_ilogb(x);
  572. }
  573. int ilogb(double x) NOEXCEPT
  574. {
  575. return internal_ilogb(x);
  576. }
  577. int ilogbf(float x) NOEXCEPT
  578. {
  579. return internal_ilogb(x);
  580. }
  581. long double logbl(long double x) NOEXCEPT
  582. {
  583. return ilogbl(x);
  584. }
  585. double logb(double x) NOEXCEPT
  586. {
  587. return ilogb(x);
  588. }
  589. float logbf(float x) NOEXCEPT
  590. {
  591. return ilogbf(x);
  592. }
  593. double log2(double x) NOEXCEPT
  594. {
  595. double ret = 0.0;
  596. __asm__(
  597. "fld1\n"
  598. "fld %%st(1)\n"
  599. "fyl2x\n"
  600. "fstp %%st(1)"
  601. : "=t"(ret)
  602. : "0"(x));
  603. return ret;
  604. }
  605. float log2f(float x) NOEXCEPT
  606. {
  607. return log2(x);
  608. }
  609. long double log2l(long double x) NOEXCEPT
  610. {
  611. return log2(x);
  612. }
  613. double frexp(double x, int* exp) NOEXCEPT
  614. {
  615. *exp = (x == 0) ? 0 : (1 + ilogb(x));
  616. return scalbn(x, -(*exp));
  617. }
  618. float frexpf(float x, int* exp) NOEXCEPT
  619. {
  620. *exp = (x == 0) ? 0 : (1 + ilogbf(x));
  621. return scalbnf(x, -(*exp));
  622. }
  623. long double frexpl(long double x, int* exp) NOEXCEPT
  624. {
  625. *exp = (x == 0) ? 0 : (1 + ilogbl(x));
  626. return scalbnl(x, -(*exp));
  627. }
  628. double round(double value) NOEXCEPT
  629. {
  630. return internal_to_integer(value, RoundingMode::ToEven);
  631. }
  632. float roundf(float value) NOEXCEPT
  633. {
  634. return internal_to_integer(value, RoundingMode::ToEven);
  635. }
  636. long double roundl(long double value) NOEXCEPT
  637. {
  638. return internal_to_integer(value, RoundingMode::ToEven);
  639. }
  640. float floorf(float value) NOEXCEPT
  641. {
  642. return internal_to_integer(value, RoundingMode::Down);
  643. }
  644. double floor(double value) NOEXCEPT
  645. {
  646. return internal_to_integer(value, RoundingMode::Down);
  647. }
  648. long double floorl(long double value) NOEXCEPT
  649. {
  650. return internal_to_integer(value, RoundingMode::Down);
  651. }
  652. long double rintl(long double value) NOEXCEPT
  653. {
  654. return internal_to_integer(value, RoundingMode { fegetround() });
  655. }
  656. double rint(double value) NOEXCEPT
  657. {
  658. return internal_to_integer(value, RoundingMode { fegetround() });
  659. }
  660. float rintf(float value) NOEXCEPT
  661. {
  662. return internal_to_integer(value, RoundingMode { fegetround() });
  663. }
  664. long lrintl(long double value) NOEXCEPT
  665. {
  666. return (long)internal_to_integer(value, RoundingMode { fegetround() });
  667. }
  668. long lrint(double value) NOEXCEPT
  669. {
  670. return (long)internal_to_integer(value, RoundingMode { fegetround() });
  671. }
  672. long lrintf(float value) NOEXCEPT
  673. {
  674. return (long)internal_to_integer(value, RoundingMode { fegetround() });
  675. }
  676. long long llrintl(long double value) NOEXCEPT
  677. {
  678. return (long long)internal_to_integer(value, RoundingMode { fegetround() });
  679. }
  680. long long llrint(double value) NOEXCEPT
  681. {
  682. return (long long)internal_to_integer(value, RoundingMode { fegetround() });
  683. }
  684. long long llrintf(float value) NOEXCEPT
  685. {
  686. return (long long)internal_to_integer(value, RoundingMode { fegetround() });
  687. }
  688. float ceilf(float value) NOEXCEPT
  689. {
  690. return internal_to_integer(value, RoundingMode::Up);
  691. }
  692. double ceil(double value) NOEXCEPT
  693. {
  694. return internal_to_integer(value, RoundingMode::Up);
  695. }
  696. long double ceill(long double value) NOEXCEPT
  697. {
  698. return internal_to_integer(value, RoundingMode::Up);
  699. }
  700. double modf(double x, double* intpart) NOEXCEPT
  701. {
  702. double integer_part = internal_to_integer(x, RoundingMode::ToZero);
  703. *intpart = integer_part;
  704. auto fraction = x - integer_part;
  705. if (signbit(fraction) != signbit(x))
  706. fraction = -fraction;
  707. return fraction;
  708. }
  709. double gamma(double x) NOEXCEPT
  710. {
  711. // Stirling approximation
  712. return sqrt(2.0 * M_PI / x) * pow(x / M_E, x);
  713. }
  714. double expm1(double x) NOEXCEPT
  715. {
  716. return exp(x) - 1;
  717. }
  718. double cbrt(double x) NOEXCEPT
  719. {
  720. if (isinf(x) || x == 0)
  721. return x;
  722. if (x < 0)
  723. return -cbrt(-x);
  724. double r = x;
  725. double ex = 0;
  726. while (r < 0.125) {
  727. r *= 8;
  728. ex--;
  729. }
  730. while (r > 1.0) {
  731. r *= 0.125;
  732. ex++;
  733. }
  734. r = (-0.46946116 * r + 1.072302) * r + 0.3812513;
  735. while (ex < 0) {
  736. r *= 0.5;
  737. ex++;
  738. }
  739. while (ex > 0) {
  740. r *= 2;
  741. ex--;
  742. }
  743. r = (2.0 / 3.0) * r + (1.0 / 3.0) * x / (r * r);
  744. r = (2.0 / 3.0) * r + (1.0 / 3.0) * x / (r * r);
  745. r = (2.0 / 3.0) * r + (1.0 / 3.0) * x / (r * r);
  746. r = (2.0 / 3.0) * r + (1.0 / 3.0) * x / (r * r);
  747. return r;
  748. }
  749. double log1p(double x) NOEXCEPT
  750. {
  751. return log(1 + x);
  752. }
  753. double acosh(double x) NOEXCEPT
  754. {
  755. return log(x + sqrt(x * x - 1));
  756. }
  757. double asinh(double x) NOEXCEPT
  758. {
  759. return log(x + sqrt(x * x + 1));
  760. }
  761. double atanh(double x) NOEXCEPT
  762. {
  763. return log((1 + x) / (1 - x)) / 2.0;
  764. }
  765. double hypot(double x, double y) NOEXCEPT
  766. {
  767. return sqrt(x * x + y * y);
  768. }
  769. double erf(double x) NOEXCEPT
  770. {
  771. // algorithm taken from Abramowitz and Stegun (no. 26.2.17)
  772. double t = 1 / (1 + 0.47047 * fabs(x));
  773. double poly = t * (0.3480242 + t * (-0.958798 + t * 0.7478556));
  774. double answer = 1 - poly * exp(-x * x);
  775. if (x < 0)
  776. return -answer;
  777. return answer;
  778. }
  779. double erfc(double x) NOEXCEPT
  780. {
  781. return 1 - erf(x);
  782. }
  783. double nextafter(double x, double target) NOEXCEPT
  784. {
  785. if (x == target)
  786. return target;
  787. return internal_nextafter(x, target >= x);
  788. }
  789. float nextafterf(float x, float target) NOEXCEPT
  790. {
  791. if (x == target)
  792. return target;
  793. return internal_nextafter(x, target >= x);
  794. }
  795. long double nextafterl(long double, long double) NOEXCEPT
  796. {
  797. TODO();
  798. }
  799. double nexttoward(double x, long double target) NOEXCEPT
  800. {
  801. if (x == target)
  802. return target;
  803. return internal_nextafter(x, target >= x);
  804. }
  805. float nexttowardf(float x, long double target) NOEXCEPT
  806. {
  807. if (x == target)
  808. return target;
  809. return internal_nextafter(x, target >= x);
  810. }
  811. long double nexttowardl(long double, long double) NOEXCEPT
  812. {
  813. TODO();
  814. }
  815. float copysignf(float x, float y) NOEXCEPT
  816. {
  817. return internal_copysign(x, y);
  818. }
  819. double copysign(double x, double y) NOEXCEPT
  820. {
  821. return internal_copysign(x, y);
  822. }
  823. long double copysignl(long double x, long double y) NOEXCEPT
  824. {
  825. return internal_copysign(x, y);
  826. }
  827. float scalbnf(float x, int exponent) NOEXCEPT
  828. {
  829. return internal_scalbn(x, exponent);
  830. }
  831. double scalbn(double x, int exponent) NOEXCEPT
  832. {
  833. return internal_scalbn(x, exponent);
  834. }
  835. long double scalbnl(long double x, int exponent) NOEXCEPT
  836. {
  837. return internal_scalbn(x, exponent);
  838. }
  839. float scalbnlf(float x, long exponent) NOEXCEPT
  840. {
  841. return internal_scalbn(x, exponent);
  842. }
  843. double scalbln(double x, long exponent) NOEXCEPT
  844. {
  845. return internal_scalbn(x, exponent);
  846. }
  847. long double scalblnl(long double x, long exponent) NOEXCEPT
  848. {
  849. return internal_scalbn(x, exponent);
  850. }
  851. }