mp_flipper_polyfill.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #include <limits.h>
  2. #include <stdint.h>
  3. #include <float.h>
  4. #include <math.h>
  5. #include <string.h>
  6. #define POLYFILL_FUN_1(name, ret, arg1) ret name(arg1)
  7. #define POLYFILL_FUN_2(name, ret, arg1, arg2) ret name(arg1, arg2)
  8. #define POLYFILL_FUN_3(name, ret, arg1, arg2, arg3) ret name(arg1, arg2, arg3)
  9. #define POLYFILL_FUN_4(name, ret, arg1, arg2, arg3, arg4) ret name(arg1, arg2, arg3, arg4)
  10. #ifndef __aeabi_l2f
  11. POLYFILL_FUN_1(__aeabi_l2f, float, long long x) {
  12. return x;
  13. }
  14. #endif
  15. #ifndef __aeabi_f2lz
  16. POLYFILL_FUN_1(__aeabi_f2lz, long long, float x) {
  17. return x;
  18. }
  19. #endif
  20. #ifndef __aeabi_dcmple
  21. POLYFILL_FUN_2(__aeabi_dcmple, double, double, double) {
  22. }
  23. #endif
  24. #ifndef __aeabi_dcmplt
  25. POLYFILL_FUN_2(__aeabi_dcmplt, double, double, double) {
  26. }
  27. #endif
  28. #ifndef __aeabi_dcmpun
  29. POLYFILL_FUN_2(__aeabi_dcmpun, double, double, double) {
  30. }
  31. #endif
  32. #ifndef __aeabi_dcmpeq
  33. POLYFILL_FUN_2(__aeabi_dcmpeq, double, double, double) {
  34. }
  35. #endif
  36. #ifndef __aeabi_dmul
  37. double __aeabi_dmul(double x, double y) {
  38. return x * y;
  39. }
  40. #endif
  41. #ifndef __aeabi_dadd
  42. POLYFILL_FUN_2(__aeabi_dadd, double, double x, double y) {
  43. return x + y;
  44. }
  45. #endif
  46. #ifndef __aeabi_ddiv
  47. POLYFILL_FUN_2(__aeabi_ddiv, double, double x, double y) {
  48. return x / y;
  49. }
  50. #endif
  51. #ifndef __aeabi_l2d
  52. POLYFILL_FUN_1(__aeabi_l2d, double, long x) {
  53. return x;
  54. }
  55. #endif
  56. #ifndef __aeabi_f2d
  57. POLYFILL_FUN_1(__aeabi_f2d, double, float x) {
  58. return x;
  59. }
  60. #endif
  61. #ifndef __aeabi_dsub
  62. POLYFILL_FUN_2(__aeabi_dsub, double, double x, double y) {
  63. return x - y;
  64. }
  65. #endif
  66. #ifndef __aeabi_dcmpge
  67. POLYFILL_FUN_2(__aeabi_dcmpge, double, double, double) {
  68. }
  69. #endif
  70. #ifndef __aeabi_i2d
  71. POLYFILL_FUN_1(__aeabi_i2d, double, int x) {
  72. return x;
  73. }
  74. #endif
  75. #ifndef __aeabi_dcmpgt
  76. POLYFILL_FUN_1(__aeabi_dcmpgt, double, double) {
  77. }
  78. #endif
  79. #ifndef __aeabi_d2iz
  80. POLYFILL_FUN_1(__aeabi_d2iz, long int, double x) {
  81. return x;
  82. }
  83. #endif
  84. #ifndef __aeabi_d2lz
  85. POLYFILL_FUN_1(__aeabi_d2lz, long, double x) {
  86. return x;
  87. }
  88. #endif
  89. #ifndef __aeabi_d2uiz
  90. POLYFILL_FUN_1(__aeabi_d2uiz, unsigned long int, double x) {
  91. return x;
  92. }
  93. #endif
  94. #ifndef __aeabi_d2f
  95. POLYFILL_FUN_1(__aeabi_d2f, float, double x) {
  96. return x;
  97. }
  98. #endif
  99. #ifndef __aeabi_ldivmod
  100. POLYFILL_FUN_2(__aeabi_ldivmod, long, long, long) {
  101. }
  102. #endif
  103. #ifndef strtox
  104. POLYFILL_FUN_4(strtox, long long unsigned int, const char*, char**, int, long long unsigned int) {
  105. }
  106. #endif
  107. #if FLT_EVAL_METHOD == 0 || FLT_EVAL_METHOD == 1
  108. #define EPS DBL_EPSILON
  109. #elif FLT_EVAL_METHOD == 2
  110. #define EPS LDBL_EPSILON
  111. #endif
  112. static const double_t toint = 1 / EPS;
  113. #ifndef FORCE_EVAL
  114. #define FORCE_EVAL(x) \
  115. do { \
  116. if(sizeof(x) == sizeof(float)) { \
  117. volatile float __x; \
  118. __x = (x); \
  119. (void)__x; \
  120. } else if(sizeof(x) == sizeof(double)) { \
  121. volatile double __x; \
  122. __x = (x); \
  123. (void)__x; \
  124. } else { \
  125. volatile long double __x; \
  126. __x = (x); \
  127. (void)__x; \
  128. } \
  129. } while(0)
  130. #endif
  131. #ifndef floorf
  132. float floorf(float x) {
  133. union {
  134. float f;
  135. uint32_t i;
  136. } u = {x};
  137. int e = (int)(u.i >> 23 & 0xff) - 0x7f;
  138. uint32_t m;
  139. if(e >= 23) return x;
  140. if(e >= 0) {
  141. m = 0x007fffff >> e;
  142. if((u.i & m) == 0) return x;
  143. FORCE_EVAL(x + 0x1p120f);
  144. if(u.i >> 31) u.i += m;
  145. u.i &= ~m;
  146. } else {
  147. FORCE_EVAL(x + 0x1p120f);
  148. if(u.i >> 31 == 0)
  149. u.i = 0;
  150. else if(u.i << 1)
  151. u.f = -1.0;
  152. }
  153. return u.f;
  154. }
  155. #endif
  156. #ifndef floor
  157. double floor(double x) {
  158. union {
  159. double f;
  160. uint64_t i;
  161. } u = {x};
  162. int e = u.i >> 52 & 0x7ff;
  163. double_t y;
  164. if(e >= 0x3ff + 52 || x == 0) return x;
  165. /* y = int(x) - x, where int(x) is an integer neighbor of x */
  166. if(u.i >> 63)
  167. y = x - toint + toint - x;
  168. else
  169. y = x + toint - toint - x;
  170. /* special case because of non-nearest rounding modes */
  171. if(e <= 0x3ff - 1) {
  172. FORCE_EVAL(y);
  173. return u.i >> 63 ? -1 : 0;
  174. }
  175. if(y > 0) return x + y - 1;
  176. return x + y;
  177. }
  178. #endif
  179. #ifndef fmodf
  180. float fmodf(float x, float y) {
  181. union {
  182. float f;
  183. uint32_t i;
  184. } ux = {x}, uy = {y};
  185. int ex = ux.i >> 23 & 0xff;
  186. int ey = uy.i >> 23 & 0xff;
  187. uint32_t sx = ux.i & 0x80000000;
  188. uint32_t i;
  189. uint32_t uxi = ux.i;
  190. if(uy.i << 1 == 0 || isnan(y) || ex == 0xff) return (x * y) / (x * y);
  191. if(uxi << 1 <= uy.i << 1) {
  192. if(uxi << 1 == uy.i << 1) return 0 * x;
  193. return x;
  194. }
  195. /* normalize x and y */
  196. if(!ex) {
  197. for(i = uxi << 9; i >> 31 == 0; ex--, i <<= 1)
  198. ;
  199. uxi <<= -ex + 1;
  200. } else {
  201. uxi &= -1U >> 9;
  202. uxi |= 1U << 23;
  203. }
  204. if(!ey) {
  205. for(i = uy.i << 9; i >> 31 == 0; ey--, i <<= 1)
  206. ;
  207. uy.i <<= -ey + 1;
  208. } else {
  209. uy.i &= -1U >> 9;
  210. uy.i |= 1U << 23;
  211. }
  212. /* x mod y */
  213. for(; ex > ey; ex--) {
  214. i = uxi - uy.i;
  215. if(i >> 31 == 0) {
  216. if(i == 0) return 0 * x;
  217. uxi = i;
  218. }
  219. uxi <<= 1;
  220. }
  221. i = uxi - uy.i;
  222. if(i >> 31 == 0) {
  223. if(i == 0) return 0 * x;
  224. uxi = i;
  225. }
  226. for(; uxi >> 23 == 0; uxi <<= 1, ex--)
  227. ;
  228. /* scale result up */
  229. if(ex > 0) {
  230. uxi -= 1U << 23;
  231. uxi |= (uint32_t)ex << 23;
  232. } else {
  233. uxi >>= -ex + 1;
  234. }
  235. uxi |= sx;
  236. ux.i = uxi;
  237. return ux.f;
  238. }
  239. #endif
  240. #ifndef fmod
  241. double fmod(double x, double y) {
  242. union {
  243. double f;
  244. uint64_t i;
  245. } ux = {x}, uy = {y};
  246. int ex = ux.i >> 52 & 0x7ff;
  247. int ey = uy.i >> 52 & 0x7ff;
  248. int sx = ux.i >> 63;
  249. uint64_t i;
  250. /* in the followings uxi should be ux.i, but then gcc wrongly adds */
  251. /* float load/store to inner loops ruining performance and code size */
  252. uint64_t uxi = ux.i;
  253. if(uy.i << 1 == 0 || isnan(y) || ex == 0x7ff) return (x * y) / (x * y);
  254. if(uxi << 1 <= uy.i << 1) {
  255. if(uxi << 1 == uy.i << 1) return 0 * x;
  256. return x;
  257. }
  258. /* normalize x and y */
  259. if(!ex) {
  260. for(i = uxi << 12; i >> 63 == 0; ex--, i <<= 1)
  261. ;
  262. uxi <<= -ex + 1;
  263. } else {
  264. uxi &= -1ULL >> 12;
  265. uxi |= 1ULL << 52;
  266. }
  267. if(!ey) {
  268. for(i = uy.i << 12; i >> 63 == 0; ey--, i <<= 1)
  269. ;
  270. uy.i <<= -ey + 1;
  271. } else {
  272. uy.i &= -1ULL >> 12;
  273. uy.i |= 1ULL << 52;
  274. }
  275. /* x mod y */
  276. for(; ex > ey; ex--) {
  277. i = uxi - uy.i;
  278. if(i >> 63 == 0) {
  279. if(i == 0) return 0 * x;
  280. uxi = i;
  281. }
  282. uxi <<= 1;
  283. }
  284. i = uxi - uy.i;
  285. if(i >> 63 == 0) {
  286. if(i == 0) return 0 * x;
  287. uxi = i;
  288. }
  289. for(; uxi >> 52 == 0; uxi <<= 1, ex--)
  290. ;
  291. /* scale result */
  292. if(ex > 0) {
  293. uxi -= 1ULL << 52;
  294. uxi |= (uint64_t)ex << 52;
  295. } else {
  296. uxi >>= -ex + 1;
  297. }
  298. uxi |= (uint64_t)sx << 63;
  299. ux.i = uxi;
  300. return ux.f;
  301. }
  302. #endif
  303. #ifndef nearbyintf
  304. float nearbyintf(float x) {
  305. union {
  306. float f;
  307. uint32_t i;
  308. } u = {x};
  309. int e = u.i >> 23 & 0xff;
  310. int s = u.i >> 31;
  311. float_t y;
  312. if(e >= 0x7f + 23) return x;
  313. if(s)
  314. y = x - 0x1p23f + 0x1p23f;
  315. else
  316. y = x + 0x1p23f - 0x1p23f;
  317. if(y == 0) return s ? -0.0f : 0.0f;
  318. return y;
  319. }
  320. #endif
  321. #ifndef stroll
  322. long long strtoll(const char* restrict s, char** restrict p, int base) {
  323. return strtox(s, p, base, LLONG_MIN);
  324. }
  325. #endif
  326. #ifndef pow
  327. double pow(double x, double y) {
  328. return powf(x, y);
  329. }
  330. #endif
  331. #ifndef rint
  332. double rint(double x) {
  333. union {
  334. double f;
  335. uint64_t i;
  336. } u = {x};
  337. int e = u.i >> 52 & 0x7ff;
  338. int s = u.i >> 63;
  339. double_t y;
  340. if(e >= 0x3ff + 52) return x;
  341. if(s)
  342. y = x - toint + toint;
  343. else
  344. y = x + toint - toint;
  345. if(y == 0) return s ? -0.0 : 0;
  346. return y;
  347. }
  348. #endif
  349. #ifndef nearbyint
  350. double nearbyint(double x) {
  351. #ifdef FE_INEXACT
  352. #pragma STDC FENV_ACCESS ON
  353. int e;
  354. e = fetestexcept(FE_INEXACT);
  355. #endif
  356. x = rint(x);
  357. #ifdef FE_INEXACT
  358. if(!e) feclearexcept(FE_INEXACT);
  359. #endif
  360. return x;
  361. }
  362. #endif