math.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* x87-backed math for the ring-3 libc.
  2. *
  3. * The target is i586 with -mno-sse, so double arithmetic is x87 anyway. The
  4. * transcendental routines use the x87 instructions with an explicit range
  5. * reduction for the trigonometric functions. Accuracy is a double-rounding
  6. * compromise, not a full fdlibm port. */
  7. #include <math.h>
  8. #include <float.h>
  9. #define PI 3.14159265358979323846
  10. #define LOG2E 1.44269504088896340736
  11. #define LN2 0.69314718055994530942
  12. static double round_mode(double x, unsigned int mode)
  13. {
  14. unsigned short cw, old;
  15. double r;
  16. if (__builtin_isnan(x) || __builtin_isinf(x) || x == 0.0)
  17. return x;
  18. __asm__ volatile ("fnstcw %0" : "=m"(cw));
  19. old = cw;
  20. cw = (unsigned short)((cw & ~0x0C00u) | (mode << 10));
  21. __asm__ volatile ("fldcw %0" : : "m"(cw));
  22. __asm__ volatile ("fldl %1; frndint; fstpl %0" : "=m"(r) : "m"(x));
  23. __asm__ volatile ("fldcw %0" : : "m"(old));
  24. return r;
  25. }
  26. double fabs(double x)
  27. {
  28. union {
  29. double d;
  30. unsigned long long u;
  31. } v;
  32. v.d = x;
  33. v.u &= 0x7fffffffffffffffULL;
  34. return v.d;
  35. }
  36. double floor(double x)
  37. {
  38. return round_mode(x, 1);
  39. }
  40. double ceil(double x)
  41. {
  42. return round_mode(x, 2);
  43. }
  44. double trunc(double x)
  45. {
  46. return round_mode(x, 3);
  47. }
  48. double round(double x)
  49. {
  50. if (__builtin_isnan(x) || __builtin_isinf(x))
  51. return x;
  52. /* Above 2^52 every double is already an integer. */
  53. if (fabs(x) >= 4503599627370496.0)
  54. return x;
  55. if (x >= 0.0)
  56. return floor(x + 0.5);
  57. return ceil(x - 0.5);
  58. }
  59. double sqrt(double x)
  60. {
  61. double r;
  62. __asm__ volatile ("fldl %1; fsqrt; fstpl %0" : "=m"(r) : "m"(x));
  63. return r;
  64. }
  65. double fmod(double x, double y)
  66. {
  67. double r;
  68. __asm__ volatile (
  69. "fldl %2\n\t"
  70. "fldl %1\n\t"
  71. "1:\n\t"
  72. "fprem\n\t"
  73. "fnstsw %%ax\n\t"
  74. "testw $0x400, %%ax\n\t"
  75. "jnz 1b\n\t"
  76. "fstp %%st(1)\n\t"
  77. "fstpl %0"
  78. : "=m"(r) : "m"(x), "m"(y) : "ax");
  79. return r;
  80. }
  81. double log(double x)
  82. {
  83. double r;
  84. __asm__ volatile ("fldln2; fldl %1; fyl2x; fstpl %0"
  85. : "=m"(r) : "m"(x));
  86. return r;
  87. }
  88. double log10(double x)
  89. {
  90. double r;
  91. __asm__ volatile ("fldlg2; fldl %1; fyl2x; fstpl %0"
  92. : "=m"(r) : "m"(x));
  93. return r;
  94. }
  95. double ldexp(double x, int exp)
  96. {
  97. double r;
  98. __asm__ volatile (
  99. "fildl %1\n\t"
  100. "fldl %2\n\t"
  101. "fscale\n\t"
  102. "fstp %%st(1)\n\t"
  103. "fstpl %0"
  104. : "=m"(r) : "m"(exp), "m"(x));
  105. return r;
  106. }
  107. double exp(double x)
  108. {
  109. double y, n, f, p;
  110. if (__builtin_isnan(x))
  111. return x;
  112. if (x > 709.782712893384)
  113. return INFINITY;
  114. if (x < -745.133219101941)
  115. return 0.0;
  116. y = x * LOG2E;
  117. n = floor(y);
  118. f = y - n;
  119. __asm__ volatile ("fldl %1; f2xm1; fld1; faddp; fstpl %0"
  120. : "=m"(p) : "m"(f));
  121. return ldexp(p, (int)n);
  122. }
  123. double pow(double x, double y)
  124. {
  125. int neg = 0;
  126. double r;
  127. if (y == 0.0)
  128. return 1.0;
  129. if (x == 1.0)
  130. return 1.0;
  131. if (__builtin_isnan(x) || __builtin_isnan(y))
  132. return NAN;
  133. if (x == 0.0) {
  134. if (y < 0.0)
  135. return INFINITY;
  136. return 0.0;
  137. }
  138. if (x < 0.0) {
  139. double yi = floor(y);
  140. if (yi != y)
  141. return NAN;
  142. x = -x;
  143. if (fmod(yi, 2.0) != 0.0)
  144. neg = 1;
  145. }
  146. /* Small integer powers are common in JavaScript and stay exact. */
  147. if (y == floor(y) && y >= -64.0 && y <= 64.0) {
  148. int n = (int)y;
  149. int e = n < 0 ? -n : n;
  150. r = 1.0;
  151. while (e) {
  152. if (e & 1)
  153. r *= x;
  154. x *= x;
  155. e >>= 1;
  156. }
  157. if (n < 0)
  158. r = 1.0 / r;
  159. } else {
  160. r = exp(y * log(x));
  161. }
  162. return neg ? -r : r;
  163. }
  164. double sin(double x)
  165. {
  166. double r;
  167. /* The x87 reduction uses a 66-bit pi internally; pre-reducing with a
  168. * double 2*pi would lose more phase for large arguments. */
  169. __asm__ volatile ("fldl %1; fsin; fstpl %0" : "=m"(r) : "m"(x));
  170. return r;
  171. }
  172. double cos(double x)
  173. {
  174. double r;
  175. __asm__ volatile ("fldl %1; fcos; fstpl %0" : "=m"(r) : "m"(x));
  176. return r;
  177. }
  178. double tan(double x)
  179. {
  180. double r;
  181. __asm__ volatile ("fldl %1; fptan; fstp %%st(0); fstpl %0"
  182. : "=m"(r) : "m"(x));
  183. return r;
  184. }
  185. double atan(double x)
  186. {
  187. double r;
  188. __asm__ volatile ("fldl %1; fld1; fpatan; fstpl %0"
  189. : "=m"(r) : "m"(x));
  190. return r;
  191. }
  192. double atan2(double y, double x)
  193. {
  194. double r;
  195. __asm__ volatile ("fldl %1; fldl %2; fpatan; fstpl %0"
  196. : "=m"(r) : "m"(y), "m"(x));
  197. return r;
  198. }
  199. double asin(double x)
  200. {
  201. if (x > 1.0 || x < -1.0)
  202. return NAN;
  203. return atan2(x, sqrt(1.0 - x * x));
  204. }
  205. double acos(double x)
  206. {
  207. if (x > 1.0 || x < -1.0)
  208. return NAN;
  209. return atan2(sqrt(1.0 - x * x), x);
  210. }
  211. double fmin(double x, double y)
  212. {
  213. if (__builtin_isnan(x))
  214. return y;
  215. if (__builtin_isnan(y))
  216. return x;
  217. return x < y ? x : y;
  218. }
  219. double fmax(double x, double y)
  220. {
  221. if (__builtin_isnan(x))
  222. return y;
  223. if (__builtin_isnan(y))
  224. return x;
  225. return x > y ? x : y;
  226. }
  227. double copysign(double x, double y)
  228. {
  229. union {
  230. double d;
  231. unsigned long long u;
  232. } a, b;
  233. a.d = x;
  234. b.d = y;
  235. a.u = (a.u & 0x7fffffffffffffffULL) |
  236. (b.u & 0x8000000000000000ULL);
  237. return a.d;
  238. }
  239. double frexp(double x, int *exp)
  240. {
  241. union {
  242. double d;
  243. unsigned long long u;
  244. } v;
  245. unsigned long long bits;
  246. unsigned int e;
  247. int extra = 0;
  248. if (__builtin_isnan(x) || __builtin_isinf(x) || x == 0.0) {
  249. if (exp)
  250. *exp = 0;
  251. return x;
  252. }
  253. if (fabs(x) < DBL_MIN) {
  254. x *= 18446744073709551616.0; /* 2^64 */
  255. extra = 64;
  256. }
  257. v.d = x;
  258. bits = v.u;
  259. e = (unsigned int)((bits >> 52) & 0x7ffu);
  260. if (exp)
  261. *exp = (int)e - 1022 - extra;
  262. bits = (bits & 0x800fffffffffffffULL) | (1022ULL << 52);
  263. v.u = bits;
  264. return v.d;
  265. }
  266. double modf(double x, double *iptr)
  267. {
  268. double ip = trunc(x);
  269. if (iptr)
  270. *iptr = ip;
  271. return x - ip;
  272. }