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. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/ExtraMathConstants.h>
  8. #include <AK/Platform.h>
  9. #include <AK/StdLibExtras.h>
  10. #include <LibC/assert.h>
  11. #include <fenv.h>
  12. #include <math.h>
  13. #include <stdint.h>
  14. #include <stdlib.h>
  15. template<size_t>
  16. constexpr double e_to_power();
  17. template<>
  18. constexpr double e_to_power<0>() { return 1; }
  19. template<size_t exponent>
  20. constexpr double e_to_power() { return M_E * e_to_power<exponent - 1>(); }
  21. template<size_t>
  22. constexpr size_t factorial();
  23. template<>
  24. constexpr size_t factorial<0>() { return 1; }
  25. template<size_t value>
  26. constexpr size_t factorial() { return value * factorial<value - 1>(); }
  27. template<size_t>
  28. constexpr size_t product_even();
  29. template<>
  30. constexpr size_t product_even<2>() { return 2; }
  31. template<size_t value>
  32. constexpr size_t product_even() { return value * product_even<value - 2>(); }
  33. template<size_t>
  34. constexpr size_t product_odd();
  35. template<>
  36. constexpr size_t product_odd<1>() { return 1; }
  37. template<size_t value>
  38. constexpr size_t product_odd() { return value * product_odd<value - 2>(); }
  39. enum class RoundingMode {
  40. ToZero = FE_TOWARDZERO,
  41. Up = FE_UPWARD,
  42. Down = FE_DOWNWARD,
  43. ToEven = FE_TONEAREST
  44. };
  45. template<typename T>
  46. union FloatExtractor;
  47. #if ARCH(I386) || ARCH(X86_64)
  48. // This assumes long double is 80 bits, which is true with GCC on Intel platforms
  49. template<>
  50. union FloatExtractor<long double> {
  51. static const int mantissa_bits = 64;
  52. static const unsigned long long mantissa_max = ~0u;
  53. static const int exponent_bias = 16383;
  54. static const int exponent_bits = 15;
  55. static const unsigned exponent_max = 32767;
  56. struct {
  57. unsigned long long mantissa;
  58. unsigned exponent : 15;
  59. unsigned sign : 1;
  60. };
  61. long double d;
  62. };
  63. #endif
  64. template<>
  65. union FloatExtractor<double> {
  66. static const int mantissa_bits = 52;
  67. static const unsigned long long mantissa_max = (1ull << 52) - 1;
  68. static const int exponent_bias = 1023;
  69. static const int exponent_bits = 11;
  70. static const unsigned exponent_max = 2047;
  71. struct {
  72. unsigned long long mantissa : 52;
  73. unsigned exponent : 11;
  74. unsigned sign : 1;
  75. };
  76. double d;
  77. };
  78. template<>
  79. union FloatExtractor<float> {
  80. static const int mantissa_bits = 23;
  81. static const unsigned mantissa_max = (1 << 23) - 1;
  82. static const int exponent_bias = 127;
  83. static const int exponent_bits = 8;
  84. static const unsigned exponent_max = 255;
  85. struct {
  86. unsigned long long mantissa : 23;
  87. unsigned exponent : 8;
  88. unsigned sign : 1;
  89. };
  90. float d;
  91. };
  92. // This is much branchier than it really needs to be
  93. template<typename FloatType>
  94. static FloatType internal_to_integer(FloatType x, RoundingMode rounding_mode)
  95. {
  96. if (!isfinite(x))
  97. return x;
  98. using Extractor = FloatExtractor<decltype(x)>;
  99. Extractor extractor;
  100. extractor.d = x;
  101. auto unbiased_exponent = extractor.exponent - Extractor::exponent_bias;
  102. bool round = false;
  103. bool guard = false;
  104. if (unbiased_exponent < 0) {
  105. // it was easier to special case [0..1) as it saves us from
  106. // handling subnormals, underflows, etc
  107. if (unbiased_exponent == -1) {
  108. round = true;
  109. }
  110. guard = extractor.mantissa != 0;
  111. extractor.mantissa = 0;
  112. extractor.exponent = 0;
  113. } else {
  114. if (unbiased_exponent >= Extractor::mantissa_bits)
  115. return x;
  116. auto dead_bitcount = Extractor::mantissa_bits - unbiased_exponent;
  117. auto dead_mask = (1ull << dead_bitcount) - 1;
  118. auto dead_bits = extractor.mantissa & dead_mask;
  119. extractor.mantissa &= ~dead_mask;
  120. auto guard_mask = dead_mask >> 1;
  121. guard = (dead_bits & guard_mask) != 0;
  122. round = (dead_bits & ~guard_mask) != 0;
  123. }
  124. bool should_round = false;
  125. switch (rounding_mode) {
  126. case RoundingMode::ToEven:
  127. should_round = round;
  128. break;
  129. case RoundingMode::Up:
  130. if (!extractor.sign)
  131. should_round = guard || round;
  132. break;
  133. case RoundingMode::Down:
  134. if (extractor.sign)
  135. should_round = guard || round;
  136. break;
  137. case RoundingMode::ToZero:
  138. break;
  139. }
  140. if (should_round) {
  141. // We could do this ourselves, but this saves us from manually
  142. // handling overflow.
  143. if (extractor.sign)
  144. extractor.d -= static_cast<FloatType>(1.0);
  145. else
  146. extractor.d += static_cast<FloatType>(1.0);
  147. }
  148. return extractor.d;
  149. }
  150. // This is much branchier than it really needs to be
  151. template<typename FloatType>
  152. static FloatType internal_nextafter(FloatType x, bool up)
  153. {
  154. if (!isfinite(x))
  155. return x;
  156. using Extractor = FloatExtractor<decltype(x)>;
  157. Extractor extractor;
  158. extractor.d = x;
  159. if (x == 0) {
  160. if (!extractor.sign) {
  161. extractor.mantissa = 1;
  162. extractor.sign = !up;
  163. return extractor.d;
  164. }
  165. if (up) {
  166. extractor.sign = false;
  167. extractor.mantissa = 1;
  168. return extractor.d;
  169. }
  170. extractor.mantissa = 1;
  171. extractor.sign = up != extractor.sign;
  172. return extractor.d;
  173. }
  174. if (up != extractor.sign) {
  175. extractor.mantissa++;
  176. if (!extractor.mantissa) {
  177. // no need to normalize the mantissa as we just hit a power
  178. // of two.
  179. extractor.exponent++;
  180. if (extractor.exponent == Extractor::exponent_max) {
  181. extractor.exponent = Extractor::exponent_max - 1;
  182. extractor.mantissa = Extractor::mantissa_max;
  183. }
  184. }
  185. return extractor.d;
  186. }
  187. if (!extractor.mantissa) {
  188. if (extractor.exponent) {
  189. extractor.exponent--;
  190. extractor.mantissa = Extractor::mantissa_max;
  191. } else {
  192. extractor.d = 0;
  193. }
  194. return extractor.d;
  195. }
  196. extractor.mantissa--;
  197. if (extractor.mantissa != Extractor::mantissa_max)
  198. return extractor.d;
  199. if (extractor.exponent) {
  200. extractor.exponent--;
  201. // normalize
  202. extractor.mantissa <<= 1;
  203. } else {
  204. if (extractor.sign) {
  205. // Negative infinity
  206. extractor.mantissa = 0;
  207. extractor.exponent = Extractor::exponent_max;
  208. }
  209. }
  210. return extractor.d;
  211. }
  212. template<typename FloatT>
  213. static int internal_ilogb(FloatT x) NOEXCEPT
  214. {
  215. if (x == 0)
  216. return FP_ILOGB0;
  217. if (isnan(x))
  218. return FP_ILOGNAN;
  219. if (!isfinite(x))
  220. return INT_MAX;
  221. using Extractor = FloatExtractor<FloatT>;
  222. Extractor extractor;
  223. extractor.d = x;
  224. return (int)extractor.exponent - Extractor::exponent_bias;
  225. }
  226. template<typename FloatT>
  227. static FloatT internal_modf(FloatT x, FloatT* intpart) NOEXCEPT
  228. {
  229. FloatT integer_part = internal_to_integer(x, RoundingMode::ToZero);
  230. *intpart = integer_part;
  231. auto fraction = x - integer_part;
  232. if (signbit(fraction) != signbit(x))
  233. fraction = -fraction;
  234. return fraction;
  235. }
  236. template<typename FloatT>
  237. static FloatT internal_scalbn(FloatT x, int exponent) NOEXCEPT
  238. {
  239. if (x == 0 || !isfinite(x) || isnan(x) || exponent == 0)
  240. return x;
  241. using Extractor = FloatExtractor<FloatT>;
  242. Extractor extractor;
  243. extractor.d = x;
  244. if (extractor.exponent != 0) {
  245. extractor.exponent = clamp((int)extractor.exponent + exponent, 0, (int)Extractor::exponent_max);
  246. return extractor.d;
  247. }
  248. unsigned leading_mantissa_zeroes = extractor.mantissa == 0 ? 32 : __builtin_clz(extractor.mantissa);
  249. int shift = min((int)leading_mantissa_zeroes, exponent);
  250. exponent = max(exponent - shift, 0);
  251. extractor.exponent <<= shift;
  252. extractor.exponent = exponent + 1;
  253. return extractor.d;
  254. }
  255. template<typename FloatT>
  256. static FloatT internal_copysign(FloatT x, FloatT y) NOEXCEPT
  257. {
  258. using Extractor = FloatExtractor<FloatT>;
  259. Extractor ex, ey;
  260. ex.d = x;
  261. ey.d = y;
  262. ex.sign = ey.sign;
  263. return ex.d;
  264. }
  265. template<typename FloatT>
  266. static FloatT internal_gamma(FloatT x) NOEXCEPT
  267. {
  268. if (isnan(x))
  269. return (FloatT)NAN;
  270. if (x == (FloatT)0.0)
  271. return signbit(x) ? (FloatT)-INFINITY : (FloatT)INFINITY;
  272. if (x < (FloatT)0 && (rintl(x) == x || isinf(x)))
  273. return (FloatT)NAN;
  274. if (isinf(x))
  275. return (FloatT)INFINITY;
  276. using Extractor = FloatExtractor<FloatT>;
  277. // These constants were obtained through use of WolframAlpha
  278. 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)));
  279. 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.");
  280. if (rintl(x) == (long double)x && x <= max_integer_whose_factorial_fits) {
  281. long long result = 1;
  282. for (long long cursor = 1; cursor <= min(max_integer_whose_factorial_fits, (long long)x); cursor++)
  283. result *= cursor;
  284. return (FloatT)result;
  285. }
  286. // Stirling approximation
  287. return sqrtl(2.0 * M_PI / static_cast<long double>(x)) * powl(static_cast<long double>(x) / M_E, static_cast<long double>(x));
  288. }
  289. extern "C" {
  290. float nanf(const char* s) NOEXCEPT
  291. {
  292. return __builtin_nanf(s);
  293. }
  294. double nan(const char* s) NOEXCEPT
  295. {
  296. return __builtin_nan(s);
  297. }
  298. long double nanl(const char* s) NOEXCEPT
  299. {
  300. return __builtin_nanl(s);
  301. }
  302. double trunc(double x) NOEXCEPT
  303. {
  304. return internal_to_integer(x, RoundingMode::ToZero);
  305. }
  306. float truncf(float x) NOEXCEPT
  307. {
  308. return internal_to_integer(x, RoundingMode::ToZero);
  309. }
  310. long double truncl(long double x) NOEXCEPT
  311. {
  312. return internal_to_integer(x, RoundingMode::ToZero);
  313. }
  314. long double cosl(long double angle) NOEXCEPT
  315. {
  316. return sinl(angle + M_PI_2);
  317. }
  318. double cos(double angle) NOEXCEPT
  319. {
  320. return sin(angle + M_PI_2);
  321. }
  322. float cosf(float angle) NOEXCEPT
  323. {
  324. return sinf(angle + static_cast<float>(M_PI_2));
  325. }
  326. long double sinl(long double angle) NOEXCEPT
  327. {
  328. long double ret = 0.0;
  329. __asm__(
  330. "fsin"
  331. : "=t"(ret)
  332. : "0"(angle));
  333. return ret;
  334. }
  335. // This can also be done with a taylor expansion, but for
  336. // now this works pretty well (and doesn't mess anything up
  337. // in quake in particular, which is very Floating-Point precision
  338. // heavy)
  339. double sin(double angle) NOEXCEPT
  340. {
  341. double ret = 0.0;
  342. __asm__(
  343. "fsin"
  344. : "=t"(ret)
  345. : "0"(angle));
  346. return ret;
  347. }
  348. float sinf(float angle) NOEXCEPT
  349. {
  350. float ret = 0.0f;
  351. __asm__(
  352. "fsin"
  353. : "=t"(ret)
  354. : "0"(angle));
  355. return ret;
  356. }
  357. long double powl(long double x, long double y) NOEXCEPT
  358. {
  359. // FIXME: Please fix me. I am naive.
  360. if (isnan(y))
  361. return y;
  362. if (y == 0)
  363. return 1;
  364. if (x == 0)
  365. return 0;
  366. if (y == 1)
  367. return x;
  368. int y_as_int = (int)y;
  369. if (y == (long double)y_as_int) {
  370. long double result = x;
  371. for (int i = 0; i < fabsl(y) - 1; ++i)
  372. result *= x;
  373. if (y < 0)
  374. result = 1.0l / result;
  375. return result;
  376. }
  377. if (x < 0) {
  378. return 1.l / exp2l(y * log2l(-x));
  379. }
  380. return exp2l(y * log2l(x));
  381. }
  382. double pow(double x, double y) NOEXCEPT
  383. {
  384. return (double)powl(x, y);
  385. }
  386. float powf(float x, float y) NOEXCEPT
  387. {
  388. return (float)powl(x, y);
  389. }
  390. // On systems where FLT_RADIX == 2, ldexp is equivalent to scalbn
  391. long double ldexpl(long double x, int exp) NOEXCEPT
  392. {
  393. return internal_scalbn(x, exp);
  394. }
  395. double ldexp(double x, int exp) NOEXCEPT
  396. {
  397. return internal_scalbn(x, exp);
  398. }
  399. float ldexpf(float x, int exp) NOEXCEPT
  400. {
  401. return internal_scalbn(x, exp);
  402. }
  403. long double tanhl(long double x) NOEXCEPT
  404. {
  405. if (x > 0) {
  406. long double exponentiated = expl(2 * x);
  407. return (exponentiated - 1) / (exponentiated + 1);
  408. }
  409. long double plusX = expl(x);
  410. long double minusX = 1 / plusX;
  411. return (plusX - minusX) / (plusX + minusX);
  412. }
  413. double tanh(double x) NOEXCEPT
  414. {
  415. return (double)tanhl(x);
  416. }
  417. float tanhf(float x) NOEXCEPT
  418. {
  419. return (float)tanhl(x);
  420. }
  421. [[maybe_unused]] static long double ampsin(long double angle) NOEXCEPT
  422. {
  423. long double looped_angle = fmodl(M_PI + angle, M_TAU) - M_PI;
  424. long double looped_angle_squared = looped_angle * looped_angle;
  425. long double quadratic_term;
  426. if (looped_angle > 0) {
  427. quadratic_term = -looped_angle_squared;
  428. } else {
  429. quadratic_term = looped_angle_squared;
  430. }
  431. long double linear_term = M_PI * looped_angle;
  432. return quadratic_term + linear_term;
  433. }
  434. long double tanl(long double angle) NOEXCEPT
  435. {
  436. long double ret = 0.0, one;
  437. __asm__(
  438. "fptan"
  439. : "=t"(one), "=u"(ret)
  440. : "0"(angle));
  441. return ret;
  442. }
  443. double tan(double angle) NOEXCEPT
  444. {
  445. return (double)tanl(angle);
  446. }
  447. float tanf(float angle) NOEXCEPT
  448. {
  449. return (float)tanl(angle);
  450. }
  451. long double sqrtl(long double x) NOEXCEPT
  452. {
  453. long double res;
  454. asm("fsqrt"
  455. : "=t"(res)
  456. : "0"(x));
  457. return res;
  458. }
  459. double sqrt(double x) NOEXCEPT
  460. {
  461. double res;
  462. __asm__("fsqrt"
  463. : "=t"(res)
  464. : "0"(x));
  465. return res;
  466. }
  467. float sqrtf(float x) NOEXCEPT
  468. {
  469. float res;
  470. __asm__("fsqrt"
  471. : "=t"(res)
  472. : "0"(x));
  473. return res;
  474. }
  475. long double sinhl(long double x) NOEXCEPT
  476. {
  477. long double exponentiated = expl(x);
  478. if (x > 0)
  479. return (exponentiated * exponentiated - 1) / 2 / exponentiated;
  480. return (exponentiated - 1 / exponentiated) / 2;
  481. }
  482. double sinh(double x) NOEXCEPT
  483. {
  484. return (double)sinhl(x);
  485. }
  486. float sinhf(float x) NOEXCEPT
  487. {
  488. return (float)sinhl(x);
  489. }
  490. long double log10l(long double x) NOEXCEPT
  491. {
  492. long double ret = 0.0l;
  493. __asm__(
  494. "fldlg2\n"
  495. "fld %%st(1)\n"
  496. "fyl2x\n"
  497. "fstp %%st(1)"
  498. : "=t"(ret)
  499. : "0"(x));
  500. return ret;
  501. }
  502. double log10(double x) NOEXCEPT
  503. {
  504. return (double)log10l(x);
  505. }
  506. float log10f(float x) NOEXCEPT
  507. {
  508. return (float)log10l(x);
  509. }
  510. long double logl(long double x) NOEXCEPT
  511. {
  512. long double ret = 0.0l;
  513. asm(
  514. "fldln2\n"
  515. "fld %%st(1)\n"
  516. "fyl2x\n"
  517. "fstp %%st(1)"
  518. : "=t"(ret)
  519. : "0"(x));
  520. return ret;
  521. }
  522. double log(double x) NOEXCEPT
  523. {
  524. return (double)logl(x);
  525. }
  526. float logf(float x) NOEXCEPT
  527. {
  528. return (float)logl(x);
  529. }
  530. long double fmodl(long double index, long double period) NOEXCEPT
  531. {
  532. return index - truncl(index / period) * period;
  533. }
  534. double fmod(double index, double period) NOEXCEPT
  535. {
  536. return index - trunc(index / period) * period;
  537. }
  538. float fmodf(float index, float period) NOEXCEPT
  539. {
  540. return index - truncf(index / period) * period;
  541. }
  542. // FIXME: These aren't exactly like fmod, but these definitions are probably good enough for now
  543. long double remainderl(long double x, long double y) NOEXCEPT
  544. {
  545. return fmodl(x, y);
  546. }
  547. double remainder(double x, double y) NOEXCEPT
  548. {
  549. return fmod(x, y);
  550. }
  551. float remainderf(float x, float y) NOEXCEPT
  552. {
  553. return fmodf(x, y);
  554. }
  555. long double expl(long double exponent) NOEXCEPT
  556. {
  557. long double res = 0;
  558. asm("fldl2e\n"
  559. "fmulp\n"
  560. "fld1\n"
  561. "fld %%st(1)\n"
  562. "fprem\n"
  563. "f2xm1\n"
  564. "faddp\n"
  565. "fscale\n"
  566. "fstp %%st(1)"
  567. : "=t"(res)
  568. : "0"(exponent));
  569. return res;
  570. }
  571. double exp(double exponent) NOEXCEPT
  572. {
  573. return (double)expl(exponent);
  574. }
  575. float expf(float exponent) NOEXCEPT
  576. {
  577. return (float)expl(exponent);
  578. }
  579. long double exp2l(long double exponent) NOEXCEPT
  580. {
  581. long double res = 0;
  582. asm("fld1\n"
  583. "fld %%st(1)\n"
  584. "fprem\n"
  585. "f2xm1\n"
  586. "faddp\n"
  587. "fscale\n"
  588. "fstp %%st(1)"
  589. : "=t"(res)
  590. : "0"(exponent));
  591. return res;
  592. }
  593. double exp2(double exponent) NOEXCEPT
  594. {
  595. return (double)exp2l(exponent);
  596. }
  597. float exp2f(float exponent) NOEXCEPT
  598. {
  599. return (float)exp2l(exponent);
  600. }
  601. long double coshl(long double x) NOEXCEPT
  602. {
  603. long double exponentiated = expl(-x);
  604. if (x < 0)
  605. return (1 + exponentiated * exponentiated) / 2 / exponentiated;
  606. return (1 / exponentiated + exponentiated) / 2;
  607. }
  608. double cosh(double x) NOEXCEPT
  609. {
  610. return (double)coshl(x);
  611. }
  612. float coshf(float x) NOEXCEPT
  613. {
  614. return (float)coshl(x);
  615. }
  616. long double atan2l(long double y, long double x) NOEXCEPT
  617. {
  618. if (x == 0) {
  619. if (y > 0)
  620. return M_PI_2;
  621. if (y < 0)
  622. return -M_PI_2;
  623. return 0;
  624. }
  625. long double result = 0; //atanl(y / x);
  626. __asm__("fpatan"
  627. : "=t"(result)
  628. : "0"(x), "u"(y)
  629. : "st(1)");
  630. return result;
  631. }
  632. double atan2(double y, double x) NOEXCEPT
  633. {
  634. return (double)atan2l(y, x);
  635. }
  636. float atan2f(float y, float x) NOEXCEPT
  637. {
  638. return (float)atan2l(y, x);
  639. }
  640. long double atanl(long double x) NOEXCEPT
  641. {
  642. if (x < 0)
  643. return -atanl(-x);
  644. if (x > 1)
  645. return M_PI_2 - atanl(1 / x);
  646. long double squared = x * x;
  647. 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)))))));
  648. }
  649. double atan(double x) NOEXCEPT
  650. {
  651. return (double)atanl(x);
  652. }
  653. float atanf(float x) NOEXCEPT
  654. {
  655. return (float)atanl(x);
  656. }
  657. long double asinl(long double x) NOEXCEPT
  658. {
  659. if (x > 1 || x < -1)
  660. return NAN;
  661. if (x > 0.5 || x < -0.5)
  662. return 2 * atanl(x / (1 + sqrtl(1 - x * x)));
  663. long double squared = x * x;
  664. long double value = x;
  665. long double i = x * squared;
  666. value += i * product_odd<1>() / product_even<2>() / 3;
  667. i *= squared;
  668. value += i * product_odd<3>() / product_even<4>() / 5;
  669. i *= squared;
  670. value += i * product_odd<5>() / product_even<6>() / 7;
  671. i *= squared;
  672. value += i * product_odd<7>() / product_even<8>() / 9;
  673. i *= squared;
  674. value += i * product_odd<9>() / product_even<10>() / 11;
  675. i *= squared;
  676. value += i * product_odd<11>() / product_even<12>() / 13;
  677. return value;
  678. }
  679. double asin(double x) NOEXCEPT
  680. {
  681. return (double)asinl(x);
  682. }
  683. float asinf(float x) NOEXCEPT
  684. {
  685. return (float)asinl(x);
  686. }
  687. long double acosl(long double x) NOEXCEPT
  688. {
  689. return M_PI_2 - asinl(x);
  690. }
  691. double acos(double x) NOEXCEPT
  692. {
  693. return M_PI_2 - asin(x);
  694. }
  695. float acosf(float x) NOEXCEPT
  696. {
  697. return static_cast<float>(M_PI_2) - asinf(x);
  698. }
  699. long double fabsl(long double value) NOEXCEPT
  700. {
  701. return value < 0 ? -value : value;
  702. }
  703. double fabs(double value) NOEXCEPT
  704. {
  705. return value < 0 ? -value : value;
  706. }
  707. float fabsf(float value) NOEXCEPT
  708. {
  709. return value < 0 ? -value : value;
  710. }
  711. int ilogbl(long double x) NOEXCEPT
  712. {
  713. return internal_ilogb(x);
  714. }
  715. int ilogb(double x) NOEXCEPT
  716. {
  717. return internal_ilogb(x);
  718. }
  719. int ilogbf(float x) NOEXCEPT
  720. {
  721. return internal_ilogb(x);
  722. }
  723. long double logbl(long double x) NOEXCEPT
  724. {
  725. return ilogbl(x);
  726. }
  727. double logb(double x) NOEXCEPT
  728. {
  729. return ilogb(x);
  730. }
  731. float logbf(float x) NOEXCEPT
  732. {
  733. return ilogbf(x);
  734. }
  735. long double log2l(long double x) NOEXCEPT
  736. {
  737. long double ret = 0.0;
  738. asm(
  739. "fld1\n"
  740. "fld %%st(1)\n"
  741. "fyl2x\n"
  742. "fstp %%st(1)"
  743. : "=t"(ret)
  744. : "0"(x));
  745. return ret;
  746. }
  747. double log2(double x) NOEXCEPT
  748. {
  749. return (double)log2l(x);
  750. }
  751. float log2f(float x) NOEXCEPT
  752. {
  753. return (float)log2l(x);
  754. }
  755. double frexp(double x, int* exp) NOEXCEPT
  756. {
  757. *exp = (x == 0) ? 0 : (1 + ilogb(x));
  758. return scalbn(x, -(*exp));
  759. }
  760. float frexpf(float x, int* exp) NOEXCEPT
  761. {
  762. *exp = (x == 0) ? 0 : (1 + ilogbf(x));
  763. return scalbnf(x, -(*exp));
  764. }
  765. long double frexpl(long double x, int* exp) NOEXCEPT
  766. {
  767. *exp = (x == 0) ? 0 : (1 + ilogbl(x));
  768. return scalbnl(x, -(*exp));
  769. }
  770. double round(double value) NOEXCEPT
  771. {
  772. return internal_to_integer(value, RoundingMode::ToEven);
  773. }
  774. float roundf(float value) NOEXCEPT
  775. {
  776. return internal_to_integer(value, RoundingMode::ToEven);
  777. }
  778. long double roundl(long double value) NOEXCEPT
  779. {
  780. return internal_to_integer(value, RoundingMode::ToEven);
  781. }
  782. long lroundf(float value) NOEXCEPT
  783. {
  784. return internal_to_integer(value, RoundingMode::ToEven);
  785. }
  786. long lround(double value) NOEXCEPT
  787. {
  788. return internal_to_integer(value, RoundingMode::ToEven);
  789. }
  790. long lroundl(long double value) NOEXCEPT
  791. {
  792. return internal_to_integer(value, RoundingMode::ToEven);
  793. }
  794. long long llroundf(float value) NOEXCEPT
  795. {
  796. return internal_to_integer(value, RoundingMode::ToEven);
  797. }
  798. long long llround(double value) NOEXCEPT
  799. {
  800. return internal_to_integer(value, RoundingMode::ToEven);
  801. }
  802. long long llroundd(long double value) NOEXCEPT
  803. {
  804. return internal_to_integer(value, RoundingMode::ToEven);
  805. }
  806. float floorf(float value) NOEXCEPT
  807. {
  808. return internal_to_integer(value, RoundingMode::Down);
  809. }
  810. double floor(double value) NOEXCEPT
  811. {
  812. return internal_to_integer(value, RoundingMode::Down);
  813. }
  814. long double floorl(long double value) NOEXCEPT
  815. {
  816. return internal_to_integer(value, RoundingMode::Down);
  817. }
  818. long double rintl(long double value) NOEXCEPT
  819. {
  820. return internal_to_integer(value, RoundingMode { fegetround() });
  821. }
  822. double rint(double value) NOEXCEPT
  823. {
  824. return internal_to_integer(value, RoundingMode { fegetround() });
  825. }
  826. float rintf(float value) NOEXCEPT
  827. {
  828. return internal_to_integer(value, RoundingMode { fegetround() });
  829. }
  830. long lrintl(long double value) NOEXCEPT
  831. {
  832. return (long)internal_to_integer(value, RoundingMode { fegetround() });
  833. }
  834. long lrint(double value) NOEXCEPT
  835. {
  836. return (long)internal_to_integer(value, RoundingMode { fegetround() });
  837. }
  838. long lrintf(float value) NOEXCEPT
  839. {
  840. return (long)internal_to_integer(value, RoundingMode { fegetround() });
  841. }
  842. long long llrintl(long double value) NOEXCEPT
  843. {
  844. return (long long)internal_to_integer(value, RoundingMode { fegetround() });
  845. }
  846. long long llrint(double value) NOEXCEPT
  847. {
  848. return (long long)internal_to_integer(value, RoundingMode { fegetround() });
  849. }
  850. long long llrintf(float value) NOEXCEPT
  851. {
  852. return (long long)internal_to_integer(value, RoundingMode { fegetround() });
  853. }
  854. float ceilf(float value) NOEXCEPT
  855. {
  856. return internal_to_integer(value, RoundingMode::Up);
  857. }
  858. double ceil(double value) NOEXCEPT
  859. {
  860. return internal_to_integer(value, RoundingMode::Up);
  861. }
  862. long double ceill(long double value) NOEXCEPT
  863. {
  864. return internal_to_integer(value, RoundingMode::Up);
  865. }
  866. long double modfl(long double x, long double* intpart) NOEXCEPT
  867. {
  868. return internal_modf(x, intpart);
  869. }
  870. double modf(double x, double* intpart) NOEXCEPT
  871. {
  872. return internal_modf(x, intpart);
  873. }
  874. float modff(float x, float* intpart) NOEXCEPT
  875. {
  876. return internal_modf(x, intpart);
  877. }
  878. double gamma(double x) NOEXCEPT
  879. {
  880. // Stirling approximation
  881. return sqrt(2.0 * M_PI / x) * pow(x / M_E, x);
  882. }
  883. long double tgammal(long double value) NOEXCEPT
  884. {
  885. return internal_gamma(value);
  886. }
  887. double tgamma(double value) NOEXCEPT
  888. {
  889. return internal_gamma(value);
  890. }
  891. float tgammaf(float value) NOEXCEPT
  892. {
  893. return internal_gamma(value);
  894. }
  895. int signgam = 0;
  896. long double lgammal(long double value) NOEXCEPT
  897. {
  898. return lgammal_r(value, &signgam);
  899. }
  900. double lgamma(double value) NOEXCEPT
  901. {
  902. return lgamma_r(value, &signgam);
  903. }
  904. float lgammaf(float value) NOEXCEPT
  905. {
  906. return lgammaf_r(value, &signgam);
  907. }
  908. long double lgammal_r(long double value, int* sign) NOEXCEPT
  909. {
  910. if (value == 1.0 || value == 2.0)
  911. return 0.0;
  912. if (isinf(value) || value == 0.0)
  913. return INFINITY;
  914. long double result = logl(internal_gamma(value));
  915. *sign = signbit(result) ? -1 : 1;
  916. return result;
  917. }
  918. double lgamma_r(double value, int* sign) NOEXCEPT
  919. {
  920. if (value == 1.0 || value == 2.0)
  921. return 0.0;
  922. if (isinf(value) || value == 0.0)
  923. return INFINITY;
  924. double result = log(internal_gamma(value));
  925. *sign = signbit(result) ? -1 : 1;
  926. return result;
  927. }
  928. float lgammaf_r(float value, int* sign) NOEXCEPT
  929. {
  930. if (value == 1.0f || value == 2.0f)
  931. return 0.0;
  932. if (isinf(value) || value == 0.0f)
  933. return INFINITY;
  934. float result = logf(internal_gamma(value));
  935. *sign = signbit(result) ? -1 : 1;
  936. return result;
  937. }
  938. long double expm1l(long double x) NOEXCEPT
  939. {
  940. return expl(x) - 1;
  941. }
  942. double expm1(double x) NOEXCEPT
  943. {
  944. return exp(x) - 1;
  945. }
  946. float expm1f(float x) NOEXCEPT
  947. {
  948. return expf(x) - 1;
  949. }
  950. long double cbrtl(long double x) NOEXCEPT
  951. {
  952. if (isinf(x) || x == 0)
  953. return x;
  954. if (x < 0)
  955. return -cbrtl(-x);
  956. long double r = x;
  957. long double ex = 0;
  958. while (r < 0.125l) {
  959. r *= 8;
  960. ex--;
  961. }
  962. while (r > 1.0l) {
  963. r *= 0.125l;
  964. ex++;
  965. }
  966. r = (-0.46946116l * r + 1.072302l) * r + 0.3812513l;
  967. while (ex < 0) {
  968. r *= 0.5l;
  969. ex++;
  970. }
  971. while (ex > 0) {
  972. r *= 2.0l;
  973. ex--;
  974. }
  975. r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);
  976. r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);
  977. r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);
  978. r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);
  979. return r;
  980. }
  981. double cbrt(double x) NOEXCEPT
  982. {
  983. return (double)cbrtl(x);
  984. }
  985. float cbrtf(float x) NOEXCEPT
  986. {
  987. return (float)cbrtl(x);
  988. }
  989. long double log1pl(long double x) NOEXCEPT
  990. {
  991. return logl(1 + x);
  992. }
  993. double log1p(double x) NOEXCEPT
  994. {
  995. return log(1 + x);
  996. }
  997. float log1pf(float x) NOEXCEPT
  998. {
  999. return logf(1 + x);
  1000. }
  1001. long double acoshl(long double x) NOEXCEPT
  1002. {
  1003. return logl(x + sqrtl(x * x - 1));
  1004. }
  1005. double acosh(double x) NOEXCEPT
  1006. {
  1007. return log(x + sqrt(x * x - 1));
  1008. }
  1009. float acoshf(float x) NOEXCEPT
  1010. {
  1011. return logf(x + sqrtf(x * x - 1));
  1012. }
  1013. long double asinhl(long double x) NOEXCEPT
  1014. {
  1015. return logl(x + sqrtl(x * x + 1));
  1016. }
  1017. double asinh(double x) NOEXCEPT
  1018. {
  1019. return log(x + sqrt(x * x + 1));
  1020. }
  1021. float asinhf(float x) NOEXCEPT
  1022. {
  1023. return logf(x + sqrtf(x * x + 1));
  1024. }
  1025. long double atanhl(long double x) NOEXCEPT
  1026. {
  1027. return logl((1 + x) / (1 - x)) / 2.0l;
  1028. }
  1029. double atanh(double x) NOEXCEPT
  1030. {
  1031. return log((1 + x) / (1 - x)) / 2.0;
  1032. }
  1033. float atanhf(float x) NOEXCEPT
  1034. {
  1035. return logf((1 + x) / (1 - x)) / 2.0f;
  1036. }
  1037. long double hypotl(long double x, long double y) NOEXCEPT
  1038. {
  1039. return sqrtl(x * x + y * y);
  1040. }
  1041. double hypot(double x, double y) NOEXCEPT
  1042. {
  1043. return sqrt(x * x + y * y);
  1044. }
  1045. float hypotf(float x, float y) NOEXCEPT
  1046. {
  1047. return sqrtf(x * x + y * y);
  1048. }
  1049. long double erfl(long double x) NOEXCEPT
  1050. {
  1051. // algorithm taken from Abramowitz and Stegun (no. 26.2.17)
  1052. long double t = 1 / (1 + 0.47047l * fabsl(x));
  1053. long double poly = t * (0.3480242l + t * (-0.958798l + t * 0.7478556l));
  1054. long double answer = 1 - poly * expl(-x * x);
  1055. if (x < 0)
  1056. return -answer;
  1057. return answer;
  1058. }
  1059. double erf(double x) NOEXCEPT
  1060. {
  1061. return (double)erfl(x);
  1062. }
  1063. float erff(float x) NOEXCEPT
  1064. {
  1065. return (float)erf(x);
  1066. }
  1067. long double erfcl(long double x) NOEXCEPT
  1068. {
  1069. return 1 - erfl(x);
  1070. }
  1071. double erfc(double x) NOEXCEPT
  1072. {
  1073. return 1 - erf(x);
  1074. }
  1075. float erfcf(float x) NOEXCEPT
  1076. {
  1077. return 1 - erff(x);
  1078. }
  1079. double nextafter(double x, double target) NOEXCEPT
  1080. {
  1081. if (x == target)
  1082. return target;
  1083. return internal_nextafter(x, target >= x);
  1084. }
  1085. float nextafterf(float x, float target) NOEXCEPT
  1086. {
  1087. if (x == target)
  1088. return target;
  1089. return internal_nextafter(x, target >= x);
  1090. }
  1091. long double nextafterl(long double x, long double target) NOEXCEPT
  1092. {
  1093. return internal_nextafter(x, target >= x);
  1094. }
  1095. double nexttoward(double x, long double target) NOEXCEPT
  1096. {
  1097. if (x == target)
  1098. return target;
  1099. return internal_nextafter(x, target >= x);
  1100. }
  1101. float nexttowardf(float x, long double target) NOEXCEPT
  1102. {
  1103. if (x == target)
  1104. return target;
  1105. return internal_nextafter(x, target >= x);
  1106. }
  1107. long double nexttowardl(long double x, long double target) NOEXCEPT
  1108. {
  1109. if (x == target)
  1110. return target;
  1111. return internal_nextafter(x, target >= x);
  1112. }
  1113. float copysignf(float x, float y) NOEXCEPT
  1114. {
  1115. return internal_copysign(x, y);
  1116. }
  1117. double copysign(double x, double y) NOEXCEPT
  1118. {
  1119. return internal_copysign(x, y);
  1120. }
  1121. long double copysignl(long double x, long double y) NOEXCEPT
  1122. {
  1123. return internal_copysign(x, y);
  1124. }
  1125. float scalbnf(float x, int exponent) NOEXCEPT
  1126. {
  1127. return internal_scalbn(x, exponent);
  1128. }
  1129. double scalbn(double x, int exponent) NOEXCEPT
  1130. {
  1131. return internal_scalbn(x, exponent);
  1132. }
  1133. long double scalbnl(long double x, int exponent) NOEXCEPT
  1134. {
  1135. return internal_scalbn(x, exponent);
  1136. }
  1137. float scalbnlf(float x, long exponent) NOEXCEPT
  1138. {
  1139. return internal_scalbn(x, exponent);
  1140. }
  1141. double scalbln(double x, long exponent) NOEXCEPT
  1142. {
  1143. return internal_scalbn(x, exponent);
  1144. }
  1145. long double scalblnl(long double x, long exponent) NOEXCEPT
  1146. {
  1147. return internal_scalbn(x, exponent);
  1148. }
  1149. long double fmaxl(long double x, long double y) NOEXCEPT
  1150. {
  1151. if (isnan(x))
  1152. return y;
  1153. if (isnan(y))
  1154. return x;
  1155. return x > y ? x : y;
  1156. }
  1157. double fmax(double x, double y) NOEXCEPT
  1158. {
  1159. if (isnan(x))
  1160. return y;
  1161. if (isnan(y))
  1162. return x;
  1163. return x > y ? x : y;
  1164. }
  1165. float fmaxf(float x, float 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. long double fminl(long double x, long 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. double fmin(double x, double 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. float fminf(float x, float 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. long double nearbyintl(long double value) NOEXCEPT
  1198. {
  1199. return internal_to_integer(value, RoundingMode { fegetround() });
  1200. }
  1201. double nearbyint(double value) NOEXCEPT
  1202. {
  1203. return internal_to_integer(value, RoundingMode { fegetround() });
  1204. }
  1205. float nearbyintf(float value) NOEXCEPT
  1206. {
  1207. return internal_to_integer(value, RoundingMode { fegetround() });
  1208. }
  1209. }