math.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  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 -= static_cast<FloatType>(1.0);
  164. else
  165. extractor.d += static_cast<FloatType>(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_modf(FloatT x, FloatT* intpart) NOEXCEPT
  247. {
  248. FloatT integer_part = internal_to_integer(x, RoundingMode::ToZero);
  249. *intpart = integer_part;
  250. auto fraction = x - integer_part;
  251. if (signbit(fraction) != signbit(x))
  252. fraction = -fraction;
  253. return fraction;
  254. }
  255. template<typename FloatT>
  256. static FloatT internal_scalbn(FloatT x, int exponent) NOEXCEPT
  257. {
  258. if (x == 0 || !isfinite(x) || isnan(x) || exponent == 0)
  259. return x;
  260. using Extractor = FloatExtractor<FloatT>;
  261. Extractor extractor;
  262. extractor.d = x;
  263. if (extractor.exponent != 0) {
  264. extractor.exponent = clamp((int)extractor.exponent + exponent, 0, (int)Extractor::exponent_max);
  265. return extractor.d;
  266. }
  267. unsigned leading_mantissa_zeroes = extractor.mantissa == 0 ? 32 : __builtin_clz(extractor.mantissa);
  268. int shift = min((int)leading_mantissa_zeroes, exponent);
  269. exponent = max(exponent - shift, 0);
  270. extractor.exponent <<= shift;
  271. extractor.exponent = exponent + 1;
  272. return extractor.d;
  273. }
  274. template<typename FloatT>
  275. static FloatT internal_copysign(FloatT x, FloatT y) NOEXCEPT
  276. {
  277. using Extractor = FloatExtractor<FloatT>;
  278. Extractor ex, ey;
  279. ex.d = x;
  280. ey.d = y;
  281. ex.sign = ey.sign;
  282. return ex.d;
  283. }
  284. template<typename FloatT>
  285. static FloatT internal_gamma(FloatT x) NOEXCEPT
  286. {
  287. if (isnan(x))
  288. return (FloatT)NAN;
  289. if (x == (FloatT)0.0)
  290. return signbit(x) ? (FloatT)-INFINITY : (FloatT)INFINITY;
  291. if (x < (FloatT)0 && (rintl(x) == x || isinf(x)))
  292. return (FloatT)NAN;
  293. if (isinf(x))
  294. return (FloatT)INFINITY;
  295. using Extractor = FloatExtractor<FloatT>;
  296. // These constants were obtained through use of WolframAlpha
  297. constexpr long long max_integer_whose_factorial_fits = (Extractor::mantissa_bits == FloatExtractor<long double>::mantissa_bits ? 20 : (Extractor::mantissa_bits == FloatExtractor<double>::mantissa_bits ? 18 : (Extractor::mantissa_bits == FloatExtractor<float>::mantissa_bits ? 10 : 0)));
  298. static_assert(max_integer_whose_factorial_fits != 0, "internal_gamma needs to be aware of the integer factorial that fits in this floating point type.");
  299. if (rintl(x) == (long double)x && x <= max_integer_whose_factorial_fits) {
  300. long long result = 1;
  301. for (long long cursor = 1; cursor <= min(max_integer_whose_factorial_fits, (long long)x); cursor++)
  302. result *= cursor;
  303. return (FloatT)result;
  304. }
  305. // Stirling approximation
  306. return sqrtl(2.0 * M_PI / static_cast<long double>(x)) * powl(static_cast<long double>(x) / M_E, static_cast<long double>(x));
  307. }
  308. extern "C" {
  309. float nanf(const char* s) NOEXCEPT
  310. {
  311. return __builtin_nanf(s);
  312. }
  313. double nan(const char* s) NOEXCEPT
  314. {
  315. return __builtin_nan(s);
  316. }
  317. long double nanl(const char* s) NOEXCEPT
  318. {
  319. return __builtin_nanl(s);
  320. }
  321. double trunc(double x) NOEXCEPT
  322. {
  323. return internal_to_integer(x, RoundingMode::ToZero);
  324. }
  325. float truncf(float x) NOEXCEPT
  326. {
  327. return internal_to_integer(x, RoundingMode::ToZero);
  328. }
  329. long double truncl(long double x) NOEXCEPT
  330. {
  331. return internal_to_integer(x, RoundingMode::ToZero);
  332. }
  333. long double cosl(long double angle) NOEXCEPT
  334. {
  335. return sinl(angle + M_PI_2);
  336. }
  337. double cos(double angle) NOEXCEPT
  338. {
  339. return sin(angle + M_PI_2);
  340. }
  341. float cosf(float angle) NOEXCEPT
  342. {
  343. return sinf(angle + static_cast<float>(M_PI_2));
  344. }
  345. long double sinl(long double angle) NOEXCEPT
  346. {
  347. long double ret = 0.0;
  348. __asm__(
  349. "fsin"
  350. : "=t"(ret)
  351. : "0"(angle));
  352. return ret;
  353. }
  354. // This can also be done with a taylor expansion, but for
  355. // now this works pretty well (and doesn't mess anything up
  356. // in quake in particular, which is very Floating-Point precision
  357. // heavy)
  358. double sin(double angle) NOEXCEPT
  359. {
  360. double ret = 0.0;
  361. __asm__(
  362. "fsin"
  363. : "=t"(ret)
  364. : "0"(angle));
  365. return ret;
  366. }
  367. float sinf(float angle) NOEXCEPT
  368. {
  369. float ret = 0.0f;
  370. __asm__(
  371. "fsin"
  372. : "=t"(ret)
  373. : "0"(angle));
  374. return ret;
  375. }
  376. long double powl(long double x, long double y) NOEXCEPT
  377. {
  378. // FIXME: Please fix me. I am naive.
  379. if (isnan(y))
  380. return y;
  381. if (y == 0)
  382. return 1;
  383. if (x == 0)
  384. return 0;
  385. if (y == 1)
  386. return x;
  387. int y_as_int = (int)y;
  388. if (y == (long double)y_as_int) {
  389. long double result = x;
  390. for (int i = 0; i < fabsl(y) - 1; ++i)
  391. result *= x;
  392. if (y < 0)
  393. result = 1.0l / result;
  394. return result;
  395. }
  396. return exp2l(y * log2l(x));
  397. }
  398. double pow(double x, double y) NOEXCEPT
  399. {
  400. return (double)powl(x, y);
  401. }
  402. float powf(float x, float y) NOEXCEPT
  403. {
  404. return (float)powl(x, y);
  405. }
  406. // On systems where FLT_RADIX == 2, ldexp is equivalent to scalbn
  407. long double ldexpl(long double x, int exp) NOEXCEPT
  408. {
  409. return internal_scalbn(x, exp);
  410. }
  411. double ldexp(double x, int exp) NOEXCEPT
  412. {
  413. return internal_scalbn(x, exp);
  414. }
  415. float ldexpf(float x, int exp) NOEXCEPT
  416. {
  417. return internal_scalbn(x, exp);
  418. }
  419. long double tanhl(long double x) NOEXCEPT
  420. {
  421. if (x > 0) {
  422. long double exponentiated = expl(2 * x);
  423. return (exponentiated - 1) / (exponentiated + 1);
  424. }
  425. long double plusX = expl(x);
  426. long double minusX = 1 / plusX;
  427. return (plusX - minusX) / (plusX + minusX);
  428. }
  429. double tanh(double x) NOEXCEPT
  430. {
  431. return (double)tanhl(x);
  432. }
  433. float tanhf(float x) NOEXCEPT
  434. {
  435. return (float)tanhl(x);
  436. }
  437. [[maybe_unused]] static long double ampsin(long double angle) NOEXCEPT
  438. {
  439. long double looped_angle = fmodl(M_PI + angle, M_TAU) - M_PI;
  440. long double looped_angle_squared = looped_angle * looped_angle;
  441. long double quadratic_term;
  442. if (looped_angle > 0) {
  443. quadratic_term = -looped_angle_squared;
  444. } else {
  445. quadratic_term = looped_angle_squared;
  446. }
  447. long double linear_term = M_PI * looped_angle;
  448. return quadratic_term + linear_term;
  449. }
  450. long double tanl(long double angle) NOEXCEPT
  451. {
  452. long double ret = 0.0, one;
  453. __asm__(
  454. "fptan"
  455. : "=t"(one), "=u"(ret)
  456. : "0"(angle));
  457. return ret;
  458. }
  459. double tan(double angle) NOEXCEPT
  460. {
  461. return (double)tanl(angle);
  462. }
  463. float tanf(float angle) NOEXCEPT
  464. {
  465. return (float)tanl(angle);
  466. }
  467. long double sqrtl(long double x) NOEXCEPT
  468. {
  469. long double res;
  470. asm("fsqrt"
  471. : "=t"(res)
  472. : "0"(x));
  473. return res;
  474. }
  475. double sqrt(double x) NOEXCEPT
  476. {
  477. double res;
  478. __asm__("fsqrt"
  479. : "=t"(res)
  480. : "0"(x));
  481. return res;
  482. }
  483. float sqrtf(float x) NOEXCEPT
  484. {
  485. float res;
  486. __asm__("fsqrt"
  487. : "=t"(res)
  488. : "0"(x));
  489. return res;
  490. }
  491. long double sinhl(long double x) NOEXCEPT
  492. {
  493. long double exponentiated = expl(x);
  494. if (x > 0)
  495. return (exponentiated * exponentiated - 1) / 2 / exponentiated;
  496. return (exponentiated - 1 / exponentiated) / 2;
  497. }
  498. double sinh(double x) NOEXCEPT
  499. {
  500. return (double)sinhl(x);
  501. }
  502. float sinhf(float x) NOEXCEPT
  503. {
  504. return (float)sinhl(x);
  505. }
  506. long double log10l(long double x) NOEXCEPT
  507. {
  508. long double ret = 0.0l;
  509. __asm__(
  510. "fldlg2\n"
  511. "fld %%st(1)\n"
  512. "fyl2x\n"
  513. "fstp %%st(1)"
  514. : "=t"(ret)
  515. : "0"(x));
  516. return ret;
  517. }
  518. double log10(double x) NOEXCEPT
  519. {
  520. return (double)log10l(x);
  521. }
  522. float log10f(float x) NOEXCEPT
  523. {
  524. return (float)log10l(x);
  525. }
  526. long double logl(long double x) NOEXCEPT
  527. {
  528. long double ret = 0.0l;
  529. asm(
  530. "fldln2\n"
  531. "fld %%st(1)\n"
  532. "fyl2x\n"
  533. "fstp %%st(1)"
  534. : "=t"(ret)
  535. : "0"(x));
  536. return ret;
  537. }
  538. double log(double x) NOEXCEPT
  539. {
  540. return (double)logl(x);
  541. }
  542. float logf(float x) NOEXCEPT
  543. {
  544. return (float)logl(x);
  545. }
  546. long double fmodl(long double index, long double period) NOEXCEPT
  547. {
  548. return index - truncl(index / period) * period;
  549. }
  550. double fmod(double index, double period) NOEXCEPT
  551. {
  552. return index - trunc(index / period) * period;
  553. }
  554. float fmodf(float index, float period) NOEXCEPT
  555. {
  556. return index - truncf(index / period) * period;
  557. }
  558. // FIXME: These aren't exactly like fmod, but these definitions are probably good enough for now
  559. long double remainderl(long double x, long double y) NOEXCEPT
  560. {
  561. return fmodl(x, y);
  562. }
  563. double remainder(double x, double y) NOEXCEPT
  564. {
  565. return fmod(x, y);
  566. }
  567. float remainderf(float x, float y) NOEXCEPT
  568. {
  569. return fmodf(x, y);
  570. }
  571. long double expl(long double exponent) NOEXCEPT
  572. {
  573. long double res = 0;
  574. asm("fldl2e\n"
  575. "fmulp\n"
  576. "fld1\n"
  577. "fld %%st(1)\n"
  578. "fprem\n"
  579. "f2xm1\n"
  580. "faddp\n"
  581. "fscale\n"
  582. "fstp %%st(1)"
  583. : "=t"(res)
  584. : "0"(exponent));
  585. return res;
  586. }
  587. double exp(double exponent) NOEXCEPT
  588. {
  589. return (double)expl(exponent);
  590. }
  591. float expf(float exponent) NOEXCEPT
  592. {
  593. return (float)expl(exponent);
  594. }
  595. long double exp2l(long double exponent) NOEXCEPT
  596. {
  597. long double res = 0;
  598. asm("fld1\n"
  599. "fld %%st(1)\n"
  600. "fprem\n"
  601. "f2xm1\n"
  602. "faddp\n"
  603. "fscale\n"
  604. "fstp %%st(1)"
  605. : "=t"(res)
  606. : "0"(exponent));
  607. return res;
  608. }
  609. double exp2(double exponent) NOEXCEPT
  610. {
  611. return (double)exp2l(exponent);
  612. }
  613. float exp2f(float exponent) NOEXCEPT
  614. {
  615. return (float)exp2l(exponent);
  616. }
  617. long double coshl(long double x) NOEXCEPT
  618. {
  619. long double exponentiated = expl(-x);
  620. if (x < 0)
  621. return (1 + exponentiated * exponentiated) / 2 / exponentiated;
  622. return (1 / exponentiated + exponentiated) / 2;
  623. }
  624. double cosh(double x) NOEXCEPT
  625. {
  626. return (double)coshl(x);
  627. }
  628. float coshf(float x) NOEXCEPT
  629. {
  630. return (float)coshl(x);
  631. }
  632. long double atan2l(long double y, long double x) NOEXCEPT
  633. {
  634. if (x == 0) {
  635. if (y > 0)
  636. return M_PI_2;
  637. if (y < 0)
  638. return -M_PI_2;
  639. return 0;
  640. }
  641. long double result = 0; //atanl(y / x);
  642. __asm__("fpatan"
  643. : "=t"(result)
  644. : "0"(x), "u"(y)
  645. : "st(1)");
  646. return result;
  647. }
  648. double atan2(double y, double x) NOEXCEPT
  649. {
  650. return (double)atan2l(y, x);
  651. }
  652. float atan2f(float y, float x) NOEXCEPT
  653. {
  654. return (float)atan2l(y, x);
  655. }
  656. long double atanl(long double x) NOEXCEPT
  657. {
  658. if (x < 0)
  659. return -atanl(-x);
  660. if (x > 1)
  661. return M_PI_2 - atanl(1 / x);
  662. long double squared = x * x;
  663. 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)))))));
  664. }
  665. double atan(double x) NOEXCEPT
  666. {
  667. return (double)atanl(x);
  668. }
  669. float atanf(float x) NOEXCEPT
  670. {
  671. return (float)atanl(x);
  672. }
  673. long double asinl(long double x) NOEXCEPT
  674. {
  675. if (x > 1 || x < -1)
  676. return NAN;
  677. if (x > 0.5 || x < -0.5)
  678. return 2 * atanl(x / (1 + sqrtl(1 - x * x)));
  679. long double squared = x * x;
  680. long double value = x;
  681. long double i = x * squared;
  682. value += i * product_odd<1>() / product_even<2>() / 3;
  683. i *= squared;
  684. value += i * product_odd<3>() / product_even<4>() / 5;
  685. i *= squared;
  686. value += i * product_odd<5>() / product_even<6>() / 7;
  687. i *= squared;
  688. value += i * product_odd<7>() / product_even<8>() / 9;
  689. i *= squared;
  690. value += i * product_odd<9>() / product_even<10>() / 11;
  691. i *= squared;
  692. value += i * product_odd<11>() / product_even<12>() / 13;
  693. return value;
  694. }
  695. double asin(double x) NOEXCEPT
  696. {
  697. return (double)asinl(x);
  698. }
  699. float asinf(float x) NOEXCEPT
  700. {
  701. return (float)asinl(x);
  702. }
  703. long double acosl(long double x) NOEXCEPT
  704. {
  705. return M_PI_2 - asinl(x);
  706. }
  707. double acos(double x) NOEXCEPT
  708. {
  709. return M_PI_2 - asin(x);
  710. }
  711. float acosf(float x) NOEXCEPT
  712. {
  713. return static_cast<float>(M_PI_2) - asinf(x);
  714. }
  715. long double fabsl(long double value) NOEXCEPT
  716. {
  717. return value < 0 ? -value : value;
  718. }
  719. double fabs(double value) NOEXCEPT
  720. {
  721. return value < 0 ? -value : value;
  722. }
  723. float fabsf(float value) NOEXCEPT
  724. {
  725. return value < 0 ? -value : value;
  726. }
  727. int ilogbl(long double x) NOEXCEPT
  728. {
  729. return internal_ilogb(x);
  730. }
  731. int ilogb(double x) NOEXCEPT
  732. {
  733. return internal_ilogb(x);
  734. }
  735. int ilogbf(float x) NOEXCEPT
  736. {
  737. return internal_ilogb(x);
  738. }
  739. long double logbl(long double x) NOEXCEPT
  740. {
  741. return ilogbl(x);
  742. }
  743. double logb(double x) NOEXCEPT
  744. {
  745. return ilogb(x);
  746. }
  747. float logbf(float x) NOEXCEPT
  748. {
  749. return ilogbf(x);
  750. }
  751. long double log2l(long double x) NOEXCEPT
  752. {
  753. long double ret = 0.0;
  754. asm(
  755. "fld1\n"
  756. "fld %%st(1)\n"
  757. "fyl2x\n"
  758. "fstp %%st(1)"
  759. : "=t"(ret)
  760. : "0"(x));
  761. return ret;
  762. }
  763. double log2(double x) NOEXCEPT
  764. {
  765. return (double)log2l(x);
  766. }
  767. float log2f(float x) NOEXCEPT
  768. {
  769. return (float)log2l(x);
  770. }
  771. double frexp(double x, int* exp) NOEXCEPT
  772. {
  773. *exp = (x == 0) ? 0 : (1 + ilogb(x));
  774. return scalbn(x, -(*exp));
  775. }
  776. float frexpf(float x, int* exp) NOEXCEPT
  777. {
  778. *exp = (x == 0) ? 0 : (1 + ilogbf(x));
  779. return scalbnf(x, -(*exp));
  780. }
  781. long double frexpl(long double x, int* exp) NOEXCEPT
  782. {
  783. *exp = (x == 0) ? 0 : (1 + ilogbl(x));
  784. return scalbnl(x, -(*exp));
  785. }
  786. double round(double value) NOEXCEPT
  787. {
  788. return internal_to_integer(value, RoundingMode::ToEven);
  789. }
  790. float roundf(float value) NOEXCEPT
  791. {
  792. return internal_to_integer(value, RoundingMode::ToEven);
  793. }
  794. long double roundl(long double value) NOEXCEPT
  795. {
  796. return internal_to_integer(value, RoundingMode::ToEven);
  797. }
  798. long lroundf(float value) NOEXCEPT
  799. {
  800. return internal_to_integer(value, RoundingMode::ToEven);
  801. }
  802. long lround(double value) NOEXCEPT
  803. {
  804. return internal_to_integer(value, RoundingMode::ToEven);
  805. }
  806. long lroundl(long double value) NOEXCEPT
  807. {
  808. return internal_to_integer(value, RoundingMode::ToEven);
  809. }
  810. long long llroundf(float value) NOEXCEPT
  811. {
  812. return internal_to_integer(value, RoundingMode::ToEven);
  813. }
  814. long long llround(double value) NOEXCEPT
  815. {
  816. return internal_to_integer(value, RoundingMode::ToEven);
  817. }
  818. long long llroundd(long double value) NOEXCEPT
  819. {
  820. return internal_to_integer(value, RoundingMode::ToEven);
  821. }
  822. float floorf(float value) NOEXCEPT
  823. {
  824. return internal_to_integer(value, RoundingMode::Down);
  825. }
  826. double floor(double value) NOEXCEPT
  827. {
  828. return internal_to_integer(value, RoundingMode::Down);
  829. }
  830. long double floorl(long double value) NOEXCEPT
  831. {
  832. return internal_to_integer(value, RoundingMode::Down);
  833. }
  834. long double rintl(long double value) NOEXCEPT
  835. {
  836. return internal_to_integer(value, RoundingMode { fegetround() });
  837. }
  838. double rint(double value) NOEXCEPT
  839. {
  840. return internal_to_integer(value, RoundingMode { fegetround() });
  841. }
  842. float rintf(float value) NOEXCEPT
  843. {
  844. return internal_to_integer(value, RoundingMode { fegetround() });
  845. }
  846. long lrintl(long double value) NOEXCEPT
  847. {
  848. return (long)internal_to_integer(value, RoundingMode { fegetround() });
  849. }
  850. long lrint(double value) NOEXCEPT
  851. {
  852. return (long)internal_to_integer(value, RoundingMode { fegetround() });
  853. }
  854. long lrintf(float value) NOEXCEPT
  855. {
  856. return (long)internal_to_integer(value, RoundingMode { fegetround() });
  857. }
  858. long long llrintl(long double value) NOEXCEPT
  859. {
  860. return (long long)internal_to_integer(value, RoundingMode { fegetround() });
  861. }
  862. long long llrint(double value) NOEXCEPT
  863. {
  864. return (long long)internal_to_integer(value, RoundingMode { fegetround() });
  865. }
  866. long long llrintf(float value) NOEXCEPT
  867. {
  868. return (long long)internal_to_integer(value, RoundingMode { fegetround() });
  869. }
  870. float ceilf(float value) NOEXCEPT
  871. {
  872. return internal_to_integer(value, RoundingMode::Up);
  873. }
  874. double ceil(double value) NOEXCEPT
  875. {
  876. return internal_to_integer(value, RoundingMode::Up);
  877. }
  878. long double ceill(long double value) NOEXCEPT
  879. {
  880. return internal_to_integer(value, RoundingMode::Up);
  881. }
  882. long double modfl(long double x, long double* intpart) NOEXCEPT
  883. {
  884. return internal_modf(x, intpart);
  885. }
  886. double modf(double x, double* intpart) NOEXCEPT
  887. {
  888. return internal_modf(x, intpart);
  889. }
  890. float modff(float x, float* intpart) NOEXCEPT
  891. {
  892. return internal_modf(x, intpart);
  893. }
  894. double gamma(double x) NOEXCEPT
  895. {
  896. // Stirling approximation
  897. return sqrt(2.0 * M_PI / x) * pow(x / M_E, x);
  898. }
  899. long double tgammal(long double value) NOEXCEPT
  900. {
  901. return internal_gamma(value);
  902. }
  903. double tgamma(double value) NOEXCEPT
  904. {
  905. return internal_gamma(value);
  906. }
  907. float tgammaf(float value) NOEXCEPT
  908. {
  909. return internal_gamma(value);
  910. }
  911. int signgam = 0;
  912. long double lgammal(long double value) NOEXCEPT
  913. {
  914. return lgammal_r(value, &signgam);
  915. }
  916. double lgamma(double value) NOEXCEPT
  917. {
  918. return lgamma_r(value, &signgam);
  919. }
  920. float lgammaf(float value) NOEXCEPT
  921. {
  922. return lgammaf_r(value, &signgam);
  923. }
  924. long double lgammal_r(long double value, int* sign) NOEXCEPT
  925. {
  926. if (value == 1.0 || value == 2.0)
  927. return 0.0;
  928. if (isinf(value) || value == 0.0)
  929. return INFINITY;
  930. long double result = logl(internal_gamma(value));
  931. *sign = signbit(result) ? -1 : 1;
  932. return result;
  933. }
  934. double lgamma_r(double value, int* sign) NOEXCEPT
  935. {
  936. if (value == 1.0 || value == 2.0)
  937. return 0.0;
  938. if (isinf(value) || value == 0.0)
  939. return INFINITY;
  940. double result = log(internal_gamma(value));
  941. *sign = signbit(result) ? -1 : 1;
  942. return result;
  943. }
  944. float lgammaf_r(float value, int* sign) NOEXCEPT
  945. {
  946. if (value == 1.0f || value == 2.0f)
  947. return 0.0;
  948. if (isinf(value) || value == 0.0f)
  949. return INFINITY;
  950. float result = logf(internal_gamma(value));
  951. *sign = signbit(result) ? -1 : 1;
  952. return result;
  953. }
  954. long double expm1l(long double x) NOEXCEPT
  955. {
  956. return expl(x) - 1;
  957. }
  958. double expm1(double x) NOEXCEPT
  959. {
  960. return exp(x) - 1;
  961. }
  962. float expm1f(float x) NOEXCEPT
  963. {
  964. return expf(x) - 1;
  965. }
  966. long double cbrtl(long double x) NOEXCEPT
  967. {
  968. if (isinf(x) || x == 0)
  969. return x;
  970. if (x < 0)
  971. return -cbrtl(-x);
  972. long double r = x;
  973. long double ex = 0;
  974. while (r < 0.125l) {
  975. r *= 8;
  976. ex--;
  977. }
  978. while (r > 1.0l) {
  979. r *= 0.125l;
  980. ex++;
  981. }
  982. r = (-0.46946116l * r + 1.072302l) * r + 0.3812513l;
  983. while (ex < 0) {
  984. r *= 0.5l;
  985. ex++;
  986. }
  987. while (ex > 0) {
  988. r *= 2.0l;
  989. ex--;
  990. }
  991. r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);
  992. r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);
  993. r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);
  994. r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);
  995. return r;
  996. }
  997. double cbrt(double x) NOEXCEPT
  998. {
  999. return (double)cbrtl(x);
  1000. }
  1001. float cbrtf(float x) NOEXCEPT
  1002. {
  1003. return (float)cbrtl(x);
  1004. }
  1005. long double log1pl(long double x) NOEXCEPT
  1006. {
  1007. return logl(1 + x);
  1008. }
  1009. double log1p(double x) NOEXCEPT
  1010. {
  1011. return log(1 + x);
  1012. }
  1013. float log1pf(float x) NOEXCEPT
  1014. {
  1015. return logf(1 + x);
  1016. }
  1017. long double acoshl(long double x) NOEXCEPT
  1018. {
  1019. return logl(x + sqrtl(x * x - 1));
  1020. }
  1021. double acosh(double x) NOEXCEPT
  1022. {
  1023. return log(x + sqrt(x * x - 1));
  1024. }
  1025. float acoshf(float x) NOEXCEPT
  1026. {
  1027. return logf(x + sqrtf(x * x - 1));
  1028. }
  1029. long double asinhl(long double x) NOEXCEPT
  1030. {
  1031. return logl(x + sqrtl(x * x + 1));
  1032. }
  1033. double asinh(double x) NOEXCEPT
  1034. {
  1035. return log(x + sqrt(x * x + 1));
  1036. }
  1037. float asinhf(float x) NOEXCEPT
  1038. {
  1039. return logf(x + sqrtf(x * x + 1));
  1040. }
  1041. long double atanhl(long double x) NOEXCEPT
  1042. {
  1043. return logl((1 + x) / (1 - x)) / 2.0l;
  1044. }
  1045. double atanh(double x) NOEXCEPT
  1046. {
  1047. return log((1 + x) / (1 - x)) / 2.0;
  1048. }
  1049. float atanhf(float x) NOEXCEPT
  1050. {
  1051. return logf((1 + x) / (1 - x)) / 2.0f;
  1052. }
  1053. long double hypotl(long double x, long double y) NOEXCEPT
  1054. {
  1055. return sqrtl(x * x + y * y);
  1056. }
  1057. double hypot(double x, double y) NOEXCEPT
  1058. {
  1059. return sqrt(x * x + y * y);
  1060. }
  1061. float hypotf(float x, float y) NOEXCEPT
  1062. {
  1063. return sqrtf(x * x + y * y);
  1064. }
  1065. long double erfl(long double x) NOEXCEPT
  1066. {
  1067. // algorithm taken from Abramowitz and Stegun (no. 26.2.17)
  1068. long double t = 1 / (1 + 0.47047l * fabsl(x));
  1069. long double poly = t * (0.3480242l + t * (-0.958798l + t * 0.7478556l));
  1070. long double answer = 1 - poly * expl(-x * x);
  1071. if (x < 0)
  1072. return -answer;
  1073. return answer;
  1074. }
  1075. double erf(double x) NOEXCEPT
  1076. {
  1077. return (double)erfl(x);
  1078. }
  1079. float erff(float x) NOEXCEPT
  1080. {
  1081. return (float)erf(x);
  1082. }
  1083. long double erfcl(long double x) NOEXCEPT
  1084. {
  1085. return 1 - erfl(x);
  1086. }
  1087. double erfc(double x) NOEXCEPT
  1088. {
  1089. return 1 - erf(x);
  1090. }
  1091. float erfcf(float x) NOEXCEPT
  1092. {
  1093. return 1 - erff(x);
  1094. }
  1095. double nextafter(double x, double target) NOEXCEPT
  1096. {
  1097. if (x == target)
  1098. return target;
  1099. return internal_nextafter(x, target >= x);
  1100. }
  1101. float nextafterf(float x, float target) NOEXCEPT
  1102. {
  1103. if (x == target)
  1104. return target;
  1105. return internal_nextafter(x, target >= x);
  1106. }
  1107. long double nextafterl(long double x, long double target) NOEXCEPT
  1108. {
  1109. return internal_nextafter(x, target >= x);
  1110. }
  1111. double nexttoward(double x, long double target) NOEXCEPT
  1112. {
  1113. if (x == target)
  1114. return target;
  1115. return internal_nextafter(x, target >= x);
  1116. }
  1117. float nexttowardf(float x, long double target) NOEXCEPT
  1118. {
  1119. if (x == target)
  1120. return target;
  1121. return internal_nextafter(x, target >= x);
  1122. }
  1123. long double nexttowardl(long double x, long double target) NOEXCEPT
  1124. {
  1125. if (x == target)
  1126. return target;
  1127. return internal_nextafter(x, target >= x);
  1128. }
  1129. float copysignf(float x, float y) NOEXCEPT
  1130. {
  1131. return internal_copysign(x, y);
  1132. }
  1133. double copysign(double x, double y) NOEXCEPT
  1134. {
  1135. return internal_copysign(x, y);
  1136. }
  1137. long double copysignl(long double x, long double y) NOEXCEPT
  1138. {
  1139. return internal_copysign(x, y);
  1140. }
  1141. float scalbnf(float x, int exponent) NOEXCEPT
  1142. {
  1143. return internal_scalbn(x, exponent);
  1144. }
  1145. double scalbn(double x, int exponent) NOEXCEPT
  1146. {
  1147. return internal_scalbn(x, exponent);
  1148. }
  1149. long double scalbnl(long double x, int exponent) NOEXCEPT
  1150. {
  1151. return internal_scalbn(x, exponent);
  1152. }
  1153. float scalbnlf(float x, long exponent) NOEXCEPT
  1154. {
  1155. return internal_scalbn(x, exponent);
  1156. }
  1157. double scalbln(double x, long exponent) NOEXCEPT
  1158. {
  1159. return internal_scalbn(x, exponent);
  1160. }
  1161. long double scalblnl(long double x, long exponent) NOEXCEPT
  1162. {
  1163. return internal_scalbn(x, exponent);
  1164. }
  1165. long double fmaxl(long double x, long double y) NOEXCEPT
  1166. {
  1167. if (isnan(x))
  1168. return y;
  1169. if (isnan(y))
  1170. return x;
  1171. return x > y ? x : y;
  1172. }
  1173. double fmax(double x, double y) NOEXCEPT
  1174. {
  1175. if (isnan(x))
  1176. return y;
  1177. if (isnan(y))
  1178. return x;
  1179. return x > y ? x : y;
  1180. }
  1181. float fmaxf(float x, float y) NOEXCEPT
  1182. {
  1183. if (isnan(x))
  1184. return y;
  1185. if (isnan(y))
  1186. return x;
  1187. return x > y ? x : y;
  1188. }
  1189. long double fminl(long double x, long double y) NOEXCEPT
  1190. {
  1191. if (isnan(x))
  1192. return y;
  1193. if (isnan(y))
  1194. return x;
  1195. return x < y ? x : y;
  1196. }
  1197. double fmin(double x, double y) NOEXCEPT
  1198. {
  1199. if (isnan(x))
  1200. return y;
  1201. if (isnan(y))
  1202. return x;
  1203. return x < y ? x : y;
  1204. }
  1205. float fminf(float x, float y) NOEXCEPT
  1206. {
  1207. if (isnan(x))
  1208. return y;
  1209. if (isnan(y))
  1210. return x;
  1211. return x < y ? x : y;
  1212. }
  1213. }