math.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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: Extremely unlikely to be standards compliant.
  92. return exp(y * log(x));
  93. }
  94. float powf(float x, float y)
  95. {
  96. return (float)exp((double)y * log((double)x));
  97. }
  98. double ldexp(double x, int exp)
  99. {
  100. // FIXME: Please fix me. I am naive.
  101. double val = pow(2, exp);
  102. return x * val;
  103. }
  104. double tanh(double x)
  105. {
  106. if (x > 0) {
  107. double exponentiated = exp(2 * x);
  108. return (exponentiated - 1) / (exponentiated + 1);
  109. }
  110. double plusX = exp(x);
  111. double minusX = 1 / plusX;
  112. return (plusX - minusX) / (plusX + minusX);
  113. }
  114. double ampsin(double angle)
  115. {
  116. double looped_angle = fmod(M_PI + angle, M_TAU) - M_PI;
  117. double looped_angle_squared = looped_angle * looped_angle;
  118. double quadratic_term;
  119. if (looped_angle > 0) {
  120. quadratic_term = -looped_angle_squared;
  121. } else {
  122. quadratic_term = looped_angle_squared;
  123. }
  124. double linear_term = M_PI * looped_angle;
  125. return quadratic_term + linear_term;
  126. }
  127. double tan(double angle)
  128. {
  129. return ampsin(angle) / ampsin(M_PI_2 + angle);
  130. }
  131. double sqrt(double x)
  132. {
  133. double res;
  134. __asm__("fsqrt"
  135. : "=t"(res)
  136. : "0"(x));
  137. return res;
  138. }
  139. float sqrtf(float x)
  140. {
  141. float res;
  142. __asm__("fsqrt"
  143. : "=t"(res)
  144. : "0"(x));
  145. return res;
  146. }
  147. double sinh(double x)
  148. {
  149. double exponentiated = exp(x);
  150. if (x > 0)
  151. return (exponentiated * exponentiated - 1) / 2 / exponentiated;
  152. return (exponentiated - 1 / exponentiated) / 2;
  153. }
  154. double log10(double x)
  155. {
  156. return log(x) / M_LN10;
  157. }
  158. double log(double x)
  159. {
  160. if (x < 0)
  161. return __builtin_nan("");
  162. if (x == 0)
  163. return -__builtin_huge_val();
  164. double y = 1 + 2 * (x - 1) / (x + 1);
  165. double exponentiated = exp(y);
  166. y = y + 2 * (x - exponentiated) / (x + exponentiated);
  167. exponentiated = exp(y);
  168. y = y + 2 * (x - exponentiated) / (x + exponentiated);
  169. exponentiated = exp(y);
  170. return y + 2 * (x - exponentiated) / (x + exponentiated);
  171. }
  172. float logf(float x)
  173. {
  174. return (float)log(x);
  175. }
  176. double fmod(double index, double period)
  177. {
  178. return index - trunc(index / period) * period;
  179. }
  180. double exp(double exponent)
  181. {
  182. double result = 1;
  183. if (exponent >= 1) {
  184. size_t integer_part = (size_t)exponent;
  185. if (integer_part & 1)
  186. result *= e_to_power<1>();
  187. if (integer_part & 2)
  188. result *= e_to_power<2>();
  189. if (integer_part > 3) {
  190. if (integer_part & 4)
  191. result *= e_to_power<4>();
  192. if (integer_part & 8)
  193. result *= e_to_power<8>();
  194. if (integer_part & 16)
  195. result *= e_to_power<16>();
  196. if (integer_part & 32)
  197. result *= e_to_power<32>();
  198. if (integer_part >= 64)
  199. return __builtin_huge_val();
  200. }
  201. exponent -= integer_part;
  202. } else if (exponent < 0)
  203. return 1 / exp(-exponent);
  204. double taylor_series_result = 1 + exponent;
  205. double taylor_series_numerator = exponent * exponent;
  206. taylor_series_result += taylor_series_numerator / factorial<2>();
  207. taylor_series_numerator *= exponent;
  208. taylor_series_result += taylor_series_numerator / factorial<3>();
  209. taylor_series_numerator *= exponent;
  210. taylor_series_result += taylor_series_numerator / factorial<4>();
  211. taylor_series_numerator *= exponent;
  212. taylor_series_result += taylor_series_numerator / factorial<5>();
  213. return result * taylor_series_result;
  214. }
  215. float expf(float exponent)
  216. {
  217. return (float)exp(exponent);
  218. }
  219. double cosh(double x)
  220. {
  221. double exponentiated = exp(-x);
  222. if (x < 0)
  223. return (1 + exponentiated * exponentiated) / 2 / exponentiated;
  224. return (1 / exponentiated + exponentiated) / 2;
  225. }
  226. double atan2(double y, double x)
  227. {
  228. if (x > 0)
  229. return atan(y / x);
  230. if (x == 0) {
  231. if (y > 0)
  232. return M_PI_2;
  233. if (y < 0)
  234. return -M_PI_2;
  235. return 0;
  236. }
  237. if (y >= 0)
  238. return atan(y / x) + M_PI;
  239. return atan(y / x) - M_PI;
  240. }
  241. float atan2f(float y, float x)
  242. {
  243. return (float)atan2(y, x);
  244. }
  245. double atan(double x)
  246. {
  247. if (x < 0)
  248. return -atan(-x);
  249. if (x > 1)
  250. return M_PI_2 - atan(1 / x);
  251. double squared = x * x;
  252. 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)))))));
  253. }
  254. double asin(double x)
  255. {
  256. if (x > 1 || x < -1)
  257. return __builtin_nan("");
  258. if (x > 0.5 || x < -0.5)
  259. return 2 * atan(x / (1 + sqrt(1 - x * x)));
  260. double squared = x * x;
  261. double value = x;
  262. double i = x * squared;
  263. value += i * product_odd<1>() / product_even<2>() / 3;
  264. i *= squared;
  265. value += i * product_odd<3>() / product_even<4>() / 5;
  266. i *= squared;
  267. value += i * product_odd<5>() / product_even<6>() / 7;
  268. i *= squared;
  269. value += i * product_odd<7>() / product_even<8>() / 9;
  270. i *= squared;
  271. value += i * product_odd<9>() / product_even<10>() / 11;
  272. i *= squared;
  273. value += i * product_odd<11>() / product_even<12>() / 13;
  274. return value;
  275. }
  276. float asinf(float x)
  277. {
  278. return (float)asin(x);
  279. }
  280. double acos(double x)
  281. {
  282. return M_PI_2 - asin(x);
  283. }
  284. float acosf(float x)
  285. {
  286. return M_PI_2 - asinf(x);
  287. }
  288. double fabs(double value)
  289. {
  290. return value < 0 ? -value : value;
  291. }
  292. double log2(double x)
  293. {
  294. return log(x) / M_LN2;
  295. }
  296. float log2f(float x)
  297. {
  298. return log2(x);
  299. }
  300. long double log2l(long double x)
  301. {
  302. return log2(x);
  303. }
  304. double frexp(double, int*)
  305. {
  306. ASSERT_NOT_REACHED();
  307. return 0;
  308. }
  309. float frexpf(float, int*)
  310. {
  311. ASSERT_NOT_REACHED();
  312. return 0;
  313. }
  314. long double frexpl(long double, int*)
  315. {
  316. ASSERT_NOT_REACHED();
  317. return 0;
  318. }
  319. float roundf(float value)
  320. {
  321. // FIXME: Please fix me. I am naive.
  322. if (value >= 0.0f)
  323. return (float)(int)(value + 0.5f);
  324. return (float)(int)(value - 0.5f);
  325. }
  326. double floor(double value)
  327. {
  328. return (int)value;
  329. }
  330. double rint(double value)
  331. {
  332. return (int)roundf(value);
  333. }
  334. float ceilf(float value)
  335. {
  336. // FIXME: Please fix me. I am naive.
  337. int as_int = (int)value;
  338. if (value == (float)as_int) {
  339. return (float)as_int;
  340. }
  341. return as_int + 1;
  342. }
  343. double ceil(double value)
  344. {
  345. // FIXME: Please fix me. I am naive.
  346. int as_int = (int)value;
  347. if (value == (double)as_int) {
  348. return (double)as_int;
  349. }
  350. return as_int + 1;
  351. }
  352. double modf(double x, double* intpart)
  353. {
  354. *intpart = (double)((int)(x));
  355. return x - (int)x;
  356. }
  357. }