lib-arm64.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * TCC runtime library for arm64.
  3. *
  4. * Copyright (c) 2015 Edmund Grimley Evans
  5. *
  6. * Copying and distribution of this file, with or without modification,
  7. * are permitted in any medium without royalty provided the copyright
  8. * notice and this notice are preserved. This file is offered as-is,
  9. * without any warranty.
  10. */
  11. #ifdef __TINYC__
  12. typedef signed char int8_t;
  13. typedef unsigned char uint8_t;
  14. typedef short int16_t;
  15. typedef unsigned short uint16_t;
  16. typedef int int32_t;
  17. typedef unsigned uint32_t;
  18. typedef long long int64_t;
  19. typedef unsigned long long uint64_t;
  20. void *memcpy(void*,void*,__SIZE_TYPE__);
  21. #else
  22. #include <stdint.h>
  23. #include <string.h>
  24. #endif
  25. void __clear_cache(void *beg, void *end)
  26. {
  27. __arm64_clear_cache(beg, end);
  28. }
  29. typedef struct {
  30. uint64_t x0, x1;
  31. } u128_t;
  32. static long double f3_zero(int sgn)
  33. {
  34. long double f;
  35. u128_t x = { 0, (uint64_t)sgn << 63 };
  36. memcpy(&f, &x, 16);
  37. return f;
  38. }
  39. static long double f3_infinity(int sgn)
  40. {
  41. long double f;
  42. u128_t x = { 0, (uint64_t)sgn << 63 | 0x7fff000000000000 };
  43. memcpy(&f, &x, 16);
  44. return f;
  45. }
  46. static long double f3_NaN(void)
  47. {
  48. long double f;
  49. #if 0
  50. // ARM's default NaN usually has just the top fraction bit set:
  51. u128_t x = { 0, 0x7fff800000000000 };
  52. #else
  53. // GCC's library sets all fraction bits:
  54. u128_t x = { -1, 0x7fffffffffffffff };
  55. #endif
  56. memcpy(&f, &x, 16);
  57. return f;
  58. }
  59. static int fp3_convert_NaN(long double *f, int sgn, u128_t mnt)
  60. {
  61. u128_t x = { mnt.x0,
  62. mnt.x1 | 0x7fff800000000000 | (uint64_t)sgn << 63 };
  63. memcpy(f, &x, 16);
  64. return 1;
  65. }
  66. static int fp3_detect_NaNs(long double *f,
  67. int a_sgn, int a_exp, u128_t a,
  68. int b_sgn, int b_exp, u128_t b)
  69. {
  70. // Detect signalling NaNs:
  71. if (a_exp == 32767 && (a.x0 | a.x1 << 16) && !(a.x1 >> 47 & 1))
  72. return fp3_convert_NaN(f, a_sgn, a);
  73. if (b_exp == 32767 && (b.x0 | b.x1 << 16) && !(b.x1 >> 47 & 1))
  74. return fp3_convert_NaN(f, b_sgn, b);
  75. // Detect quiet NaNs:
  76. if (a_exp == 32767 && (a.x0 | a.x1 << 16))
  77. return fp3_convert_NaN(f, a_sgn, a);
  78. if (b_exp == 32767 && (b.x0 | b.x1 << 16))
  79. return fp3_convert_NaN(f, b_sgn, b);
  80. return 0;
  81. }
  82. static void f3_unpack(int *sgn, int32_t *exp, u128_t *mnt, long double f)
  83. {
  84. u128_t x;
  85. memcpy(&x, &f, 16);
  86. *sgn = x.x1 >> 63;
  87. *exp = x.x1 >> 48 & 32767;
  88. x.x1 = x.x1 << 16 >> 16;
  89. if (*exp)
  90. x.x1 |= (uint64_t)1 << 48;
  91. else
  92. *exp = 1;
  93. *mnt = x;
  94. }
  95. static u128_t f3_normalise(int32_t *exp, u128_t mnt)
  96. {
  97. int sh;
  98. if (!(mnt.x0 | mnt.x1))
  99. return mnt;
  100. if (!mnt.x1) {
  101. mnt.x1 = mnt.x0;
  102. mnt.x0 = 0;
  103. *exp -= 64;
  104. }
  105. for (sh = 32; sh; sh >>= 1) {
  106. if (!(mnt.x1 >> (64 - sh))) {
  107. mnt.x1 = mnt.x1 << sh | mnt.x0 >> (64 - sh);
  108. mnt.x0 = mnt.x0 << sh;
  109. *exp -= sh;
  110. }
  111. }
  112. return mnt;
  113. }
  114. static u128_t f3_sticky_shift(int32_t sh, u128_t x)
  115. {
  116. if (sh >= 128) {
  117. x.x0 = !!(x.x0 | x.x1);
  118. x.x1 = 0;
  119. return x;
  120. }
  121. if (sh >= 64) {
  122. x.x0 = x.x1 | !!x.x0;
  123. x.x1 = 0;
  124. sh -= 64;
  125. }
  126. if (sh > 0) {
  127. x.x0 = x.x0 >> sh | x.x1 << (64 - sh) | !!(x.x0 << (64 - sh));
  128. x.x1 = x.x1 >> sh;
  129. }
  130. return x;
  131. }
  132. static long double f3_round(int sgn, int32_t exp, u128_t x)
  133. {
  134. long double f;
  135. int error;
  136. if (exp > 0) {
  137. x = f3_sticky_shift(13, x);
  138. }
  139. else {
  140. x = f3_sticky_shift(14 - exp, x);
  141. exp = 0;
  142. }
  143. error = x.x0 & 3;
  144. x.x0 = x.x0 >> 2 | x.x1 << 62;
  145. x.x1 = x.x1 >> 2;
  146. if (error == 3 || ((error == 2) & (x.x0 & 1))) {
  147. if (!++x.x0) {
  148. ++x.x1;
  149. if (x.x1 == (uint64_t)1 << 48)
  150. exp = 1;
  151. else if (x.x1 == (uint64_t)1 << 49) {
  152. ++exp;
  153. x.x0 = x.x0 >> 1 | x.x1 << 63;
  154. x.x1 = x.x1 >> 1;
  155. }
  156. }
  157. }
  158. if (exp >= 32767)
  159. return f3_infinity(sgn);
  160. x.x1 = x.x1 << 16 >> 16 | (uint64_t)exp << 48 | (uint64_t)sgn << 63;
  161. memcpy(&f, &x, 16);
  162. return f;
  163. }
  164. static long double f3_add(long double fa, long double fb, int neg)
  165. {
  166. u128_t a, b, x;
  167. int32_t a_exp, b_exp, x_exp;
  168. int a_sgn, b_sgn, x_sgn;
  169. long double fx;
  170. f3_unpack(&a_sgn, &a_exp, &a, fa);
  171. f3_unpack(&b_sgn, &b_exp, &b, fb);
  172. if (fp3_detect_NaNs(&fx, a_sgn, a_exp, a, b_sgn, b_exp, b))
  173. return fx;
  174. b_sgn ^= neg;
  175. // Handle infinities and zeroes:
  176. if (a_exp == 32767 && b_exp == 32767 && a_sgn != b_sgn)
  177. return f3_NaN();
  178. if (a_exp == 32767)
  179. return f3_infinity(a_sgn);
  180. if (b_exp == 32767)
  181. return f3_infinity(b_sgn);
  182. if (!(a.x0 | a.x1 | b.x0 | b.x1))
  183. return f3_zero(a_sgn & b_sgn);
  184. a.x1 = a.x1 << 3 | a.x0 >> 61;
  185. a.x0 = a.x0 << 3;
  186. b.x1 = b.x1 << 3 | b.x0 >> 61;
  187. b.x0 = b.x0 << 3;
  188. if (a_exp <= b_exp) {
  189. a = f3_sticky_shift(b_exp - a_exp, a);
  190. a_exp = b_exp;
  191. }
  192. else {
  193. b = f3_sticky_shift(a_exp - b_exp, b);
  194. b_exp = a_exp;
  195. }
  196. x_sgn = a_sgn;
  197. x_exp = a_exp;
  198. if (a_sgn == b_sgn) {
  199. x.x0 = a.x0 + b.x0;
  200. x.x1 = a.x1 + b.x1 + (x.x0 < a.x0);
  201. }
  202. else {
  203. x.x0 = a.x0 - b.x0;
  204. x.x1 = a.x1 - b.x1 - (x.x0 > a.x0);
  205. if (x.x1 >> 63) {
  206. x_sgn ^= 1;
  207. x.x0 = -x.x0;
  208. x.x1 = -x.x1 - !!x.x0;
  209. }
  210. }
  211. if (!(x.x0 | x.x1))
  212. return f3_zero(0);
  213. x = f3_normalise(&x_exp, x);
  214. return f3_round(x_sgn, x_exp + 12, x);
  215. }
  216. long double __addtf3(long double a, long double b)
  217. {
  218. return f3_add(a, b, 0);
  219. }
  220. long double __subtf3(long double a, long double b)
  221. {
  222. return f3_add(a, b, 1);
  223. }
  224. long double __multf3(long double fa, long double fb)
  225. {
  226. u128_t a, b, x;
  227. int32_t a_exp, b_exp, x_exp;
  228. int a_sgn, b_sgn, x_sgn;
  229. long double fx;
  230. f3_unpack(&a_sgn, &a_exp, &a, fa);
  231. f3_unpack(&b_sgn, &b_exp, &b, fb);
  232. if (fp3_detect_NaNs(&fx, a_sgn, a_exp, a, b_sgn, b_exp, b))
  233. return fx;
  234. // Handle infinities and zeroes:
  235. if ((a_exp == 32767 && !(b.x0 | b.x1)) ||
  236. (b_exp == 32767 && !(a.x0 | a.x1)))
  237. return f3_NaN();
  238. if (a_exp == 32767 || b_exp == 32767)
  239. return f3_infinity(a_sgn ^ b_sgn);
  240. if (!(a.x0 | a.x1) || !(b.x0 | b.x1))
  241. return f3_zero(a_sgn ^ b_sgn);
  242. a = f3_normalise(&a_exp, a);
  243. b = f3_normalise(&b_exp, b);
  244. x_sgn = a_sgn ^ b_sgn;
  245. x_exp = a_exp + b_exp - 16352;
  246. {
  247. // Convert to base (1 << 30), discarding bottom 6 bits, which are zero,
  248. // so there are (32, 30, 30, 30) bits in (a3, a2, a1, a0):
  249. uint64_t a0 = a.x0 << 28 >> 34;
  250. uint64_t b0 = b.x0 << 28 >> 34;
  251. uint64_t a1 = a.x0 >> 36 | a.x1 << 62 >> 34;
  252. uint64_t b1 = b.x0 >> 36 | b.x1 << 62 >> 34;
  253. uint64_t a2 = a.x1 << 32 >> 34;
  254. uint64_t b2 = b.x1 << 32 >> 34;
  255. uint64_t a3 = a.x1 >> 32;
  256. uint64_t b3 = b.x1 >> 32;
  257. // Use 16 small multiplications and additions that do not overflow:
  258. uint64_t x0 = a0 * b0;
  259. uint64_t x1 = (x0 >> 30) + a0 * b1 + a1 * b0;
  260. uint64_t x2 = (x1 >> 30) + a0 * b2 + a1 * b1 + a2 * b0;
  261. uint64_t x3 = (x2 >> 30) + a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0;
  262. uint64_t x4 = (x3 >> 30) + a1 * b3 + a2 * b2 + a3 * b1;
  263. uint64_t x5 = (x4 >> 30) + a2 * b3 + a3 * b2;
  264. uint64_t x6 = (x5 >> 30) + a3 * b3;
  265. // We now have (64, 30, 30, ...) bits in (x6, x5, x4, ...).
  266. // Take the top 128 bits, setting bottom bit if any lower bits were set:
  267. uint64_t y0 = (x5 << 34 | x4 << 34 >> 30 | x3 << 34 >> 60 |
  268. !!(x3 << 38 | (x2 | x1 | x0) << 34));
  269. uint64_t y1 = x6;
  270. // Top bit may be zero. Renormalise:
  271. if (!(y1 >> 63)) {
  272. y1 = y1 << 1 | y0 >> 63;
  273. y0 = y0 << 1;
  274. --x_exp;
  275. }
  276. x.x0 = y0;
  277. x.x1 = y1;
  278. }
  279. return f3_round(x_sgn, x_exp, x);
  280. }
  281. long double __divtf3(long double fa, long double fb)
  282. {
  283. u128_t a, b, x;
  284. int32_t a_exp, b_exp, x_exp;
  285. int a_sgn, b_sgn, x_sgn, i;
  286. long double fx;
  287. f3_unpack(&a_sgn, &a_exp, &a, fa);
  288. f3_unpack(&b_sgn, &b_exp, &b, fb);
  289. if (fp3_detect_NaNs(&fx, a_sgn, a_exp, a, b_sgn, b_exp, b))
  290. return fx;
  291. // Handle infinities and zeroes:
  292. if ((a_exp == 32767 && b_exp == 32767) ||
  293. (!(a.x0 | a.x1) && !(b.x0 | b.x1)))
  294. return f3_NaN();
  295. if (a_exp == 32767 || !(b.x0 | b.x1))
  296. return f3_infinity(a_sgn ^ b_sgn);
  297. if (!(a.x0 | a.x1) || b_exp == 32767)
  298. return f3_zero(a_sgn ^ b_sgn);
  299. a = f3_normalise(&a_exp, a);
  300. b = f3_normalise(&b_exp, b);
  301. x_sgn = a_sgn ^ b_sgn;
  302. x_exp = a_exp - b_exp + 16395;
  303. a.x0 = a.x0 >> 1 | a.x1 << 63;
  304. a.x1 = a.x1 >> 1;
  305. b.x0 = b.x0 >> 1 | b.x1 << 63;
  306. b.x1 = b.x1 >> 1;
  307. x.x0 = 0;
  308. x.x1 = 0;
  309. for (i = 0; i < 116; i++) {
  310. x.x1 = x.x1 << 1 | x.x0 >> 63;
  311. x.x0 = x.x0 << 1;
  312. if (a.x1 > b.x1 || (a.x1 == b.x1 && a.x0 >= b.x0)) {
  313. a.x1 = a.x1 - b.x1 - (a.x0 < b.x0);
  314. a.x0 = a.x0 - b.x0;
  315. x.x0 |= 1;
  316. }
  317. a.x1 = a.x1 << 1 | a.x0 >> 63;
  318. a.x0 = a.x0 << 1;
  319. }
  320. x.x0 |= !!(a.x0 | a.x1);
  321. x = f3_normalise(&x_exp, x);
  322. return f3_round(x_sgn, x_exp, x);
  323. }
  324. long double __extendsftf2(float f)
  325. {
  326. long double fx;
  327. u128_t x;
  328. uint32_t a;
  329. uint64_t aa;
  330. memcpy(&a, &f, 4);
  331. aa = a;
  332. x.x0 = 0;
  333. if (!(a << 1))
  334. x.x1 = aa << 32;
  335. else if (a << 1 >> 24 == 255)
  336. x.x1 = (0x7fff000000000000 | aa >> 31 << 63 | aa << 41 >> 16 |
  337. (uint64_t)!!(a << 9) << 47);
  338. else
  339. x.x1 = (aa >> 31 << 63 | ((aa >> 23 & 255) + 16256) << 48 |
  340. aa << 41 >> 16);
  341. memcpy(&fx, &x, 16);
  342. return fx;
  343. }
  344. long double __extenddftf2(double f)
  345. {
  346. long double fx;
  347. u128_t x;
  348. uint64_t a;
  349. memcpy(&a, &f, 8);
  350. x.x0 = a << 60;
  351. if (!(a << 1))
  352. x.x1 = a;
  353. else if (a << 1 >> 53 == 2047)
  354. x.x1 = (0x7fff000000000000 | a >> 63 << 63 | a << 12 >> 16 |
  355. (uint64_t)!!(a << 12) << 47);
  356. else
  357. x.x1 = a >> 63 << 63 | ((a >> 52 & 2047) + 15360) << 48 | a << 12 >> 16;
  358. memcpy(&fx, &x, 16);
  359. return fx;
  360. }
  361. float __trunctfsf2(long double f)
  362. {
  363. u128_t mnt;
  364. int32_t exp;
  365. int sgn;
  366. uint32_t x;
  367. float fx;
  368. f3_unpack(&sgn, &exp, &mnt, f);
  369. if (exp == 32767 && (mnt.x0 | mnt.x1 << 16))
  370. x = 0x7fc00000 | (uint32_t)sgn << 31 | (mnt.x1 >> 25 & 0x007fffff);
  371. else if (exp > 16510)
  372. x = 0x7f800000 | (uint32_t)sgn << 31;
  373. else if (exp < 16233)
  374. x = (uint32_t)sgn << 31;
  375. else {
  376. exp -= 16257;
  377. x = mnt.x1 >> 23 | !!(mnt.x0 | mnt.x1 << 41);
  378. if (exp < 0) {
  379. x = x >> -exp | !!(x << (32 + exp));
  380. exp = 0;
  381. }
  382. if ((x & 3) == 3 || (x & 7) == 6)
  383. x += 4;
  384. x = ((x >> 2) + (exp << 23)) | (uint32_t)sgn << 31;
  385. }
  386. memcpy(&fx, &x, 4);
  387. return fx;
  388. }
  389. double __trunctfdf2(long double f)
  390. {
  391. u128_t mnt;
  392. int32_t exp;
  393. int sgn;
  394. uint64_t x;
  395. double fx;
  396. f3_unpack(&sgn, &exp, &mnt, f);
  397. if (exp == 32767 && (mnt.x0 | mnt.x1 << 16))
  398. x = (0x7ff8000000000000 | (uint64_t)sgn << 63 |
  399. mnt.x1 << 16 >> 12 | mnt.x0 >> 60);
  400. else if (exp > 17406)
  401. x = 0x7ff0000000000000 | (uint64_t)sgn << 63;
  402. else if (exp < 15308)
  403. x = (uint64_t)sgn << 63;
  404. else {
  405. exp -= 15361;
  406. x = mnt.x1 << 6 | mnt.x0 >> 58 | !!(mnt.x0 << 6);
  407. if (exp < 0) {
  408. x = x >> -exp | !!(x << (64 + exp));
  409. exp = 0;
  410. }
  411. if ((x & 3) == 3 || (x & 7) == 6)
  412. x += 4;
  413. x = ((x >> 2) + ((uint64_t)exp << 52)) | (uint64_t)sgn << 63;
  414. }
  415. memcpy(&fx, &x, 8);
  416. return fx;
  417. }
  418. int32_t __fixtfsi(long double fa)
  419. {
  420. u128_t a;
  421. int32_t a_exp;
  422. int a_sgn;
  423. int32_t x;
  424. f3_unpack(&a_sgn, &a_exp, &a, fa);
  425. if (a_exp < 16369)
  426. return 0;
  427. if (a_exp > 16413)
  428. return a_sgn ? -0x80000000 : 0x7fffffff;
  429. x = a.x1 >> (16431 - a_exp);
  430. return a_sgn ? -x : x;
  431. }
  432. int64_t __fixtfdi(long double fa)
  433. {
  434. u128_t a;
  435. int32_t a_exp;
  436. int a_sgn;
  437. int64_t x;
  438. f3_unpack(&a_sgn, &a_exp, &a, fa);
  439. if (a_exp < 16383)
  440. return 0;
  441. if (a_exp > 16445)
  442. return a_sgn ? -0x8000000000000000 : 0x7fffffffffffffff;
  443. x = (a.x1 << 15 | a.x0 >> 49) >> (16446 - a_exp);
  444. return a_sgn ? -x : x;
  445. }
  446. uint32_t __fixunstfsi(long double fa)
  447. {
  448. u128_t a;
  449. int32_t a_exp;
  450. int a_sgn;
  451. f3_unpack(&a_sgn, &a_exp, &a, fa);
  452. if (a_sgn || a_exp < 16369)
  453. return 0;
  454. if (a_exp > 16414)
  455. return -1;
  456. return a.x1 >> (16431 - a_exp);
  457. }
  458. uint64_t __fixunstfdi(long double fa)
  459. {
  460. u128_t a;
  461. int32_t a_exp;
  462. int a_sgn;
  463. f3_unpack(&a_sgn, &a_exp, &a, fa);
  464. if (a_sgn || a_exp < 16383)
  465. return 0;
  466. if (a_exp > 16446)
  467. return -1;
  468. return (a.x1 << 15 | a.x0 >> 49) >> (16446 - a_exp);
  469. }
  470. long double __floatsitf(int32_t a)
  471. {
  472. int sgn = 0;
  473. int exp = 16414;
  474. uint32_t mnt = a;
  475. u128_t x = { 0, 0 };
  476. long double f;
  477. int i;
  478. if (a) {
  479. if (a < 0) {
  480. sgn = 1;
  481. mnt = -mnt;
  482. }
  483. for (i = 16; i; i >>= 1)
  484. if (!(mnt >> (32 - i))) {
  485. mnt <<= i;
  486. exp -= i;
  487. }
  488. x.x1 = ((uint64_t)sgn << 63 | (uint64_t)exp << 48 |
  489. (uint64_t)(mnt << 1) << 16);
  490. }
  491. memcpy(&f, &x, 16);
  492. return f;
  493. }
  494. long double __floatditf(int64_t a)
  495. {
  496. int sgn = 0;
  497. int exp = 16446;
  498. uint64_t mnt = a;
  499. u128_t x = { 0, 0 };
  500. long double f;
  501. int i;
  502. if (a) {
  503. if (a < 0) {
  504. sgn = 1;
  505. mnt = -mnt;
  506. }
  507. for (i = 32; i; i >>= 1)
  508. if (!(mnt >> (64 - i))) {
  509. mnt <<= i;
  510. exp -= i;
  511. }
  512. x.x0 = mnt << 49;
  513. x.x1 = (uint64_t)sgn << 63 | (uint64_t)exp << 48 | mnt << 1 >> 16;
  514. }
  515. memcpy(&f, &x, 16);
  516. return f;
  517. }
  518. long double __floatunsitf(uint32_t a)
  519. {
  520. int exp = 16414;
  521. uint32_t mnt = a;
  522. u128_t x = { 0, 0 };
  523. long double f;
  524. int i;
  525. if (a) {
  526. for (i = 16; i; i >>= 1)
  527. if (!(mnt >> (32 - i))) {
  528. mnt <<= i;
  529. exp -= i;
  530. }
  531. x.x1 = (uint64_t)exp << 48 | (uint64_t)(mnt << 1) << 16;
  532. }
  533. memcpy(&f, &x, 16);
  534. return f;
  535. }
  536. long double __floatunditf(uint64_t a)
  537. {
  538. int exp = 16446;
  539. uint64_t mnt = a;
  540. u128_t x = { 0, 0 };
  541. long double f;
  542. int i;
  543. if (a) {
  544. for (i = 32; i; i >>= 1)
  545. if (!(mnt >> (64 - i))) {
  546. mnt <<= i;
  547. exp -= i;
  548. }
  549. x.x0 = mnt << 49;
  550. x.x1 = (uint64_t)exp << 48 | mnt << 1 >> 16;
  551. }
  552. memcpy(&f, &x, 16);
  553. return f;
  554. }
  555. static int f3_cmp(long double fa, long double fb)
  556. {
  557. u128_t a, b;
  558. memcpy(&a, &fa, 16);
  559. memcpy(&b, &fb, 16);
  560. return (!(a.x0 | a.x1 << 1 | b.x0 | b.x1 << 1) ? 0 :
  561. ((a.x1 << 1 >> 49 == 0x7fff && (a.x0 | a.x1 << 16)) ||
  562. (b.x1 << 1 >> 49 == 0x7fff && (b.x0 | b.x1 << 16))) ? 2 :
  563. a.x1 >> 63 != b.x1 >> 63 ? (int)(b.x1 >> 63) - (int)(a.x1 >> 63) :
  564. a.x1 < b.x1 ? (int)(a.x1 >> 63 << 1) - 1 :
  565. a.x1 > b.x1 ? 1 - (int)(a.x1 >> 63 << 1) :
  566. a.x0 < b.x0 ? (int)(a.x1 >> 63 << 1) - 1 :
  567. b.x0 < a.x0 ? 1 - (int)(a.x1 >> 63 << 1) : 0);
  568. }
  569. int __eqtf2(long double a, long double b)
  570. {
  571. return !!f3_cmp(a, b);
  572. }
  573. int __netf2(long double a, long double b)
  574. {
  575. return !!f3_cmp(a, b);
  576. }
  577. int __lttf2(long double a, long double b)
  578. {
  579. return f3_cmp(a, b);
  580. }
  581. int __letf2(long double a, long double b)
  582. {
  583. return f3_cmp(a, b);
  584. }
  585. int __gttf2(long double a, long double b)
  586. {
  587. return -f3_cmp(b, a);
  588. }
  589. int __getf2(long double a, long double b)
  590. {
  591. return -f3_cmp(b, a);
  592. }