math.cpp 23 KB

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