math.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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/BuiltinWrappers.h>
  8. #include <AK/ExtraMathConstants.h>
  9. #include <AK/Math.h>
  10. #include <AK/Platform.h>
  11. #include <AK/StdLibExtras.h>
  12. #include <LibC/assert.h>
  13. #include <fenv.h>
  14. #include <math.h>
  15. #include <stdint.h>
  16. #include <stdlib.h>
  17. #ifdef __clang__
  18. # pragma clang diagnostic push
  19. # pragma clang diagnostic ignored "-Wdouble-promotion"
  20. #endif
  21. template<size_t>
  22. constexpr double e_to_power();
  23. template<>
  24. constexpr double e_to_power<0>() { return 1; }
  25. template<size_t exponent>
  26. constexpr double e_to_power() { return M_E * e_to_power<exponent - 1>(); }
  27. template<size_t>
  28. constexpr size_t factorial();
  29. template<>
  30. constexpr size_t factorial<0>() { return 1; }
  31. template<size_t value>
  32. constexpr size_t factorial() { return value * factorial<value - 1>(); }
  33. template<size_t>
  34. constexpr size_t product_even();
  35. template<>
  36. constexpr size_t product_even<2>() { return 2; }
  37. template<size_t value>
  38. constexpr size_t product_even() { return value * product_even<value - 2>(); }
  39. template<size_t>
  40. constexpr size_t product_odd();
  41. template<>
  42. constexpr size_t product_odd<1>() { return 1; }
  43. template<size_t value>
  44. constexpr size_t product_odd() { return value * product_odd<value - 2>(); }
  45. enum class RoundingMode {
  46. ToZero = FE_TOWARDZERO,
  47. Up = FE_UPWARD,
  48. Down = FE_DOWNWARD,
  49. ToEven = FE_TONEAREST
  50. };
  51. template<typename T>
  52. union FloatExtractor;
  53. #if ARCH(I386) || ARCH(X86_64)
  54. // This assumes long double is 80 bits, which is true with GCC on Intel platforms
  55. template<>
  56. union FloatExtractor<long double> {
  57. static const int mantissa_bits = 64;
  58. static const unsigned long long mantissa_max = ~0u;
  59. static const int exponent_bias = 16383;
  60. static const int exponent_bits = 15;
  61. static const unsigned exponent_max = 32767;
  62. struct {
  63. unsigned long long mantissa;
  64. unsigned exponent : 15;
  65. unsigned sign : 1;
  66. };
  67. long double d;
  68. };
  69. #endif
  70. template<>
  71. union FloatExtractor<double> {
  72. static const int mantissa_bits = 52;
  73. static const unsigned long long mantissa_max = (1ull << 52) - 1;
  74. static const int exponent_bias = 1023;
  75. static const int exponent_bits = 11;
  76. static const unsigned exponent_max = 2047;
  77. struct {
  78. unsigned long long mantissa : 52;
  79. unsigned exponent : 11;
  80. unsigned sign : 1;
  81. };
  82. double d;
  83. };
  84. template<>
  85. union FloatExtractor<float> {
  86. static const int mantissa_bits = 23;
  87. static const unsigned mantissa_max = (1 << 23) - 1;
  88. static const int exponent_bias = 127;
  89. static const int exponent_bits = 8;
  90. static const unsigned exponent_max = 255;
  91. struct {
  92. unsigned long long mantissa : 23;
  93. unsigned exponent : 8;
  94. unsigned sign : 1;
  95. };
  96. float d;
  97. };
  98. // This is much branchier than it really needs to be
  99. template<typename FloatType>
  100. static FloatType internal_to_integer(FloatType x, RoundingMode rounding_mode)
  101. {
  102. if (!isfinite(x))
  103. return x;
  104. using Extractor = FloatExtractor<decltype(x)>;
  105. Extractor extractor;
  106. extractor.d = x;
  107. auto unbiased_exponent = extractor.exponent - Extractor::exponent_bias;
  108. bool round = false;
  109. bool guard = false;
  110. if (unbiased_exponent < 0) {
  111. // it was easier to special case [0..1) as it saves us from
  112. // handling subnormals, underflows, etc
  113. if (unbiased_exponent == -1) {
  114. round = true;
  115. }
  116. guard = extractor.mantissa != 0;
  117. extractor.mantissa = 0;
  118. extractor.exponent = 0;
  119. } else {
  120. if (unbiased_exponent >= Extractor::mantissa_bits)
  121. return x;
  122. auto dead_bitcount = Extractor::mantissa_bits - unbiased_exponent;
  123. auto dead_mask = (1ull << dead_bitcount) - 1;
  124. auto dead_bits = extractor.mantissa & dead_mask;
  125. extractor.mantissa &= ~dead_mask;
  126. auto guard_mask = dead_mask >> 1;
  127. guard = (dead_bits & guard_mask) != 0;
  128. round = (dead_bits & ~guard_mask) != 0;
  129. }
  130. bool should_round = false;
  131. switch (rounding_mode) {
  132. case RoundingMode::ToEven:
  133. should_round = round;
  134. break;
  135. case RoundingMode::Up:
  136. if (!extractor.sign)
  137. should_round = guard || round;
  138. break;
  139. case RoundingMode::Down:
  140. if (extractor.sign)
  141. should_round = guard || round;
  142. break;
  143. case RoundingMode::ToZero:
  144. break;
  145. }
  146. if (should_round) {
  147. // We could do this ourselves, but this saves us from manually
  148. // handling overflow.
  149. if (extractor.sign)
  150. extractor.d -= static_cast<FloatType>(1.0);
  151. else
  152. extractor.d += static_cast<FloatType>(1.0);
  153. }
  154. return extractor.d;
  155. }
  156. // This is much branchier than it really needs to be
  157. template<typename FloatType>
  158. static FloatType internal_nextafter(FloatType x, bool up)
  159. {
  160. if (!isfinite(x))
  161. return x;
  162. using Extractor = FloatExtractor<decltype(x)>;
  163. Extractor extractor;
  164. extractor.d = x;
  165. if (x == 0) {
  166. if (!extractor.sign) {
  167. extractor.mantissa = 1;
  168. extractor.sign = !up;
  169. return extractor.d;
  170. }
  171. if (up) {
  172. extractor.sign = false;
  173. extractor.mantissa = 1;
  174. return extractor.d;
  175. }
  176. extractor.mantissa = 1;
  177. extractor.sign = up != extractor.sign;
  178. return extractor.d;
  179. }
  180. if (up != extractor.sign) {
  181. extractor.mantissa++;
  182. if (!extractor.mantissa) {
  183. // no need to normalize the mantissa as we just hit a power
  184. // of two.
  185. extractor.exponent++;
  186. if (extractor.exponent == Extractor::exponent_max) {
  187. extractor.exponent = Extractor::exponent_max - 1;
  188. extractor.mantissa = Extractor::mantissa_max;
  189. }
  190. }
  191. return extractor.d;
  192. }
  193. if (!extractor.mantissa) {
  194. if (extractor.exponent) {
  195. extractor.exponent--;
  196. extractor.mantissa = Extractor::mantissa_max;
  197. } else {
  198. extractor.d = 0;
  199. }
  200. return extractor.d;
  201. }
  202. extractor.mantissa--;
  203. if (extractor.mantissa != Extractor::mantissa_max)
  204. return extractor.d;
  205. if (extractor.exponent) {
  206. extractor.exponent--;
  207. // normalize
  208. extractor.mantissa <<= 1;
  209. } else {
  210. if (extractor.sign) {
  211. // Negative infinity
  212. extractor.mantissa = 0;
  213. extractor.exponent = Extractor::exponent_max;
  214. }
  215. }
  216. return extractor.d;
  217. }
  218. template<typename FloatT>
  219. static int internal_ilogb(FloatT x) NOEXCEPT
  220. {
  221. if (x == 0)
  222. return FP_ILOGB0;
  223. if (isnan(x))
  224. return FP_ILOGNAN;
  225. if (!isfinite(x))
  226. return INT_MAX;
  227. using Extractor = FloatExtractor<FloatT>;
  228. Extractor extractor;
  229. extractor.d = x;
  230. return (int)extractor.exponent - Extractor::exponent_bias;
  231. }
  232. template<typename FloatT>
  233. static FloatT internal_modf(FloatT x, FloatT* intpart) NOEXCEPT
  234. {
  235. FloatT integer_part = internal_to_integer(x, RoundingMode::ToZero);
  236. *intpart = integer_part;
  237. auto fraction = x - integer_part;
  238. if (signbit(fraction) != signbit(x))
  239. fraction = -fraction;
  240. return fraction;
  241. }
  242. template<typename FloatT>
  243. static FloatT internal_scalbn(FloatT x, int exponent) NOEXCEPT
  244. {
  245. if (x == 0 || !isfinite(x) || isnan(x) || exponent == 0)
  246. return x;
  247. using Extractor = FloatExtractor<FloatT>;
  248. Extractor extractor;
  249. extractor.d = x;
  250. if (extractor.exponent != 0) {
  251. extractor.exponent = clamp((int)extractor.exponent + exponent, 0, (int)Extractor::exponent_max);
  252. return extractor.d;
  253. }
  254. unsigned leading_mantissa_zeroes = extractor.mantissa == 0 ? 32 : count_leading_zeroes(extractor.mantissa);
  255. int shift = min((int)leading_mantissa_zeroes, exponent);
  256. exponent = max(exponent - shift, 0);
  257. extractor.exponent <<= shift;
  258. extractor.exponent = exponent + 1;
  259. return extractor.d;
  260. }
  261. template<typename FloatT>
  262. static FloatT internal_copysign(FloatT x, FloatT y) NOEXCEPT
  263. {
  264. using Extractor = FloatExtractor<FloatT>;
  265. Extractor ex, ey;
  266. ex.d = x;
  267. ey.d = y;
  268. ex.sign = ey.sign;
  269. return ex.d;
  270. }
  271. template<typename FloatT>
  272. static FloatT internal_gamma(FloatT x) NOEXCEPT
  273. {
  274. if (isnan(x))
  275. return (FloatT)NAN;
  276. if (x == (FloatT)0.0)
  277. return signbit(x) ? (FloatT)-INFINITY : (FloatT)INFINITY;
  278. if (x < (FloatT)0 && (rintl(x) == x || isinf(x)))
  279. return (FloatT)NAN;
  280. if (isinf(x))
  281. return (FloatT)INFINITY;
  282. using Extractor = FloatExtractor<FloatT>;
  283. // These constants were obtained through use of WolframAlpha
  284. 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)));
  285. 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.");
  286. if ((int)x == x && x <= max_integer_whose_factorial_fits + 1) {
  287. long long result = 1;
  288. for (long long cursor = 2; cursor < (long long)x; cursor++)
  289. result *= cursor;
  290. return (FloatT)result;
  291. }
  292. // Stirling approximation
  293. return sqrtl(2.0 * M_PIl / static_cast<long double>(x)) * powl(static_cast<long double>(x) / M_El, static_cast<long double>(x));
  294. }
  295. extern "C" {
  296. float nanf(const char* s) NOEXCEPT
  297. {
  298. return __builtin_nanf(s);
  299. }
  300. double nan(const char* s) NOEXCEPT
  301. {
  302. return __builtin_nan(s);
  303. }
  304. long double nanl(const char* s) NOEXCEPT
  305. {
  306. return __builtin_nanl(s);
  307. }
  308. #define MAKE_AK_BACKED1(name) \
  309. long double name##l(long double arg) NOEXCEPT \
  310. { \
  311. return AK::name<long double>(arg); \
  312. } \
  313. double name(double arg) NOEXCEPT \
  314. { \
  315. return AK::name<double>(arg); \
  316. } \
  317. float name##f(float arg) NOEXCEPT \
  318. { \
  319. return AK::name<float>(arg); \
  320. }
  321. #define MAKE_AK_BACKED2(name) \
  322. long double name##l(long double arg1, long double arg2) NOEXCEPT \
  323. { \
  324. return AK::name<long double>(arg1, arg2); \
  325. } \
  326. double name(double arg1, double arg2) NOEXCEPT \
  327. { \
  328. return AK::name<double>(arg1, arg2); \
  329. } \
  330. float name##f(float arg1, float arg2) NOEXCEPT \
  331. { \
  332. return AK::name<float>(arg1, arg2); \
  333. }
  334. MAKE_AK_BACKED1(sin);
  335. MAKE_AK_BACKED1(cos);
  336. MAKE_AK_BACKED1(tan);
  337. MAKE_AK_BACKED1(asin);
  338. MAKE_AK_BACKED1(acos);
  339. MAKE_AK_BACKED1(atan);
  340. MAKE_AK_BACKED1(sinh);
  341. MAKE_AK_BACKED1(cosh);
  342. MAKE_AK_BACKED1(tanh);
  343. MAKE_AK_BACKED1(asinh);
  344. MAKE_AK_BACKED1(acosh);
  345. MAKE_AK_BACKED1(atanh);
  346. MAKE_AK_BACKED1(sqrt);
  347. MAKE_AK_BACKED1(cbrt);
  348. MAKE_AK_BACKED1(log);
  349. MAKE_AK_BACKED1(log2);
  350. MAKE_AK_BACKED1(log10);
  351. MAKE_AK_BACKED1(exp);
  352. MAKE_AK_BACKED1(exp2);
  353. MAKE_AK_BACKED1(fabs);
  354. MAKE_AK_BACKED2(atan2);
  355. MAKE_AK_BACKED2(hypot);
  356. MAKE_AK_BACKED2(fmod);
  357. MAKE_AK_BACKED2(pow);
  358. MAKE_AK_BACKED2(remainder);
  359. long double truncl(long double x) NOEXCEPT
  360. {
  361. if (fabsl(x) < LONG_LONG_MAX) {
  362. // This is 1.6 times faster than the implementation using the "internal_to_integer"
  363. // helper (on x86_64)
  364. // https://quick-bench.com/q/xBmxuY8am9qibSYVna90Y6PIvqA
  365. u64 temp;
  366. asm(
  367. "fisttpq %[temp]\n"
  368. "fildq %[temp]"
  369. : "+t"(x)
  370. : [temp] "m"(temp));
  371. return x;
  372. }
  373. return internal_to_integer(x, RoundingMode::ToZero);
  374. }
  375. double trunc(double x) NOEXCEPT
  376. {
  377. if (fabs(x) < LONG_LONG_MAX) {
  378. u64 temp;
  379. asm(
  380. "fisttpq %[temp]\n"
  381. "fildq %[temp]"
  382. : "+t"(x)
  383. : [temp] "m"(temp));
  384. return x;
  385. }
  386. return internal_to_integer(x, RoundingMode::ToZero);
  387. }
  388. float truncf(float x) NOEXCEPT
  389. {
  390. if (fabsf(x) < LONG_LONG_MAX) {
  391. u64 temp;
  392. asm(
  393. "fisttpq %[temp]\n"
  394. "fildq %[temp]"
  395. : "+t"(x)
  396. : [temp] "m"(temp));
  397. return x;
  398. }
  399. return internal_to_integer(x, RoundingMode::ToZero);
  400. }
  401. long double rintl(long double value)
  402. {
  403. double res;
  404. asm(
  405. "frndint\n"
  406. : "=t"(res)
  407. : "0"(value));
  408. return res;
  409. }
  410. double rint(double value)
  411. {
  412. double res;
  413. asm(
  414. "frndint\n"
  415. : "=t"(res)
  416. : "0"(value));
  417. return res;
  418. }
  419. float rintf(float value)
  420. {
  421. double res;
  422. asm(
  423. "frndint\n"
  424. : "=t"(res)
  425. : "0"(value));
  426. return res;
  427. }
  428. long lrintl(long double value)
  429. {
  430. long res;
  431. asm(
  432. "fistpl %0\n"
  433. : "+m"(res)
  434. : "t"(value)
  435. : "st");
  436. return res;
  437. }
  438. long lrint(double value)
  439. {
  440. long res;
  441. asm(
  442. "fistpl %0\n"
  443. : "+m"(res)
  444. : "t"(value)
  445. : "st");
  446. return res;
  447. }
  448. long lrintf(float value)
  449. {
  450. long res;
  451. asm(
  452. "fistpl %0\n"
  453. : "+m"(res)
  454. : "t"(value)
  455. : "st");
  456. return res;
  457. }
  458. long long llrintl(long double value)
  459. {
  460. long long res;
  461. asm(
  462. "fistpq %0\n"
  463. : "+m"(res)
  464. : "t"(value)
  465. : "st");
  466. return res;
  467. }
  468. long long llrint(double value)
  469. {
  470. long long res;
  471. asm(
  472. "fistpq %0\n"
  473. : "+m"(res)
  474. : "t"(value)
  475. : "st");
  476. return res;
  477. }
  478. long long llrintf(float value)
  479. {
  480. long long res;
  481. asm(
  482. "fistpq %0\n"
  483. : "+m"(res)
  484. : "t"(value)
  485. : "st");
  486. return res;
  487. }
  488. // On systems where FLT_RADIX == 2, ldexp is equivalent to scalbn
  489. long double ldexpl(long double x, int exp) NOEXCEPT
  490. {
  491. return internal_scalbn(x, exp);
  492. }
  493. double ldexp(double x, int exp) NOEXCEPT
  494. {
  495. return internal_scalbn(x, exp);
  496. }
  497. float ldexpf(float x, int exp) NOEXCEPT
  498. {
  499. return internal_scalbn(x, exp);
  500. }
  501. [[maybe_unused]] static long double ampsin(long double angle) NOEXCEPT
  502. {
  503. long double looped_angle = fmodl(M_PI + angle, M_TAU) - M_PI;
  504. long double looped_angle_squared = looped_angle * looped_angle;
  505. long double quadratic_term;
  506. if (looped_angle > 0) {
  507. quadratic_term = -looped_angle_squared;
  508. } else {
  509. quadratic_term = looped_angle_squared;
  510. }
  511. long double linear_term = M_PI * looped_angle;
  512. return quadratic_term + linear_term;
  513. }
  514. int ilogbl(long double x) NOEXCEPT
  515. {
  516. return internal_ilogb(x);
  517. }
  518. int ilogb(double x) NOEXCEPT
  519. {
  520. return internal_ilogb(x);
  521. }
  522. int ilogbf(float x) NOEXCEPT
  523. {
  524. return internal_ilogb(x);
  525. }
  526. long double logbl(long double x) NOEXCEPT
  527. {
  528. return ilogbl(x);
  529. }
  530. double logb(double x) NOEXCEPT
  531. {
  532. return ilogb(x);
  533. }
  534. float logbf(float x) NOEXCEPT
  535. {
  536. return ilogbf(x);
  537. }
  538. double frexp(double x, int* exp) NOEXCEPT
  539. {
  540. *exp = (x == 0) ? 0 : (1 + ilogb(x));
  541. return scalbn(x, -(*exp));
  542. }
  543. float frexpf(float x, int* exp) NOEXCEPT
  544. {
  545. *exp = (x == 0) ? 0 : (1 + ilogbf(x));
  546. return scalbnf(x, -(*exp));
  547. }
  548. long double frexpl(long double x, int* exp) NOEXCEPT
  549. {
  550. *exp = (x == 0) ? 0 : (1 + ilogbl(x));
  551. return scalbnl(x, -(*exp));
  552. }
  553. double round(double value) NOEXCEPT
  554. {
  555. return internal_to_integer(value, RoundingMode::ToEven);
  556. }
  557. float roundf(float value) NOEXCEPT
  558. {
  559. return internal_to_integer(value, RoundingMode::ToEven);
  560. }
  561. long double roundl(long double value) NOEXCEPT
  562. {
  563. return internal_to_integer(value, RoundingMode::ToEven);
  564. }
  565. long lroundf(float value) NOEXCEPT
  566. {
  567. return internal_to_integer(value, RoundingMode::ToEven);
  568. }
  569. long lround(double value) NOEXCEPT
  570. {
  571. return internal_to_integer(value, RoundingMode::ToEven);
  572. }
  573. long lroundl(long double value) NOEXCEPT
  574. {
  575. return internal_to_integer(value, RoundingMode::ToEven);
  576. }
  577. long long llroundf(float value) NOEXCEPT
  578. {
  579. return internal_to_integer(value, RoundingMode::ToEven);
  580. }
  581. long long llround(double value) NOEXCEPT
  582. {
  583. return internal_to_integer(value, RoundingMode::ToEven);
  584. }
  585. long long llroundd(long double value) NOEXCEPT
  586. {
  587. return internal_to_integer(value, RoundingMode::ToEven);
  588. }
  589. float floorf(float value) NOEXCEPT
  590. {
  591. return internal_to_integer(value, RoundingMode::Down);
  592. }
  593. double floor(double value) NOEXCEPT
  594. {
  595. return internal_to_integer(value, RoundingMode::Down);
  596. }
  597. long double floorl(long double value) NOEXCEPT
  598. {
  599. return internal_to_integer(value, RoundingMode::Down);
  600. }
  601. float ceilf(float value) NOEXCEPT
  602. {
  603. return internal_to_integer(value, RoundingMode::Up);
  604. }
  605. double ceil(double value) NOEXCEPT
  606. {
  607. return internal_to_integer(value, RoundingMode::Up);
  608. }
  609. long double ceill(long double value) NOEXCEPT
  610. {
  611. return internal_to_integer(value, RoundingMode::Up);
  612. }
  613. long double modfl(long double x, long double* intpart) NOEXCEPT
  614. {
  615. return internal_modf(x, intpart);
  616. }
  617. double modf(double x, double* intpart) NOEXCEPT
  618. {
  619. return internal_modf(x, intpart);
  620. }
  621. float modff(float x, float* intpart) NOEXCEPT
  622. {
  623. return internal_modf(x, intpart);
  624. }
  625. double gamma(double x) NOEXCEPT
  626. {
  627. // Stirling approximation
  628. return sqrt(2.0 * M_PI / x) * pow(x / M_E, x);
  629. }
  630. long double tgammal(long double value) NOEXCEPT
  631. {
  632. return internal_gamma(value);
  633. }
  634. double tgamma(double value) NOEXCEPT
  635. {
  636. return internal_gamma(value);
  637. }
  638. float tgammaf(float value) NOEXCEPT
  639. {
  640. return internal_gamma(value);
  641. }
  642. int signgam = 0;
  643. long double lgammal(long double value) NOEXCEPT
  644. {
  645. return lgammal_r(value, &signgam);
  646. }
  647. double lgamma(double value) NOEXCEPT
  648. {
  649. return lgamma_r(value, &signgam);
  650. }
  651. float lgammaf(float value) NOEXCEPT
  652. {
  653. return lgammaf_r(value, &signgam);
  654. }
  655. long double lgammal_r(long double value, int* sign) NOEXCEPT
  656. {
  657. if (value == 1.0 || value == 2.0)
  658. return 0.0;
  659. if (isinf(value) || value == 0.0)
  660. return INFINITY;
  661. long double result = logl(internal_gamma(value));
  662. *sign = signbit(result) ? -1 : 1;
  663. return result;
  664. }
  665. double lgamma_r(double value, int* sign) NOEXCEPT
  666. {
  667. if (value == 1.0 || value == 2.0)
  668. return 0.0;
  669. if (isinf(value) || value == 0.0)
  670. return INFINITY;
  671. double result = log(internal_gamma(value));
  672. *sign = signbit(result) ? -1 : 1;
  673. return result;
  674. }
  675. float lgammaf_r(float value, int* sign) NOEXCEPT
  676. {
  677. if (value == 1.0f || value == 2.0f)
  678. return 0.0;
  679. if (isinf(value) || value == 0.0f)
  680. return INFINITY;
  681. float result = logf(internal_gamma(value));
  682. *sign = signbit(result) ? -1 : 1;
  683. return result;
  684. }
  685. long double expm1l(long double x) NOEXCEPT
  686. {
  687. return expl(x) - 1;
  688. }
  689. double expm1(double x) NOEXCEPT
  690. {
  691. return exp(x) - 1;
  692. }
  693. float expm1f(float x) NOEXCEPT
  694. {
  695. return expf(x) - 1;
  696. }
  697. long double log1pl(long double x) NOEXCEPT
  698. {
  699. return logl(1 + x);
  700. }
  701. double log1p(double x) NOEXCEPT
  702. {
  703. return log(1 + x);
  704. }
  705. float log1pf(float x) NOEXCEPT
  706. {
  707. return logf(1 + x);
  708. }
  709. long double erfl(long double x) NOEXCEPT
  710. {
  711. // algorithm taken from Abramowitz and Stegun (no. 26.2.17)
  712. long double t = 1 / (1 + 0.47047l * fabsl(x));
  713. long double poly = t * (0.3480242l + t * (-0.958798l + t * 0.7478556l));
  714. long double answer = 1 - poly * expl(-x * x);
  715. if (x < 0)
  716. return -answer;
  717. return answer;
  718. }
  719. double erf(double x) NOEXCEPT
  720. {
  721. return (double)erfl(x);
  722. }
  723. float erff(float x) NOEXCEPT
  724. {
  725. return (float)erf(x);
  726. }
  727. long double erfcl(long double x) NOEXCEPT
  728. {
  729. return 1 - erfl(x);
  730. }
  731. double erfc(double x) NOEXCEPT
  732. {
  733. return 1 - erf(x);
  734. }
  735. float erfcf(float x) NOEXCEPT
  736. {
  737. return 1 - erff(x);
  738. }
  739. double nextafter(double x, double target) NOEXCEPT
  740. {
  741. if (x == target)
  742. return target;
  743. return internal_nextafter(x, target >= x);
  744. }
  745. float nextafterf(float x, float target) NOEXCEPT
  746. {
  747. if (x == target)
  748. return target;
  749. return internal_nextafter(x, target >= x);
  750. }
  751. long double nextafterl(long double x, long double target) NOEXCEPT
  752. {
  753. return internal_nextafter(x, target >= x);
  754. }
  755. double nexttoward(double x, long double target) NOEXCEPT
  756. {
  757. if (x == target)
  758. return target;
  759. return internal_nextafter(x, target >= x);
  760. }
  761. float nexttowardf(float x, long double target) NOEXCEPT
  762. {
  763. if (x == target)
  764. return target;
  765. return internal_nextafter(x, target >= x);
  766. }
  767. long double nexttowardl(long double x, long double target) NOEXCEPT
  768. {
  769. if (x == target)
  770. return target;
  771. return internal_nextafter(x, target >= x);
  772. }
  773. float copysignf(float x, float y) NOEXCEPT
  774. {
  775. return internal_copysign(x, y);
  776. }
  777. double copysign(double x, double y) NOEXCEPT
  778. {
  779. return internal_copysign(x, y);
  780. }
  781. long double copysignl(long double x, long double y) NOEXCEPT
  782. {
  783. return internal_copysign(x, y);
  784. }
  785. float scalbnf(float x, int exponent) NOEXCEPT
  786. {
  787. return internal_scalbn(x, exponent);
  788. }
  789. double scalbn(double x, int exponent) NOEXCEPT
  790. {
  791. return internal_scalbn(x, exponent);
  792. }
  793. long double scalbnl(long double x, int exponent) NOEXCEPT
  794. {
  795. return internal_scalbn(x, exponent);
  796. }
  797. float scalbnlf(float x, long exponent) NOEXCEPT
  798. {
  799. return internal_scalbn(x, exponent);
  800. }
  801. double scalbln(double x, long exponent) NOEXCEPT
  802. {
  803. return internal_scalbn(x, exponent);
  804. }
  805. long double scalblnl(long double x, long exponent) NOEXCEPT
  806. {
  807. return internal_scalbn(x, exponent);
  808. }
  809. long double fmaxl(long double x, long double y) NOEXCEPT
  810. {
  811. if (isnan(x))
  812. return y;
  813. if (isnan(y))
  814. return x;
  815. return x > y ? x : y;
  816. }
  817. double fmax(double x, double y) NOEXCEPT
  818. {
  819. if (isnan(x))
  820. return y;
  821. if (isnan(y))
  822. return x;
  823. return x > y ? x : y;
  824. }
  825. float fmaxf(float x, float y) NOEXCEPT
  826. {
  827. if (isnan(x))
  828. return y;
  829. if (isnan(y))
  830. return x;
  831. return x > y ? x : y;
  832. }
  833. long double fminl(long double x, long double y) NOEXCEPT
  834. {
  835. if (isnan(x))
  836. return y;
  837. if (isnan(y))
  838. return x;
  839. return x < y ? x : y;
  840. }
  841. double fmin(double x, double y) NOEXCEPT
  842. {
  843. if (isnan(x))
  844. return y;
  845. if (isnan(y))
  846. return x;
  847. return x < y ? x : y;
  848. }
  849. float fminf(float x, float y) NOEXCEPT
  850. {
  851. if (isnan(x))
  852. return y;
  853. if (isnan(y))
  854. return x;
  855. return x < y ? x : y;
  856. }
  857. long double nearbyintl(long double value) NOEXCEPT
  858. {
  859. return internal_to_integer(value, RoundingMode { fegetround() });
  860. }
  861. double nearbyint(double value) NOEXCEPT
  862. {
  863. return internal_to_integer(value, RoundingMode { fegetround() });
  864. }
  865. float nearbyintf(float value) NOEXCEPT
  866. {
  867. return internal_to_integer(value, RoundingMode { fegetround() });
  868. }
  869. }
  870. #ifdef __clang__
  871. # pragma clang diagnostic pop
  872. #endif