math.cpp 23 KB

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