math.cpp 30 KB

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