math.cpp 27 KB

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