math.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #include <math.h>
  2. double exp(double x) {
  3. return __builtin_exp(x);
  4. }
  5. double floor(double x) {
  6. if (x > -1.0 && x < 1.0) {
  7. if (x >= 0) {
  8. return 0.0;
  9. } else {
  10. return -1.0;
  11. }
  12. }
  13. if (x < 0) {
  14. int x_i = x;
  15. return (double)(x_i - 1);
  16. } else {
  17. int x_i = x;
  18. return (double)x_i;
  19. }
  20. }
  21. int abs(int j) {
  22. return (j < 0 ? -j : j);
  23. }
  24. double pow(double x, double y) {
  25. double out;
  26. asm volatile (
  27. "fyl2x;"
  28. "fld %%st;"
  29. "frndint;"
  30. "fsub %%st,%%st(1);"
  31. "fxch;"
  32. "fchs;"
  33. "f2xm1;"
  34. "fld1;"
  35. "faddp;"
  36. "fxch;"
  37. "fld1;"
  38. "fscale;"
  39. "fstp %%st(1);"
  40. "fmulp;" : "=t"(out) : "0"(x),"u"(y) : "st(1)" );
  41. return out;
  42. }
  43. double fabs(double x) {
  44. return __builtin_fabs(x);
  45. }
  46. double fmod(double x, double y) {
  47. if (x >= 0.0) {
  48. while (x > y) {
  49. x -= y;
  50. }
  51. return x;
  52. } else {
  53. return 0.0;
  54. }
  55. }
  56. double sqrt(double x) {
  57. return __builtin_sqrt(x);
  58. }
  59. static double bad_sine_table[] = {
  60. 0,
  61. 0.01745240644,
  62. 0.03489949671,
  63. 0.05233595625,
  64. 0.06975647375,
  65. 0.08715574276,
  66. 0.1045284633,
  67. 0.1218693434,
  68. 0.139173101,
  69. 0.1564344651,
  70. 0.1736481777,
  71. 0.1908089954,
  72. 0.2079116908,
  73. 0.2249510544,
  74. 0.2419218956,
  75. 0.2588190451,
  76. 0.2756373559,
  77. 0.2923717048,
  78. 0.3090169944,
  79. 0.3255681545,
  80. 0.3420201434,
  81. 0.3583679496,
  82. 0.3746065935,
  83. 0.3907311285,
  84. 0.4067366431,
  85. 0.4226182618,
  86. 0.4383711468,
  87. 0.4539904998,
  88. 0.4694715628,
  89. 0.4848096203,
  90. 0.5000000001,
  91. 0.515038075,
  92. 0.5299192643,
  93. 0.5446390351,
  94. 0.5591929035,
  95. 0.5735764364,
  96. 0.5877852524,
  97. 0.6018150232,
  98. 0.6156614754,
  99. 0.6293203911,
  100. 0.6427876098,
  101. 0.6560590291,
  102. 0.6691306064,
  103. 0.6819983601,
  104. 0.6946583705,
  105. 0.7071067813,
  106. 0.7193398004,
  107. 0.7313537017,
  108. 0.7431448256,
  109. 0.7547095803,
  110. 0.7660444432,
  111. 0.7771459615,
  112. 0.7880107537,
  113. 0.7986355101,
  114. 0.8090169944,
  115. 0.8191520444,
  116. 0.8290375726,
  117. 0.838670568,
  118. 0.8480480962,
  119. 0.8571673008,
  120. 0.8660254039,
  121. 0.8746197072,
  122. 0.8829475929,
  123. 0.8910065243,
  124. 0.8987940464,
  125. 0.9063077871,
  126. 0.9135454577,
  127. 0.9205048535,
  128. 0.9271838546,
  129. 0.9335804266,
  130. 0.9396926208,
  131. 0.9455185757,
  132. 0.9510565163,
  133. 0.956304756,
  134. 0.961261696,
  135. 0.9659258263,
  136. 0.9702957263,
  137. 0.9743700648,
  138. 0.9781476008,
  139. 0.9816271835,
  140. 0.984807753,
  141. 0.9876883406,
  142. 0.9902680688,
  143. 0.9925461517,
  144. 0.9945218954,
  145. 0.9961946981,
  146. 0.9975640503,
  147. 0.9986295348,
  148. 0.999390827,
  149. 0.9998476952,
  150. 1,
  151. 0.9998476952,
  152. 0.999390827,
  153. 0.9986295347,
  154. 0.9975640502,
  155. 0.9961946981,
  156. 0.9945218953,
  157. 0.9925461516,
  158. 0.9902680687,
  159. 0.9876883406,
  160. 0.984807753,
  161. 0.9816271834,
  162. 0.9781476007,
  163. 0.9743700647,
  164. 0.9702957262,
  165. 0.9659258262,
  166. 0.9612616959,
  167. 0.9563047559,
  168. 0.9510565162,
  169. 0.9455185755,
  170. 0.9396926207,
  171. 0.9335804264,
  172. 0.9271838545,
  173. 0.9205048534,
  174. 0.9135454575,
  175. 0.9063077869,
  176. 0.8987940462,
  177. 0.8910065241,
  178. 0.8829475927,
  179. 0.874619707,
  180. 0.8660254036,
  181. 0.8571673006,
  182. 0.848048096,
  183. 0.8386705678,
  184. 0.8290375724,
  185. 0.8191520441,
  186. 0.8090169942,
  187. 0.7986355099,
  188. 0.7880107534,
  189. 0.7771459613,
  190. 0.7660444429,
  191. 0.75470958,
  192. 0.7431448253,
  193. 0.7313537014,
  194. 0.7193398001,
  195. 0.707106781,
  196. 0.6946583702,
  197. 0.6819983598,
  198. 0.6691306061,
  199. 0.6560590288,
  200. 0.6427876094,
  201. 0.6293203908,
  202. 0.6156614751,
  203. 0.6018150229,
  204. 0.587785252,
  205. 0.5735764361,
  206. 0.5591929032,
  207. 0.5446390347,
  208. 0.5299192639,
  209. 0.5150380746,
  210. 0.4999999997,
  211. 0.4848096199,
  212. 0.4694715625,
  213. 0.4539904994,
  214. 0.4383711465,
  215. 0.4226182614,
  216. 0.4067366428,
  217. 0.3907311282,
  218. 0.3746065931,
  219. 0.3583679492,
  220. 0.342020143,
  221. 0.3255681541,
  222. 0.309016994,
  223. 0.2923717044,
  224. 0.2756373555,
  225. 0.2588190447,
  226. 0.2419218952,
  227. 0.224951054,
  228. 0.2079116904,
  229. 0.190808995,
  230. 0.1736481773,
  231. 0.1564344647,
  232. 0.1391731006,
  233. 0.121869343,
  234. 0.1045284629,
  235. 0.08715574235,
  236. 0.06975647334,
  237. 0.05233595584,
  238. 0.0348994963,
  239. 0.01745240603,
  240. -0.000000000410206857,
  241. -0.01745240685,
  242. -0.03489949712,
  243. -0.05233595666,
  244. -0.06975647416,
  245. -0.08715574317,
  246. -0.1045284637,
  247. -0.1218693438,
  248. -0.1391731014,
  249. -0.1564344655,
  250. -0.1736481781,
  251. -0.1908089958,
  252. -0.2079116912,
  253. -0.2249510548,
  254. -0.241921896,
  255. -0.2588190455,
  256. -0.2756373562,
  257. -0.2923717052,
  258. -0.3090169948,
  259. -0.3255681549,
  260. -0.3420201438,
  261. -0.35836795,
  262. -0.3746065938,
  263. -0.3907311289,
  264. -0.4067366435,
  265. -0.4226182622,
  266. -0.4383711472,
  267. -0.4539905002,
  268. -0.4694715632,
  269. -0.4848096207,
  270. -0.5000000004,
  271. -0.5150380753,
  272. -0.5299192646,
  273. -0.5446390354,
  274. -0.5591929039,
  275. -0.5735764368,
  276. -0.5877852527,
  277. -0.6018150235,
  278. -0.6156614757,
  279. -0.6293203914,
  280. -0.6427876101,
  281. -0.6560590294,
  282. -0.6691306067,
  283. -0.6819983604,
  284. -0.6946583708,
  285. -0.7071067815,
  286. -0.7193398007,
  287. -0.731353702,
  288. -0.7431448258,
  289. -0.7547095806,
  290. -0.7660444435,
  291. -0.7771459618,
  292. -0.7880107539,
  293. -0.7986355104,
  294. -0.8090169947,
  295. -0.8191520446,
  296. -0.8290375729,
  297. -0.8386705682,
  298. -0.8480480964,
  299. -0.857167301,
  300. -0.8660254041,
  301. -0.8746197074,
  302. -0.8829475931,
  303. -0.8910065244,
  304. -0.8987940465,
  305. -0.9063077873,
  306. -0.9135454579,
  307. -0.9205048537,
  308. -0.9271838548,
  309. -0.9335804267,
  310. -0.939692621,
  311. -0.9455185758,
  312. -0.9510565165,
  313. -0.9563047561,
  314. -0.9612616961,
  315. -0.9659258264,
  316. -0.9702957264,
  317. -0.9743700649,
  318. -0.9781476009,
  319. -0.9816271836,
  320. -0.9848077531,
  321. -0.9876883407,
  322. -0.9902680688,
  323. -0.9925461517,
  324. -0.9945218954,
  325. -0.9961946981,
  326. -0.9975640503,
  327. -0.9986295348,
  328. -0.999390827,
  329. -0.9998476952,
  330. -1,
  331. -0.9998476951,
  332. -0.999390827,
  333. -0.9986295347,
  334. -0.9975640502,
  335. -0.996194698,
  336. -0.9945218953,
  337. -0.9925461516,
  338. -0.9902680687,
  339. -0.9876883405,
  340. -0.9848077529,
  341. -0.9816271833,
  342. -0.9781476006,
  343. -0.9743700646,
  344. -0.9702957261,
  345. -0.9659258261,
  346. -0.9612616958,
  347. -0.9563047558,
  348. -0.9510565161,
  349. -0.9455185754,
  350. -0.9396926206,
  351. -0.9335804263,
  352. -0.9271838543,
  353. -0.9205048532,
  354. -0.9135454574,
  355. -0.9063077868,
  356. -0.898794046,
  357. -0.8910065239,
  358. -0.8829475925,
  359. -0.8746197068,
  360. -0.8660254034,
  361. -0.8571673003,
  362. -0.8480480958,
  363. -0.8386705676,
  364. -0.8290375722,
  365. -0.8191520439,
  366. -0.809016994,
  367. -0.7986355096,
  368. -0.7880107532,
  369. -0.777145961,
  370. -0.7660444427,
  371. -0.7547095798,
  372. -0.743144825,
  373. -0.7313537011,
  374. -0.7193397998,
  375. -0.7071067807,
  376. -0.6946583699,
  377. -0.6819983595,
  378. -0.6691306058,
  379. -0.6560590284,
  380. -0.6427876091,
  381. -0.6293203905,
  382. -0.6156614747,
  383. -0.6018150226,
  384. -0.5877852517,
  385. -0.5735764357,
  386. -0.5591929029,
  387. -0.5446390344,
  388. -0.5299192636,
  389. -0.5150380743,
  390. -0.4999999993,
  391. -0.4848096196,
  392. -0.4694715621,
  393. -0.4539904991,
  394. -0.4383711461,
  395. -0.422618261,
  396. -0.4067366424,
  397. -0.3907311278,
  398. -0.3746065927,
  399. -0.3583679488,
  400. -0.3420201426,
  401. -0.3255681537,
  402. -0.3090169936,
  403. -0.292371704,
  404. -0.2756373551,
  405. -0.2588190443,
  406. -0.2419218948,
  407. -0.2249510536,
  408. -0.20791169,
  409. -0.1908089946,
  410. -0.1736481769,
  411. -0.1564344643,
  412. -0.1391731002,
  413. -0.1218693426,
  414. -0.1045284625,
  415. -0.08715574194,
  416. -0.06975647293,
  417. -0.05233595543,
  418. -0.03489949589,
  419. -0.01745240562,
  420. 0.0
  421. };
  422. double sin(double x) {
  423. if (x < 0.0) {
  424. x += 3.141592654 * 2.0 * 100.0;
  425. }
  426. double z = fmod(x, 3.141592654 * 2.0);
  427. int i = z * 360.0 / (3.141582654 * 2.0);
  428. return bad_sine_table[i];
  429. }
  430. double cos(double x) {
  431. return sin(x + 3.141592654 / 2.0);
  432. }