math.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibC/assert.h>
  27. #include <LibM/math.h>
  28. #include <stdint.h>
  29. #include <stdlib.h>
  30. template<size_t>
  31. constexpr double e_to_power();
  32. template<>
  33. constexpr double e_to_power<0>() { return 1; }
  34. template<size_t exponent>
  35. constexpr double e_to_power() { return M_E * e_to_power<exponent - 1>(); }
  36. template<size_t>
  37. constexpr size_t factorial();
  38. template<>
  39. constexpr size_t factorial<0>() { return 1; }
  40. template<size_t value>
  41. constexpr size_t factorial() { return value * factorial<value - 1>(); }
  42. template<size_t>
  43. constexpr size_t product_even();
  44. template<>
  45. constexpr size_t product_even<2>() { return 2; }
  46. template<size_t value>
  47. constexpr size_t product_even() { return value * product_even<value - 2>(); }
  48. template<size_t>
  49. constexpr size_t product_odd();
  50. template<>
  51. constexpr size_t product_odd<1>() { return 1; }
  52. template<size_t value>
  53. constexpr size_t product_odd() { return value * product_odd<value - 2>(); }
  54. extern "C" {
  55. double trunc(double x)
  56. {
  57. return (int64_t)x;
  58. }
  59. double cos(double angle)
  60. {
  61. return sin(angle + M_PI_2);
  62. }
  63. float cosf(float angle)
  64. {
  65. return sinf(angle + M_PI_2);
  66. }
  67. // This can also be done with a taylor expansion, but for
  68. // now this works pretty well (and doesn't mess anything up
  69. // in quake in particular, which is very Floating-Point precision
  70. // heavy)
  71. double sin(double angle)
  72. {
  73. double ret = 0.0;
  74. __asm__(
  75. "fsin"
  76. : "=t"(ret)
  77. : "0"(angle));
  78. return ret;
  79. }
  80. float sinf(float angle)
  81. {
  82. float ret = 0.0f;
  83. __asm__(
  84. "fsin"
  85. : "=t"(ret)
  86. : "0"(angle));
  87. return ret;
  88. }
  89. double pow(double x, double y)
  90. {
  91. // FIXME: Please fix me. I am naive.
  92. if (y == 0)
  93. return 1;
  94. if (y == 1)
  95. return x;
  96. int y_as_int = (int)y;
  97. if (y == (double)y_as_int) {
  98. double result = x;
  99. for (int i = 0; i < abs(y) - 1; ++i)
  100. result *= x;
  101. if (y < 0)
  102. result = 1.0 / result;
  103. return result;
  104. }
  105. return exp(y * log(x));
  106. }
  107. float powf(float x, float y)
  108. {
  109. // FIXME: Please fix me. I am naive.
  110. if (y == 0)
  111. return 1;
  112. if (y == 1)
  113. return x;
  114. int y_as_int = (int)y;
  115. if (y == (float)y_as_int) {
  116. float result = x;
  117. for (int i = 0; i < abs(y) - 1; ++i)
  118. result *= x;
  119. if (y < 0)
  120. result = 1.0 / result;
  121. return result;
  122. }
  123. return (float)exp((double)y * log((double)x));
  124. }
  125. double ldexp(double x, int exp)
  126. {
  127. // FIXME: Please fix me. I am naive.
  128. double val = pow(2, exp);
  129. return x * val;
  130. }
  131. double tanh(double x)
  132. {
  133. if (x > 0) {
  134. double exponentiated = exp(2 * x);
  135. return (exponentiated - 1) / (exponentiated + 1);
  136. }
  137. double plusX = exp(x);
  138. double minusX = 1 / plusX;
  139. return (plusX - minusX) / (plusX + minusX);
  140. }
  141. double ampsin(double angle)
  142. {
  143. double looped_angle = fmod(M_PI + angle, M_TAU) - M_PI;
  144. double looped_angle_squared = looped_angle * looped_angle;
  145. double quadratic_term;
  146. if (looped_angle > 0) {
  147. quadratic_term = -looped_angle_squared;
  148. } else {
  149. quadratic_term = looped_angle_squared;
  150. }
  151. double linear_term = M_PI * looped_angle;
  152. return quadratic_term + linear_term;
  153. }
  154. double tan(double angle)
  155. {
  156. return ampsin(angle) / ampsin(M_PI_2 + angle);
  157. }
  158. double sqrt(double x)
  159. {
  160. double res;
  161. __asm__("fsqrt"
  162. : "=t"(res)
  163. : "0"(x));
  164. return res;
  165. }
  166. float sqrtf(float x)
  167. {
  168. float res;
  169. __asm__("fsqrt"
  170. : "=t"(res)
  171. : "0"(x));
  172. return res;
  173. }
  174. double sinh(double x)
  175. {
  176. double exponentiated = exp(x);
  177. if (x > 0)
  178. return (exponentiated * exponentiated - 1) / 2 / exponentiated;
  179. return (exponentiated - 1 / exponentiated) / 2;
  180. }
  181. double log10(double x)
  182. {
  183. return log(x) / M_LN10;
  184. }
  185. double log(double x)
  186. {
  187. if (x < 0)
  188. return __builtin_nan("");
  189. if (x == 0)
  190. return -__builtin_huge_val();
  191. double y = 1 + 2 * (x - 1) / (x + 1);
  192. double exponentiated = exp(y);
  193. y = y + 2 * (x - exponentiated) / (x + exponentiated);
  194. exponentiated = exp(y);
  195. y = y + 2 * (x - exponentiated) / (x + exponentiated);
  196. exponentiated = exp(y);
  197. return y + 2 * (x - exponentiated) / (x + exponentiated);
  198. }
  199. float logf(float x)
  200. {
  201. return (float)log(x);
  202. }
  203. double fmod(double index, double period)
  204. {
  205. return index - trunc(index / period) * period;
  206. }
  207. float fmodf(float index, float period)
  208. {
  209. return index - trunc(index / period) * period;
  210. }
  211. double exp(double exponent)
  212. {
  213. double result = 1;
  214. if (exponent >= 1) {
  215. size_t integer_part = (size_t)exponent;
  216. if (integer_part & 1)
  217. result *= e_to_power<1>();
  218. if (integer_part & 2)
  219. result *= e_to_power<2>();
  220. if (integer_part > 3) {
  221. if (integer_part & 4)
  222. result *= e_to_power<4>();
  223. if (integer_part & 8)
  224. result *= e_to_power<8>();
  225. if (integer_part & 16)
  226. result *= e_to_power<16>();
  227. if (integer_part & 32)
  228. result *= e_to_power<32>();
  229. if (integer_part >= 64)
  230. return __builtin_huge_val();
  231. }
  232. exponent -= integer_part;
  233. } else if (exponent < 0)
  234. return 1 / exp(-exponent);
  235. double taylor_series_result = 1 + exponent;
  236. double taylor_series_numerator = exponent * exponent;
  237. taylor_series_result += taylor_series_numerator / factorial<2>();
  238. taylor_series_numerator *= exponent;
  239. taylor_series_result += taylor_series_numerator / factorial<3>();
  240. taylor_series_numerator *= exponent;
  241. taylor_series_result += taylor_series_numerator / factorial<4>();
  242. taylor_series_numerator *= exponent;
  243. taylor_series_result += taylor_series_numerator / factorial<5>();
  244. return result * taylor_series_result;
  245. }
  246. float expf(float exponent)
  247. {
  248. return (float)exp(exponent);
  249. }
  250. double cosh(double x)
  251. {
  252. double exponentiated = exp(-x);
  253. if (x < 0)
  254. return (1 + exponentiated * exponentiated) / 2 / exponentiated;
  255. return (1 / exponentiated + exponentiated) / 2;
  256. }
  257. double atan2(double y, double x)
  258. {
  259. if (x > 0)
  260. return atan(y / x);
  261. if (x == 0) {
  262. if (y > 0)
  263. return M_PI_2;
  264. if (y < 0)
  265. return -M_PI_2;
  266. return 0;
  267. }
  268. if (y >= 0)
  269. return atan(y / x) + M_PI;
  270. return atan(y / x) - M_PI;
  271. }
  272. float atan2f(float y, float x)
  273. {
  274. return (float)atan2(y, x);
  275. }
  276. double atan(double x)
  277. {
  278. if (x < 0)
  279. return -atan(-x);
  280. if (x > 1)
  281. return M_PI_2 - atan(1 / x);
  282. double squared = x * x;
  283. return x / (1 + 1 * 1 * squared / (3 + 2 * 2 * squared / (5 + 3 * 3 * squared / (7 + 4 * 4 * squared / (9 + 5 * 5 * squared / (11 + 6 * 6 * squared / (13 + 7 * 7 * squared)))))));
  284. }
  285. double asin(double x)
  286. {
  287. if (x > 1 || x < -1)
  288. return __builtin_nan("");
  289. if (x > 0.5 || x < -0.5)
  290. return 2 * atan(x / (1 + sqrt(1 - x * x)));
  291. double squared = x * x;
  292. double value = x;
  293. double i = x * squared;
  294. value += i * product_odd<1>() / product_even<2>() / 3;
  295. i *= squared;
  296. value += i * product_odd<3>() / product_even<4>() / 5;
  297. i *= squared;
  298. value += i * product_odd<5>() / product_even<6>() / 7;
  299. i *= squared;
  300. value += i * product_odd<7>() / product_even<8>() / 9;
  301. i *= squared;
  302. value += i * product_odd<9>() / product_even<10>() / 11;
  303. i *= squared;
  304. value += i * product_odd<11>() / product_even<12>() / 13;
  305. return value;
  306. }
  307. float asinf(float x)
  308. {
  309. return (float)asin(x);
  310. }
  311. double acos(double x)
  312. {
  313. return M_PI_2 - asin(x);
  314. }
  315. float acosf(float x)
  316. {
  317. return M_PI_2 - asinf(x);
  318. }
  319. double fabs(double value)
  320. {
  321. return value < 0 ? -value : value;
  322. }
  323. double log2(double x)
  324. {
  325. return log(x) / M_LN2;
  326. }
  327. float log2f(float x)
  328. {
  329. return log2(x);
  330. }
  331. long double log2l(long double x)
  332. {
  333. return log2(x);
  334. }
  335. double frexp(double, int*)
  336. {
  337. ASSERT_NOT_REACHED();
  338. return 0;
  339. }
  340. float frexpf(float, int*)
  341. {
  342. ASSERT_NOT_REACHED();
  343. return 0;
  344. }
  345. long double frexpl(long double, int*)
  346. {
  347. ASSERT_NOT_REACHED();
  348. return 0;
  349. }
  350. double round(double value)
  351. {
  352. // FIXME: Please fix me. I am naive.
  353. if (value >= 0.0)
  354. return (double)(int)(value + 0.5);
  355. return (double)(int)(value - 0.5);
  356. }
  357. float roundf(float value)
  358. {
  359. // FIXME: Please fix me. I am naive.
  360. if (value >= 0.0f)
  361. return (float)(int)(value + 0.5f);
  362. return (float)(int)(value - 0.5f);
  363. }
  364. double floor(double value)
  365. {
  366. return (int)value;
  367. }
  368. double rint(double value)
  369. {
  370. return (int)roundf(value);
  371. }
  372. float ceilf(float value)
  373. {
  374. // FIXME: Please fix me. I am naive.
  375. int as_int = (int)value;
  376. if (value == (float)as_int)
  377. return as_int;
  378. if (value < 0) {
  379. if (as_int == 0)
  380. return -0;
  381. return as_int;
  382. }
  383. return as_int + 1;
  384. }
  385. double ceil(double value)
  386. {
  387. // FIXME: Please fix me. I am naive.
  388. int as_int = (int)value;
  389. if (value == (double)as_int)
  390. return as_int;
  391. if (value < 0) {
  392. if (as_int == 0)
  393. return -0;
  394. return as_int;
  395. }
  396. return as_int + 1;
  397. }
  398. double modf(double x, double* intpart)
  399. {
  400. *intpart = (double)((int)(x));
  401. return x - (int)x;
  402. }
  403. }