bid128_noncomp.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /* Copyright (C) 2007-2022 Free Software Foundation, Inc.
  2. This file is part of GCC.
  3. GCC is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free
  5. Software Foundation; either version 3, or (at your option) any later
  6. version.
  7. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  8. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  10. for more details.
  11. Under Section 7 of GPL version 3, you are granted additional
  12. permissions described in the GCC Runtime Library Exception, version
  13. 3.1, as published by the Free Software Foundation.
  14. You should have received a copy of the GNU General Public License and
  15. a copy of the GCC Runtime Library Exception along with this program;
  16. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  17. <http://www.gnu.org/licenses/>. */
  18. #include "bid_internal.h"
  19. /*****************************************************************************
  20. *
  21. * BID128 non-computational functions:
  22. * - bid128_isSigned
  23. * - bid128_isNormal
  24. * - bid128_isSubnormal
  25. * - bid128_isFinite
  26. * - bid128_isZero
  27. * - bid128_isInf
  28. * - bid128_isSignaling
  29. * - bid128_isCanonical
  30. * - bid128_isNaN
  31. * - bid128_copy
  32. * - bid128_negate
  33. * - bid128_abs
  34. * - bid128_copySign
  35. * - bid128_class
  36. * - bid128_totalOrder
  37. * - bid128_totalOrderMag
  38. * - bid128_sameQuantum
  39. * - bid128_radix
  40. ****************************************************************************/
  41. #if DECIMAL_CALL_BY_REFERENCE
  42. void
  43. bid128_isSigned (int *pres,
  44. UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  45. UINT128 x = *px;
  46. #else
  47. int
  48. bid128_isSigned (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  49. #endif
  50. int res;
  51. res = ((x.w[HIGH_128W] & MASK_SIGN) == MASK_SIGN);
  52. BID_RETURN (res);
  53. }
  54. // return 1 iff x is not zero, nor NaN nor subnormal nor infinity
  55. #if DECIMAL_CALL_BY_REFERENCE
  56. void
  57. bid128_isNormal (int *pres,
  58. UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  59. UINT128 x = *px;
  60. #else
  61. int
  62. bid128_isNormal (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  63. #endif
  64. int res;
  65. UINT64 x_exp, C1_hi, C1_lo;
  66. BID_UI64DOUBLE tmp1;
  67. int exp, q, x_nr_bits;
  68. BID_SWAP128 (x);
  69. // test for special values - infinity or NaN
  70. if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) {
  71. // x is special
  72. res = 0;
  73. BID_RETURN (res);
  74. }
  75. // unpack x
  76. x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
  77. C1_hi = x.w[1] & MASK_COEFF;
  78. C1_lo = x.w[0];
  79. // test for zero
  80. if (C1_hi == 0 && C1_lo == 0) {
  81. res = 0;
  82. BID_RETURN (res);
  83. }
  84. // test for non-canonical values of the argument x
  85. if ((((C1_hi > 0x0001ed09bead87c0ull)
  86. || ((C1_hi == 0x0001ed09bead87c0ull)
  87. && (C1_lo > 0x378d8e63ffffffffull)))
  88. && ((x.w[1] & 0x6000000000000000ull) != 0x6000000000000000ull))
  89. || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
  90. res = 0;
  91. BID_RETURN (res);
  92. }
  93. // x is subnormal or normal
  94. // determine the number of digits q in the significand
  95. // q = nr. of decimal digits in x
  96. // determine first the nr. of bits in x
  97. if (C1_hi == 0) {
  98. if (C1_lo >= 0x0020000000000000ull) { // x >= 2^53
  99. // split the 64-bit value in two 32-bit halves to avoid rounding errors
  100. if (C1_lo >= 0x0000000100000000ull) { // x >= 2^32
  101. tmp1.d = (double) (C1_lo >> 32); // exact conversion
  102. x_nr_bits =
  103. 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  104. } else { // x < 2^32
  105. tmp1.d = (double) (C1_lo); // exact conversion
  106. x_nr_bits =
  107. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  108. }
  109. } else { // if x < 2^53
  110. tmp1.d = (double) C1_lo; // exact conversion
  111. x_nr_bits =
  112. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  113. }
  114. } else { // C1_hi != 0 => nr. bits = 64 + nr_bits (C1_hi)
  115. tmp1.d = (double) C1_hi; // exact conversion
  116. x_nr_bits =
  117. 65 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  118. }
  119. q = nr_digits[x_nr_bits - 1].digits;
  120. if (q == 0) {
  121. q = nr_digits[x_nr_bits - 1].digits1;
  122. if (C1_hi > nr_digits[x_nr_bits - 1].threshold_hi ||
  123. (C1_hi == nr_digits[x_nr_bits - 1].threshold_hi &&
  124. C1_lo >= nr_digits[x_nr_bits - 1].threshold_lo))
  125. q++;
  126. }
  127. exp = (int) (x_exp >> 49) - 6176;
  128. // test for subnormal values of x
  129. if (exp + q <= -6143) {
  130. res = 0;
  131. BID_RETURN (res);
  132. } else {
  133. res = 1;
  134. BID_RETURN (res);
  135. }
  136. }
  137. // return 1 iff x is not zero, nor NaN nor normal nor infinity
  138. #if DECIMAL_CALL_BY_REFERENCE
  139. void
  140. bid128_isSubnormal (int *pres,
  141. UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  142. UINT128 x = *px;
  143. #else
  144. int
  145. bid128_isSubnormal (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  146. #endif
  147. int res;
  148. UINT64 x_exp, C1_hi, C1_lo;
  149. BID_UI64DOUBLE tmp1;
  150. int exp, q, x_nr_bits;
  151. BID_SWAP128 (x);
  152. // test for special values - infinity or NaN
  153. if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) {
  154. // x is special
  155. res = 0;
  156. BID_RETURN (res);
  157. }
  158. // unpack x
  159. x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
  160. C1_hi = x.w[1] & MASK_COEFF;
  161. C1_lo = x.w[0];
  162. // test for zero
  163. if (C1_hi == 0 && C1_lo == 0) {
  164. res = 0;
  165. BID_RETURN (res);
  166. }
  167. // test for non-canonical values of the argument x
  168. if ((((C1_hi > 0x0001ed09bead87c0ull)
  169. || ((C1_hi == 0x0001ed09bead87c0ull)
  170. && (C1_lo > 0x378d8e63ffffffffull)))
  171. && ((x.w[1] & 0x6000000000000000ull) != 0x6000000000000000ull))
  172. || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
  173. res = 0;
  174. BID_RETURN (res);
  175. }
  176. // x is subnormal or normal
  177. // determine the number of digits q in the significand
  178. // q = nr. of decimal digits in x
  179. // determine first the nr. of bits in x
  180. if (C1_hi == 0) {
  181. if (C1_lo >= 0x0020000000000000ull) { // x >= 2^53
  182. // split the 64-bit value in two 32-bit halves to avoid rounding errors
  183. if (C1_lo >= 0x0000000100000000ull) { // x >= 2^32
  184. tmp1.d = (double) (C1_lo >> 32); // exact conversion
  185. x_nr_bits =
  186. 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  187. } else { // x < 2^32
  188. tmp1.d = (double) (C1_lo); // exact conversion
  189. x_nr_bits =
  190. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  191. }
  192. } else { // if x < 2^53
  193. tmp1.d = (double) C1_lo; // exact conversion
  194. x_nr_bits =
  195. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  196. }
  197. } else { // C1_hi != 0 => nr. bits = 64 + nr_bits (C1_hi)
  198. tmp1.d = (double) C1_hi; // exact conversion
  199. x_nr_bits =
  200. 65 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  201. }
  202. q = nr_digits[x_nr_bits - 1].digits;
  203. if (q == 0) {
  204. q = nr_digits[x_nr_bits - 1].digits1;
  205. if (C1_hi > nr_digits[x_nr_bits - 1].threshold_hi ||
  206. (C1_hi == nr_digits[x_nr_bits - 1].threshold_hi &&
  207. C1_lo >= nr_digits[x_nr_bits - 1].threshold_lo))
  208. q++;
  209. }
  210. exp = (int) (x_exp >> 49) - 6176;
  211. // test for subnormal values of x
  212. if (exp + q <= -6143) {
  213. res = 1;
  214. } else {
  215. res = 0;
  216. }
  217. BID_RETURN (res);
  218. }
  219. #if DECIMAL_CALL_BY_REFERENCE
  220. void
  221. bid128_isFinite (int *pres,
  222. UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  223. UINT128 x = *px;
  224. #else
  225. int
  226. bid128_isFinite (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  227. #endif
  228. int res;
  229. res = ((x.w[HIGH_128W] & MASK_INF) != MASK_INF);
  230. BID_RETURN (res);
  231. }
  232. #if DECIMAL_CALL_BY_REFERENCE
  233. void
  234. bid128_isZero (int *pres, UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  235. UINT128 x = *px;
  236. #else
  237. int
  238. bid128_isZero (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  239. #endif
  240. int res;
  241. UINT128 sig_x;
  242. BID_SWAP128 (x);
  243. if ((x.w[1] & MASK_INF) == MASK_INF) {
  244. res = 0;
  245. BID_RETURN (res);
  246. }
  247. sig_x.w[1] = x.w[1] & 0x0001ffffffffffffull;
  248. sig_x.w[0] = x.w[0];
  249. if ((sig_x.w[1] > 0x0001ed09bead87c0ull) || // significand is non-canonical
  250. ((sig_x.w[1] == 0x0001ed09bead87c0ull) && (sig_x.w[0] > 0x378d8e63ffffffffull)) || // significand is non-canonical
  251. ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull && (x.w[1] & MASK_INF) != MASK_INF) || // significand is non-canonical
  252. (sig_x.w[1] == 0 && sig_x.w[0] == 0)) { // significand is 0
  253. res = 1;
  254. BID_RETURN (res);
  255. }
  256. res = 0;
  257. BID_RETURN (res);
  258. }
  259. #if DECIMAL_CALL_BY_REFERENCE
  260. void
  261. bid128_isInf (int *pres, UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  262. UINT128 x = *px;
  263. #else
  264. int
  265. bid128_isInf (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  266. #endif
  267. int res;
  268. res = ((x.w[HIGH_128W] & MASK_INF) == MASK_INF)
  269. && ((x.w[HIGH_128W] & MASK_NAN) != MASK_NAN);
  270. BID_RETURN (res);
  271. }
  272. #if DECIMAL_CALL_BY_REFERENCE
  273. void
  274. bid128_isSignaling (int *pres,
  275. UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  276. UINT128 x = *px;
  277. #else
  278. int
  279. bid128_isSignaling (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  280. #endif
  281. int res;
  282. res = ((x.w[HIGH_128W] & MASK_SNAN) == MASK_SNAN);
  283. BID_RETURN (res);
  284. }
  285. // return 1 iff x is a canonical number ,infinity, or NaN.
  286. #if DECIMAL_CALL_BY_REFERENCE
  287. void
  288. bid128_isCanonical (int *pres,
  289. UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  290. UINT128 x = *px;
  291. #else
  292. int
  293. bid128_isCanonical (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  294. #endif
  295. int res;
  296. UINT128 sig_x;
  297. BID_SWAP128 (x);
  298. if ((x.w[1] & MASK_NAN) == MASK_NAN) { // NaN
  299. if (x.w[1] & 0x01ffc00000000000ull) {
  300. res = 0;
  301. BID_RETURN (res);
  302. }
  303. sig_x.w[1] = x.w[1] & 0x00003fffffffffffull; // 46 bits
  304. sig_x.w[0] = x.w[0]; // 64 bits
  305. // payload must be < 10^33 = 0x0000314dc6448d93_38c15b0a00000000
  306. if (sig_x.w[1] < 0x0000314dc6448d93ull
  307. || (sig_x.w[1] == 0x0000314dc6448d93ull
  308. && sig_x.w[0] < 0x38c15b0a00000000ull)) {
  309. res = 1;
  310. } else {
  311. res = 0;
  312. }
  313. BID_RETURN (res);
  314. } else if ((x.w[1] & MASK_INF) == MASK_INF) { // infinity
  315. if ((x.w[1] & 0x03ffffffffffffffull) || x.w[0]) {
  316. res = 0;
  317. } else {
  318. res = 1;
  319. }
  320. BID_RETURN (res);
  321. }
  322. // not NaN or infinity; extract significand to ensure it is canonical
  323. sig_x.w[1] = x.w[1] & 0x0001ffffffffffffull;
  324. sig_x.w[0] = x.w[0];
  325. // a canonical number has a coefficient < 10^34
  326. // (0x0001ed09_bead87c0_378d8e64_00000000)
  327. if ((sig_x.w[1] > 0x0001ed09bead87c0ull) || // significand is non-canonical
  328. ((sig_x.w[1] == 0x0001ed09bead87c0ull) && (sig_x.w[0] > 0x378d8e63ffffffffull)) || // significand is non-canonical
  329. ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
  330. res = 0;
  331. } else {
  332. res = 1;
  333. }
  334. BID_RETURN (res);
  335. }
  336. #if DECIMAL_CALL_BY_REFERENCE
  337. void
  338. bid128_isNaN (int *pres, UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  339. UINT128 x = *px;
  340. #else
  341. int
  342. bid128_isNaN (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  343. #endif
  344. int res;
  345. res = ((x.w[HIGH_128W] & MASK_NAN) == MASK_NAN);
  346. BID_RETURN (res);
  347. }
  348. // copies a floating-point operand x to destination y, with no change
  349. #if DECIMAL_CALL_BY_REFERENCE
  350. void
  351. bid128_copy (UINT128 * pres,
  352. UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  353. UINT128 x = *px;
  354. #else
  355. UINT128
  356. bid128_copy (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  357. #endif
  358. UINT128 res;
  359. res = x;
  360. BID_RETURN (res);
  361. }
  362. // copies a floating-point operand x to destination y, reversing the sign
  363. #if DECIMAL_CALL_BY_REFERENCE
  364. void
  365. bid128_negate (UINT128 * pres,
  366. UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  367. UINT128 x = *px;
  368. #else
  369. UINT128
  370. bid128_negate (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  371. #endif
  372. UINT128 res;
  373. x.w[HIGH_128W] ^= MASK_SIGN;
  374. res = x;
  375. BID_RETURN (res);
  376. }
  377. // copies a floating-point operand x to destination y, changing the sign to positive
  378. #if DECIMAL_CALL_BY_REFERENCE
  379. void
  380. bid128_abs (UINT128 * pres,
  381. UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  382. UINT128 x = *px;
  383. #else
  384. UINT128
  385. bid128_abs (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  386. #endif
  387. UINT128 res;
  388. x.w[HIGH_128W] &= ~MASK_SIGN;
  389. res = x;
  390. BID_RETURN (res);
  391. }
  392. // copies operand x to destination in the same format as x, but with the sign of y
  393. #if DECIMAL_CALL_BY_REFERENCE
  394. void
  395. bid128_copySign (UINT128 * pres, UINT128 * px,
  396. UINT128 * py _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  397. UINT128 x = *px;
  398. UINT128 y = *py;
  399. #else
  400. UINT128
  401. bid128_copySign (UINT128 x, UINT128 y _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  402. #endif
  403. UINT128 res;
  404. x.w[HIGH_128W] =
  405. (x.w[HIGH_128W] & ~MASK_SIGN) | (y.w[HIGH_128W] & MASK_SIGN);
  406. res = x;
  407. BID_RETURN (res);
  408. }
  409. #if DECIMAL_CALL_BY_REFERENCE
  410. void
  411. bid128_class (int *pres, UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  412. UINT128 x = *px;
  413. #else
  414. int
  415. bid128_class (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  416. #endif
  417. int res;
  418. UINT256 sig_x_prime256;
  419. UINT192 sig_x_prime192;
  420. UINT128 sig_x;
  421. int exp_x;
  422. BID_SWAP128 (x);
  423. if ((x.w[1] & MASK_NAN) == MASK_NAN) {
  424. if ((x.w[1] & MASK_SNAN) == MASK_SNAN) {
  425. res = signalingNaN;
  426. } else {
  427. res = quietNaN;
  428. }
  429. BID_RETURN (res);
  430. }
  431. if ((x.w[1] & MASK_INF) == MASK_INF) {
  432. if ((x.w[1] & MASK_SIGN) == MASK_SIGN) {
  433. res = negativeInfinity;
  434. } else {
  435. res = positiveInfinity;
  436. }
  437. BID_RETURN (res);
  438. }
  439. // decode number into exponent and significand
  440. sig_x.w[1] = x.w[1] & 0x0001ffffffffffffull;
  441. sig_x.w[0] = x.w[0];
  442. // check for zero or non-canonical
  443. if ((sig_x.w[1] > 0x0001ed09bead87c0ull)
  444. || ((sig_x.w[1] == 0x0001ed09bead87c0ull)
  445. && (sig_x.w[0] > 0x378d8e63ffffffffull))
  446. || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)
  447. || ((sig_x.w[1] == 0) && (sig_x.w[0] == 0))) {
  448. if ((x.w[1] & MASK_SIGN) == MASK_SIGN) {
  449. res = negativeZero;
  450. } else {
  451. res = positiveZero;
  452. }
  453. BID_RETURN (res);
  454. }
  455. exp_x = (x.w[1] >> 49) & 0x000000000003fffull;
  456. // if exponent is less than -6176, the number may be subnormal
  457. // (less than the smallest normal value)
  458. // the smallest normal value is 1 x 10^-6143 = 10^33 x 10^-6176
  459. // if (exp_x - 6176 < -6143)
  460. if (exp_x < 33) { // sig_x * 10^exp_x
  461. if (exp_x > 19) {
  462. __mul_128x128_to_256 (sig_x_prime256, sig_x,
  463. ten2k128[exp_x - 20]);
  464. // 10^33 = 0x0000314dc6448d93_38c15b0a00000000
  465. if ((sig_x_prime256.w[3] == 0) && (sig_x_prime256.w[2] == 0)
  466. && ((sig_x_prime256.w[1] < 0x0000314dc6448d93ull)
  467. || ((sig_x_prime256.w[1] == 0x0000314dc6448d93ull)
  468. && (sig_x_prime256.w[0] < 0x38c15b0a00000000ull)))) {
  469. res = ((x.w[1] & MASK_SIGN) == MASK_SIGN) ? negativeSubnormal :
  470. positiveSubnormal;
  471. BID_RETURN (res);
  472. }
  473. } else {
  474. __mul_64x128_to_192 (sig_x_prime192, ten2k64[exp_x], sig_x);
  475. // 10^33 = 0x0000314dc6448d93_38c15b0a00000000
  476. if ((sig_x_prime192.w[2] == 0)
  477. && ((sig_x_prime192.w[1] < 0x0000314dc6448d93ull)
  478. || ((sig_x_prime192.w[1] == 0x0000314dc6448d93ull)
  479. && (sig_x_prime192.w[0] < 0x38c15b0a00000000ull)))) {
  480. res = ((x.w[1] & MASK_SIGN) == MASK_SIGN) ? negativeSubnormal :
  481. positiveSubnormal;
  482. BID_RETURN (res);
  483. }
  484. }
  485. }
  486. // otherwise, normal number, determine the sign
  487. res =
  488. ((x.w[1] & MASK_SIGN) ==
  489. MASK_SIGN) ? negativeNormal : positiveNormal;
  490. BID_RETURN (res);
  491. }
  492. // true if the exponents of x and y are the same, false otherwise.
  493. // The special cases of sameQuantum(NaN, NaN) and sameQuantum(Inf, Inf) are true
  494. // If exactly one operand is infinite or exactly one operand is NaN, then false
  495. #if DECIMAL_CALL_BY_REFERENCE
  496. void
  497. bid128_sameQuantum (int *pres, UINT128 * px,
  498. UINT128 * py _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  499. UINT128 x = *px;
  500. UINT128 y = *py;
  501. #else
  502. int
  503. bid128_sameQuantum (UINT128 x,
  504. UINT128 y _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  505. #endif
  506. int res;
  507. UINT64 x_exp, y_exp;
  508. BID_SWAP128 (x);
  509. BID_SWAP128 (y);
  510. // if both operands are NaN, return true
  511. if ((x.w[1] & MASK_NAN) == MASK_NAN
  512. || ((y.w[1] & MASK_NAN) == MASK_NAN)) {
  513. res = ((x.w[1] & MASK_NAN) == MASK_NAN
  514. && (y.w[1] & MASK_NAN) == MASK_NAN);
  515. BID_RETURN (res);
  516. }
  517. // if both operands are INF, return true
  518. if ((x.w[1] & MASK_INF) == MASK_INF
  519. || (y.w[1] & MASK_INF) == MASK_INF) {
  520. res = ((x.w[1] & MASK_INF) == MASK_INF)
  521. && ((y.w[1] & MASK_INF) == MASK_INF);
  522. BID_RETURN (res);
  523. }
  524. // decode exponents for both numbers, and return true if they match
  525. if ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) { // G0_G1=11
  526. x_exp = (x.w[1] << 2) & MASK_EXP; // biased and shifted left 49 bits
  527. } else { // G0_G1 != 11
  528. x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bits
  529. }
  530. if ((y.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) { // G0_G1=11
  531. y_exp = (y.w[1] << 2) & MASK_EXP; // biased and shifted left 49 bits
  532. } else { // G0_G1 != 11
  533. y_exp = y.w[1] & MASK_EXP; // biased and shifted left 49 bits
  534. }
  535. res = (x_exp == y_exp);
  536. BID_RETURN (res);
  537. }
  538. #if DECIMAL_CALL_BY_REFERENCE
  539. void
  540. bid128_totalOrder (int *pres, UINT128 * px,
  541. UINT128 * py _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  542. UINT128 x = *px;
  543. UINT128 y = *py;
  544. #else
  545. int
  546. bid128_totalOrder (UINT128 x,
  547. UINT128 y _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  548. #endif
  549. int res;
  550. int exp_x, exp_y;
  551. UINT128 sig_x, sig_y, pyld_y, pyld_x;
  552. UINT192 sig_n_prime192;
  553. UINT256 sig_n_prime256;
  554. char x_is_zero = 0, y_is_zero = 0;
  555. BID_SWAP128 (x);
  556. BID_SWAP128 (y);
  557. // NaN (CASE 1)
  558. // if x and y are unordered numerically because either operand is NaN
  559. // (1) totalOrder(-NaN, number) is true
  560. // (2) totalOrder(number, +NaN) is true
  561. // (3) if x and y are both NaN:
  562. // i) negative sign bit < positive sign bit
  563. // ii) signaling < quiet for +NaN, reverse for -NaN
  564. // iii) lesser payload < greater payload for +NaN (reverse for -NaN)
  565. // iv) else if bitwise identical (in canonical form), return 1
  566. if ((x.w[1] & MASK_NAN) == MASK_NAN) {
  567. // if x is -NaN
  568. if ((x.w[1] & MASK_SIGN) == MASK_SIGN) {
  569. // return true, unless y is -NaN also
  570. if ((y.w[1] & MASK_NAN) != MASK_NAN
  571. || (y.w[1] & MASK_SIGN) != MASK_SIGN) {
  572. res = 1; // y is a number, return 1
  573. BID_RETURN (res);
  574. } else { // if y and x are both -NaN
  575. pyld_x.w[1] = x.w[1] & 0x00003fffffffffffull;
  576. pyld_x.w[0] = x.w[0];
  577. pyld_y.w[1] = y.w[1] & 0x00003fffffffffffull;
  578. pyld_y.w[0] = y.w[0];
  579. if ((pyld_x.w[1] > 0x0000314dc6448d93ull)
  580. || ((pyld_x.w[1] == 0x0000314dc6448d93ull)
  581. && (pyld_x.w[0] > 0x38c15b09ffffffffull))) {
  582. pyld_x.w[1] = 0;
  583. pyld_x.w[0] = 0;
  584. }
  585. if ((pyld_y.w[1] > 0x0000314dc6448d93ull)
  586. || ((pyld_y.w[1] == 0x0000314dc6448d93ull)
  587. && (pyld_y.w[0] > 0x38c15b09ffffffffull))) {
  588. pyld_y.w[1] = 0;
  589. pyld_y.w[0] = 0;
  590. }
  591. // if x and y are both -SNaN or both -QNaN, we have to compare payloads
  592. // this statement evaluates to true if both are SNaN or QNaN
  593. if (!
  594. (((y.w[1] & MASK_SNAN) == MASK_SNAN) ^
  595. ((x.w[1] & MASK_SNAN) == MASK_SNAN))) {
  596. // it comes down to the payload. we want to return true if x has a
  597. // larger payload, or if the payloads are equal (canonical forms
  598. // are bitwise identical)
  599. if ((pyld_x.w[1] > pyld_y.w[1]) ||
  600. ((pyld_x.w[1] == pyld_y.w[1])
  601. && (pyld_x.w[0] >= pyld_y.w[0])))
  602. res = 1;
  603. else
  604. res = 0;
  605. BID_RETURN (res);
  606. } else {
  607. // either x = -SNaN and y = -QNaN or x = -QNaN and y = -SNaN
  608. res = ((y.w[1] & MASK_SNAN) == MASK_SNAN);
  609. // totalOrder (-QNaN, -SNaN) == 1
  610. BID_RETURN (res);
  611. }
  612. }
  613. } else { // x is +NaN
  614. // return false, unless y is +NaN also
  615. if ((y.w[1] & MASK_NAN) != MASK_NAN
  616. || (y.w[1] & MASK_SIGN) == MASK_SIGN) {
  617. res = 0; // y is a number, return 1
  618. BID_RETURN (res);
  619. } else {
  620. // x and y are both +NaN;
  621. pyld_x.w[1] = x.w[1] & 0x00003fffffffffffull;
  622. pyld_x.w[0] = x.w[0];
  623. pyld_y.w[1] = y.w[1] & 0x00003fffffffffffull;
  624. pyld_y.w[0] = y.w[0];
  625. if ((pyld_x.w[1] > 0x0000314dc6448d93ull)
  626. || ((pyld_x.w[1] == 0x0000314dc6448d93ull)
  627. && (pyld_x.w[0] > 0x38c15b09ffffffffull))) {
  628. pyld_x.w[1] = 0;
  629. pyld_x.w[0] = 0;
  630. }
  631. if ((pyld_y.w[1] > 0x0000314dc6448d93ull)
  632. || ((pyld_y.w[1] == 0x0000314dc6448d93ull)
  633. && (pyld_y.w[0] > 0x38c15b09ffffffffull))) {
  634. pyld_y.w[1] = 0;
  635. pyld_y.w[0] = 0;
  636. }
  637. // if x and y are both +SNaN or both +QNaN, we have to compare payloads
  638. // this statement evaluates to true if both are SNaN or QNaN
  639. if (!
  640. (((y.w[1] & MASK_SNAN) == MASK_SNAN) ^
  641. ((x.w[1] & MASK_SNAN) == MASK_SNAN))) {
  642. // it comes down to the payload. we want to return true if x has a
  643. // smaller payload, or if the payloads are equal (canonical forms
  644. // are bitwise identical)
  645. if ((pyld_x.w[1] < pyld_y.w[1]) ||
  646. ((pyld_x.w[1] == pyld_y.w[1])
  647. && (pyld_x.w[0] <= pyld_y.w[0])))
  648. res = 1;
  649. else
  650. res = 0;
  651. BID_RETURN (res);
  652. } else {
  653. // either x = SNaN and y = QNaN or x = QNaN and y = SNaN
  654. res = ((x.w[1] & MASK_SNAN) == MASK_SNAN);
  655. // totalOrder (-QNaN, -SNaN) == 1
  656. BID_RETURN (res);
  657. }
  658. }
  659. }
  660. } else if ((y.w[1] & MASK_NAN) == MASK_NAN) {
  661. // x is certainly not NAN in this case.
  662. // return true if y is positive
  663. res = ((y.w[1] & MASK_SIGN) != MASK_SIGN);
  664. BID_RETURN (res);
  665. }
  666. // SIMPLE (CASE 2)
  667. // if all the bits are the same, the numbers are equal.
  668. if ((x.w[1] == y.w[1]) && (x.w[0] == y.w[0])) {
  669. res = 1;
  670. BID_RETURN (res);
  671. }
  672. // OPPOSITE SIGNS (CASE 3)
  673. // if signs are opposite, return 1 if x is negative
  674. // (if x < y, totalOrder is true)
  675. if (((x.w[1] & MASK_SIGN) == MASK_SIGN) ^ ((y.w[1] & MASK_SIGN) ==
  676. MASK_SIGN)) {
  677. res = ((x.w[1] & MASK_SIGN) == MASK_SIGN);
  678. BID_RETURN (res);
  679. }
  680. // INFINITY (CASE 4)
  681. if ((x.w[1] & MASK_INF) == MASK_INF) {
  682. // if x == neg_inf, return (y == neg_inf);
  683. if ((x.w[1] & MASK_SIGN) == MASK_SIGN) {
  684. res = 1;
  685. BID_RETURN (res);
  686. } else {
  687. // x is positive infinity, only return1 if y is positive infinity as well
  688. res = ((y.w[1] & MASK_INF) == MASK_INF);
  689. BID_RETURN (res);
  690. // && (y & MASK_SIGN) != MASK_SIGN); (we know y has same sign as x)
  691. }
  692. } else if ((y.w[1] & MASK_INF) == MASK_INF) {
  693. // x is finite, so:
  694. // if y is +inf, x<y
  695. // if y is -inf, x>y
  696. res = ((y.w[1] & MASK_SIGN) != MASK_SIGN);
  697. BID_RETURN (res);
  698. }
  699. // CONVERT x
  700. sig_x.w[1] = x.w[1] & 0x0001ffffffffffffull;
  701. sig_x.w[0] = x.w[0];
  702. exp_x = (x.w[1] >> 49) & 0x000000000003fffull;
  703. // CHECK IF x IS CANONICAL
  704. // 9999999999999999999999999999999999 (decimal) =
  705. // 1ed09_bead87c0_378d8e63_ffffffff(hexadecimal)
  706. // [0, 10^34) is the 754r supported canonical range.
  707. // If the value exceeds that, it is interpreted as 0.
  708. if ((((sig_x.w[1] > 0x0001ed09bead87c0ull) ||
  709. ((sig_x.w[1] == 0x0001ed09bead87c0ull) &&
  710. (sig_x.w[0] > 0x378d8e63ffffffffull))) &&
  711. ((x.w[1] & 0x6000000000000000ull) != 0x6000000000000000ull)) ||
  712. ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) ||
  713. ((sig_x.w[1] == 0) && (sig_x.w[0] == 0))) {
  714. x_is_zero = 1;
  715. // check for the case where the exponent is shifted right by 2 bits!
  716. if ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) {
  717. exp_x = (x.w[1] >> 47) & 0x000000000003fffull;
  718. }
  719. }
  720. // CONVERT y
  721. exp_y = (y.w[1] >> 49) & 0x0000000000003fffull;
  722. sig_y.w[1] = y.w[1] & 0x0001ffffffffffffull;
  723. sig_y.w[0] = y.w[0];
  724. // CHECK IF y IS CANONICAL
  725. // 9999999999999999999999999999999999(decimal) =
  726. // 1ed09_bead87c0_378d8e63_ffffffff(hexadecimal)
  727. // [0, 10^34) is the 754r supported canonical range.
  728. // If the value exceeds that, it is interpreted as 0.
  729. if ((((sig_y.w[1] > 0x0001ed09bead87c0ull) ||
  730. ((sig_y.w[1] == 0x0001ed09bead87c0ull) &&
  731. (sig_y.w[0] > 0x378d8e63ffffffffull))) &&
  732. ((y.w[1] & 0x6000000000000000ull) != 0x6000000000000000ull)) ||
  733. ((y.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) ||
  734. ((sig_y.w[1] == 0) && (sig_y.w[0] == 0))) {
  735. y_is_zero = 1;
  736. // check for the case where the exponent is shifted right by 2 bits!
  737. if ((y.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) {
  738. exp_y = (y.w[1] >> 47) & 0x000000000003fffull;
  739. }
  740. }
  741. // ZERO (CASE 5)
  742. // if x and y represent the same entities, and both are negative
  743. // return true iff exp_x <= exp_y
  744. if (x_is_zero && y_is_zero) {
  745. // we know that signs must be the same because we would have caught it
  746. // in case3 if signs were different
  747. // totalOrder(x,y) iff exp_x >= exp_y for negative numbers
  748. // totalOrder(x,y) iff exp_x <= exp_y for positive numbers
  749. if (exp_x == exp_y) {
  750. res = 1;
  751. BID_RETURN (res);
  752. }
  753. res = ((exp_x <= exp_y) ^ ((x.w[1] & MASK_SIGN) == MASK_SIGN));
  754. BID_RETURN (res);
  755. }
  756. // if x is zero and y isn't, clearly x has the smaller payload
  757. if (x_is_zero) {
  758. res = ((y.w[1] & MASK_SIGN) != MASK_SIGN);
  759. BID_RETURN (res);
  760. }
  761. // if y is zero, and x isn't, clearly y has the smaller payload
  762. if (y_is_zero) {
  763. res = ((x.w[1] & MASK_SIGN) == MASK_SIGN);
  764. BID_RETURN (res);
  765. }
  766. // REDUNDANT REPRESENTATIONS (CASE 6)
  767. // if both components are either bigger or smaller
  768. if (((sig_x.w[1] > sig_y.w[1])
  769. || (sig_x.w[1] == sig_y.w[1] && sig_x.w[0] > sig_y.w[0]))
  770. && exp_x >= exp_y) {
  771. res = ((x.w[1] & MASK_SIGN) == MASK_SIGN);
  772. BID_RETURN (res);
  773. }
  774. if (((sig_x.w[1] < sig_y.w[1])
  775. || (sig_x.w[1] == sig_y.w[1] && sig_x.w[0] < sig_y.w[0]))
  776. && exp_x <= exp_y) {
  777. res = ((x.w[1] & MASK_SIGN) != MASK_SIGN);
  778. BID_RETURN (res);
  779. }
  780. // if |exp_x - exp_y| < 33, it comes down to the compensated significand
  781. if (exp_x > exp_y) {
  782. // if exp_x is 33 greater than exp_y, it is definitely larger,
  783. // so no need for compensation
  784. if (exp_x - exp_y > 33) {
  785. res = ((x.w[1] & MASK_SIGN) == MASK_SIGN);
  786. BID_RETURN (res);
  787. // difference cannot be greater than 10^33
  788. }
  789. // otherwise adjust the x significand upwards
  790. if (exp_x - exp_y > 19) {
  791. __mul_128x128_to_256 (sig_n_prime256, sig_x,
  792. ten2k128[exp_x - exp_y - 20]);
  793. // the compensated significands are equal (ie "x and y represent the same
  794. // entities") return 1 if (negative && expx > expy) ||
  795. // (positive && expx < expy)
  796. if ((sig_n_prime256.w[3] == 0) && (sig_n_prime256.w[2] == 0)
  797. && (sig_n_prime256.w[1] == sig_y.w[1])
  798. && (sig_n_prime256.w[0] == sig_y.w[0])) {
  799. // the case exp_x == exp_y cannot occur, because all bits must be
  800. // the same - would have been caught if (x == y)
  801. res = ((exp_x <= exp_y) ^ ((x.w[1] & MASK_SIGN) == MASK_SIGN));
  802. BID_RETURN (res);
  803. }
  804. // if positive, return 1 if adjusted x is smaller than y
  805. res = (((sig_n_prime256.w[3] == 0) && (sig_n_prime256.w[2] == 0)
  806. && ((sig_n_prime256.w[1] < sig_y.w[1])
  807. || (sig_n_prime256.w[1] == sig_y.w[1]
  808. && sig_n_prime256.w[0] <
  809. sig_y.w[0]))) ^ ((x.w[1] & MASK_SIGN) ==
  810. MASK_SIGN));
  811. BID_RETURN (res);
  812. }
  813. __mul_64x128_to_192 (sig_n_prime192, ten2k64[exp_x - exp_y], sig_x);
  814. // if positive, return whichever significand is larger
  815. // (converse if negative)
  816. if ((sig_n_prime192.w[2] == 0) && sig_n_prime192.w[1] == sig_y.w[1]
  817. && (sig_n_prime192.w[0] == sig_y.w[0])) {
  818. res = ((exp_x <= exp_y) ^ ((x.w[1] & MASK_SIGN) == MASK_SIGN));
  819. BID_RETURN (res);
  820. }
  821. res = (((sig_n_prime192.w[2] == 0)
  822. && ((sig_n_prime192.w[1] < sig_y.w[1])
  823. || (sig_n_prime192.w[1] == sig_y.w[1]
  824. && sig_n_prime192.w[0] <
  825. sig_y.w[0]))) ^ ((x.w[1] & MASK_SIGN) ==
  826. MASK_SIGN));
  827. BID_RETURN (res);
  828. }
  829. // if exp_x is 33 less than exp_y, it is definitely smaller,
  830. // no need for compensation
  831. if (exp_y - exp_x > 33) {
  832. res = ((x.w[1] & MASK_SIGN) != MASK_SIGN);
  833. BID_RETURN (res);
  834. }
  835. if (exp_y - exp_x > 19) {
  836. // adjust the y significand upwards
  837. __mul_128x128_to_256 (sig_n_prime256, sig_y,
  838. ten2k128[exp_y - exp_x - 20]);
  839. // if x and y represent the same entities and both are negative
  840. // return true iff exp_x <= exp_y
  841. if ((sig_n_prime256.w[3] == 0) && (sig_n_prime256.w[2] == 0)
  842. && (sig_n_prime256.w[1] == sig_x.w[1])
  843. && (sig_n_prime256.w[0] == sig_x.w[0])) {
  844. res = (exp_x <= exp_y) ^ ((x.w[1] & MASK_SIGN) == MASK_SIGN);
  845. BID_RETURN (res);
  846. }
  847. // values are not equal, for positive numbers return 1 if x is less than y
  848. // and 0 otherwise
  849. res = (((sig_n_prime256.w[3] != 0) ||
  850. // if upper128 bits of compensated y are non-zero, y is bigger
  851. (sig_n_prime256.w[2] != 0) ||
  852. // if upper128 bits of compensated y are non-zero, y is bigger
  853. (sig_n_prime256.w[1] > sig_x.w[1]) ||
  854. // if compensated y is bigger, y is bigger
  855. (sig_n_prime256.w[1] == sig_x.w[1]
  856. && sig_n_prime256.w[0] >
  857. sig_x.w[0])) ^ ((x.w[1] & MASK_SIGN) == MASK_SIGN));
  858. BID_RETURN (res);
  859. }
  860. __mul_64x128_to_192 (sig_n_prime192, ten2k64[exp_y - exp_x], sig_y);
  861. if ((sig_n_prime192.w[2] == 0) && (sig_n_prime192.w[1] == sig_x.w[1])
  862. && (sig_n_prime192.w[0] == sig_x.w[0])) {
  863. res = (exp_x <= exp_y) ^ ((x.w[1] & MASK_SIGN) == MASK_SIGN);
  864. BID_RETURN (res);
  865. }
  866. res = (((sig_n_prime192.w[2] != 0) ||
  867. // if upper128 bits of compensated y are non-zero, y is bigger
  868. (sig_n_prime192.w[1] > sig_x.w[1]) ||
  869. // if compensated y is bigger, y is bigger
  870. (sig_n_prime192.w[1] == sig_x.w[1]
  871. && sig_n_prime192.w[0] >
  872. sig_x.w[0])) ^ ((x.w[1] & MASK_SIGN) == MASK_SIGN));
  873. BID_RETURN (res);
  874. }
  875. #if DECIMAL_CALL_BY_REFERENCE
  876. void
  877. bid128_totalOrderMag (int *pres, UINT128 * px,
  878. UINT128 * py _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  879. UINT128 x = *px;
  880. UINT128 y = *py;
  881. #else
  882. int
  883. bid128_totalOrderMag (UINT128 x,
  884. UINT128 y _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  885. #endif
  886. int res;
  887. int exp_x, exp_y;
  888. UINT128 sig_x, sig_y, pyld_y, pyld_x;
  889. UINT192 sig_n_prime192;
  890. UINT256 sig_n_prime256;
  891. char x_is_zero = 0, y_is_zero = 0;
  892. BID_SWAP128 (x);
  893. BID_SWAP128 (y);
  894. x.w[1] = x.w[1] & 0x7fffffffffffffffull;
  895. y.w[1] = y.w[1] & 0x7fffffffffffffffull;
  896. // NaN (CASE 1)
  897. // if x and y are unordered numerically because either operand is NaN
  898. // (1) totalOrder(number, +NaN) is true
  899. // (2) if x and y are both NaN:
  900. // i) signaling < quiet for +NaN
  901. // ii) lesser payload < greater payload for +NaN
  902. // iii) else if bitwise identical (in canonical form), return 1
  903. if ((x.w[1] & MASK_NAN) == MASK_NAN) {
  904. // x is +NaN
  905. // return false, unless y is +NaN also
  906. if ((y.w[1] & MASK_NAN) != MASK_NAN) {
  907. res = 0; // y is a number, return 0
  908. BID_RETURN (res);
  909. } else {
  910. // x and y are both +NaN;
  911. pyld_x.w[1] = x.w[1] & 0x00003fffffffffffull;
  912. pyld_x.w[0] = x.w[0];
  913. pyld_y.w[1] = y.w[1] & 0x00003fffffffffffull;
  914. pyld_y.w[0] = y.w[0];
  915. if ((pyld_x.w[1] > 0x0000314dc6448d93ull)
  916. || ((pyld_x.w[1] == 0x0000314dc6448d93ull)
  917. && (pyld_x.w[0] > 0x38c15b09ffffffffull))) {
  918. pyld_x.w[1] = 0;
  919. pyld_x.w[0] = 0;
  920. }
  921. if ((pyld_y.w[1] > 0x0000314dc6448d93ull)
  922. || ((pyld_y.w[1] == 0x0000314dc6448d93ull)
  923. && (pyld_y.w[0] > 0x38c15b09ffffffffull))) {
  924. pyld_y.w[1] = 0;
  925. pyld_y.w[0] = 0;
  926. }
  927. // if x and y are both +SNaN or both +QNaN, we have to compare payloads
  928. // this statement evaluates to true if both are SNaN or QNaN
  929. if (!
  930. (((y.w[1] & MASK_SNAN) == MASK_SNAN) ^
  931. ((x.w[1] & MASK_SNAN) == MASK_SNAN))) {
  932. // it comes down to the payload. we want to return true if x has a
  933. // smaller payload, or if the payloads are equal (canonical forms
  934. // are bitwise identical)
  935. if ((pyld_x.w[1] < pyld_y.w[1]) ||
  936. ((pyld_x.w[1] == pyld_y.w[1])
  937. && (pyld_x.w[0] <= pyld_y.w[0]))) {
  938. res = 1;
  939. } else {
  940. res = 0;
  941. }
  942. BID_RETURN (res);
  943. } else {
  944. // either x = SNaN and y = QNaN or x = QNaN and y = SNaN
  945. res = ((x.w[1] & MASK_SNAN) == MASK_SNAN);
  946. // totalOrder (-QNaN, -SNaN) == 1
  947. BID_RETURN (res);
  948. }
  949. }
  950. } else if ((y.w[1] & MASK_NAN) == MASK_NAN) {
  951. // x is certainly not NAN in this case.
  952. // return true because y is positive
  953. res = 1;
  954. BID_RETURN (res);
  955. }
  956. // SIMPLE (CASE 2)
  957. // if all the bits are the same, the numbers are equal.
  958. if ((x.w[1] == y.w[1]) && (x.w[0] == y.w[0])) {
  959. res = 1;
  960. BID_RETURN (res);
  961. }
  962. // INFINITY (CASE 3)
  963. if ((x.w[1] & MASK_INF) == MASK_INF) {
  964. // x is positive infinity, only return 1 if y is positive infinity as well
  965. res = ((y.w[1] & MASK_INF) == MASK_INF);
  966. BID_RETURN (res);
  967. // (we know y has same sign as x)
  968. } else if ((y.w[1] & MASK_INF) == MASK_INF) {
  969. // x is finite, so:
  970. // since y is +inf, x<y
  971. res = 1;
  972. BID_RETURN (res);
  973. } else {
  974. ; // continue
  975. }
  976. // CONVERT x
  977. sig_x.w[1] = x.w[1] & 0x0001ffffffffffffull;
  978. sig_x.w[0] = x.w[0];
  979. exp_x = (x.w[1] >> 49) & 0x000000000003fffull;
  980. // CHECK IF x IS CANONICAL
  981. // 9999999999999999999999999999999999 (decimal) =
  982. // 1ed09_bead87c0_378d8e63_ffffffff(hexadecimal)
  983. // [0, 10^34) is the 754r supported canonical range.
  984. // If the value exceeds that, it is interpreted as 0.
  985. if ((((sig_x.w[1] > 0x0001ed09bead87c0ull) ||
  986. ((sig_x.w[1] == 0x0001ed09bead87c0ull) &&
  987. (sig_x.w[0] > 0x378d8e63ffffffffull))) &&
  988. ((x.w[1] & 0x6000000000000000ull) != 0x6000000000000000ull)) ||
  989. ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) ||
  990. ((sig_x.w[1] == 0) && (sig_x.w[0] == 0))) {
  991. x_is_zero = 1;
  992. // check for the case where the exponent is shifted right by 2 bits!
  993. if ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) {
  994. exp_x = (x.w[1] >> 47) & 0x000000000003fffull;
  995. }
  996. }
  997. // CONVERT y
  998. exp_y = (y.w[1] >> 49) & 0x0000000000003fffull;
  999. sig_y.w[1] = y.w[1] & 0x0001ffffffffffffull;
  1000. sig_y.w[0] = y.w[0];
  1001. // CHECK IF y IS CANONICAL
  1002. // 9999999999999999999999999999999999(decimal) =
  1003. // 1ed09_bead87c0_378d8e63_ffffffff(hexadecimal)
  1004. // [0, 10^34) is the 754r supported canonical range.
  1005. // If the value exceeds that, it is interpreted as 0.
  1006. if ((((sig_y.w[1] > 0x0001ed09bead87c0ull) ||
  1007. ((sig_y.w[1] == 0x0001ed09bead87c0ull) &&
  1008. (sig_y.w[0] > 0x378d8e63ffffffffull))) &&
  1009. ((y.w[1] & 0x6000000000000000ull) != 0x6000000000000000ull)) ||
  1010. ((y.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) ||
  1011. ((sig_y.w[1] == 0) && (sig_y.w[0] == 0))) {
  1012. y_is_zero = 1;
  1013. // check for the case where the exponent is shifted right by 2 bits!
  1014. if ((y.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) {
  1015. exp_y = (y.w[1] >> 47) & 0x000000000003fffull;
  1016. }
  1017. }
  1018. // ZERO (CASE 4)
  1019. if (x_is_zero && y_is_zero) {
  1020. // we know that signs must be the same because we would have caught it
  1021. // in case3 if signs were different
  1022. // totalOrder(x,y) iff exp_x <= exp_y for positive numbers
  1023. if (exp_x == exp_y) {
  1024. res = 1;
  1025. BID_RETURN (res);
  1026. }
  1027. res = (exp_x <= exp_y);
  1028. BID_RETURN (res);
  1029. }
  1030. // if x is zero and y isn't, clearly x has the smaller payload
  1031. if (x_is_zero) {
  1032. res = 1;
  1033. BID_RETURN (res);
  1034. }
  1035. // if y is zero, and x isn't, clearly y has the smaller payload
  1036. if (y_is_zero) {
  1037. res = 0;
  1038. BID_RETURN (res);
  1039. }
  1040. // REDUNDANT REPRESENTATIONS (CASE 5)
  1041. // if both components are either bigger or smaller
  1042. if (((sig_x.w[1] > sig_y.w[1])
  1043. || (sig_x.w[1] == sig_y.w[1] && sig_x.w[0] > sig_y.w[0]))
  1044. && exp_x >= exp_y) {
  1045. res = 0;
  1046. BID_RETURN (res);
  1047. }
  1048. if (((sig_x.w[1] < sig_y.w[1])
  1049. || (sig_x.w[1] == sig_y.w[1] && sig_x.w[0] < sig_y.w[0]))
  1050. && exp_x <= exp_y) {
  1051. res = 1;
  1052. BID_RETURN (res);
  1053. }
  1054. // if |exp_x - exp_y| < 33, it comes down to the compensated significand
  1055. if (exp_x > exp_y) {
  1056. // if exp_x is 33 greater than exp_y, it is definitely larger,
  1057. // so no need for compensation
  1058. if (exp_x - exp_y > 33) {
  1059. res = 0; // difference cannot be greater than 10^33
  1060. BID_RETURN (res);
  1061. }
  1062. // otherwise adjust the x significand upwards
  1063. if (exp_x - exp_y > 19) {
  1064. __mul_128x128_to_256 (sig_n_prime256, sig_x,
  1065. ten2k128[exp_x - exp_y - 20]);
  1066. // the compensated significands are equal (ie "x and y represent the same
  1067. // entities") return 1 if (negative && expx > expy) ||
  1068. // (positive && expx < expy)
  1069. if ((sig_n_prime256.w[3] == 0) && (sig_n_prime256.w[2] == 0)
  1070. && (sig_n_prime256.w[1] == sig_y.w[1])
  1071. && (sig_n_prime256.w[0] == sig_y.w[0])) {
  1072. // the case (exp_x == exp_y) cannot occur, because all bits must be
  1073. // the same - would have been caught if (x == y)
  1074. res = (exp_x <= exp_y);
  1075. BID_RETURN (res);
  1076. }
  1077. // since positive, return 1 if adjusted x is smaller than y
  1078. res = ((sig_n_prime256.w[3] == 0) && (sig_n_prime256.w[2] == 0)
  1079. && ((sig_n_prime256.w[1] < sig_y.w[1])
  1080. || (sig_n_prime256.w[1] == sig_y.w[1]
  1081. && sig_n_prime256.w[0] < sig_y.w[0])));
  1082. BID_RETURN (res);
  1083. }
  1084. __mul_64x128_to_192 (sig_n_prime192, ten2k64[exp_x - exp_y], sig_x);
  1085. // if positive, return whichever significand is larger
  1086. // (converse if negative)
  1087. if ((sig_n_prime192.w[2] == 0) && sig_n_prime192.w[1] == sig_y.w[1]
  1088. && (sig_n_prime192.w[0] == sig_y.w[0])) {
  1089. res = (exp_x <= exp_y);
  1090. BID_RETURN (res);
  1091. }
  1092. res = ((sig_n_prime192.w[2] == 0)
  1093. && ((sig_n_prime192.w[1] < sig_y.w[1])
  1094. || (sig_n_prime192.w[1] == sig_y.w[1]
  1095. && sig_n_prime192.w[0] < sig_y.w[0])));
  1096. BID_RETURN (res);
  1097. }
  1098. // if exp_x is 33 less than exp_y, it is definitely smaller,
  1099. // no need for compensation
  1100. if (exp_y - exp_x > 33) {
  1101. res = 1;
  1102. BID_RETURN (res);
  1103. }
  1104. if (exp_y - exp_x > 19) {
  1105. // adjust the y significand upwards
  1106. __mul_128x128_to_256 (sig_n_prime256, sig_y,
  1107. ten2k128[exp_y - exp_x - 20]);
  1108. if ((sig_n_prime256.w[3] == 0) && (sig_n_prime256.w[2] == 0)
  1109. && (sig_n_prime256.w[1] == sig_x.w[1])
  1110. && (sig_n_prime256.w[0] == sig_x.w[0])) {
  1111. res = (exp_x <= exp_y);
  1112. BID_RETURN (res);
  1113. }
  1114. // values are not equal, for positive numbers return 1 if x is less than y
  1115. // and 0 otherwise
  1116. res = ((sig_n_prime256.w[3] != 0) ||
  1117. // if upper128 bits of compensated y are non-zero, y is bigger
  1118. (sig_n_prime256.w[2] != 0) ||
  1119. // if upper128 bits of compensated y are non-zero, y is bigger
  1120. (sig_n_prime256.w[1] > sig_x.w[1]) ||
  1121. // if compensated y is bigger, y is bigger
  1122. (sig_n_prime256.w[1] == sig_x.w[1]
  1123. && sig_n_prime256.w[0] > sig_x.w[0]));
  1124. BID_RETURN (res);
  1125. }
  1126. __mul_64x128_to_192 (sig_n_prime192, ten2k64[exp_y - exp_x], sig_y);
  1127. if ((sig_n_prime192.w[2] == 0) && (sig_n_prime192.w[1] == sig_x.w[1])
  1128. && (sig_n_prime192.w[0] == sig_x.w[0])) {
  1129. res = (exp_x <= exp_y);
  1130. BID_RETURN (res);
  1131. }
  1132. res = ((sig_n_prime192.w[2] != 0) ||
  1133. // if upper128 bits of compensated y are non-zero, y is bigger
  1134. (sig_n_prime192.w[1] > sig_x.w[1]) ||
  1135. // if compensated y is bigger, y is bigger
  1136. (sig_n_prime192.w[1] == sig_x.w[1]
  1137. && sig_n_prime192.w[0] > sig_x.w[0]));
  1138. BID_RETURN (res);
  1139. }
  1140. #if DECIMAL_CALL_BY_REFERENCE
  1141. void
  1142. bid128_radix (int *pres, UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  1143. UINT128 x = *px;
  1144. #else
  1145. int
  1146. bid128_radix (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  1147. #endif
  1148. int res;
  1149. if (x.w[LOW_128W]) // dummy test
  1150. res = 10;
  1151. else
  1152. res = 10;
  1153. BID_RETURN (res);
  1154. }