math.cpp 25 KB

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