bid64_compare.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172
  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. static const UINT64 mult_factor[16] = {
  20. 1ull, 10ull, 100ull, 1000ull,
  21. 10000ull, 100000ull, 1000000ull, 10000000ull,
  22. 100000000ull, 1000000000ull, 10000000000ull, 100000000000ull,
  23. 1000000000000ull, 10000000000000ull,
  24. 100000000000000ull, 1000000000000000ull
  25. };
  26. #if DECIMAL_CALL_BY_REFERENCE
  27. void
  28. bid64_quiet_equal (int *pres, UINT64 * px,
  29. UINT64 *
  30. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  31. _EXC_INFO_PARAM) {
  32. UINT64 x = *px;
  33. UINT64 y = *py;
  34. #else
  35. int
  36. bid64_quiet_equal (UINT64 x,
  37. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  38. _EXC_INFO_PARAM) {
  39. #endif
  40. int res;
  41. int exp_x, exp_y, exp_t;
  42. UINT64 sig_x, sig_y, sig_t;
  43. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y, lcv;
  44. // NaN (CASE1)
  45. // if either number is NAN, the comparison is unordered,
  46. // rather than equal : return 0
  47. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  48. if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
  49. *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
  50. }
  51. res = 0;
  52. BID_RETURN (res);
  53. }
  54. // SIMPLE (CASE2)
  55. // if all the bits are the same, these numbers are equivalent.
  56. if (x == y) {
  57. res = 1;
  58. BID_RETURN (res);
  59. }
  60. // INFINITY (CASE3)
  61. if (((x & MASK_INF) == MASK_INF) && ((y & MASK_INF) == MASK_INF)) {
  62. res = (((x ^ y) & MASK_SIGN) != MASK_SIGN);
  63. BID_RETURN (res);
  64. }
  65. // ONE INFINITY (CASE3')
  66. if (((x & MASK_INF) == MASK_INF) || ((y & MASK_INF) == MASK_INF)) {
  67. res = 0;
  68. BID_RETURN (res);
  69. }
  70. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  71. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  72. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  73. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  74. if (sig_x > 9999999999999999ull) {
  75. non_canon_x = 1;
  76. } else {
  77. non_canon_x = 0;
  78. }
  79. } else {
  80. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  81. sig_x = (x & MASK_BINARY_SIG1);
  82. non_canon_x = 0;
  83. }
  84. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  85. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  86. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  87. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  88. if (sig_y > 9999999999999999ull) {
  89. non_canon_y = 1;
  90. } else {
  91. non_canon_y = 0;
  92. }
  93. } else {
  94. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  95. sig_y = (y & MASK_BINARY_SIG1);
  96. non_canon_y = 0;
  97. }
  98. // ZERO (CASE4)
  99. // some properties:
  100. // (+ZERO==-ZERO) => therefore ignore the sign
  101. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  102. // therefore ignore the exponent field
  103. // (Any non-canonical # is considered 0)
  104. if (non_canon_x || sig_x == 0) {
  105. x_is_zero = 1;
  106. }
  107. if (non_canon_y || sig_y == 0) {
  108. y_is_zero = 1;
  109. }
  110. if (x_is_zero && y_is_zero) {
  111. res = 1;
  112. BID_RETURN (res);
  113. } else if ((x_is_zero && !y_is_zero) || (!x_is_zero && y_is_zero)) {
  114. res = 0;
  115. BID_RETURN (res);
  116. }
  117. // OPPOSITE SIGN (CASE5)
  118. // now, if the sign bits differ => not equal : return 0
  119. if ((x ^ y) & MASK_SIGN) {
  120. res = 0;
  121. BID_RETURN (res);
  122. }
  123. // REDUNDANT REPRESENTATIONS (CASE6)
  124. if (exp_x > exp_y) { // to simplify the loop below,
  125. SWAP (exp_x, exp_y, exp_t); // put the larger exp in y,
  126. SWAP (sig_x, sig_y, sig_t); // and the smaller exp in x
  127. }
  128. if (exp_y - exp_x > 15) {
  129. res = 0; // difference cannot be greater than 10^15
  130. BID_RETURN (res);
  131. }
  132. for (lcv = 0; lcv < (exp_y - exp_x); lcv++) {
  133. // recalculate y's significand upwards
  134. sig_y = sig_y * 10;
  135. if (sig_y > 9999999999999999ull) {
  136. res = 0;
  137. BID_RETURN (res);
  138. }
  139. }
  140. res = (sig_y == sig_x);
  141. BID_RETURN (res);
  142. }
  143. #if DECIMAL_CALL_BY_REFERENCE
  144. void
  145. bid64_quiet_greater (int *pres, UINT64 * px,
  146. UINT64 *
  147. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  148. _EXC_INFO_PARAM) {
  149. UINT64 x = *px;
  150. UINT64 y = *py;
  151. #else
  152. int
  153. bid64_quiet_greater (UINT64 x,
  154. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  155. _EXC_INFO_PARAM) {
  156. #endif
  157. int res;
  158. int exp_x, exp_y;
  159. UINT64 sig_x, sig_y;
  160. UINT128 sig_n_prime;
  161. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  162. // NaN (CASE1)
  163. // if either number is NAN, the comparison is unordered, rather than equal :
  164. // return 0
  165. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  166. if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
  167. *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
  168. }
  169. res = 0;
  170. BID_RETURN (res);
  171. }
  172. // SIMPLE (CASE2)
  173. // if all the bits are the same, these numbers are equal (not Greater).
  174. if (x == y) {
  175. res = 0;
  176. BID_RETURN (res);
  177. }
  178. // INFINITY (CASE3)
  179. if ((x & MASK_INF) == MASK_INF) {
  180. // if x is neg infinity, there is no way it is greater than y, return 0
  181. if (((x & MASK_SIGN) == MASK_SIGN)) {
  182. res = 0;
  183. BID_RETURN (res);
  184. } else {
  185. // x is pos infinity, it is greater, unless y is positive
  186. // infinity => return y!=pos_infinity
  187. res = (((y & MASK_INF) != MASK_INF)
  188. || ((y & MASK_SIGN) == MASK_SIGN));
  189. BID_RETURN (res);
  190. }
  191. } else if ((y & MASK_INF) == MASK_INF) {
  192. // x is finite, so if y is positive infinity, then x is less, return 0
  193. // if y is negative infinity, then x is greater, return 1
  194. res = ((y & MASK_SIGN) == MASK_SIGN);
  195. BID_RETURN (res);
  196. }
  197. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  198. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  199. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  200. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  201. if (sig_x > 9999999999999999ull) {
  202. non_canon_x = 1;
  203. } else {
  204. non_canon_x = 0;
  205. }
  206. } else {
  207. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  208. sig_x = (x & MASK_BINARY_SIG1);
  209. non_canon_x = 0;
  210. }
  211. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  212. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  213. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  214. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  215. if (sig_y > 9999999999999999ull) {
  216. non_canon_y = 1;
  217. } else {
  218. non_canon_y = 0;
  219. }
  220. } else {
  221. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  222. sig_y = (y & MASK_BINARY_SIG1);
  223. non_canon_y = 0;
  224. }
  225. // ZERO (CASE4)
  226. // some properties:
  227. //(+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
  228. //(ZERO x 10^A == ZERO x 10^B) for any valid A, B => therefore ignore the
  229. // exponent field
  230. // (Any non-canonical # is considered 0)
  231. if (non_canon_x || sig_x == 0) {
  232. x_is_zero = 1;
  233. }
  234. if (non_canon_y || sig_y == 0) {
  235. y_is_zero = 1;
  236. }
  237. // if both numbers are zero, neither is greater => return NOTGREATERTHAN
  238. if (x_is_zero && y_is_zero) {
  239. res = 0;
  240. BID_RETURN (res);
  241. } else if (x_is_zero) {
  242. // is x is zero, it is greater if Y is negative
  243. res = ((y & MASK_SIGN) == MASK_SIGN);
  244. BID_RETURN (res);
  245. } else if (y_is_zero) {
  246. // is y is zero, X is greater if it is positive
  247. res = ((x & MASK_SIGN) != MASK_SIGN);
  248. BID_RETURN (res);
  249. }
  250. // OPPOSITE SIGN (CASE5)
  251. // now, if the sign bits differ, x is greater if y is negative
  252. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  253. res = ((y & MASK_SIGN) == MASK_SIGN);
  254. BID_RETURN (res);
  255. }
  256. // REDUNDANT REPRESENTATIONS (CASE6)
  257. // if both components are either bigger or smaller,
  258. // it is clear what needs to be done
  259. if (sig_x > sig_y && exp_x > exp_y) {
  260. res = ((x & MASK_SIGN) != MASK_SIGN);
  261. BID_RETURN (res);
  262. }
  263. if (sig_x < sig_y && exp_x < exp_y) {
  264. res = ((x & MASK_SIGN) == MASK_SIGN);
  265. BID_RETURN (res);
  266. }
  267. // if exp_x is 15 greater than exp_y, no need for compensation
  268. if (exp_x - exp_y > 15) { // difference cannot be greater than 10^15
  269. if (x & MASK_SIGN) // if both are negative
  270. res = 0;
  271. else // if both are positive
  272. res = 1;
  273. BID_RETURN (res);
  274. }
  275. // if exp_x is 15 less than exp_y, no need for compensation
  276. if (exp_y - exp_x > 15) {
  277. if (x & MASK_SIGN) // if both are negative
  278. res = 1;
  279. else // if both are positive
  280. res = 0;
  281. BID_RETURN (res);
  282. }
  283. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  284. if (exp_x > exp_y) { // to simplify the loop below,
  285. // otherwise adjust the x significand upwards
  286. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  287. mult_factor[exp_x - exp_y]);
  288. // if postitive, return whichever significand is larger (converse if neg.)
  289. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  290. res = 0;
  291. BID_RETURN (res);
  292. }
  293. res = (((sig_n_prime.w[1] > 0)
  294. || sig_n_prime.w[0] > sig_y) ^ ((x & MASK_SIGN) ==
  295. MASK_SIGN));
  296. BID_RETURN (res);
  297. }
  298. // adjust the y significand upwards
  299. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  300. mult_factor[exp_y - exp_x]);
  301. // if postitive, return whichever significand is larger
  302. // (converse if negative)
  303. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  304. res = 0;
  305. BID_RETURN (res);
  306. }
  307. res = (((sig_n_prime.w[1] == 0)
  308. && (sig_x > sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
  309. MASK_SIGN));
  310. BID_RETURN (res);
  311. }
  312. #if DECIMAL_CALL_BY_REFERENCE
  313. void
  314. bid64_quiet_greater_equal (int *pres, UINT64 * px,
  315. UINT64 *
  316. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  317. _EXC_INFO_PARAM) {
  318. UINT64 x = *px;
  319. UINT64 y = *py;
  320. #else
  321. int
  322. bid64_quiet_greater_equal (UINT64 x,
  323. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  324. _EXC_INFO_PARAM) {
  325. #endif
  326. int res;
  327. int exp_x, exp_y;
  328. UINT64 sig_x, sig_y;
  329. UINT128 sig_n_prime;
  330. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  331. // NaN (CASE1)
  332. // if either number is NAN, the comparison is unordered : return 1
  333. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  334. if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
  335. *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
  336. }
  337. res = 0;
  338. BID_RETURN (res);
  339. }
  340. // SIMPLE (CASE2)
  341. // if all the bits are the same, these numbers are equal.
  342. if (x == y) {
  343. res = 1;
  344. BID_RETURN (res);
  345. }
  346. // INFINITY (CASE3)
  347. if ((x & MASK_INF) == MASK_INF) {
  348. // if x==neg_inf, { res = (y == neg_inf)?1:0; BID_RETURN (res) }
  349. if ((x & MASK_SIGN) == MASK_SIGN) {
  350. // x is -inf, so it is less than y unless y is -inf
  351. res = (((y & MASK_INF) == MASK_INF)
  352. && (y & MASK_SIGN) == MASK_SIGN);
  353. BID_RETURN (res);
  354. } else { // x is pos_inf, no way for it to be less than y
  355. res = 1;
  356. BID_RETURN (res);
  357. }
  358. } else if ((y & MASK_INF) == MASK_INF) {
  359. // x is finite, so:
  360. // if y is +inf, x<y
  361. // if y is -inf, x>y
  362. res = ((y & MASK_SIGN) == MASK_SIGN);
  363. BID_RETURN (res);
  364. }
  365. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  366. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  367. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  368. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  369. if (sig_x > 9999999999999999ull) {
  370. non_canon_x = 1;
  371. } else {
  372. non_canon_x = 0;
  373. }
  374. } else {
  375. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  376. sig_x = (x & MASK_BINARY_SIG1);
  377. non_canon_x = 0;
  378. }
  379. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  380. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  381. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  382. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  383. if (sig_y > 9999999999999999ull) {
  384. non_canon_y = 1;
  385. } else {
  386. non_canon_y = 0;
  387. }
  388. } else {
  389. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  390. sig_y = (y & MASK_BINARY_SIG1);
  391. non_canon_y = 0;
  392. }
  393. // ZERO (CASE4)
  394. // some properties:
  395. // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
  396. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  397. // therefore ignore the exponent field
  398. // (Any non-canonical # is considered 0)
  399. if (non_canon_x || sig_x == 0) {
  400. x_is_zero = 1;
  401. }
  402. if (non_canon_y || sig_y == 0) {
  403. y_is_zero = 1;
  404. }
  405. if (x_is_zero && y_is_zero) {
  406. // if both numbers are zero, they are equal
  407. res = 1;
  408. BID_RETURN (res);
  409. } else if (x_is_zero) {
  410. // if x is zero, it is lessthan if Y is positive
  411. res = ((y & MASK_SIGN) == MASK_SIGN);
  412. BID_RETURN (res);
  413. } else if (y_is_zero) {
  414. // if y is zero, X is less if it is negative
  415. res = ((x & MASK_SIGN) != MASK_SIGN);
  416. BID_RETURN (res);
  417. }
  418. // OPPOSITE SIGN (CASE5)
  419. // now, if the sign bits differ, x is less than if y is positive
  420. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  421. res = ((y & MASK_SIGN) == MASK_SIGN);
  422. BID_RETURN (res);
  423. }
  424. // REDUNDANT REPRESENTATIONS (CASE6)
  425. // if both components are either bigger or smaller
  426. if (sig_x > sig_y && exp_x >= exp_y) {
  427. res = ((x & MASK_SIGN) != MASK_SIGN);
  428. BID_RETURN (res);
  429. }
  430. if (sig_x < sig_y && exp_x <= exp_y) {
  431. res = ((x & MASK_SIGN) == MASK_SIGN);
  432. BID_RETURN (res);
  433. }
  434. // if exp_x is 15 greater than exp_y, no need for compensation
  435. if (exp_x - exp_y > 15) {
  436. res = ((x & MASK_SIGN) != MASK_SIGN);
  437. // difference cannot be greater than 10^15
  438. BID_RETURN (res);
  439. }
  440. // if exp_x is 15 less than exp_y, no need for compensation
  441. if (exp_y - exp_x > 15) {
  442. res = ((x & MASK_SIGN) == MASK_SIGN);
  443. BID_RETURN (res);
  444. }
  445. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  446. if (exp_x > exp_y) { // to simplify the loop below,
  447. // otherwise adjust the x significand upwards
  448. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  449. mult_factor[exp_x - exp_y]);
  450. // return 1 if values are equal
  451. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  452. res = 1;
  453. BID_RETURN (res);
  454. }
  455. // if postitive, return whichever significand abs is smaller
  456. // (converse if negative)
  457. res = (((sig_n_prime.w[1] == 0)
  458. && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) !=
  459. MASK_SIGN));
  460. BID_RETURN (res);
  461. }
  462. // adjust the y significand upwards
  463. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  464. mult_factor[exp_y - exp_x]);
  465. // return 0 if values are equal
  466. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  467. res = 1;
  468. BID_RETURN (res);
  469. }
  470. // if positive, return whichever significand abs is smaller
  471. // (converse if negative)
  472. res = (((sig_n_prime.w[1] > 0)
  473. || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) !=
  474. MASK_SIGN));
  475. BID_RETURN (res);
  476. }
  477. #if DECIMAL_CALL_BY_REFERENCE
  478. void
  479. bid64_quiet_greater_unordered (int *pres, UINT64 * px,
  480. UINT64 *
  481. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  482. _EXC_INFO_PARAM) {
  483. UINT64 x = *px;
  484. UINT64 y = *py;
  485. #else
  486. int
  487. bid64_quiet_greater_unordered (UINT64 x,
  488. UINT64 y _EXC_FLAGS_PARAM
  489. _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  490. #endif
  491. int res;
  492. int exp_x, exp_y;
  493. UINT64 sig_x, sig_y;
  494. UINT128 sig_n_prime;
  495. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  496. // NaN (CASE1)
  497. // if either number is NAN, the comparison is unordered, rather than equal :
  498. // return 0
  499. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  500. if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
  501. *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
  502. }
  503. res = 1;
  504. BID_RETURN (res);
  505. }
  506. // SIMPLE (CASE2)
  507. // if all the bits are the same, these numbers are equal (not Greater).
  508. if (x == y) {
  509. res = 0;
  510. BID_RETURN (res);
  511. }
  512. // INFINITY (CASE3)
  513. if ((x & MASK_INF) == MASK_INF) {
  514. // if x is neg infinity, there is no way it is greater than y, return 0
  515. if (((x & MASK_SIGN) == MASK_SIGN)) {
  516. res = 0;
  517. BID_RETURN (res);
  518. } else {
  519. // x is pos infinity, it is greater, unless y is positive infinity =>
  520. // return y!=pos_infinity
  521. res = (((y & MASK_INF) != MASK_INF)
  522. || ((y & MASK_SIGN) == MASK_SIGN));
  523. BID_RETURN (res);
  524. }
  525. } else if ((y & MASK_INF) == MASK_INF) {
  526. // x is finite, so if y is positive infinity, then x is less, return 0
  527. // if y is negative infinity, then x is greater, return 1
  528. res = ((y & MASK_SIGN) == MASK_SIGN);
  529. BID_RETURN (res);
  530. }
  531. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  532. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  533. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  534. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  535. if (sig_x > 9999999999999999ull) {
  536. non_canon_x = 1;
  537. } else {
  538. non_canon_x = 0;
  539. }
  540. } else {
  541. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  542. sig_x = (x & MASK_BINARY_SIG1);
  543. non_canon_x = 0;
  544. }
  545. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  546. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  547. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  548. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  549. if (sig_y > 9999999999999999ull) {
  550. non_canon_y = 1;
  551. } else {
  552. non_canon_y = 0;
  553. }
  554. } else {
  555. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  556. sig_y = (y & MASK_BINARY_SIG1);
  557. non_canon_y = 0;
  558. }
  559. // ZERO (CASE4)
  560. // some properties:
  561. // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
  562. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  563. // therefore ignore the exponent field
  564. // (Any non-canonical # is considered 0)
  565. if (non_canon_x || sig_x == 0) {
  566. x_is_zero = 1;
  567. }
  568. if (non_canon_y || sig_y == 0) {
  569. y_is_zero = 1;
  570. }
  571. // if both numbers are zero, neither is greater => return NOTGREATERTHAN
  572. if (x_is_zero && y_is_zero) {
  573. res = 0;
  574. BID_RETURN (res);
  575. } else if (x_is_zero) {
  576. // is x is zero, it is greater if Y is negative
  577. res = ((y & MASK_SIGN) == MASK_SIGN);
  578. BID_RETURN (res);
  579. } else if (y_is_zero) {
  580. // is y is zero, X is greater if it is positive
  581. res = ((x & MASK_SIGN) != MASK_SIGN);
  582. BID_RETURN (res);
  583. }
  584. // OPPOSITE SIGN (CASE5)
  585. // now, if the sign bits differ, x is greater if y is negative
  586. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  587. res = ((y & MASK_SIGN) == MASK_SIGN);
  588. BID_RETURN (res);
  589. }
  590. // REDUNDANT REPRESENTATIONS (CASE6)
  591. // if both components are either bigger or smaller
  592. if (sig_x > sig_y && exp_x >= exp_y) {
  593. res = ((x & MASK_SIGN) != MASK_SIGN);
  594. BID_RETURN (res);
  595. }
  596. if (sig_x < sig_y && exp_x <= exp_y) {
  597. res = ((x & MASK_SIGN) == MASK_SIGN);
  598. BID_RETURN (res);
  599. }
  600. // if exp_x is 15 greater than exp_y, no need for compensation
  601. if (exp_x - exp_y > 15) {
  602. // difference cannot be greater than 10^15
  603. res = ((x & MASK_SIGN) != MASK_SIGN);
  604. BID_RETURN (res);
  605. }
  606. // if exp_x is 15 less than exp_y, no need for compensation
  607. if (exp_y - exp_x > 15) {
  608. res = ((x & MASK_SIGN) == MASK_SIGN);
  609. BID_RETURN (res);
  610. }
  611. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  612. if (exp_x > exp_y) { // to simplify the loop below,
  613. // otherwise adjust the x significand upwards
  614. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  615. mult_factor[exp_x - exp_y]);
  616. // if postitive, return whichever significand is larger
  617. // (converse if negative)
  618. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  619. res = 0;
  620. BID_RETURN (res);
  621. }
  622. res = (((sig_n_prime.w[1] > 0)
  623. || sig_n_prime.w[0] > sig_y) ^ ((x & MASK_SIGN) ==
  624. MASK_SIGN));
  625. BID_RETURN (res);
  626. }
  627. // adjust the y significand upwards
  628. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  629. mult_factor[exp_y - exp_x]);
  630. // if postitive, return whichever significand is larger (converse if negative)
  631. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  632. res = 0;
  633. BID_RETURN (res);
  634. }
  635. res = (((sig_n_prime.w[1] == 0)
  636. && (sig_x > sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
  637. MASK_SIGN));
  638. BID_RETURN (res);
  639. }
  640. #if DECIMAL_CALL_BY_REFERENCE
  641. void
  642. bid64_quiet_less (int *pres, UINT64 * px,
  643. UINT64 *
  644. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM _EXC_INFO_PARAM)
  645. {
  646. UINT64 x = *px;
  647. UINT64 y = *py;
  648. #else
  649. int
  650. bid64_quiet_less (UINT64 x,
  651. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  652. _EXC_INFO_PARAM) {
  653. #endif
  654. int res;
  655. int exp_x, exp_y;
  656. UINT64 sig_x, sig_y;
  657. UINT128 sig_n_prime;
  658. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  659. // NaN (CASE1)
  660. // if either number is NAN, the comparison is unordered : return 0
  661. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  662. if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
  663. *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
  664. }
  665. res = 0;
  666. BID_RETURN (res);
  667. }
  668. // SIMPLE (CASE2)
  669. // if all the bits are the same, these numbers are equal.
  670. if (x == y) {
  671. res = 0;
  672. BID_RETURN (res);
  673. }
  674. // INFINITY (CASE3)
  675. if ((x & MASK_INF) == MASK_INF) {
  676. // if x==neg_inf, { res = (y == neg_inf)?0:1; BID_RETURN (res) }
  677. if ((x & MASK_SIGN) == MASK_SIGN) {
  678. // x is -inf, so it is less than y unless y is -inf
  679. res = (((y & MASK_INF) != MASK_INF)
  680. || (y & MASK_SIGN) != MASK_SIGN);
  681. BID_RETURN (res);
  682. } else {
  683. // x is pos_inf, no way for it to be less than y
  684. res = 0;
  685. BID_RETURN (res);
  686. }
  687. } else if ((y & MASK_INF) == MASK_INF) {
  688. // x is finite, so:
  689. // if y is +inf, x<y
  690. // if y is -inf, x>y
  691. res = ((y & MASK_SIGN) != MASK_SIGN);
  692. BID_RETURN (res);
  693. }
  694. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  695. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  696. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  697. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  698. if (sig_x > 9999999999999999ull) {
  699. non_canon_x = 1;
  700. } else {
  701. non_canon_x = 0;
  702. }
  703. } else {
  704. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  705. sig_x = (x & MASK_BINARY_SIG1);
  706. non_canon_x = 0;
  707. }
  708. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  709. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  710. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  711. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  712. if (sig_y > 9999999999999999ull) {
  713. non_canon_y = 1;
  714. } else {
  715. non_canon_y = 0;
  716. }
  717. } else {
  718. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  719. sig_y = (y & MASK_BINARY_SIG1);
  720. non_canon_y = 0;
  721. }
  722. // ZERO (CASE4)
  723. // some properties:
  724. // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
  725. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  726. // therefore ignore the exponent field
  727. // (Any non-canonical # is considered 0)
  728. if (non_canon_x || sig_x == 0) {
  729. x_is_zero = 1;
  730. }
  731. if (non_canon_y || sig_y == 0) {
  732. y_is_zero = 1;
  733. }
  734. if (x_is_zero && y_is_zero) {
  735. // if both numbers are zero, they are equal
  736. res = 0;
  737. BID_RETURN (res);
  738. } else if (x_is_zero) {
  739. // if x is zero, it is lessthan if Y is positive
  740. res = ((y & MASK_SIGN) != MASK_SIGN);
  741. BID_RETURN (res);
  742. } else if (y_is_zero) {
  743. // if y is zero, X is less if it is negative
  744. res = ((x & MASK_SIGN) == MASK_SIGN);
  745. BID_RETURN (res);
  746. }
  747. // OPPOSITE SIGN (CASE5)
  748. // now, if the sign bits differ, x is less than if y is positive
  749. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  750. res = ((y & MASK_SIGN) != MASK_SIGN);
  751. BID_RETURN (res);
  752. }
  753. // REDUNDANT REPRESENTATIONS (CASE6)
  754. // if both components are either bigger or smaller,
  755. // it is clear what needs to be done
  756. if (sig_x > sig_y && exp_x >= exp_y) {
  757. res = ((x & MASK_SIGN) == MASK_SIGN);
  758. BID_RETURN (res);
  759. }
  760. if (sig_x < sig_y && exp_x <= exp_y) {
  761. res = ((x & MASK_SIGN) != MASK_SIGN);
  762. BID_RETURN (res);
  763. }
  764. // if exp_x is 15 greater than exp_y, no need for compensation
  765. if (exp_x - exp_y > 15) {
  766. res = ((x & MASK_SIGN) == MASK_SIGN);
  767. // difference cannot be greater than 10^15
  768. BID_RETURN (res);
  769. }
  770. // if exp_x is 15 less than exp_y, no need for compensation
  771. if (exp_y - exp_x > 15) {
  772. res = ((x & MASK_SIGN) != MASK_SIGN);
  773. BID_RETURN (res);
  774. }
  775. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  776. if (exp_x > exp_y) { // to simplify the loop below,
  777. // otherwise adjust the x significand upwards
  778. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  779. mult_factor[exp_x - exp_y]);
  780. // return 0 if values are equal
  781. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  782. res = 0;
  783. BID_RETURN (res);
  784. }
  785. // if postitive, return whichever significand abs is smaller
  786. // (converse if negative)
  787. res = (((sig_n_prime.w[1] == 0)
  788. && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
  789. MASK_SIGN));
  790. BID_RETURN (res);
  791. }
  792. // adjust the y significand upwards
  793. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  794. mult_factor[exp_y - exp_x]);
  795. // return 0 if values are equal
  796. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  797. res = 0;
  798. BID_RETURN (res);
  799. }
  800. // if positive, return whichever significand abs is smaller
  801. // (converse if negative)
  802. res = (((sig_n_prime.w[1] > 0)
  803. || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
  804. MASK_SIGN));
  805. BID_RETURN (res);
  806. }
  807. #if DECIMAL_CALL_BY_REFERENCE
  808. void
  809. bid64_quiet_less_equal (int *pres, UINT64 * px,
  810. UINT64 *
  811. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  812. _EXC_INFO_PARAM) {
  813. UINT64 x = *px;
  814. UINT64 y = *py;
  815. #else
  816. int
  817. bid64_quiet_less_equal (UINT64 x,
  818. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  819. _EXC_INFO_PARAM) {
  820. #endif
  821. int res;
  822. int exp_x, exp_y;
  823. UINT64 sig_x, sig_y;
  824. UINT128 sig_n_prime;
  825. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  826. // NaN (CASE1)
  827. // if either number is NAN, the comparison is unordered, rather than equal :
  828. // return 0
  829. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  830. if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
  831. *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
  832. }
  833. res = 0;
  834. BID_RETURN (res);
  835. }
  836. // SIMPLE (CASE2)
  837. // if all the bits are the same, these numbers are equal (LESSEQUAL).
  838. if (x == y) {
  839. res = 1;
  840. BID_RETURN (res);
  841. }
  842. // INFINITY (CASE3)
  843. if ((x & MASK_INF) == MASK_INF) {
  844. if (((x & MASK_SIGN) == MASK_SIGN)) {
  845. // if x is neg infinity, it must be lessthan or equal to y return 1
  846. res = 1;
  847. BID_RETURN (res);
  848. } else {
  849. // x is pos infinity, it is greater, unless y is positive infinity =>
  850. // return y==pos_infinity
  851. res = !(((y & MASK_INF) != MASK_INF)
  852. || ((y & MASK_SIGN) == MASK_SIGN));
  853. BID_RETURN (res);
  854. }
  855. } else if ((y & MASK_INF) == MASK_INF) {
  856. // x is finite, so if y is positive infinity, then x is less, return 1
  857. // if y is negative infinity, then x is greater, return 0
  858. res = ((y & MASK_SIGN) != MASK_SIGN);
  859. BID_RETURN (res);
  860. }
  861. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  862. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  863. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  864. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  865. if (sig_x > 9999999999999999ull) {
  866. non_canon_x = 1;
  867. } else {
  868. non_canon_x = 0;
  869. }
  870. } else {
  871. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  872. sig_x = (x & MASK_BINARY_SIG1);
  873. non_canon_x = 0;
  874. }
  875. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  876. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  877. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  878. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  879. if (sig_y > 9999999999999999ull) {
  880. non_canon_y = 1;
  881. } else {
  882. non_canon_y = 0;
  883. }
  884. } else {
  885. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  886. sig_y = (y & MASK_BINARY_SIG1);
  887. non_canon_y = 0;
  888. }
  889. // ZERO (CASE4)
  890. // some properties:
  891. // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
  892. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  893. // therefore ignore the exponent field
  894. // (Any non-canonical # is considered 0)
  895. if (non_canon_x || sig_x == 0) {
  896. x_is_zero = 1;
  897. }
  898. if (non_canon_y || sig_y == 0) {
  899. y_is_zero = 1;
  900. }
  901. if (x_is_zero && y_is_zero) {
  902. // if both numbers are zero, they are equal -> return 1
  903. res = 1;
  904. BID_RETURN (res);
  905. } else if (x_is_zero) {
  906. // if x is zero, it is lessthan if Y is positive
  907. res = ((y & MASK_SIGN) != MASK_SIGN);
  908. BID_RETURN (res);
  909. } else if (y_is_zero) {
  910. // if y is zero, X is less if it is negative
  911. res = ((x & MASK_SIGN) == MASK_SIGN);
  912. BID_RETURN (res);
  913. }
  914. // OPPOSITE SIGN (CASE5)
  915. // now, if the sign bits differ, x is less than if y is positive
  916. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  917. res = ((y & MASK_SIGN) != MASK_SIGN);
  918. BID_RETURN (res);
  919. }
  920. // REDUNDANT REPRESENTATIONS (CASE6)
  921. // if both components are either bigger or smaller
  922. if (sig_x > sig_y && exp_x >= exp_y) {
  923. res = ((x & MASK_SIGN) == MASK_SIGN);
  924. BID_RETURN (res);
  925. }
  926. if (sig_x < sig_y && exp_x <= exp_y) {
  927. res = ((x & MASK_SIGN) != MASK_SIGN);
  928. BID_RETURN (res);
  929. }
  930. // if exp_x is 15 greater than exp_y, no need for compensation
  931. if (exp_x - exp_y > 15) {
  932. res = ((x & MASK_SIGN) == MASK_SIGN);
  933. // difference cannot be greater than 10^15
  934. BID_RETURN (res);
  935. }
  936. // if exp_x is 15 less than exp_y, no need for compensation
  937. if (exp_y - exp_x > 15) {
  938. res = ((x & MASK_SIGN) != MASK_SIGN);
  939. BID_RETURN (res);
  940. }
  941. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  942. if (exp_x > exp_y) { // to simplify the loop below,
  943. // otherwise adjust the x significand upwards
  944. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  945. mult_factor[exp_x - exp_y]);
  946. // return 1 if values are equal
  947. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  948. res = 1;
  949. BID_RETURN (res);
  950. }
  951. // if postitive, return whichever significand abs is smaller
  952. // (converse if negative)
  953. res = (((sig_n_prime.w[1] == 0)
  954. && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
  955. MASK_SIGN));
  956. BID_RETURN (res);
  957. }
  958. // adjust the y significand upwards
  959. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  960. mult_factor[exp_y - exp_x]);
  961. // return 1 if values are equal
  962. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  963. res = 1;
  964. BID_RETURN (res);
  965. }
  966. // if positive, return whichever significand abs is smaller
  967. // (converse if negative)
  968. res = (((sig_n_prime.w[1] > 0)
  969. || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
  970. MASK_SIGN));
  971. BID_RETURN (res);
  972. }
  973. #if DECIMAL_CALL_BY_REFERENCE
  974. void
  975. bid64_quiet_less_unordered (int *pres, UINT64 * px,
  976. UINT64 *
  977. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  978. _EXC_INFO_PARAM) {
  979. UINT64 x = *px;
  980. UINT64 y = *py;
  981. #else
  982. int
  983. bid64_quiet_less_unordered (UINT64 x,
  984. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  985. _EXC_INFO_PARAM) {
  986. #endif
  987. int res;
  988. int exp_x, exp_y;
  989. UINT64 sig_x, sig_y;
  990. UINT128 sig_n_prime;
  991. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  992. // NaN (CASE1)
  993. // if either number is NAN, the comparison is unordered : return 0
  994. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  995. if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
  996. *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
  997. }
  998. res = 1;
  999. BID_RETURN (res);
  1000. }
  1001. // SIMPLE (CASE2)
  1002. // if all the bits are the same, these numbers are equal.
  1003. if (x == y) {
  1004. res = 0;
  1005. BID_RETURN (res);
  1006. }
  1007. // INFINITY (CASE3)
  1008. if ((x & MASK_INF) == MASK_INF) {
  1009. // if x==neg_inf, { res = (y == neg_inf)?0:1; BID_RETURN (res) }
  1010. if ((x & MASK_SIGN) == MASK_SIGN) {
  1011. // x is -inf, so it is less than y unless y is -inf
  1012. res = (((y & MASK_INF) != MASK_INF)
  1013. || (y & MASK_SIGN) != MASK_SIGN);
  1014. BID_RETURN (res);
  1015. } else {
  1016. // x is pos_inf, no way for it to be less than y
  1017. res = 0;
  1018. BID_RETURN (res);
  1019. }
  1020. } else if ((y & MASK_INF) == MASK_INF) {
  1021. // x is finite, so:
  1022. // if y is +inf, x<y
  1023. // if y is -inf, x>y
  1024. res = ((y & MASK_SIGN) != MASK_SIGN);
  1025. BID_RETURN (res);
  1026. }
  1027. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1028. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1029. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  1030. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1031. if (sig_x > 9999999999999999ull) {
  1032. non_canon_x = 1;
  1033. } else {
  1034. non_canon_x = 0;
  1035. }
  1036. } else {
  1037. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  1038. sig_x = (x & MASK_BINARY_SIG1);
  1039. non_canon_x = 0;
  1040. }
  1041. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1042. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1043. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  1044. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1045. if (sig_y > 9999999999999999ull) {
  1046. non_canon_y = 1;
  1047. } else {
  1048. non_canon_y = 0;
  1049. }
  1050. } else {
  1051. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  1052. sig_y = (y & MASK_BINARY_SIG1);
  1053. non_canon_y = 0;
  1054. }
  1055. // ZERO (CASE4)
  1056. // some properties:
  1057. // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
  1058. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  1059. // therefore ignore the exponent field
  1060. // (Any non-canonical # is considered 0)
  1061. if (non_canon_x || sig_x == 0) {
  1062. x_is_zero = 1;
  1063. }
  1064. if (non_canon_y || sig_y == 0) {
  1065. y_is_zero = 1;
  1066. }
  1067. if (x_is_zero && y_is_zero) {
  1068. // if both numbers are zero, they are equal
  1069. res = 0;
  1070. BID_RETURN (res);
  1071. } else if (x_is_zero) {
  1072. // if x is zero, it is lessthan if Y is positive
  1073. res = ((y & MASK_SIGN) != MASK_SIGN);
  1074. BID_RETURN (res);
  1075. } else if (y_is_zero) {
  1076. // if y is zero, X is less if it is negative
  1077. res = ((x & MASK_SIGN) == MASK_SIGN);
  1078. BID_RETURN (res);
  1079. }
  1080. // OPPOSITE SIGN (CASE5)
  1081. // now, if the sign bits differ, x is less than if y is positive
  1082. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  1083. res = ((y & MASK_SIGN) != MASK_SIGN);
  1084. BID_RETURN (res);
  1085. }
  1086. // REDUNDANT REPRESENTATIONS (CASE6)
  1087. // if both components are either bigger or smaller
  1088. if (sig_x > sig_y && exp_x >= exp_y) {
  1089. res = ((x & MASK_SIGN) == MASK_SIGN);
  1090. BID_RETURN (res);
  1091. }
  1092. if (sig_x < sig_y && exp_x <= exp_y) {
  1093. res = ((x & MASK_SIGN) != MASK_SIGN);
  1094. BID_RETURN (res);
  1095. }
  1096. // if exp_x is 15 greater than exp_y, no need for compensation
  1097. if (exp_x - exp_y > 15) {
  1098. res = ((x & MASK_SIGN) == MASK_SIGN);
  1099. // difference cannot be greater than 10^15
  1100. BID_RETURN (res);
  1101. }
  1102. // if exp_x is 15 less than exp_y, no need for compensation
  1103. if (exp_y - exp_x > 15) {
  1104. res = ((x & MASK_SIGN) != MASK_SIGN);
  1105. BID_RETURN (res);
  1106. }
  1107. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  1108. if (exp_x > exp_y) { // to simplify the loop below,
  1109. // otherwise adjust the x significand upwards
  1110. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  1111. mult_factor[exp_x - exp_y]);
  1112. // return 0 if values are equal
  1113. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  1114. res = 0;
  1115. BID_RETURN (res);
  1116. }
  1117. // if postitive, return whichever significand abs is smaller
  1118. // (converse if negative)
  1119. res = (((sig_n_prime.w[1] == 0)
  1120. && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
  1121. MASK_SIGN));
  1122. BID_RETURN (res);
  1123. }
  1124. // adjust the y significand upwards
  1125. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  1126. mult_factor[exp_y - exp_x]);
  1127. // return 0 if values are equal
  1128. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  1129. res = 0;
  1130. BID_RETURN (res);
  1131. }
  1132. // if positive, return whichever significand abs is smaller
  1133. // (converse if negative)
  1134. res = (((sig_n_prime.w[1] > 0)
  1135. || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
  1136. MASK_SIGN));
  1137. BID_RETURN (res);
  1138. }
  1139. #if DECIMAL_CALL_BY_REFERENCE
  1140. void
  1141. bid64_quiet_not_equal (int *pres, UINT64 * px,
  1142. UINT64 *
  1143. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1144. _EXC_INFO_PARAM) {
  1145. UINT64 x = *px;
  1146. UINT64 y = *py;
  1147. #else
  1148. int
  1149. bid64_quiet_not_equal (UINT64 x,
  1150. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1151. _EXC_INFO_PARAM) {
  1152. #endif
  1153. int res;
  1154. int exp_x, exp_y, exp_t;
  1155. UINT64 sig_x, sig_y, sig_t;
  1156. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y, lcv;
  1157. // NaN (CASE1)
  1158. // if either number is NAN, the comparison is unordered,
  1159. // rather than equal : return 1
  1160. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  1161. if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
  1162. *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
  1163. }
  1164. res = 1;
  1165. BID_RETURN (res);
  1166. }
  1167. // SIMPLE (CASE2)
  1168. // if all the bits are the same, these numbers are equivalent.
  1169. if (x == y) {
  1170. res = 0;
  1171. BID_RETURN (res);
  1172. }
  1173. // INFINITY (CASE3)
  1174. if (((x & MASK_INF) == MASK_INF) && ((y & MASK_INF) == MASK_INF)) {
  1175. res = (((x ^ y) & MASK_SIGN) == MASK_SIGN);
  1176. BID_RETURN (res);
  1177. }
  1178. // ONE INFINITY (CASE3')
  1179. if (((x & MASK_INF) == MASK_INF) || ((y & MASK_INF) == MASK_INF)) {
  1180. res = 1;
  1181. BID_RETURN (res);
  1182. }
  1183. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1184. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1185. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  1186. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1187. if (sig_x > 9999999999999999ull) {
  1188. non_canon_x = 1;
  1189. } else {
  1190. non_canon_x = 0;
  1191. }
  1192. } else {
  1193. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  1194. sig_x = (x & MASK_BINARY_SIG1);
  1195. non_canon_x = 0;
  1196. }
  1197. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1198. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1199. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  1200. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1201. if (sig_y > 9999999999999999ull) {
  1202. non_canon_y = 1;
  1203. } else {
  1204. non_canon_y = 0;
  1205. }
  1206. } else {
  1207. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  1208. sig_y = (y & MASK_BINARY_SIG1);
  1209. non_canon_y = 0;
  1210. }
  1211. // ZERO (CASE4)
  1212. // some properties:
  1213. // (+ZERO==-ZERO) => therefore ignore the sign
  1214. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  1215. // therefore ignore the exponent field
  1216. // (Any non-canonical # is considered 0)
  1217. if (non_canon_x || sig_x == 0) {
  1218. x_is_zero = 1;
  1219. }
  1220. if (non_canon_y || sig_y == 0) {
  1221. y_is_zero = 1;
  1222. }
  1223. if (x_is_zero && y_is_zero) {
  1224. res = 0;
  1225. BID_RETURN (res);
  1226. } else if ((x_is_zero && !y_is_zero) || (!x_is_zero && y_is_zero)) {
  1227. res = 1;
  1228. BID_RETURN (res);
  1229. }
  1230. // OPPOSITE SIGN (CASE5)
  1231. // now, if the sign bits differ => not equal : return 1
  1232. if ((x ^ y) & MASK_SIGN) {
  1233. res = 1;
  1234. BID_RETURN (res);
  1235. }
  1236. // REDUNDANT REPRESENTATIONS (CASE6)
  1237. if (exp_x > exp_y) { // to simplify the loop below,
  1238. SWAP (exp_x, exp_y, exp_t); // put the larger exp in y,
  1239. SWAP (sig_x, sig_y, sig_t); // and the smaller exp in x
  1240. }
  1241. if (exp_y - exp_x > 15) {
  1242. res = 1;
  1243. BID_RETURN (res);
  1244. }
  1245. // difference cannot be greater than 10^16
  1246. for (lcv = 0; lcv < (exp_y - exp_x); lcv++) {
  1247. // recalculate y's significand upwards
  1248. sig_y = sig_y * 10;
  1249. if (sig_y > 9999999999999999ull) {
  1250. res = 1;
  1251. BID_RETURN (res);
  1252. }
  1253. }
  1254. {
  1255. res = sig_y != sig_x;
  1256. BID_RETURN (res);
  1257. }
  1258. }
  1259. #if DECIMAL_CALL_BY_REFERENCE
  1260. void
  1261. bid64_quiet_not_greater (int *pres, UINT64 * px,
  1262. UINT64 *
  1263. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1264. _EXC_INFO_PARAM) {
  1265. UINT64 x = *px;
  1266. UINT64 y = *py;
  1267. #else
  1268. int
  1269. bid64_quiet_not_greater (UINT64 x,
  1270. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1271. _EXC_INFO_PARAM) {
  1272. #endif
  1273. int res;
  1274. int exp_x, exp_y;
  1275. UINT64 sig_x, sig_y;
  1276. UINT128 sig_n_prime;
  1277. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  1278. // NaN (CASE1)
  1279. // if either number is NAN, the comparison is unordered,
  1280. // rather than equal : return 0
  1281. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  1282. if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
  1283. *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
  1284. }
  1285. res = 1;
  1286. BID_RETURN (res);
  1287. }
  1288. // SIMPLE (CASE2)
  1289. // if all the bits are the same, these numbers are equal (LESSEQUAL).
  1290. if (x == y) {
  1291. res = 1;
  1292. BID_RETURN (res);
  1293. }
  1294. // INFINITY (CASE3)
  1295. if ((x & MASK_INF) == MASK_INF) {
  1296. // if x is neg infinity, it must be lessthan or equal to y return 1
  1297. if (((x & MASK_SIGN) == MASK_SIGN)) {
  1298. res = 1;
  1299. BID_RETURN (res);
  1300. }
  1301. // x is pos infinity, it is greater, unless y is positive
  1302. // infinity => return y==pos_infinity
  1303. else {
  1304. res = !(((y & MASK_INF) != MASK_INF)
  1305. || ((y & MASK_SIGN) == MASK_SIGN));
  1306. BID_RETURN (res);
  1307. }
  1308. } else if ((y & MASK_INF) == MASK_INF) {
  1309. // x is finite, so if y is positive infinity, then x is less, return 1
  1310. // if y is negative infinity, then x is greater, return 0
  1311. {
  1312. res = ((y & MASK_SIGN) != MASK_SIGN);
  1313. BID_RETURN (res);
  1314. }
  1315. }
  1316. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1317. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1318. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  1319. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1320. if (sig_x > 9999999999999999ull) {
  1321. non_canon_x = 1;
  1322. } else {
  1323. non_canon_x = 0;
  1324. }
  1325. } else {
  1326. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  1327. sig_x = (x & MASK_BINARY_SIG1);
  1328. non_canon_x = 0;
  1329. }
  1330. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1331. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1332. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  1333. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1334. if (sig_y > 9999999999999999ull) {
  1335. non_canon_y = 1;
  1336. } else {
  1337. non_canon_y = 0;
  1338. }
  1339. } else {
  1340. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  1341. sig_y = (y & MASK_BINARY_SIG1);
  1342. non_canon_y = 0;
  1343. }
  1344. // ZERO (CASE4)
  1345. // some properties:
  1346. // (+ZERO==-ZERO) => therefore ignore the sign, and neither
  1347. // number is greater
  1348. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  1349. // therefore ignore the exponent field
  1350. // (Any non-canonical # is considered 0)
  1351. if (non_canon_x || sig_x == 0) {
  1352. x_is_zero = 1;
  1353. }
  1354. if (non_canon_y || sig_y == 0) {
  1355. y_is_zero = 1;
  1356. }
  1357. // if both numbers are zero, they are equal -> return 1
  1358. if (x_is_zero && y_is_zero) {
  1359. res = 1;
  1360. BID_RETURN (res);
  1361. }
  1362. // if x is zero, it is lessthan if Y is positive
  1363. else if (x_is_zero) {
  1364. res = ((y & MASK_SIGN) != MASK_SIGN);
  1365. BID_RETURN (res);
  1366. }
  1367. // if y is zero, X is less if it is negative
  1368. else if (y_is_zero) {
  1369. res = ((x & MASK_SIGN) == MASK_SIGN);
  1370. BID_RETURN (res);
  1371. }
  1372. // OPPOSITE SIGN (CASE5)
  1373. // now, if the sign bits differ, x is less than if y is positive
  1374. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  1375. res = ((y & MASK_SIGN) != MASK_SIGN);
  1376. BID_RETURN (res);
  1377. }
  1378. // REDUNDANT REPRESENTATIONS (CASE6)
  1379. // if both components are either bigger or smaller
  1380. if (sig_x > sig_y && exp_x >= exp_y) {
  1381. res = ((x & MASK_SIGN) == MASK_SIGN);
  1382. BID_RETURN (res);
  1383. }
  1384. if (sig_x < sig_y && exp_x <= exp_y) {
  1385. res = ((x & MASK_SIGN) != MASK_SIGN);
  1386. BID_RETURN (res);
  1387. }
  1388. // if exp_x is 15 greater than exp_y, no need for compensation
  1389. if (exp_x - exp_y > 15) {
  1390. res = ((x & MASK_SIGN) == MASK_SIGN);
  1391. BID_RETURN (res);
  1392. }
  1393. // difference cannot be greater than 10^15
  1394. // if exp_x is 15 less than exp_y, no need for compensation
  1395. if (exp_y - exp_x > 15) {
  1396. res = ((x & MASK_SIGN) != MASK_SIGN);
  1397. BID_RETURN (res);
  1398. }
  1399. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  1400. if (exp_x > exp_y) { // to simplify the loop below,
  1401. // otherwise adjust the x significand upwards
  1402. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  1403. mult_factor[exp_x - exp_y]);
  1404. // return 1 if values are equal
  1405. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  1406. res = 1;
  1407. BID_RETURN (res);
  1408. }
  1409. // if postitive, return whichever significand abs is smaller
  1410. // (converse if negative)
  1411. {
  1412. res = (((sig_n_prime.w[1] == 0)
  1413. && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
  1414. MASK_SIGN));
  1415. BID_RETURN (res);
  1416. }
  1417. }
  1418. // adjust the y significand upwards
  1419. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  1420. mult_factor[exp_y - exp_x]);
  1421. // return 1 if values are equal
  1422. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  1423. res = 1;
  1424. BID_RETURN (res);
  1425. }
  1426. // if positive, return whichever significand abs is smaller
  1427. // (converse if negative)
  1428. {
  1429. res = (((sig_n_prime.w[1] > 0)
  1430. || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
  1431. MASK_SIGN));
  1432. BID_RETURN (res);
  1433. }
  1434. }
  1435. #if DECIMAL_CALL_BY_REFERENCE
  1436. void
  1437. bid64_quiet_not_less (int *pres, UINT64 * px,
  1438. UINT64 *
  1439. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1440. _EXC_INFO_PARAM) {
  1441. UINT64 x = *px;
  1442. UINT64 y = *py;
  1443. #else
  1444. int
  1445. bid64_quiet_not_less (UINT64 x,
  1446. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1447. _EXC_INFO_PARAM) {
  1448. #endif
  1449. int res;
  1450. int exp_x, exp_y;
  1451. UINT64 sig_x, sig_y;
  1452. UINT128 sig_n_prime;
  1453. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  1454. // NaN (CASE1)
  1455. // if either number is NAN, the comparison is unordered : return 1
  1456. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  1457. if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
  1458. *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
  1459. }
  1460. res = 1;
  1461. BID_RETURN (res);
  1462. }
  1463. // SIMPLE (CASE2)
  1464. // if all the bits are the same, these numbers are equal.
  1465. if (x == y) {
  1466. res = 1;
  1467. BID_RETURN (res);
  1468. }
  1469. // INFINITY (CASE3)
  1470. if ((x & MASK_INF) == MASK_INF) {
  1471. // if x==neg_inf, { res = (y == neg_inf)?1:0; BID_RETURN (res) }
  1472. if ((x & MASK_SIGN) == MASK_SIGN)
  1473. // x is -inf, so it is less than y unless y is -inf
  1474. {
  1475. res = (((y & MASK_INF) == MASK_INF)
  1476. && (y & MASK_SIGN) == MASK_SIGN);
  1477. BID_RETURN (res);
  1478. } else
  1479. // x is pos_inf, no way for it to be less than y
  1480. {
  1481. res = 1;
  1482. BID_RETURN (res);
  1483. }
  1484. } else if ((y & MASK_INF) == MASK_INF) {
  1485. // x is finite, so:
  1486. // if y is +inf, x<y
  1487. // if y is -inf, x>y
  1488. {
  1489. res = ((y & MASK_SIGN) == MASK_SIGN);
  1490. BID_RETURN (res);
  1491. }
  1492. }
  1493. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1494. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1495. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  1496. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1497. if (sig_x > 9999999999999999ull) {
  1498. non_canon_x = 1;
  1499. } else {
  1500. non_canon_x = 0;
  1501. }
  1502. } else {
  1503. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  1504. sig_x = (x & MASK_BINARY_SIG1);
  1505. non_canon_x = 0;
  1506. }
  1507. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1508. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1509. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  1510. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1511. if (sig_y > 9999999999999999ull) {
  1512. non_canon_y = 1;
  1513. } else {
  1514. non_canon_y = 0;
  1515. }
  1516. } else {
  1517. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  1518. sig_y = (y & MASK_BINARY_SIG1);
  1519. non_canon_y = 0;
  1520. }
  1521. // ZERO (CASE4)
  1522. // some properties:
  1523. // (+ZERO==-ZERO) => therefore ignore the sign, and neither
  1524. // number is greater
  1525. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  1526. // therefore ignore the exponent field
  1527. // (Any non-canonical # is considered 0)
  1528. if (non_canon_x || sig_x == 0) {
  1529. x_is_zero = 1;
  1530. }
  1531. if (non_canon_y || sig_y == 0) {
  1532. y_is_zero = 1;
  1533. }
  1534. // if both numbers are zero, they are equal
  1535. if (x_is_zero && y_is_zero) {
  1536. res = 1;
  1537. BID_RETURN (res);
  1538. }
  1539. // if x is zero, it is lessthan if Y is positive
  1540. else if (x_is_zero) {
  1541. res = ((y & MASK_SIGN) == MASK_SIGN);
  1542. BID_RETURN (res);
  1543. }
  1544. // if y is zero, X is less if it is negative
  1545. else if (y_is_zero) {
  1546. res = ((x & MASK_SIGN) != MASK_SIGN);
  1547. BID_RETURN (res);
  1548. }
  1549. // OPPOSITE SIGN (CASE5)
  1550. // now, if the sign bits differ, x is less than if y is positive
  1551. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  1552. res = ((y & MASK_SIGN) == MASK_SIGN);
  1553. BID_RETURN (res);
  1554. }
  1555. // REDUNDANT REPRESENTATIONS (CASE6)
  1556. // if both components are either bigger or smaller
  1557. if (sig_x > sig_y && exp_x >= exp_y) {
  1558. res = ((x & MASK_SIGN) != MASK_SIGN);
  1559. BID_RETURN (res);
  1560. }
  1561. if (sig_x < sig_y && exp_x <= exp_y) {
  1562. res = ((x & MASK_SIGN) == MASK_SIGN);
  1563. BID_RETURN (res);
  1564. }
  1565. // if exp_x is 15 greater than exp_y, no need for compensation
  1566. if (exp_x - exp_y > 15) {
  1567. res = ((x & MASK_SIGN) != MASK_SIGN);
  1568. BID_RETURN (res);
  1569. }
  1570. // difference cannot be greater than 10^15
  1571. // if exp_x is 15 less than exp_y, no need for compensation
  1572. if (exp_y - exp_x > 15) {
  1573. res = ((x & MASK_SIGN) == MASK_SIGN);
  1574. BID_RETURN (res);
  1575. }
  1576. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  1577. if (exp_x > exp_y) { // to simplify the loop below,
  1578. // otherwise adjust the x significand upwards
  1579. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  1580. mult_factor[exp_x - exp_y]);
  1581. // return 0 if values are equal
  1582. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  1583. res = 1;
  1584. BID_RETURN (res);
  1585. }
  1586. // if postitive, return whichever significand abs is smaller
  1587. // (converse if negative)
  1588. {
  1589. res = (((sig_n_prime.w[1] == 0)
  1590. && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) !=
  1591. MASK_SIGN));
  1592. BID_RETURN (res);
  1593. }
  1594. }
  1595. // adjust the y significand upwards
  1596. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  1597. mult_factor[exp_y - exp_x]);
  1598. // return 0 if values are equal
  1599. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  1600. res = 1;
  1601. BID_RETURN (res);
  1602. }
  1603. // if positive, return whichever significand abs is smaller
  1604. // (converse if negative)
  1605. {
  1606. res = (((sig_n_prime.w[1] > 0)
  1607. || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) !=
  1608. MASK_SIGN));
  1609. BID_RETURN (res);
  1610. }
  1611. }
  1612. #if DECIMAL_CALL_BY_REFERENCE
  1613. void
  1614. bid64_quiet_ordered (int *pres, UINT64 * px,
  1615. UINT64 *
  1616. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1617. _EXC_INFO_PARAM) {
  1618. UINT64 x = *px;
  1619. UINT64 y = *py;
  1620. #else
  1621. int
  1622. bid64_quiet_ordered (UINT64 x,
  1623. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1624. _EXC_INFO_PARAM) {
  1625. #endif
  1626. int res;
  1627. // NaN (CASE1)
  1628. // if either number is NAN, the comparison is ordered, rather than equal : return 0
  1629. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  1630. if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
  1631. *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
  1632. }
  1633. res = 0;
  1634. BID_RETURN (res);
  1635. } else {
  1636. res = 1;
  1637. BID_RETURN (res);
  1638. }
  1639. }
  1640. #if DECIMAL_CALL_BY_REFERENCE
  1641. void
  1642. bid64_quiet_unordered (int *pres, UINT64 * px,
  1643. UINT64 *
  1644. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1645. _EXC_INFO_PARAM) {
  1646. UINT64 x = *px;
  1647. UINT64 y = *py;
  1648. #else
  1649. int
  1650. bid64_quiet_unordered (UINT64 x,
  1651. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1652. _EXC_INFO_PARAM) {
  1653. #endif
  1654. int res;
  1655. // NaN (CASE1)
  1656. // if either number is NAN, the comparison is unordered,
  1657. // rather than equal : return 0
  1658. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  1659. if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
  1660. *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
  1661. }
  1662. res = 1;
  1663. BID_RETURN (res);
  1664. } else {
  1665. res = 0;
  1666. BID_RETURN (res);
  1667. }
  1668. }
  1669. #if DECIMAL_CALL_BY_REFERENCE
  1670. void
  1671. bid64_signaling_greater (int *pres, UINT64 * px,
  1672. UINT64 *
  1673. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1674. _EXC_INFO_PARAM) {
  1675. UINT64 x = *px;
  1676. UINT64 y = *py;
  1677. #else
  1678. int
  1679. bid64_signaling_greater (UINT64 x,
  1680. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1681. _EXC_INFO_PARAM) {
  1682. #endif
  1683. int res;
  1684. int exp_x, exp_y;
  1685. UINT64 sig_x, sig_y;
  1686. UINT128 sig_n_prime;
  1687. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  1688. // NaN (CASE1)
  1689. // if either number is NAN, the comparison is unordered,
  1690. // rather than equal : return 0
  1691. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  1692. *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
  1693. res = 0;
  1694. BID_RETURN (res);
  1695. }
  1696. // SIMPLE (CASE2)
  1697. // if all the bits are the same, these numbers are equal (not Greater).
  1698. if (x == y) {
  1699. res = 0;
  1700. BID_RETURN (res);
  1701. }
  1702. // INFINITY (CASE3)
  1703. if ((x & MASK_INF) == MASK_INF) {
  1704. // if x is neg infinity, there is no way it is greater than y, return 0
  1705. if (((x & MASK_SIGN) == MASK_SIGN)) {
  1706. res = 0;
  1707. BID_RETURN (res);
  1708. }
  1709. // x is pos infinity, it is greater,
  1710. // unless y is positive infinity => return y!=pos_infinity
  1711. else {
  1712. res = (((y & MASK_INF) != MASK_INF)
  1713. || ((y & MASK_SIGN) == MASK_SIGN));
  1714. BID_RETURN (res);
  1715. }
  1716. } else if ((y & MASK_INF) == MASK_INF) {
  1717. // x is finite, so if y is positive infinity, then x is less, return 0
  1718. // if y is negative infinity, then x is greater, return 1
  1719. {
  1720. res = ((y & MASK_SIGN) == MASK_SIGN);
  1721. BID_RETURN (res);
  1722. }
  1723. }
  1724. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1725. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1726. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  1727. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1728. if (sig_x > 9999999999999999ull) {
  1729. non_canon_x = 1;
  1730. } else {
  1731. non_canon_x = 0;
  1732. }
  1733. } else {
  1734. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  1735. sig_x = (x & MASK_BINARY_SIG1);
  1736. non_canon_x = 0;
  1737. }
  1738. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1739. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1740. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  1741. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1742. if (sig_y > 9999999999999999ull) {
  1743. non_canon_y = 1;
  1744. } else {
  1745. non_canon_y = 0;
  1746. }
  1747. } else {
  1748. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  1749. sig_y = (y & MASK_BINARY_SIG1);
  1750. non_canon_y = 0;
  1751. }
  1752. // ZERO (CASE4)
  1753. // some properties:
  1754. // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
  1755. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  1756. // therefore ignore the exponent field
  1757. // (Any non-canonical # is considered 0)
  1758. if (non_canon_x || sig_x == 0) {
  1759. x_is_zero = 1;
  1760. }
  1761. if (non_canon_y || sig_y == 0) {
  1762. y_is_zero = 1;
  1763. }
  1764. // if both numbers are zero, neither is greater => return NOTGREATERTHAN
  1765. if (x_is_zero && y_is_zero) {
  1766. res = 0;
  1767. BID_RETURN (res);
  1768. }
  1769. // is x is zero, it is greater if Y is negative
  1770. else if (x_is_zero) {
  1771. res = ((y & MASK_SIGN) == MASK_SIGN);
  1772. BID_RETURN (res);
  1773. }
  1774. // is y is zero, X is greater if it is positive
  1775. else if (y_is_zero) {
  1776. res = ((x & MASK_SIGN) != MASK_SIGN);
  1777. BID_RETURN (res);
  1778. }
  1779. // OPPOSITE SIGN (CASE5)
  1780. // now, if the sign bits differ, x is greater if y is negative
  1781. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  1782. res = ((y & MASK_SIGN) == MASK_SIGN);
  1783. BID_RETURN (res);
  1784. }
  1785. // REDUNDANT REPRESENTATIONS (CASE6)
  1786. // if both components are either bigger or smaller
  1787. if (sig_x > sig_y && exp_x >= exp_y) {
  1788. res = ((x & MASK_SIGN) != MASK_SIGN);
  1789. BID_RETURN (res);
  1790. }
  1791. if (sig_x < sig_y && exp_x <= exp_y) {
  1792. res = ((x & MASK_SIGN) == MASK_SIGN);
  1793. BID_RETURN (res);
  1794. }
  1795. // if exp_x is 15 greater than exp_y, no need for compensation
  1796. if (exp_x - exp_y > 15) {
  1797. res = ((x & MASK_SIGN) != MASK_SIGN);
  1798. BID_RETURN (res);
  1799. }
  1800. // difference cannot be greater than 10^15
  1801. // if exp_x is 15 less than exp_y, no need for compensation
  1802. if (exp_y - exp_x > 15) {
  1803. res = ((x & MASK_SIGN) == MASK_SIGN);
  1804. BID_RETURN (res);
  1805. }
  1806. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  1807. if (exp_x > exp_y) { // to simplify the loop below,
  1808. // otherwise adjust the x significand upwards
  1809. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  1810. mult_factor[exp_x - exp_y]);
  1811. // if postitive, return whichever significand is larger
  1812. // (converse if negative)
  1813. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  1814. res = 0;
  1815. BID_RETURN (res);
  1816. }
  1817. {
  1818. res = (((sig_n_prime.w[1] > 0)
  1819. || sig_n_prime.w[0] > sig_y) ^ ((x & MASK_SIGN) ==
  1820. MASK_SIGN));
  1821. BID_RETURN (res);
  1822. }
  1823. }
  1824. // adjust the y significand upwards
  1825. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  1826. mult_factor[exp_y - exp_x]);
  1827. // if postitive, return whichever significand is larger
  1828. // (converse if negative)
  1829. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  1830. res = 0;
  1831. BID_RETURN (res);
  1832. }
  1833. {
  1834. res = (((sig_n_prime.w[1] == 0)
  1835. && (sig_x > sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
  1836. MASK_SIGN));
  1837. BID_RETURN (res);
  1838. }
  1839. }
  1840. #if DECIMAL_CALL_BY_REFERENCE
  1841. void
  1842. bid64_signaling_greater_equal (int *pres, UINT64 * px,
  1843. UINT64 *
  1844. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1845. _EXC_INFO_PARAM) {
  1846. UINT64 x = *px;
  1847. UINT64 y = *py;
  1848. #else
  1849. int
  1850. bid64_signaling_greater_equal (UINT64 x,
  1851. UINT64 y _EXC_FLAGS_PARAM
  1852. _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  1853. #endif
  1854. int res;
  1855. int exp_x, exp_y;
  1856. UINT64 sig_x, sig_y;
  1857. UINT128 sig_n_prime;
  1858. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  1859. // NaN (CASE1)
  1860. // if either number is NAN, the comparison is unordered : return 1
  1861. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  1862. *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
  1863. res = 0;
  1864. BID_RETURN (res);
  1865. }
  1866. // SIMPLE (CASE2)
  1867. // if all the bits are the same, these numbers are equal.
  1868. if (x == y) {
  1869. res = 1;
  1870. BID_RETURN (res);
  1871. }
  1872. // INFINITY (CASE3)
  1873. if ((x & MASK_INF) == MASK_INF) {
  1874. // if x==neg_inf, { res = (y == neg_inf)?1:0; BID_RETURN (res) }
  1875. if ((x & MASK_SIGN) == MASK_SIGN)
  1876. // x is -inf, so it is less than y unless y is -inf
  1877. {
  1878. res = (((y & MASK_INF) == MASK_INF)
  1879. && (y & MASK_SIGN) == MASK_SIGN);
  1880. BID_RETURN (res);
  1881. } else
  1882. // x is pos_inf, no way for it to be less than y
  1883. {
  1884. res = 1;
  1885. BID_RETURN (res);
  1886. }
  1887. } else if ((y & MASK_INF) == MASK_INF) {
  1888. // x is finite, so:
  1889. // if y is +inf, x<y
  1890. // if y is -inf, x>y
  1891. {
  1892. res = ((y & MASK_SIGN) == MASK_SIGN);
  1893. BID_RETURN (res);
  1894. }
  1895. }
  1896. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1897. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1898. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  1899. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1900. if (sig_x > 9999999999999999ull) {
  1901. non_canon_x = 1;
  1902. } else {
  1903. non_canon_x = 0;
  1904. }
  1905. } else {
  1906. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  1907. sig_x = (x & MASK_BINARY_SIG1);
  1908. non_canon_x = 0;
  1909. }
  1910. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1911. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1912. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  1913. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1914. if (sig_y > 9999999999999999ull) {
  1915. non_canon_y = 1;
  1916. } else {
  1917. non_canon_y = 0;
  1918. }
  1919. } else {
  1920. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  1921. sig_y = (y & MASK_BINARY_SIG1);
  1922. non_canon_y = 0;
  1923. }
  1924. // ZERO (CASE4)
  1925. // some properties:
  1926. // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
  1927. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  1928. // therefore ignore the exponent field
  1929. // (Any non-canonical # is considered 0)
  1930. if (non_canon_x || sig_x == 0) {
  1931. x_is_zero = 1;
  1932. }
  1933. if (non_canon_y || sig_y == 0) {
  1934. y_is_zero = 1;
  1935. }
  1936. // if both numbers are zero, they are equal
  1937. if (x_is_zero && y_is_zero) {
  1938. res = 1;
  1939. BID_RETURN (res);
  1940. }
  1941. // if x is zero, it is lessthan if Y is positive
  1942. else if (x_is_zero) {
  1943. res = ((y & MASK_SIGN) == MASK_SIGN);
  1944. BID_RETURN (res);
  1945. }
  1946. // if y is zero, X is less if it is negative
  1947. else if (y_is_zero) {
  1948. res = ((x & MASK_SIGN) != MASK_SIGN);
  1949. BID_RETURN (res);
  1950. }
  1951. // OPPOSITE SIGN (CASE5)
  1952. // now, if the sign bits differ, x is less than if y is positive
  1953. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  1954. res = ((y & MASK_SIGN) == MASK_SIGN);
  1955. BID_RETURN (res);
  1956. }
  1957. // REDUNDANT REPRESENTATIONS (CASE6)
  1958. // if both components are either bigger or smaller
  1959. if (sig_x > sig_y && exp_x >= exp_y) {
  1960. res = ((x & MASK_SIGN) != MASK_SIGN);
  1961. BID_RETURN (res);
  1962. }
  1963. if (sig_x < sig_y && exp_x <= exp_y) {
  1964. res = ((x & MASK_SIGN) == MASK_SIGN);
  1965. BID_RETURN (res);
  1966. }
  1967. // if exp_x is 15 greater than exp_y, no need for compensation
  1968. if (exp_x - exp_y > 15) {
  1969. res = ((x & MASK_SIGN) != MASK_SIGN);
  1970. BID_RETURN (res);
  1971. }
  1972. // difference cannot be greater than 10^15
  1973. // if exp_x is 15 less than exp_y, no need for compensation
  1974. if (exp_y - exp_x > 15) {
  1975. res = ((x & MASK_SIGN) == MASK_SIGN);
  1976. BID_RETURN (res);
  1977. }
  1978. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  1979. if (exp_x > exp_y) { // to simplify the loop below,
  1980. // otherwise adjust the x significand upwards
  1981. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  1982. mult_factor[exp_x - exp_y]);
  1983. // return 1 if values are equal
  1984. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  1985. res = 1;
  1986. BID_RETURN (res);
  1987. }
  1988. // if postitive, return whichever significand abs is smaller
  1989. // (converse if negative)
  1990. {
  1991. res = (((sig_n_prime.w[1] == 0)
  1992. && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) !=
  1993. MASK_SIGN));
  1994. BID_RETURN (res);
  1995. }
  1996. }
  1997. // adjust the y significand upwards
  1998. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  1999. mult_factor[exp_y - exp_x]);
  2000. // return 0 if values are equal
  2001. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  2002. res = 1;
  2003. BID_RETURN (res);
  2004. }
  2005. // if positive, return whichever significand abs is smaller
  2006. // (converse if negative)
  2007. {
  2008. res = (((sig_n_prime.w[1] > 0)
  2009. || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) !=
  2010. MASK_SIGN));
  2011. BID_RETURN (res);
  2012. }
  2013. }
  2014. #if DECIMAL_CALL_BY_REFERENCE
  2015. void
  2016. bid64_signaling_greater_unordered (int *pres, UINT64 * px,
  2017. UINT64 *
  2018. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  2019. _EXC_INFO_PARAM) {
  2020. UINT64 x = *px;
  2021. UINT64 y = *py;
  2022. #else
  2023. int
  2024. bid64_signaling_greater_unordered (UINT64 x,
  2025. UINT64 y _EXC_FLAGS_PARAM
  2026. _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  2027. #endif
  2028. int res;
  2029. int exp_x, exp_y;
  2030. UINT64 sig_x, sig_y;
  2031. UINT128 sig_n_prime;
  2032. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  2033. // NaN (CASE1)
  2034. // if either number is NAN, the comparison is unordered,
  2035. // rather than equal : return 0
  2036. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  2037. *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
  2038. res = 1;
  2039. BID_RETURN (res);
  2040. }
  2041. // SIMPLE (CASE2)
  2042. // if all the bits are the same, these numbers are equal (not Greater).
  2043. if (x == y) {
  2044. res = 0;
  2045. BID_RETURN (res);
  2046. }
  2047. // INFINITY (CASE3)
  2048. if ((x & MASK_INF) == MASK_INF) {
  2049. // if x is neg infinity, there is no way it is greater than y, return 0
  2050. if (((x & MASK_SIGN) == MASK_SIGN)) {
  2051. res = 0;
  2052. BID_RETURN (res);
  2053. }
  2054. // x is pos infinity, it is greater,
  2055. // unless y is positive infinity => return y!=pos_infinity
  2056. else {
  2057. res = (((y & MASK_INF) != MASK_INF)
  2058. || ((y & MASK_SIGN) == MASK_SIGN));
  2059. BID_RETURN (res);
  2060. }
  2061. } else if ((y & MASK_INF) == MASK_INF) {
  2062. // x is finite, so if y is positive infinity, then x is less, return 0
  2063. // if y is negative infinity, then x is greater, return 1
  2064. {
  2065. res = ((y & MASK_SIGN) == MASK_SIGN);
  2066. BID_RETURN (res);
  2067. }
  2068. }
  2069. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  2070. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  2071. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  2072. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  2073. if (sig_x > 9999999999999999ull) {
  2074. non_canon_x = 1;
  2075. } else {
  2076. non_canon_x = 0;
  2077. }
  2078. } else {
  2079. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  2080. sig_x = (x & MASK_BINARY_SIG1);
  2081. non_canon_x = 0;
  2082. }
  2083. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  2084. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  2085. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  2086. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  2087. if (sig_y > 9999999999999999ull) {
  2088. non_canon_y = 1;
  2089. } else {
  2090. non_canon_y = 0;
  2091. }
  2092. } else {
  2093. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  2094. sig_y = (y & MASK_BINARY_SIG1);
  2095. non_canon_y = 0;
  2096. }
  2097. // ZERO (CASE4)
  2098. // some properties:
  2099. // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
  2100. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  2101. // therefore ignore the exponent field
  2102. // (Any non-canonical # is considered 0)
  2103. if (non_canon_x || sig_x == 0) {
  2104. x_is_zero = 1;
  2105. }
  2106. if (non_canon_y || sig_y == 0) {
  2107. y_is_zero = 1;
  2108. }
  2109. // if both numbers are zero, neither is greater => return NOTGREATERTHAN
  2110. if (x_is_zero && y_is_zero) {
  2111. res = 0;
  2112. BID_RETURN (res);
  2113. }
  2114. // is x is zero, it is greater if Y is negative
  2115. else if (x_is_zero) {
  2116. res = ((y & MASK_SIGN) == MASK_SIGN);
  2117. BID_RETURN (res);
  2118. }
  2119. // is y is zero, X is greater if it is positive
  2120. else if (y_is_zero) {
  2121. res = ((x & MASK_SIGN) != MASK_SIGN);
  2122. BID_RETURN (res);
  2123. }
  2124. // OPPOSITE SIGN (CASE5)
  2125. // now, if the sign bits differ, x is greater if y is negative
  2126. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  2127. res = ((y & MASK_SIGN) == MASK_SIGN);
  2128. BID_RETURN (res);
  2129. }
  2130. // REDUNDANT REPRESENTATIONS (CASE6)
  2131. // if both components are either bigger or smaller
  2132. if (sig_x > sig_y && exp_x >= exp_y) {
  2133. res = ((x & MASK_SIGN) != MASK_SIGN);
  2134. BID_RETURN (res);
  2135. }
  2136. if (sig_x < sig_y && exp_x <= exp_y) {
  2137. res = ((x & MASK_SIGN) == MASK_SIGN);
  2138. BID_RETURN (res);
  2139. }
  2140. // if exp_x is 15 greater than exp_y, no need for compensation
  2141. if (exp_x - exp_y > 15) {
  2142. res = ((x & MASK_SIGN) != MASK_SIGN);
  2143. BID_RETURN (res);
  2144. }
  2145. // difference cannot be greater than 10^15
  2146. // if exp_x is 15 less than exp_y, no need for compensation
  2147. if (exp_y - exp_x > 15) {
  2148. res = ((x & MASK_SIGN) == MASK_SIGN);
  2149. BID_RETURN (res);
  2150. }
  2151. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  2152. if (exp_x > exp_y) { // to simplify the loop below,
  2153. // otherwise adjust the x significand upwards
  2154. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  2155. mult_factor[exp_x - exp_y]);
  2156. // if postitive, return whichever significand is larger
  2157. // (converse if negative)
  2158. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  2159. res = 0;
  2160. BID_RETURN (res);
  2161. }
  2162. {
  2163. res = (((sig_n_prime.w[1] > 0)
  2164. || sig_n_prime.w[0] > sig_y) ^ ((x & MASK_SIGN) ==
  2165. MASK_SIGN));
  2166. BID_RETURN (res);
  2167. }
  2168. }
  2169. // adjust the y significand upwards
  2170. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  2171. mult_factor[exp_y - exp_x]);
  2172. // if postitive, return whichever significand is larger
  2173. // (converse if negative)
  2174. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  2175. res = 0;
  2176. BID_RETURN (res);
  2177. }
  2178. {
  2179. res = (((sig_n_prime.w[1] == 0)
  2180. && (sig_x > sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
  2181. MASK_SIGN));
  2182. BID_RETURN (res);
  2183. }
  2184. }
  2185. #if DECIMAL_CALL_BY_REFERENCE
  2186. void
  2187. bid64_signaling_less (int *pres, UINT64 * px,
  2188. UINT64 *
  2189. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  2190. _EXC_INFO_PARAM) {
  2191. UINT64 x = *px;
  2192. UINT64 y = *py;
  2193. #else
  2194. int
  2195. bid64_signaling_less (UINT64 x,
  2196. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  2197. _EXC_INFO_PARAM) {
  2198. #endif
  2199. int res;
  2200. int exp_x, exp_y;
  2201. UINT64 sig_x, sig_y;
  2202. UINT128 sig_n_prime;
  2203. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  2204. // NaN (CASE1)
  2205. // if either number is NAN, the comparison is unordered : return 0
  2206. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  2207. *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
  2208. res = 0;
  2209. BID_RETURN (res);
  2210. }
  2211. // SIMPLE (CASE2)
  2212. // if all the bits are the same, these numbers are equal.
  2213. if (x == y) {
  2214. res = 0;
  2215. BID_RETURN (res);
  2216. }
  2217. // INFINITY (CASE3)
  2218. if ((x & MASK_INF) == MASK_INF) {
  2219. // if x==neg_inf, { res = (y == neg_inf)?0:1; BID_RETURN (res) }
  2220. if ((x & MASK_SIGN) == MASK_SIGN)
  2221. // x is -inf, so it is less than y unless y is -inf
  2222. {
  2223. res = (((y & MASK_INF) != MASK_INF)
  2224. || (y & MASK_SIGN) != MASK_SIGN);
  2225. BID_RETURN (res);
  2226. } else
  2227. // x is pos_inf, no way for it to be less than y
  2228. {
  2229. res = 0;
  2230. BID_RETURN (res);
  2231. }
  2232. } else if ((y & MASK_INF) == MASK_INF) {
  2233. // x is finite, so:
  2234. // if y is +inf, x<y
  2235. // if y is -inf, x>y
  2236. {
  2237. res = ((y & MASK_SIGN) != MASK_SIGN);
  2238. BID_RETURN (res);
  2239. }
  2240. }
  2241. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  2242. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  2243. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  2244. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  2245. if (sig_x > 9999999999999999ull) {
  2246. non_canon_x = 1;
  2247. } else {
  2248. non_canon_x = 0;
  2249. }
  2250. } else {
  2251. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  2252. sig_x = (x & MASK_BINARY_SIG1);
  2253. non_canon_x = 0;
  2254. }
  2255. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  2256. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  2257. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  2258. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  2259. if (sig_y > 9999999999999999ull) {
  2260. non_canon_y = 1;
  2261. } else {
  2262. non_canon_y = 0;
  2263. }
  2264. } else {
  2265. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  2266. sig_y = (y & MASK_BINARY_SIG1);
  2267. non_canon_y = 0;
  2268. }
  2269. // ZERO (CASE4)
  2270. // some properties:
  2271. // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
  2272. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  2273. // therefore ignore the exponent field
  2274. // (Any non-canonical # is considered 0)
  2275. if (non_canon_x || sig_x == 0) {
  2276. x_is_zero = 1;
  2277. }
  2278. if (non_canon_y || sig_y == 0) {
  2279. y_is_zero = 1;
  2280. }
  2281. // if both numbers are zero, they are equal
  2282. if (x_is_zero && y_is_zero) {
  2283. res = 0;
  2284. BID_RETURN (res);
  2285. }
  2286. // if x is zero, it is lessthan if Y is positive
  2287. else if (x_is_zero) {
  2288. res = ((y & MASK_SIGN) != MASK_SIGN);
  2289. BID_RETURN (res);
  2290. }
  2291. // if y is zero, X is less if it is negative
  2292. else if (y_is_zero) {
  2293. res = ((x & MASK_SIGN) == MASK_SIGN);
  2294. BID_RETURN (res);
  2295. }
  2296. // OPPOSITE SIGN (CASE5)
  2297. // now, if the sign bits differ, x is less than if y is positive
  2298. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  2299. res = ((y & MASK_SIGN) != MASK_SIGN);
  2300. BID_RETURN (res);
  2301. }
  2302. // REDUNDANT REPRESENTATIONS (CASE6)
  2303. // if both components are either bigger or smaller
  2304. if (sig_x > sig_y && exp_x >= exp_y) {
  2305. res = ((x & MASK_SIGN) == MASK_SIGN);
  2306. BID_RETURN (res);
  2307. }
  2308. if (sig_x < sig_y && exp_x <= exp_y) {
  2309. res = ((x & MASK_SIGN) != MASK_SIGN);
  2310. BID_RETURN (res);
  2311. }
  2312. // if exp_x is 15 greater than exp_y, no need for compensation
  2313. if (exp_x - exp_y > 15) {
  2314. res = ((x & MASK_SIGN) == MASK_SIGN);
  2315. BID_RETURN (res);
  2316. }
  2317. // difference cannot be greater than 10^15
  2318. // if exp_x is 15 less than exp_y, no need for compensation
  2319. if (exp_y - exp_x > 15) {
  2320. res = ((x & MASK_SIGN) != MASK_SIGN);
  2321. BID_RETURN (res);
  2322. }
  2323. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  2324. if (exp_x > exp_y) { // to simplify the loop below,
  2325. // otherwise adjust the x significand upwards
  2326. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  2327. mult_factor[exp_x - exp_y]);
  2328. // return 0 if values are equal
  2329. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  2330. res = 0;
  2331. BID_RETURN (res);
  2332. }
  2333. // if postitive, return whichever significand abs is smaller
  2334. // (converse if negative)
  2335. {
  2336. res = (((sig_n_prime.w[1] == 0)
  2337. && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
  2338. MASK_SIGN));
  2339. BID_RETURN (res);
  2340. }
  2341. }
  2342. // adjust the y significand upwards
  2343. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  2344. mult_factor[exp_y - exp_x]);
  2345. // return 0 if values are equal
  2346. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  2347. res = 0;
  2348. BID_RETURN (res);
  2349. }
  2350. // if positive, return whichever significand abs is smaller
  2351. // (converse if negative)
  2352. {
  2353. res = (((sig_n_prime.w[1] > 0)
  2354. || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
  2355. MASK_SIGN));
  2356. BID_RETURN (res);
  2357. }
  2358. }
  2359. #if DECIMAL_CALL_BY_REFERENCE
  2360. void
  2361. bid64_signaling_less_equal (int *pres, UINT64 * px,
  2362. UINT64 *
  2363. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  2364. _EXC_INFO_PARAM) {
  2365. UINT64 x = *px;
  2366. UINT64 y = *py;
  2367. #else
  2368. int
  2369. bid64_signaling_less_equal (UINT64 x,
  2370. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  2371. _EXC_INFO_PARAM) {
  2372. #endif
  2373. int res;
  2374. int exp_x, exp_y;
  2375. UINT64 sig_x, sig_y;
  2376. UINT128 sig_n_prime;
  2377. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  2378. // NaN (CASE1)
  2379. // if either number is NAN, the comparison is unordered,
  2380. // rather than equal : return 0
  2381. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  2382. *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
  2383. res = 0;
  2384. BID_RETURN (res);
  2385. }
  2386. // SIMPLE (CASE2)
  2387. // if all the bits are the same, these numbers are equal (LESSEQUAL).
  2388. if (x == y) {
  2389. res = 1;
  2390. BID_RETURN (res);
  2391. }
  2392. // INFINITY (CASE3)
  2393. if ((x & MASK_INF) == MASK_INF) {
  2394. // if x is neg infinity, it must be lessthan or equal to y return 1
  2395. if (((x & MASK_SIGN) == MASK_SIGN)) {
  2396. res = 1;
  2397. BID_RETURN (res);
  2398. }
  2399. // x is pos infinity, it is greater,
  2400. // unless y is positive infinity => return y==pos_infinity
  2401. else {
  2402. res = !(((y & MASK_INF) != MASK_INF)
  2403. || ((y & MASK_SIGN) == MASK_SIGN));
  2404. BID_RETURN (res);
  2405. }
  2406. } else if ((y & MASK_INF) == MASK_INF) {
  2407. // x is finite, so if y is positive infinity, then x is less, return 1
  2408. // if y is negative infinity, then x is greater, return 0
  2409. {
  2410. res = ((y & MASK_SIGN) != MASK_SIGN);
  2411. BID_RETURN (res);
  2412. }
  2413. }
  2414. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  2415. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  2416. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  2417. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  2418. if (sig_x > 9999999999999999ull) {
  2419. non_canon_x = 1;
  2420. } else {
  2421. non_canon_x = 0;
  2422. }
  2423. } else {
  2424. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  2425. sig_x = (x & MASK_BINARY_SIG1);
  2426. non_canon_x = 0;
  2427. }
  2428. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  2429. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  2430. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  2431. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  2432. if (sig_y > 9999999999999999ull) {
  2433. non_canon_y = 1;
  2434. } else {
  2435. non_canon_y = 0;
  2436. }
  2437. } else {
  2438. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  2439. sig_y = (y & MASK_BINARY_SIG1);
  2440. non_canon_y = 0;
  2441. }
  2442. // ZERO (CASE4)
  2443. // some properties:
  2444. // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
  2445. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  2446. // therefore ignore the exponent field
  2447. // (Any non-canonical # is considered 0)
  2448. if (non_canon_x || sig_x == 0) {
  2449. x_is_zero = 1;
  2450. }
  2451. if (non_canon_y || sig_y == 0) {
  2452. y_is_zero = 1;
  2453. }
  2454. // if both numbers are zero, they are equal -> return 1
  2455. if (x_is_zero && y_is_zero) {
  2456. res = 1;
  2457. BID_RETURN (res);
  2458. }
  2459. // if x is zero, it is lessthan if Y is positive
  2460. else if (x_is_zero) {
  2461. res = ((y & MASK_SIGN) != MASK_SIGN);
  2462. BID_RETURN (res);
  2463. }
  2464. // if y is zero, X is less if it is negative
  2465. else if (y_is_zero) {
  2466. res = ((x & MASK_SIGN) == MASK_SIGN);
  2467. BID_RETURN (res);
  2468. }
  2469. // OPPOSITE SIGN (CASE5)
  2470. // now, if the sign bits differ, x is less than if y is positive
  2471. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  2472. res = ((y & MASK_SIGN) != MASK_SIGN);
  2473. BID_RETURN (res);
  2474. }
  2475. // REDUNDANT REPRESENTATIONS (CASE6)
  2476. // if both components are either bigger or smaller
  2477. if (sig_x > sig_y && exp_x >= exp_y) {
  2478. res = ((x & MASK_SIGN) == MASK_SIGN);
  2479. BID_RETURN (res);
  2480. }
  2481. if (sig_x < sig_y && exp_x <= exp_y) {
  2482. res = ((x & MASK_SIGN) != MASK_SIGN);
  2483. BID_RETURN (res);
  2484. }
  2485. // if exp_x is 15 greater than exp_y, no need for compensation
  2486. if (exp_x - exp_y > 15) {
  2487. res = ((x & MASK_SIGN) == MASK_SIGN);
  2488. BID_RETURN (res);
  2489. }
  2490. // difference cannot be greater than 10^15
  2491. // if exp_x is 15 less than exp_y, no need for compensation
  2492. if (exp_y - exp_x > 15) {
  2493. res = ((x & MASK_SIGN) != MASK_SIGN);
  2494. BID_RETURN (res);
  2495. }
  2496. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  2497. if (exp_x > exp_y) { // to simplify the loop below,
  2498. // otherwise adjust the x significand upwards
  2499. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  2500. mult_factor[exp_x - exp_y]);
  2501. // return 1 if values are equal
  2502. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  2503. res = 1;
  2504. BID_RETURN (res);
  2505. }
  2506. // if postitive, return whichever significand abs is smaller
  2507. // (converse if negative)
  2508. {
  2509. res = (((sig_n_prime.w[1] == 0)
  2510. && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
  2511. MASK_SIGN));
  2512. BID_RETURN (res);
  2513. }
  2514. }
  2515. // adjust the y significand upwards
  2516. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  2517. mult_factor[exp_y - exp_x]);
  2518. // return 1 if values are equal
  2519. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  2520. res = 1;
  2521. BID_RETURN (res);
  2522. }
  2523. // if positive, return whichever significand abs is smaller
  2524. // (converse if negative)
  2525. {
  2526. res = (((sig_n_prime.w[1] > 0)
  2527. || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
  2528. MASK_SIGN));
  2529. BID_RETURN (res);
  2530. }
  2531. }
  2532. #if DECIMAL_CALL_BY_REFERENCE
  2533. void
  2534. bid64_signaling_less_unordered (int *pres, UINT64 * px,
  2535. UINT64 *
  2536. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  2537. _EXC_INFO_PARAM) {
  2538. UINT64 x = *px;
  2539. UINT64 y = *py;
  2540. #else
  2541. int
  2542. bid64_signaling_less_unordered (UINT64 x,
  2543. UINT64 y _EXC_FLAGS_PARAM
  2544. _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  2545. #endif
  2546. int res;
  2547. int exp_x, exp_y;
  2548. UINT64 sig_x, sig_y;
  2549. UINT128 sig_n_prime;
  2550. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  2551. // NaN (CASE1)
  2552. // if either number is NAN, the comparison is unordered : return 0
  2553. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  2554. *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
  2555. res = 1;
  2556. BID_RETURN (res);
  2557. }
  2558. // SIMPLE (CASE2)
  2559. // if all the bits are the same, these numbers are equal.
  2560. if (x == y) {
  2561. res = 0;
  2562. BID_RETURN (res);
  2563. }
  2564. // INFINITY (CASE3)
  2565. if ((x & MASK_INF) == MASK_INF) {
  2566. // if x==neg_inf, { res = (y == neg_inf)?0:1; BID_RETURN (res) }
  2567. if ((x & MASK_SIGN) == MASK_SIGN)
  2568. // x is -inf, so it is less than y unless y is -inf
  2569. {
  2570. res = (((y & MASK_INF) != MASK_INF)
  2571. || (y & MASK_SIGN) != MASK_SIGN);
  2572. BID_RETURN (res);
  2573. } else
  2574. // x is pos_inf, no way for it to be less than y
  2575. {
  2576. res = 0;
  2577. BID_RETURN (res);
  2578. }
  2579. } else if ((y & MASK_INF) == MASK_INF) {
  2580. // x is finite, so:
  2581. // if y is +inf, x<y
  2582. // if y is -inf, x>y
  2583. {
  2584. res = ((y & MASK_SIGN) != MASK_SIGN);
  2585. BID_RETURN (res);
  2586. }
  2587. }
  2588. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  2589. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  2590. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  2591. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  2592. if (sig_x > 9999999999999999ull) {
  2593. non_canon_x = 1;
  2594. } else {
  2595. non_canon_x = 0;
  2596. }
  2597. } else {
  2598. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  2599. sig_x = (x & MASK_BINARY_SIG1);
  2600. non_canon_x = 0;
  2601. }
  2602. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  2603. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  2604. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  2605. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  2606. if (sig_y > 9999999999999999ull) {
  2607. non_canon_y = 1;
  2608. } else {
  2609. non_canon_y = 0;
  2610. }
  2611. } else {
  2612. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  2613. sig_y = (y & MASK_BINARY_SIG1);
  2614. non_canon_y = 0;
  2615. }
  2616. // ZERO (CASE4)
  2617. // some properties:
  2618. // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
  2619. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  2620. // therefore ignore the exponent field
  2621. // (Any non-canonical # is considered 0)
  2622. if (non_canon_x || sig_x == 0) {
  2623. x_is_zero = 1;
  2624. }
  2625. if (non_canon_y || sig_y == 0) {
  2626. y_is_zero = 1;
  2627. }
  2628. // if both numbers are zero, they are equal
  2629. if (x_is_zero && y_is_zero) {
  2630. res = 0;
  2631. BID_RETURN (res);
  2632. }
  2633. // if x is zero, it is lessthan if Y is positive
  2634. else if (x_is_zero) {
  2635. res = ((y & MASK_SIGN) != MASK_SIGN);
  2636. BID_RETURN (res);
  2637. }
  2638. // if y is zero, X is less if it is negative
  2639. else if (y_is_zero) {
  2640. res = ((x & MASK_SIGN) == MASK_SIGN);
  2641. BID_RETURN (res);
  2642. }
  2643. // OPPOSITE SIGN (CASE5)
  2644. // now, if the sign bits differ, x is less than if y is positive
  2645. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  2646. res = ((y & MASK_SIGN) != MASK_SIGN);
  2647. BID_RETURN (res);
  2648. }
  2649. // REDUNDANT REPRESENTATIONS (CASE6)
  2650. // if both components are either bigger or smaller
  2651. if (sig_x > sig_y && exp_x >= exp_y) {
  2652. res = ((x & MASK_SIGN) == MASK_SIGN);
  2653. BID_RETURN (res);
  2654. }
  2655. if (sig_x < sig_y && exp_x <= exp_y) {
  2656. res = ((x & MASK_SIGN) != MASK_SIGN);
  2657. BID_RETURN (res);
  2658. }
  2659. // if exp_x is 15 greater than exp_y, no need for compensation
  2660. if (exp_x - exp_y > 15) {
  2661. res = ((x & MASK_SIGN) == MASK_SIGN);
  2662. BID_RETURN (res);
  2663. }
  2664. // difference cannot be greater than 10^15
  2665. // if exp_x is 15 less than exp_y, no need for compensation
  2666. if (exp_y - exp_x > 15) {
  2667. res = ((x & MASK_SIGN) != MASK_SIGN);
  2668. BID_RETURN (res);
  2669. }
  2670. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  2671. if (exp_x > exp_y) { // to simplify the loop below,
  2672. // otherwise adjust the x significand upwards
  2673. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  2674. mult_factor[exp_x - exp_y]);
  2675. // return 0 if values are equal
  2676. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  2677. res = 0;
  2678. BID_RETURN (res);
  2679. }
  2680. // if postitive, return whichever significand abs is smaller
  2681. // (converse if negative)
  2682. {
  2683. res = (((sig_n_prime.w[1] == 0)
  2684. && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
  2685. MASK_SIGN));
  2686. BID_RETURN (res);
  2687. }
  2688. }
  2689. // adjust the y significand upwards
  2690. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  2691. mult_factor[exp_y - exp_x]);
  2692. // return 0 if values are equal
  2693. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  2694. res = 0;
  2695. BID_RETURN (res);
  2696. }
  2697. // if positive, return whichever significand abs is smaller
  2698. // (converse if negative)
  2699. {
  2700. res = (((sig_n_prime.w[1] > 0)
  2701. || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
  2702. MASK_SIGN));
  2703. BID_RETURN (res);
  2704. }
  2705. }
  2706. #if DECIMAL_CALL_BY_REFERENCE
  2707. void
  2708. bid64_signaling_not_greater (int *pres, UINT64 * px,
  2709. UINT64 *
  2710. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  2711. _EXC_INFO_PARAM) {
  2712. UINT64 x = *px;
  2713. UINT64 y = *py;
  2714. #else
  2715. int
  2716. bid64_signaling_not_greater (UINT64 x,
  2717. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  2718. _EXC_INFO_PARAM) {
  2719. #endif
  2720. int res;
  2721. int exp_x, exp_y;
  2722. UINT64 sig_x, sig_y;
  2723. UINT128 sig_n_prime;
  2724. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  2725. // NaN (CASE1)
  2726. // if either number is NAN, the comparison is unordered,
  2727. // rather than equal : return 0
  2728. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  2729. *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
  2730. res = 1;
  2731. BID_RETURN (res);
  2732. }
  2733. // SIMPLE (CASE2)
  2734. // if all the bits are the same, these numbers are equal (LESSEQUAL).
  2735. if (x == y) {
  2736. res = 1;
  2737. BID_RETURN (res);
  2738. }
  2739. // INFINITY (CASE3)
  2740. if ((x & MASK_INF) == MASK_INF) {
  2741. // if x is neg infinity, it must be lessthan or equal to y return 1
  2742. if (((x & MASK_SIGN) == MASK_SIGN)) {
  2743. res = 1;
  2744. BID_RETURN (res);
  2745. }
  2746. // x is pos infinity, it is greater,
  2747. // unless y is positive infinity => return y==pos_infinity
  2748. else {
  2749. res = !(((y & MASK_INF) != MASK_INF)
  2750. || ((y & MASK_SIGN) == MASK_SIGN));
  2751. BID_RETURN (res);
  2752. }
  2753. } else if ((y & MASK_INF) == MASK_INF) {
  2754. // x is finite, so if y is positive infinity, then x is less, return 1
  2755. // if y is negative infinity, then x is greater, return 0
  2756. {
  2757. res = ((y & MASK_SIGN) != MASK_SIGN);
  2758. BID_RETURN (res);
  2759. }
  2760. }
  2761. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  2762. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  2763. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  2764. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  2765. if (sig_x > 9999999999999999ull) {
  2766. non_canon_x = 1;
  2767. } else {
  2768. non_canon_x = 0;
  2769. }
  2770. } else {
  2771. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  2772. sig_x = (x & MASK_BINARY_SIG1);
  2773. non_canon_x = 0;
  2774. }
  2775. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  2776. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  2777. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  2778. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  2779. if (sig_y > 9999999999999999ull) {
  2780. non_canon_y = 1;
  2781. } else {
  2782. non_canon_y = 0;
  2783. }
  2784. } else {
  2785. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  2786. sig_y = (y & MASK_BINARY_SIG1);
  2787. non_canon_y = 0;
  2788. }
  2789. // ZERO (CASE4)
  2790. // some properties:
  2791. // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
  2792. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  2793. // therefore ignore the exponent field
  2794. // (Any non-canonical # is considered 0)
  2795. if (non_canon_x || sig_x == 0) {
  2796. x_is_zero = 1;
  2797. }
  2798. if (non_canon_y || sig_y == 0) {
  2799. y_is_zero = 1;
  2800. }
  2801. // if both numbers are zero, they are equal -> return 1
  2802. if (x_is_zero && y_is_zero) {
  2803. res = 1;
  2804. BID_RETURN (res);
  2805. }
  2806. // if x is zero, it is lessthan if Y is positive
  2807. else if (x_is_zero) {
  2808. res = ((y & MASK_SIGN) != MASK_SIGN);
  2809. BID_RETURN (res);
  2810. }
  2811. // if y is zero, X is less if it is negative
  2812. else if (y_is_zero) {
  2813. res = ((x & MASK_SIGN) == MASK_SIGN);
  2814. BID_RETURN (res);
  2815. }
  2816. // OPPOSITE SIGN (CASE5)
  2817. // now, if the sign bits differ, x is less than if y is positive
  2818. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  2819. res = ((y & MASK_SIGN) != MASK_SIGN);
  2820. BID_RETURN (res);
  2821. }
  2822. // REDUNDANT REPRESENTATIONS (CASE6)
  2823. // if both components are either bigger or smaller
  2824. if (sig_x > sig_y && exp_x >= exp_y) {
  2825. res = ((x & MASK_SIGN) == MASK_SIGN);
  2826. BID_RETURN (res);
  2827. }
  2828. if (sig_x < sig_y && exp_x <= exp_y) {
  2829. res = ((x & MASK_SIGN) != MASK_SIGN);
  2830. BID_RETURN (res);
  2831. }
  2832. // if exp_x is 15 greater than exp_y, no need for compensation
  2833. if (exp_x - exp_y > 15) {
  2834. res = ((x & MASK_SIGN) == MASK_SIGN);
  2835. BID_RETURN (res);
  2836. }
  2837. // difference cannot be greater than 10^15
  2838. // if exp_x is 15 less than exp_y, no need for compensation
  2839. if (exp_y - exp_x > 15) {
  2840. res = ((x & MASK_SIGN) != MASK_SIGN);
  2841. BID_RETURN (res);
  2842. }
  2843. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  2844. if (exp_x > exp_y) { // to simplify the loop below,
  2845. // otherwise adjust the x significand upwards
  2846. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  2847. mult_factor[exp_x - exp_y]);
  2848. // return 1 if values are equal
  2849. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  2850. res = 1;
  2851. BID_RETURN (res);
  2852. }
  2853. // if postitive, return whichever significand abs is smaller
  2854. // (converse if negative)
  2855. {
  2856. res = (((sig_n_prime.w[1] == 0)
  2857. && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
  2858. MASK_SIGN));
  2859. BID_RETURN (res);
  2860. }
  2861. }
  2862. // adjust the y significand upwards
  2863. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  2864. mult_factor[exp_y - exp_x]);
  2865. // return 1 if values are equal
  2866. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  2867. res = 1;
  2868. BID_RETURN (res);
  2869. }
  2870. // if positive, return whichever significand abs is smaller
  2871. // (converse if negative)
  2872. {
  2873. res = (((sig_n_prime.w[1] > 0)
  2874. || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
  2875. MASK_SIGN));
  2876. BID_RETURN (res);
  2877. }
  2878. }
  2879. #if DECIMAL_CALL_BY_REFERENCE
  2880. void
  2881. bid64_signaling_not_less (int *pres, UINT64 * px,
  2882. UINT64 *
  2883. py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  2884. _EXC_INFO_PARAM) {
  2885. UINT64 x = *px;
  2886. UINT64 y = *py;
  2887. #else
  2888. int
  2889. bid64_signaling_not_less (UINT64 x,
  2890. UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  2891. _EXC_INFO_PARAM) {
  2892. #endif
  2893. int res;
  2894. int exp_x, exp_y;
  2895. UINT64 sig_x, sig_y;
  2896. UINT128 sig_n_prime;
  2897. char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
  2898. // NaN (CASE1)
  2899. // if either number is NAN, the comparison is unordered : return 1
  2900. if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
  2901. *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
  2902. res = 1;
  2903. BID_RETURN (res);
  2904. }
  2905. // SIMPLE (CASE2)
  2906. // if all the bits are the same, these numbers are equal.
  2907. if (x == y) {
  2908. res = 1;
  2909. BID_RETURN (res);
  2910. }
  2911. // INFINITY (CASE3)
  2912. if ((x & MASK_INF) == MASK_INF) {
  2913. // if x==neg_inf, { res = (y == neg_inf)?1:0; BID_RETURN (res) }
  2914. if ((x & MASK_SIGN) == MASK_SIGN)
  2915. // x is -inf, so it is less than y unless y is -inf
  2916. {
  2917. res = (((y & MASK_INF) == MASK_INF)
  2918. && (y & MASK_SIGN) == MASK_SIGN);
  2919. BID_RETURN (res);
  2920. } else
  2921. // x is pos_inf, no way for it to be less than y
  2922. {
  2923. res = 1;
  2924. BID_RETURN (res);
  2925. }
  2926. } else if ((y & MASK_INF) == MASK_INF) {
  2927. // x is finite, so:
  2928. // if y is +inf, x<y
  2929. // if y is -inf, x>y
  2930. {
  2931. res = ((y & MASK_SIGN) == MASK_SIGN);
  2932. BID_RETURN (res);
  2933. }
  2934. }
  2935. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  2936. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  2937. exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
  2938. sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  2939. if (sig_x > 9999999999999999ull) {
  2940. non_canon_x = 1;
  2941. } else {
  2942. non_canon_x = 0;
  2943. }
  2944. } else {
  2945. exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
  2946. sig_x = (x & MASK_BINARY_SIG1);
  2947. non_canon_x = 0;
  2948. }
  2949. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  2950. if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  2951. exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
  2952. sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  2953. if (sig_y > 9999999999999999ull) {
  2954. non_canon_y = 1;
  2955. } else {
  2956. non_canon_y = 0;
  2957. }
  2958. } else {
  2959. exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
  2960. sig_y = (y & MASK_BINARY_SIG1);
  2961. non_canon_y = 0;
  2962. }
  2963. // ZERO (CASE4)
  2964. // some properties:
  2965. // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
  2966. // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
  2967. // therefore ignore the exponent field
  2968. // (Any non-canonical # is considered 0)
  2969. if (non_canon_x || sig_x == 0) {
  2970. x_is_zero = 1;
  2971. }
  2972. if (non_canon_y || sig_y == 0) {
  2973. y_is_zero = 1;
  2974. }
  2975. // if both numbers are zero, they are equal
  2976. if (x_is_zero && y_is_zero) {
  2977. res = 1;
  2978. BID_RETURN (res);
  2979. }
  2980. // if x is zero, it is lessthan if Y is positive
  2981. else if (x_is_zero) {
  2982. res = ((y & MASK_SIGN) == MASK_SIGN);
  2983. BID_RETURN (res);
  2984. }
  2985. // if y is zero, X is less if it is negative
  2986. else if (y_is_zero) {
  2987. res = ((x & MASK_SIGN) != MASK_SIGN);
  2988. BID_RETURN (res);
  2989. }
  2990. // OPPOSITE SIGN (CASE5)
  2991. // now, if the sign bits differ, x is less than if y is positive
  2992. if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
  2993. res = ((y & MASK_SIGN) == MASK_SIGN);
  2994. BID_RETURN (res);
  2995. }
  2996. // REDUNDANT REPRESENTATIONS (CASE6)
  2997. // if both components are either bigger or smaller
  2998. if (sig_x > sig_y && exp_x >= exp_y) {
  2999. res = ((x & MASK_SIGN) != MASK_SIGN);
  3000. BID_RETURN (res);
  3001. }
  3002. if (sig_x < sig_y && exp_x <= exp_y) {
  3003. res = ((x & MASK_SIGN) == MASK_SIGN);
  3004. BID_RETURN (res);
  3005. }
  3006. // if exp_x is 15 greater than exp_y, no need for compensation
  3007. if (exp_x - exp_y > 15) {
  3008. res = ((x & MASK_SIGN) != MASK_SIGN);
  3009. BID_RETURN (res);
  3010. }
  3011. // difference cannot be greater than 10^15
  3012. // if exp_x is 15 less than exp_y, no need for compensation
  3013. if (exp_y - exp_x > 15) {
  3014. res = ((x & MASK_SIGN) == MASK_SIGN);
  3015. BID_RETURN (res);
  3016. }
  3017. // if |exp_x - exp_y| < 15, it comes down to the compensated significand
  3018. if (exp_x > exp_y) { // to simplify the loop below,
  3019. // otherwise adjust the x significand upwards
  3020. __mul_64x64_to_128MACH (sig_n_prime, sig_x,
  3021. mult_factor[exp_x - exp_y]);
  3022. // return 0 if values are equal
  3023. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
  3024. res = 1;
  3025. BID_RETURN (res);
  3026. }
  3027. // if postitive, return whichever significand abs is smaller
  3028. // (converse if negative)
  3029. {
  3030. res = (((sig_n_prime.w[1] == 0)
  3031. && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) !=
  3032. MASK_SIGN));
  3033. BID_RETURN (res);
  3034. }
  3035. }
  3036. // adjust the y significand upwards
  3037. __mul_64x64_to_128MACH (sig_n_prime, sig_y,
  3038. mult_factor[exp_y - exp_x]);
  3039. // return 0 if values are equal
  3040. if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
  3041. res = 1;
  3042. BID_RETURN (res);
  3043. }
  3044. // if positive, return whichever significand abs is smaller
  3045. // (converse if negative)
  3046. {
  3047. res = (((sig_n_prime.w[1] > 0)
  3048. || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) !=
  3049. MASK_SIGN));
  3050. BID_RETURN (res);
  3051. }
  3052. }