math.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Mițca Dumitru <dumitru0mitca@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/ExtraMathConstants.h>
  8. #include <AK/Platform.h>
  9. #include <AK/StdLibExtras.h>
  10. #include <LibC/assert.h>
  11. #include <fenv.h>
  12. #include <math.h>
  13. #include <stdint.h>
  14. #include <stdlib.h>
  15. #ifdef __clang__
  16. # pragma clang diagnostic push
  17. # pragma clang diagnostic ignored "-Wdouble-promotion"
  18. #endif
  19. template<size_t>
  20. constexpr double e_to_power();
  21. template<>
  22. constexpr double e_to_power<0>() { return 1; }
  23. template<size_t exponent>
  24. constexpr double e_to_power() { return M_E * e_to_power<exponent - 1>(); }
  25. template<size_t>
  26. constexpr size_t factorial();
  27. template<>
  28. constexpr size_t factorial<0>() { return 1; }
  29. template<size_t value>
  30. constexpr size_t factorial() { return value * factorial<value - 1>(); }
  31. template<size_t>
  32. constexpr size_t product_even();
  33. template<>
  34. constexpr size_t product_even<2>() { return 2; }
  35. template<size_t value>
  36. constexpr size_t product_even() { return value * product_even<value - 2>(); }
  37. template<size_t>
  38. constexpr size_t product_odd();
  39. template<>
  40. constexpr size_t product_odd<1>() { return 1; }
  41. template<size_t value>
  42. constexpr size_t product_odd() { return value * product_odd<value - 2>(); }
  43. enum class RoundingMode {
  44. ToZero = FE_TOWARDZERO,
  45. Up = FE_UPWARD,
  46. Down = FE_DOWNWARD,
  47. ToEven = FE_TONEAREST
  48. };
  49. template<typename T>
  50. union FloatExtractor;
  51. #if ARCH(I386) || ARCH(X86_64)
  52. // This assumes long double is 80 bits, which is true with GCC on Intel platforms
  53. template<>
  54. union FloatExtractor<long double> {
  55. static const int mantissa_bits = 64;
  56. static const unsigned long long mantissa_max = ~0u;
  57. static const int exponent_bias = 16383;
  58. static const int exponent_bits = 15;
  59. static const unsigned exponent_max = 32767;
  60. struct {
  61. unsigned long long mantissa;
  62. unsigned exponent : 15;
  63. unsigned sign : 1;
  64. };
  65. long double d;
  66. };
  67. #endif
  68. template<>
  69. union FloatExtractor<double> {
  70. static const int mantissa_bits = 52;
  71. static const unsigned long long mantissa_max = (1ull << 52) - 1;
  72. static const int exponent_bias = 1023;
  73. static const int exponent_bits = 11;
  74. static const unsigned exponent_max = 2047;
  75. struct {
  76. unsigned long long mantissa : 52;
  77. unsigned exponent : 11;
  78. unsigned sign : 1;
  79. };
  80. double d;
  81. };
  82. template<>
  83. union FloatExtractor<float> {
  84. static const int mantissa_bits = 23;
  85. static const unsigned mantissa_max = (1 << 23) - 1;
  86. static const int exponent_bias = 127;
  87. static const int exponent_bits = 8;
  88. static const unsigned exponent_max = 255;
  89. struct {
  90. unsigned long long mantissa : 23;
  91. unsigned exponent : 8;
  92. unsigned sign : 1;
  93. };
  94. float d;
  95. };
  96. // This is much branchier than it really needs to be
  97. template<typename FloatType>
  98. static FloatType internal_to_integer(FloatType x, RoundingMode rounding_mode)
  99. {
  100. if (!isfinite(x))
  101. return x;
  102. using Extractor = FloatExtractor<decltype(x)>;
  103. Extractor extractor;
  104. extractor.d = x;
  105. auto unbiased_exponent = extractor.exponent - Extractor::exponent_bias;
  106. bool round = false;
  107. bool guard = false;
  108. if (unbiased_exponent < 0) {
  109. // it was easier to special case [0..1) as it saves us from
  110. // handling subnormals, underflows, etc
  111. if (unbiased_exponent == -1) {
  112. round = true;
  113. }
  114. guard = extractor.mantissa != 0;
  115. extractor.mantissa = 0;
  116. extractor.exponent = 0;
  117. } else {
  118. if (unbiased_exponent >= Extractor::mantissa_bits)
  119. return x;
  120. auto dead_bitcount = Extractor::mantissa_bits - unbiased_exponent;
  121. auto dead_mask = (1ull << dead_bitcount) - 1;
  122. auto dead_bits = extractor.mantissa & dead_mask;
  123. extractor.mantissa &= ~dead_mask;
  124. auto guard_mask = dead_mask >> 1;
  125. guard = (dead_bits & guard_mask) != 0;
  126. round = (dead_bits & ~guard_mask) != 0;
  127. }
  128. bool should_round = false;
  129. switch (rounding_mode) {
  130. case RoundingMode::ToEven:
  131. should_round = round;
  132. break;
  133. case RoundingMode::Up:
  134. if (!extractor.sign)
  135. should_round = guard || round;
  136. break;
  137. case RoundingMode::Down:
  138. if (extractor.sign)
  139. should_round = guard || round;
  140. break;
  141. case RoundingMode::ToZero:
  142. break;
  143. }
  144. if (should_round) {
  145. // We could do this ourselves, but this saves us from manually
  146. // handling overflow.
  147. if (extractor.sign)
  148. extractor.d -= static_cast<FloatType>(1.0);
  149. else
  150. extractor.d += static_cast<FloatType>(1.0);
  151. }
  152. return extractor.d;
  153. }
  154. // This is much branchier than it really needs to be
  155. template<typename FloatType>
  156. static FloatType internal_nextafter(FloatType x, bool up)
  157. {
  158. if (!isfinite(x))
  159. return x;
  160. using Extractor = FloatExtractor<decltype(x)>;
  161. Extractor extractor;
  162. extractor.d = x;
  163. if (x == 0) {
  164. if (!extractor.sign) {
  165. extractor.mantissa = 1;
  166. extractor.sign = !up;
  167. return extractor.d;
  168. }
  169. if (up) {
  170. extractor.sign = false;
  171. extractor.mantissa = 1;
  172. return extractor.d;
  173. }
  174. extractor.mantissa = 1;
  175. extractor.sign = up != extractor.sign;
  176. return extractor.d;
  177. }
  178. if (up != extractor.sign) {
  179. extractor.mantissa++;
  180. if (!extractor.mantissa) {
  181. // no need to normalize the mantissa as we just hit a power
  182. // of two.
  183. extractor.exponent++;
  184. if (extractor.exponent == Extractor::exponent_max) {
  185. extractor.exponent = Extractor::exponent_max - 1;
  186. extractor.mantissa = Extractor::mantissa_max;
  187. }
  188. }
  189. return extractor.d;
  190. }
  191. if (!extractor.mantissa) {
  192. if (extractor.exponent) {
  193. extractor.exponent--;
  194. extractor.mantissa = Extractor::mantissa_max;
  195. } else {
  196. extractor.d = 0;
  197. }
  198. return extractor.d;
  199. }
  200. extractor.mantissa--;
  201. if (extractor.mantissa != Extractor::mantissa_max)
  202. return extractor.d;
  203. if (extractor.exponent) {
  204. extractor.exponent--;
  205. // normalize
  206. extractor.mantissa <<= 1;
  207. } else {
  208. if (extractor.sign) {
  209. // Negative infinity
  210. extractor.mantissa = 0;
  211. extractor.exponent = Extractor::exponent_max;
  212. }
  213. }
  214. return extractor.d;
  215. }
  216. template<typename FloatT>
  217. static int internal_ilogb(FloatT x) NOEXCEPT
  218. {
  219. if (x == 0)
  220. return FP_ILOGB0;
  221. if (isnan(x))
  222. return FP_ILOGNAN;
  223. if (!isfinite(x))
  224. return INT_MAX;
  225. using Extractor = FloatExtractor<FloatT>;
  226. Extractor extractor;
  227. extractor.d = x;
  228. return (int)extractor.exponent - Extractor::exponent_bias;
  229. }
  230. template<typename FloatT>
  231. static FloatT internal_modf(FloatT x, FloatT* intpart) NOEXCEPT
  232. {
  233. FloatT integer_part = internal_to_integer(x, RoundingMode::ToZero);
  234. *intpart = integer_part;
  235. auto fraction = x - integer_part;
  236. if (signbit(fraction) != signbit(x))
  237. fraction = -fraction;
  238. return fraction;
  239. }
  240. template<typename FloatT>
  241. static FloatT internal_scalbn(FloatT x, int exponent) NOEXCEPT
  242. {
  243. if (x == 0 || !isfinite(x) || isnan(x) || exponent == 0)
  244. return x;
  245. using Extractor = FloatExtractor<FloatT>;
  246. Extractor extractor;
  247. extractor.d = x;
  248. if (extractor.exponent != 0) {
  249. extractor.exponent = clamp((int)extractor.exponent + exponent, 0, (int)Extractor::exponent_max);
  250. return extractor.d;
  251. }
  252. unsigned leading_mantissa_zeroes = extractor.mantissa == 0 ? 32 : __builtin_clz(extractor.mantissa);
  253. int shift = min((int)leading_mantissa_zeroes, exponent);
  254. exponent = max(exponent - shift, 0);
  255. extractor.exponent <<= shift;
  256. extractor.exponent = exponent + 1;
  257. return extractor.d;
  258. }
  259. template<typename FloatT>
  260. static FloatT internal_copysign(FloatT x, FloatT y) NOEXCEPT
  261. {
  262. using Extractor = FloatExtractor<FloatT>;
  263. Extractor ex, ey;
  264. ex.d = x;
  265. ey.d = y;
  266. ex.sign = ey.sign;
  267. return ex.d;
  268. }
  269. template<typename FloatT>
  270. static FloatT internal_gamma(FloatT x) NOEXCEPT
  271. {
  272. if (isnan(x))
  273. return (FloatT)NAN;
  274. if (x == (FloatT)0.0)
  275. return signbit(x) ? (FloatT)-INFINITY : (FloatT)INFINITY;
  276. if (x < (FloatT)0 && (rintl(x) == x || isinf(x)))
  277. return (FloatT)NAN;
  278. if (isinf(x))
  279. return (FloatT)INFINITY;
  280. using Extractor = FloatExtractor<FloatT>;
  281. // These constants were obtained through use of WolframAlpha
  282. 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)));
  283. 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.");
  284. if (rintl(x) == (long double)x && x <= max_integer_whose_factorial_fits) {
  285. long long result = 1;
  286. for (long long cursor = 1; cursor <= min(max_integer_whose_factorial_fits, (long long)x); cursor++)
  287. result *= cursor;
  288. return (FloatT)result;
  289. }
  290. // Stirling approximation
  291. return sqrtl(2.0 * M_PIl / static_cast<long double>(x)) * powl(static_cast<long double>(x) / M_El, static_cast<long double>(x));
  292. }
  293. extern "C" {
  294. float nanf(const char* s) NOEXCEPT
  295. {
  296. return __builtin_nanf(s);
  297. }
  298. double nan(const char* s) NOEXCEPT
  299. {
  300. return __builtin_nan(s);
  301. }
  302. long double nanl(const char* s) NOEXCEPT
  303. {
  304. return __builtin_nanl(s);
  305. }
  306. double trunc(double x) NOEXCEPT
  307. {
  308. return internal_to_integer(x, RoundingMode::ToZero);
  309. }
  310. float truncf(float x) NOEXCEPT
  311. {
  312. return internal_to_integer(x, RoundingMode::ToZero);
  313. }
  314. long double truncl(long double x) NOEXCEPT
  315. {
  316. return internal_to_integer(x, RoundingMode::ToZero);
  317. }
  318. long double cosl(long double angle) NOEXCEPT
  319. {
  320. long double ret = 0.0;
  321. asm(
  322. "fcos"
  323. : "=t"(ret)
  324. : "0"(angle));
  325. return ret;
  326. }
  327. double cos(double angle) NOEXCEPT
  328. {
  329. double ret = 0.0;
  330. asm(
  331. "fcos"
  332. : "=t"(ret)
  333. : "0"(angle));
  334. return ret;
  335. }
  336. float cosf(float angle) NOEXCEPT
  337. {
  338. float ret = 0.0;
  339. asm(
  340. "fcos"
  341. : "=t"(ret)
  342. : "0"(angle));
  343. return ret;
  344. }
  345. long double sinl(long double angle) NOEXCEPT
  346. {
  347. long double ret = 0.0;
  348. asm(
  349. "fsin"
  350. : "=t"(ret)
  351. : "0"(angle));
  352. return ret;
  353. }
  354. // This can also be done with a taylor expansion, but for
  355. // now this works pretty well (and doesn't mess anything up
  356. // in quake in particular, which is very Floating-Point precision
  357. // heavy)
  358. double sin(double angle) NOEXCEPT
  359. {
  360. double ret = 0.0;
  361. asm(
  362. "fsin"
  363. : "=t"(ret)
  364. : "0"(angle));
  365. return ret;
  366. }
  367. float sinf(float angle) NOEXCEPT
  368. {
  369. float ret = 0.0f;
  370. asm(
  371. "fsin"
  372. : "=t"(ret)
  373. : "0"(angle));
  374. return ret;
  375. }
  376. long double powl(long double x, long double y) NOEXCEPT
  377. {
  378. // FIXME: Please fix me. I am naive.
  379. if (isnan(y))
  380. return y;
  381. if (y == 0)
  382. return 1;
  383. if (x == 0)
  384. return 0;
  385. if (y == 1)
  386. return x;
  387. int y_as_int = (int)y;
  388. if (y == (long double)y_as_int) {
  389. long double result = x;
  390. for (int i = 0; i < fabsl(y) - 1; ++i)
  391. result *= x;
  392. if (y < 0)
  393. result = 1.0l / result;
  394. return result;
  395. }
  396. if (x < 0) {
  397. return 1.l / exp2l(y * log2l(-x));
  398. }
  399. return exp2l(y * log2l(x));
  400. }
  401. double pow(double x, double y) NOEXCEPT
  402. {
  403. return (double)powl(x, y);
  404. }
  405. float powf(float x, float y) NOEXCEPT
  406. {
  407. return (float)powl(x, y);
  408. }
  409. // On systems where FLT_RADIX == 2, ldexp is equivalent to scalbn
  410. long double ldexpl(long double x, int exp) NOEXCEPT
  411. {
  412. return internal_scalbn(x, exp);
  413. }
  414. double ldexp(double x, int exp) NOEXCEPT
  415. {
  416. return internal_scalbn(x, exp);
  417. }
  418. float ldexpf(float x, int exp) NOEXCEPT
  419. {
  420. return internal_scalbn(x, exp);
  421. }
  422. long double tanhl(long double x) NOEXCEPT
  423. {
  424. if (x > 0) {
  425. long double exponentiated = expl(2 * x);
  426. return (exponentiated - 1) / (exponentiated + 1);
  427. }
  428. long double plusX = expl(x);
  429. long double minusX = 1 / plusX;
  430. return (plusX - minusX) / (plusX + minusX);
  431. }
  432. double tanh(double x) NOEXCEPT
  433. {
  434. return (double)tanhl(x);
  435. }
  436. float tanhf(float x) NOEXCEPT
  437. {
  438. return (float)tanhl(x);
  439. }
  440. [[maybe_unused]] static long double ampsin(long double angle) NOEXCEPT
  441. {
  442. long double looped_angle = fmodl(M_PI + angle, M_TAU) - M_PI;
  443. long double looped_angle_squared = looped_angle * looped_angle;
  444. long double quadratic_term;
  445. if (looped_angle > 0) {
  446. quadratic_term = -looped_angle_squared;
  447. } else {
  448. quadratic_term = looped_angle_squared;
  449. }
  450. long double linear_term = M_PI * looped_angle;
  451. return quadratic_term + linear_term;
  452. }
  453. long double tanl(long double angle) NOEXCEPT
  454. {
  455. long double ret = 0.0, one;
  456. __asm__(
  457. "fptan"
  458. : "=t"(one), "=u"(ret)
  459. : "0"(angle));
  460. return ret;
  461. }
  462. double tan(double angle) NOEXCEPT
  463. {
  464. return (double)tanl(angle);
  465. }
  466. float tanf(float angle) NOEXCEPT
  467. {
  468. return (float)tanl(angle);
  469. }
  470. long double sqrtl(long double x) NOEXCEPT
  471. {
  472. long double res;
  473. asm("fsqrt"
  474. : "=t"(res)
  475. : "0"(x));
  476. return res;
  477. }
  478. double sqrt(double x) NOEXCEPT
  479. {
  480. double res;
  481. __asm__("fsqrt"
  482. : "=t"(res)
  483. : "0"(x));
  484. return res;
  485. }
  486. float sqrtf(float x) NOEXCEPT
  487. {
  488. float res;
  489. __asm__("fsqrt"
  490. : "=t"(res)
  491. : "0"(x));
  492. return res;
  493. }
  494. long double sinhl(long double x) NOEXCEPT
  495. {
  496. long double exponentiated = expl(x);
  497. if (x > 0)
  498. return (exponentiated * exponentiated - 1) / 2 / exponentiated;
  499. return (exponentiated - 1 / exponentiated) / 2;
  500. }
  501. double sinh(double x) NOEXCEPT
  502. {
  503. return (double)sinhl(x);
  504. }
  505. float sinhf(float x) NOEXCEPT
  506. {
  507. return (float)sinhl(x);
  508. }
  509. long double log10l(long double x) NOEXCEPT
  510. {
  511. long double ret = 0.0l;
  512. __asm__(
  513. "fldlg2\n"
  514. "fld %%st(1)\n"
  515. "fyl2x\n"
  516. "fstp %%st(1)"
  517. : "=t"(ret)
  518. : "0"(x));
  519. return ret;
  520. }
  521. double log10(double x) NOEXCEPT
  522. {
  523. return (double)log10l(x);
  524. }
  525. float log10f(float x) NOEXCEPT
  526. {
  527. return (float)log10l(x);
  528. }
  529. long double logl(long double x) NOEXCEPT
  530. {
  531. long double ret = 0.0l;
  532. asm(
  533. "fldln2\n"
  534. "fld %%st(1)\n"
  535. "fyl2x\n"
  536. "fstp %%st(1)"
  537. : "=t"(ret)
  538. : "0"(x));
  539. return ret;
  540. }
  541. double log(double x) NOEXCEPT
  542. {
  543. return (double)logl(x);
  544. }
  545. float logf(float x) NOEXCEPT
  546. {
  547. return (float)logl(x);
  548. }
  549. long double fmodl(long double index, long double period) NOEXCEPT
  550. {
  551. return index - truncl(index / period) * period;
  552. }
  553. double fmod(double index, double period) NOEXCEPT
  554. {
  555. return index - trunc(index / period) * period;
  556. }
  557. float fmodf(float index, float period) NOEXCEPT
  558. {
  559. return index - truncf(index / period) * period;
  560. }
  561. // FIXME: These aren't exactly like fmod, but these definitions are probably good enough for now
  562. long double remainderl(long double x, long double y) NOEXCEPT
  563. {
  564. return fmodl(x, y);
  565. }
  566. double remainder(double x, double y) NOEXCEPT
  567. {
  568. return fmod(x, y);
  569. }
  570. float remainderf(float x, float y) NOEXCEPT
  571. {
  572. return fmodf(x, y);
  573. }
  574. long double expl(long double exponent) NOEXCEPT
  575. {
  576. long double res = 0;
  577. asm("fldl2e\n"
  578. "fmulp\n"
  579. "fld1\n"
  580. "fld %%st(1)\n"
  581. "fprem\n"
  582. "f2xm1\n"
  583. "faddp\n"
  584. "fscale\n"
  585. "fstp %%st(1)"
  586. : "=t"(res)
  587. : "0"(exponent));
  588. return res;
  589. }
  590. double exp(double exponent) NOEXCEPT
  591. {
  592. return (double)expl(exponent);
  593. }
  594. float expf(float exponent) NOEXCEPT
  595. {
  596. return (float)expl(exponent);
  597. }
  598. long double exp2l(long double exponent) NOEXCEPT
  599. {
  600. long double res = 0;
  601. asm("fld1\n"
  602. "fld %%st(1)\n"
  603. "fprem\n"
  604. "f2xm1\n"
  605. "faddp\n"
  606. "fscale\n"
  607. "fstp %%st(1)"
  608. : "=t"(res)
  609. : "0"(exponent));
  610. return res;
  611. }
  612. double exp2(double exponent) NOEXCEPT
  613. {
  614. return (double)exp2l(exponent);
  615. }
  616. float exp2f(float exponent) NOEXCEPT
  617. {
  618. return (float)exp2l(exponent);
  619. }
  620. long double coshl(long double x) NOEXCEPT
  621. {
  622. long double exponentiated = expl(-x);
  623. if (x < 0)
  624. return (1 + exponentiated * exponentiated) / 2 / exponentiated;
  625. return (1 / exponentiated + exponentiated) / 2;
  626. }
  627. double cosh(double x) NOEXCEPT
  628. {
  629. return (double)coshl(x);
  630. }
  631. float coshf(float x) NOEXCEPT
  632. {
  633. return (float)coshl(x);
  634. }
  635. long double atan2l(long double y, long double x) NOEXCEPT
  636. {
  637. long double result = 0; //atanl(y / x);
  638. asm("fpatan"
  639. : "=t"(result)
  640. : "0"(x), "u"(y)
  641. : "st(1)");
  642. return result;
  643. }
  644. double atan2(double y, double x) NOEXCEPT
  645. {
  646. double result = 0; //atanl(y / x);
  647. asm("fpatan"
  648. : "=t"(result)
  649. : "0"(x), "u"(y)
  650. : "st(1)");
  651. return result;
  652. }
  653. float atan2f(float y, float x) NOEXCEPT
  654. {
  655. float result = 0; //atanl(y / x);
  656. asm("fpatan"
  657. : "=t"(result)
  658. : "0"(x), "u"(y)
  659. : "st(1)");
  660. return result;
  661. }
  662. long double atanl(long double x) NOEXCEPT
  663. {
  664. asm(
  665. "fld1\n"
  666. "fpatan\n"
  667. : "=t"(x)
  668. : "0"(x));
  669. return x;
  670. }
  671. double atan(double x) NOEXCEPT
  672. {
  673. asm(
  674. "fld1\n"
  675. "fpatan\n"
  676. : "=t"(x)
  677. : "0"(x));
  678. return x;
  679. }
  680. float atanf(float x) NOEXCEPT
  681. {
  682. asm(
  683. "fld1\n"
  684. "fpatan\n"
  685. : "=t"(x)
  686. : "0"(x));
  687. return x;
  688. }
  689. long double asinl(long double x) NOEXCEPT
  690. {
  691. if (x > 1 || x < -1)
  692. return NAN;
  693. if (x > 0.5 || x < -0.5)
  694. return 2 * atanl(x / (1 + sqrtl(1 - x * x)));
  695. long double squared = x * x;
  696. long double value = x;
  697. long double i = x * squared;
  698. value += i * product_odd<1>() / product_even<2>() / 3;
  699. i *= squared;
  700. value += i * product_odd<3>() / product_even<4>() / 5;
  701. i *= squared;
  702. value += i * product_odd<5>() / product_even<6>() / 7;
  703. i *= squared;
  704. value += i * product_odd<7>() / product_even<8>() / 9;
  705. i *= squared;
  706. value += i * product_odd<9>() / product_even<10>() / 11;
  707. i *= squared;
  708. value += i * product_odd<11>() / product_even<12>() / 13;
  709. return value;
  710. }
  711. double asin(double x) NOEXCEPT
  712. {
  713. return (double)asinl(x);
  714. }
  715. float asinf(float x) NOEXCEPT
  716. {
  717. return (float)asinl(x);
  718. }
  719. long double acosl(long double x) NOEXCEPT
  720. {
  721. return M_PI_2 - asinl(x);
  722. }
  723. double acos(double x) NOEXCEPT
  724. {
  725. return M_PI_2 - asin(x);
  726. }
  727. float acosf(float x) NOEXCEPT
  728. {
  729. return static_cast<float>(M_PI_2) - asinf(x);
  730. }
  731. long double fabsl(long double value) NOEXCEPT
  732. {
  733. return value < 0 ? -value : value;
  734. }
  735. double fabs(double value) NOEXCEPT
  736. {
  737. return value < 0 ? -value : value;
  738. }
  739. float fabsf(float value) NOEXCEPT
  740. {
  741. return value < 0 ? -value : value;
  742. }
  743. int ilogbl(long double x) NOEXCEPT
  744. {
  745. return internal_ilogb(x);
  746. }
  747. int ilogb(double x) NOEXCEPT
  748. {
  749. return internal_ilogb(x);
  750. }
  751. int ilogbf(float x) NOEXCEPT
  752. {
  753. return internal_ilogb(x);
  754. }
  755. long double logbl(long double x) NOEXCEPT
  756. {
  757. return ilogbl(x);
  758. }
  759. double logb(double x) NOEXCEPT
  760. {
  761. return ilogb(x);
  762. }
  763. float logbf(float x) NOEXCEPT
  764. {
  765. return ilogbf(x);
  766. }
  767. long double log2l(long double x) NOEXCEPT
  768. {
  769. long double ret = 0.0;
  770. asm(
  771. "fld1\n"
  772. "fld %%st(1)\n"
  773. "fyl2x\n"
  774. "fstp %%st(1)"
  775. : "=t"(ret)
  776. : "0"(x));
  777. return ret;
  778. }
  779. double log2(double x) NOEXCEPT
  780. {
  781. return (double)log2l(x);
  782. }
  783. float log2f(float x) NOEXCEPT
  784. {
  785. return (float)log2l(x);
  786. }
  787. double frexp(double x, int* exp) NOEXCEPT
  788. {
  789. *exp = (x == 0) ? 0 : (1 + ilogb(x));
  790. return scalbn(x, -(*exp));
  791. }
  792. float frexpf(float x, int* exp) NOEXCEPT
  793. {
  794. *exp = (x == 0) ? 0 : (1 + ilogbf(x));
  795. return scalbnf(x, -(*exp));
  796. }
  797. long double frexpl(long double x, int* exp) NOEXCEPT
  798. {
  799. *exp = (x == 0) ? 0 : (1 + ilogbl(x));
  800. return scalbnl(x, -(*exp));
  801. }
  802. double round(double value) NOEXCEPT
  803. {
  804. return internal_to_integer(value, RoundingMode::ToEven);
  805. }
  806. float roundf(float value) NOEXCEPT
  807. {
  808. return internal_to_integer(value, RoundingMode::ToEven);
  809. }
  810. long double roundl(long double value) NOEXCEPT
  811. {
  812. return internal_to_integer(value, RoundingMode::ToEven);
  813. }
  814. long lroundf(float value) NOEXCEPT
  815. {
  816. return internal_to_integer(value, RoundingMode::ToEven);
  817. }
  818. long lround(double value) NOEXCEPT
  819. {
  820. return internal_to_integer(value, RoundingMode::ToEven);
  821. }
  822. long lroundl(long double value) NOEXCEPT
  823. {
  824. return internal_to_integer(value, RoundingMode::ToEven);
  825. }
  826. long long llroundf(float value) NOEXCEPT
  827. {
  828. return internal_to_integer(value, RoundingMode::ToEven);
  829. }
  830. long long llround(double value) NOEXCEPT
  831. {
  832. return internal_to_integer(value, RoundingMode::ToEven);
  833. }
  834. long long llroundd(long double value) NOEXCEPT
  835. {
  836. return internal_to_integer(value, RoundingMode::ToEven);
  837. }
  838. float floorf(float value) NOEXCEPT
  839. {
  840. return internal_to_integer(value, RoundingMode::Down);
  841. }
  842. double floor(double value) NOEXCEPT
  843. {
  844. return internal_to_integer(value, RoundingMode::Down);
  845. }
  846. long double floorl(long double value) NOEXCEPT
  847. {
  848. return internal_to_integer(value, RoundingMode::Down);
  849. }
  850. long double rintl(long double value) NOEXCEPT
  851. {
  852. return internal_to_integer(value, RoundingMode { fegetround() });
  853. }
  854. double rint(double value) NOEXCEPT
  855. {
  856. return internal_to_integer(value, RoundingMode { fegetround() });
  857. }
  858. float rintf(float value) NOEXCEPT
  859. {
  860. return internal_to_integer(value, RoundingMode { fegetround() });
  861. }
  862. long lrintl(long double value) NOEXCEPT
  863. {
  864. return (long)internal_to_integer(value, RoundingMode { fegetround() });
  865. }
  866. long lrint(double value) NOEXCEPT
  867. {
  868. return (long)internal_to_integer(value, RoundingMode { fegetround() });
  869. }
  870. long lrintf(float value) NOEXCEPT
  871. {
  872. return (long)internal_to_integer(value, RoundingMode { fegetround() });
  873. }
  874. long long llrintl(long double value) NOEXCEPT
  875. {
  876. return (long long)internal_to_integer(value, RoundingMode { fegetround() });
  877. }
  878. long long llrint(double value) NOEXCEPT
  879. {
  880. return (long long)internal_to_integer(value, RoundingMode { fegetround() });
  881. }
  882. long long llrintf(float value) NOEXCEPT
  883. {
  884. return (long long)internal_to_integer(value, RoundingMode { fegetround() });
  885. }
  886. float ceilf(float value) NOEXCEPT
  887. {
  888. return internal_to_integer(value, RoundingMode::Up);
  889. }
  890. double ceil(double value) NOEXCEPT
  891. {
  892. return internal_to_integer(value, RoundingMode::Up);
  893. }
  894. long double ceill(long double value) NOEXCEPT
  895. {
  896. return internal_to_integer(value, RoundingMode::Up);
  897. }
  898. long double modfl(long double x, long double* intpart) NOEXCEPT
  899. {
  900. return internal_modf(x, intpart);
  901. }
  902. double modf(double x, double* intpart) NOEXCEPT
  903. {
  904. return internal_modf(x, intpart);
  905. }
  906. float modff(float x, float* intpart) NOEXCEPT
  907. {
  908. return internal_modf(x, intpart);
  909. }
  910. double gamma(double x) NOEXCEPT
  911. {
  912. // Stirling approximation
  913. return sqrt(2.0 * M_PI / x) * pow(x / M_E, x);
  914. }
  915. long double tgammal(long double value) NOEXCEPT
  916. {
  917. return internal_gamma(value);
  918. }
  919. double tgamma(double value) NOEXCEPT
  920. {
  921. return internal_gamma(value);
  922. }
  923. float tgammaf(float value) NOEXCEPT
  924. {
  925. return internal_gamma(value);
  926. }
  927. int signgam = 0;
  928. long double lgammal(long double value) NOEXCEPT
  929. {
  930. return lgammal_r(value, &signgam);
  931. }
  932. double lgamma(double value) NOEXCEPT
  933. {
  934. return lgamma_r(value, &signgam);
  935. }
  936. float lgammaf(float value) NOEXCEPT
  937. {
  938. return lgammaf_r(value, &signgam);
  939. }
  940. long double lgammal_r(long double value, int* sign) NOEXCEPT
  941. {
  942. if (value == 1.0 || value == 2.0)
  943. return 0.0;
  944. if (isinf(value) || value == 0.0)
  945. return INFINITY;
  946. long double result = logl(internal_gamma(value));
  947. *sign = signbit(result) ? -1 : 1;
  948. return result;
  949. }
  950. double lgamma_r(double value, int* sign) NOEXCEPT
  951. {
  952. if (value == 1.0 || value == 2.0)
  953. return 0.0;
  954. if (isinf(value) || value == 0.0)
  955. return INFINITY;
  956. double result = log(internal_gamma(value));
  957. *sign = signbit(result) ? -1 : 1;
  958. return result;
  959. }
  960. float lgammaf_r(float value, int* sign) NOEXCEPT
  961. {
  962. if (value == 1.0f || value == 2.0f)
  963. return 0.0;
  964. if (isinf(value) || value == 0.0f)
  965. return INFINITY;
  966. float result = logf(internal_gamma(value));
  967. *sign = signbit(result) ? -1 : 1;
  968. return result;
  969. }
  970. long double expm1l(long double x) NOEXCEPT
  971. {
  972. return expl(x) - 1;
  973. }
  974. double expm1(double x) NOEXCEPT
  975. {
  976. return exp(x) - 1;
  977. }
  978. float expm1f(float x) NOEXCEPT
  979. {
  980. return expf(x) - 1;
  981. }
  982. long double cbrtl(long double x) NOEXCEPT
  983. {
  984. if (isinf(x) || x == 0)
  985. return x;
  986. if (x < 0)
  987. return -cbrtl(-x);
  988. long double r = x;
  989. long double ex = 0;
  990. while (r < 0.125l) {
  991. r *= 8;
  992. ex--;
  993. }
  994. while (r > 1.0l) {
  995. r *= 0.125l;
  996. ex++;
  997. }
  998. r = (-0.46946116l * r + 1.072302l) * r + 0.3812513l;
  999. while (ex < 0) {
  1000. r *= 0.5l;
  1001. ex++;
  1002. }
  1003. while (ex > 0) {
  1004. r *= 2.0l;
  1005. ex--;
  1006. }
  1007. r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);
  1008. r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);
  1009. r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);
  1010. r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);
  1011. return r;
  1012. }
  1013. double cbrt(double x) NOEXCEPT
  1014. {
  1015. return (double)cbrtl(x);
  1016. }
  1017. float cbrtf(float x) NOEXCEPT
  1018. {
  1019. return (float)cbrtl(x);
  1020. }
  1021. long double log1pl(long double x) NOEXCEPT
  1022. {
  1023. return logl(1 + x);
  1024. }
  1025. double log1p(double x) NOEXCEPT
  1026. {
  1027. return log(1 + x);
  1028. }
  1029. float log1pf(float x) NOEXCEPT
  1030. {
  1031. return logf(1 + x);
  1032. }
  1033. long double acoshl(long double x) NOEXCEPT
  1034. {
  1035. return logl(x + sqrtl(x * x - 1));
  1036. }
  1037. double acosh(double x) NOEXCEPT
  1038. {
  1039. return log(x + sqrt(x * x - 1));
  1040. }
  1041. float acoshf(float x) NOEXCEPT
  1042. {
  1043. return logf(x + sqrtf(x * x - 1));
  1044. }
  1045. long double asinhl(long double x) NOEXCEPT
  1046. {
  1047. return logl(x + sqrtl(x * x + 1));
  1048. }
  1049. double asinh(double x) NOEXCEPT
  1050. {
  1051. return log(x + sqrt(x * x + 1));
  1052. }
  1053. float asinhf(float x) NOEXCEPT
  1054. {
  1055. return logf(x + sqrtf(x * x + 1));
  1056. }
  1057. long double atanhl(long double x) NOEXCEPT
  1058. {
  1059. return logl((1 + x) / (1 - x)) / 2.0l;
  1060. }
  1061. double atanh(double x) NOEXCEPT
  1062. {
  1063. return log((1 + x) / (1 - x)) / 2.0;
  1064. }
  1065. float atanhf(float x) NOEXCEPT
  1066. {
  1067. return logf((1 + x) / (1 - x)) / 2.0f;
  1068. }
  1069. long double hypotl(long double x, long double y) NOEXCEPT
  1070. {
  1071. return sqrtl(x * x + y * y);
  1072. }
  1073. double hypot(double x, double y) NOEXCEPT
  1074. {
  1075. return sqrt(x * x + y * y);
  1076. }
  1077. float hypotf(float x, float y) NOEXCEPT
  1078. {
  1079. return sqrtf(x * x + y * y);
  1080. }
  1081. long double erfl(long double x) NOEXCEPT
  1082. {
  1083. // algorithm taken from Abramowitz and Stegun (no. 26.2.17)
  1084. long double t = 1 / (1 + 0.47047l * fabsl(x));
  1085. long double poly = t * (0.3480242l + t * (-0.958798l + t * 0.7478556l));
  1086. long double answer = 1 - poly * expl(-x * x);
  1087. if (x < 0)
  1088. return -answer;
  1089. return answer;
  1090. }
  1091. double erf(double x) NOEXCEPT
  1092. {
  1093. return (double)erfl(x);
  1094. }
  1095. float erff(float x) NOEXCEPT
  1096. {
  1097. return (float)erf(x);
  1098. }
  1099. long double erfcl(long double x) NOEXCEPT
  1100. {
  1101. return 1 - erfl(x);
  1102. }
  1103. double erfc(double x) NOEXCEPT
  1104. {
  1105. return 1 - erf(x);
  1106. }
  1107. float erfcf(float x) NOEXCEPT
  1108. {
  1109. return 1 - erff(x);
  1110. }
  1111. double nextafter(double x, double target) NOEXCEPT
  1112. {
  1113. if (x == target)
  1114. return target;
  1115. return internal_nextafter(x, target >= x);
  1116. }
  1117. float nextafterf(float x, float target) NOEXCEPT
  1118. {
  1119. if (x == target)
  1120. return target;
  1121. return internal_nextafter(x, target >= x);
  1122. }
  1123. long double nextafterl(long double x, long double target) NOEXCEPT
  1124. {
  1125. return internal_nextafter(x, target >= x);
  1126. }
  1127. double nexttoward(double x, long double target) NOEXCEPT
  1128. {
  1129. if (x == target)
  1130. return target;
  1131. return internal_nextafter(x, target >= x);
  1132. }
  1133. float nexttowardf(float x, long double target) NOEXCEPT
  1134. {
  1135. if (x == target)
  1136. return target;
  1137. return internal_nextafter(x, target >= x);
  1138. }
  1139. long double nexttowardl(long double x, long double target) NOEXCEPT
  1140. {
  1141. if (x == target)
  1142. return target;
  1143. return internal_nextafter(x, target >= x);
  1144. }
  1145. float copysignf(float x, float y) NOEXCEPT
  1146. {
  1147. return internal_copysign(x, y);
  1148. }
  1149. double copysign(double x, double y) NOEXCEPT
  1150. {
  1151. return internal_copysign(x, y);
  1152. }
  1153. long double copysignl(long double x, long double y) NOEXCEPT
  1154. {
  1155. return internal_copysign(x, y);
  1156. }
  1157. float scalbnf(float x, int exponent) NOEXCEPT
  1158. {
  1159. return internal_scalbn(x, exponent);
  1160. }
  1161. double scalbn(double x, int exponent) NOEXCEPT
  1162. {
  1163. return internal_scalbn(x, exponent);
  1164. }
  1165. long double scalbnl(long double x, int exponent) NOEXCEPT
  1166. {
  1167. return internal_scalbn(x, exponent);
  1168. }
  1169. float scalbnlf(float x, long exponent) NOEXCEPT
  1170. {
  1171. return internal_scalbn(x, exponent);
  1172. }
  1173. double scalbln(double x, long exponent) NOEXCEPT
  1174. {
  1175. return internal_scalbn(x, exponent);
  1176. }
  1177. long double scalblnl(long double x, long exponent) NOEXCEPT
  1178. {
  1179. return internal_scalbn(x, exponent);
  1180. }
  1181. long double fmaxl(long double x, long double y) NOEXCEPT
  1182. {
  1183. if (isnan(x))
  1184. return y;
  1185. if (isnan(y))
  1186. return x;
  1187. return x > y ? x : y;
  1188. }
  1189. double fmax(double x, double y) NOEXCEPT
  1190. {
  1191. if (isnan(x))
  1192. return y;
  1193. if (isnan(y))
  1194. return x;
  1195. return x > y ? x : y;
  1196. }
  1197. float fmaxf(float x, float y) NOEXCEPT
  1198. {
  1199. if (isnan(x))
  1200. return y;
  1201. if (isnan(y))
  1202. return x;
  1203. return x > y ? x : y;
  1204. }
  1205. long double fminl(long double x, long double y) NOEXCEPT
  1206. {
  1207. if (isnan(x))
  1208. return y;
  1209. if (isnan(y))
  1210. return x;
  1211. return x < y ? x : y;
  1212. }
  1213. double fmin(double x, double y) NOEXCEPT
  1214. {
  1215. if (isnan(x))
  1216. return y;
  1217. if (isnan(y))
  1218. return x;
  1219. return x < y ? x : y;
  1220. }
  1221. float fminf(float x, float y) NOEXCEPT
  1222. {
  1223. if (isnan(x))
  1224. return y;
  1225. if (isnan(y))
  1226. return x;
  1227. return x < y ? x : y;
  1228. }
  1229. long double nearbyintl(long double value) NOEXCEPT
  1230. {
  1231. return internal_to_integer(value, RoundingMode { fegetround() });
  1232. }
  1233. double nearbyint(double value) NOEXCEPT
  1234. {
  1235. return internal_to_integer(value, RoundingMode { fegetround() });
  1236. }
  1237. float nearbyintf(float value) NOEXCEPT
  1238. {
  1239. return internal_to_integer(value, RoundingMode { fegetround() });
  1240. }
  1241. }
  1242. #ifdef __clang__
  1243. # pragma clang diagnostic pop
  1244. #endif