bid64_to_int32.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587
  1. /* Copyright (C) 2007-2022 Free Software Foundation, Inc.
  2. This file is part of GCC.
  3. GCC is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free
  5. Software Foundation; either version 3, or (at your option) any later
  6. version.
  7. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  8. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  10. for more details.
  11. Under Section 7 of GPL version 3, you are granted additional
  12. permissions described in the GCC Runtime Library Exception, version
  13. 3.1, as published by the Free Software Foundation.
  14. You should have received a copy of the GNU General Public License and
  15. a copy of the GCC Runtime Library Exception along with this program;
  16. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  17. <http://www.gnu.org/licenses/>. */
  18. #include "bid_internal.h"
  19. /*****************************************************************************
  20. * BID64_to_int32_rnint
  21. ****************************************************************************/
  22. #if DECIMAL_CALL_BY_REFERENCE
  23. void
  24. bid64_to_int32_rnint (int *pres,
  25. UINT64 *
  26. px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  27. _EXC_INFO_PARAM) {
  28. UINT64 x = *px;
  29. #else
  30. int
  31. bid64_to_int32_rnint (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  32. _EXC_INFO_PARAM) {
  33. #endif
  34. int res;
  35. UINT64 x_sign;
  36. UINT64 x_exp;
  37. int exp; // unbiased exponent
  38. // Note: C1 represents x_significand (UINT64)
  39. UINT64 tmp64;
  40. BID_UI64DOUBLE tmp1;
  41. unsigned int x_nr_bits;
  42. int q, ind, shift;
  43. UINT64 C1;
  44. UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
  45. UINT128 fstar;
  46. UINT128 P128;
  47. // check for NaN or Infinity
  48. if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
  49. // set invalid flag
  50. *pfpsf |= INVALID_EXCEPTION;
  51. // return Integer Indefinite
  52. res = 0x80000000;
  53. BID_RETURN (res);
  54. }
  55. // unpack x
  56. x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
  57. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  58. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  59. x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
  60. C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  61. if (C1 > 9999999999999999ull) { // non-canonical
  62. x_exp = 0;
  63. C1 = 0;
  64. }
  65. } else {
  66. x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
  67. C1 = x & MASK_BINARY_SIG1;
  68. }
  69. // check for zeros (possibly from non-canonical values)
  70. if (C1 == 0x0ull) {
  71. // x is 0
  72. res = 0x00000000;
  73. BID_RETURN (res);
  74. }
  75. // x is not special and is not zero
  76. // q = nr. of decimal digits in x (1 <= q <= 54)
  77. // determine first the nr. of bits in x
  78. if (C1 >= 0x0020000000000000ull) { // x >= 2^53
  79. // split the 64-bit value in two 32-bit halves to avoid rounding errors
  80. if (C1 >= 0x0000000100000000ull) { // x >= 2^32
  81. tmp1.d = (double) (C1 >> 32); // exact conversion
  82. x_nr_bits =
  83. 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  84. } else { // x < 2^32
  85. tmp1.d = (double) C1; // exact conversion
  86. x_nr_bits =
  87. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  88. }
  89. } else { // if x < 2^53
  90. tmp1.d = (double) C1; // exact conversion
  91. x_nr_bits =
  92. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  93. }
  94. q = nr_digits[x_nr_bits - 1].digits;
  95. if (q == 0) {
  96. q = nr_digits[x_nr_bits - 1].digits1;
  97. if (C1 >= nr_digits[x_nr_bits - 1].threshold_lo)
  98. q++;
  99. }
  100. exp = x_exp - 398; // unbiased exponent
  101. if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
  102. // set invalid flag
  103. *pfpsf |= INVALID_EXCEPTION;
  104. // return Integer Indefinite
  105. res = 0x80000000;
  106. BID_RETURN (res);
  107. } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
  108. // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
  109. // so x rounded to an integer may or may not fit in a signed 32-bit int
  110. // the cases that do not fit are identified here; the ones that fit
  111. // fall through and will be handled with other cases further,
  112. // under '1 <= q + exp <= 10'
  113. if (x_sign) { // if n < 0 and q + exp = 10
  114. // if n < -2^31 - 1/2 then n is too large
  115. // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31+1/2
  116. // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x500000005, 1<=q<=16
  117. // <=> C * 10^(11-q) > 0x500000005, 1<=q<=16
  118. if (q <= 11) {
  119. // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
  120. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  121. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  122. if (tmp64 > 0x500000005ull) {
  123. // set invalid flag
  124. *pfpsf |= INVALID_EXCEPTION;
  125. // return Integer Indefinite
  126. res = 0x80000000;
  127. BID_RETURN (res);
  128. }
  129. // else cases that can be rounded to a 32-bit int fall through
  130. // to '1 <= q + exp <= 10'
  131. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  132. // C * 10^(11-q) > 0x500000005 <=>
  133. // C > 0x500000005 * 10^(q-11) where 1 <= q - 11 <= 5
  134. // (scale 2^31+1/2 up)
  135. // Note: 0x500000005*10^(q-11) has q-1 or q digits, where q <= 16
  136. tmp64 = 0x500000005ull * ten2k64[q - 11];
  137. if (C1 > tmp64) {
  138. // set invalid flag
  139. *pfpsf |= INVALID_EXCEPTION;
  140. // return Integer Indefinite
  141. res = 0x80000000;
  142. BID_RETURN (res);
  143. }
  144. // else cases that can be rounded to a 32-bit int fall through
  145. // to '1 <= q + exp <= 10'
  146. }
  147. } else { // if n > 0 and q + exp = 10
  148. // if n >= 2^31 - 1/2 then n is too large
  149. // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31-1/2
  150. // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x4fffffffb, 1<=q<=16
  151. // <=> C * 10^(11-q) >= 0x4fffffffb, 1<=q<=16
  152. if (q <= 11) {
  153. // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
  154. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  155. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  156. if (tmp64 >= 0x4fffffffbull) {
  157. // set invalid flag
  158. *pfpsf |= INVALID_EXCEPTION;
  159. // return Integer Indefinite
  160. res = 0x80000000;
  161. BID_RETURN (res);
  162. }
  163. // else cases that can be rounded to a 32-bit int fall through
  164. // to '1 <= q + exp <= 10'
  165. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  166. // C * 10^(11-q) >= 0x4fffffffb <=>
  167. // C >= 0x4fffffffb * 10^(q-11) where 1 <= q - 11 <= 5
  168. // (scale 2^31-1/2 up)
  169. // Note: 0x4fffffffb*10^(q-11) has q-1 or q digits, where q <= 16
  170. tmp64 = 0x4fffffffbull * ten2k64[q - 11];
  171. if (C1 >= tmp64) {
  172. // set invalid flag
  173. *pfpsf |= INVALID_EXCEPTION;
  174. // return Integer Indefinite
  175. res = 0x80000000;
  176. BID_RETURN (res);
  177. }
  178. // else cases that can be rounded to a 32-bit int fall through
  179. // to '1 <= q + exp <= 10'
  180. }
  181. }
  182. }
  183. // n is not too large to be converted to int32: -2^31 - 1/2 <= n < 2^31 - 1/2
  184. // Note: some of the cases tested for above fall through to this point
  185. if ((q + exp) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
  186. // return 0
  187. res = 0x00000000;
  188. BID_RETURN (res);
  189. } else if ((q + exp) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
  190. // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
  191. // res = 0
  192. // else
  193. // res = +/-1
  194. ind = q - 1;
  195. if (C1 <= midpoint64[ind]) {
  196. res = 0x00000000; // return 0
  197. } else if (x_sign) { // n < 0
  198. res = 0xffffffff; // return -1
  199. } else { // n > 0
  200. res = 0x00000001; // return +1
  201. }
  202. } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
  203. // -2^31-1/2 <= x <= -1 or 1 <= x < 2^31-1/2 so x can be rounded
  204. // to nearest to a 32-bit signed integer
  205. if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
  206. ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
  207. // chop off ind digits from the lower part of C1
  208. // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
  209. C1 = C1 + midpoint64[ind - 1];
  210. // calculate C* and f*
  211. // C* is actually floor(C*) in this case
  212. // C* and f* need shifting and masking, as shown by
  213. // shiftright128[] and maskhigh128[]
  214. // 1 <= x <= 15
  215. // kx = 10^(-x) = ten2mk64[ind - 1]
  216. // C* = (C1 + 1/2 * 10^x) * 10^(-x)
  217. // the approximation of 10^(-x) was rounded up to 54 bits
  218. __mul_64x64_to_128MACH (P128, C1, ten2mk64[ind - 1]);
  219. Cstar = P128.w[1];
  220. fstar.w[1] = P128.w[1] & maskhigh128[ind - 1];
  221. fstar.w[0] = P128.w[0];
  222. // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
  223. // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
  224. // if (0 < f* < 10^(-x)) then the result is a midpoint
  225. // if floor(C*) is even then C* = floor(C*) - logical right
  226. // shift; C* has p decimal digits, correct by Prop. 1)
  227. // else if floor(C*) is odd C* = floor(C*)-1 (logical right
  228. // shift; C* has p decimal digits, correct by Pr. 1)
  229. // else
  230. // C* = floor(C*) (logical right shift; C has p decimal digits,
  231. // correct by Property 1)
  232. // n = C* * 10^(e+x)
  233. // shift right C* by Ex-64 = shiftright128[ind]
  234. shift = shiftright128[ind - 1]; // 0 <= shift <= 39
  235. Cstar = Cstar >> shift;
  236. // if the result was a midpoint it was rounded away from zero, so
  237. // it will need a correction
  238. // check for midpoints
  239. if ((fstar.w[1] == 0) && fstar.w[0]
  240. && (fstar.w[0] <= ten2mk128trunc[ind - 1].w[1])) {
  241. // ten2mk128trunc[ind -1].w[1] is identical to
  242. // ten2mk128[ind -1].w[1]
  243. // the result is a midpoint; round to nearest
  244. if (Cstar & 0x01) { // Cstar is odd; MP in [EVEN, ODD]
  245. // if floor(C*) is odd C = floor(C*) - 1; the result >= 1
  246. Cstar--; // Cstar is now even
  247. } // else MP in [ODD, EVEN]
  248. }
  249. if (x_sign)
  250. res = -Cstar;
  251. else
  252. res = Cstar;
  253. } else if (exp == 0) {
  254. // 1 <= q <= 10
  255. // res = +/-C (exact)
  256. if (x_sign)
  257. res = -C1;
  258. else
  259. res = C1;
  260. } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
  261. // res = +/-C * 10^exp (exact)
  262. if (x_sign)
  263. res = -C1 * ten2k64[exp];
  264. else
  265. res = C1 * ten2k64[exp];
  266. }
  267. }
  268. BID_RETURN (res);
  269. }
  270. /*****************************************************************************
  271. * BID64_to_int32_xrnint
  272. ****************************************************************************/
  273. #if DECIMAL_CALL_BY_REFERENCE
  274. void
  275. bid64_to_int32_xrnint (int *pres,
  276. UINT64 *
  277. px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  278. _EXC_INFO_PARAM) {
  279. UINT64 x = *px;
  280. #else
  281. int
  282. bid64_to_int32_xrnint (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  283. _EXC_INFO_PARAM) {
  284. #endif
  285. int res;
  286. UINT64 x_sign;
  287. UINT64 x_exp;
  288. int exp; // unbiased exponent
  289. // Note: C1 represents x_significand (UINT64)
  290. UINT64 tmp64;
  291. BID_UI64DOUBLE tmp1;
  292. unsigned int x_nr_bits;
  293. int q, ind, shift;
  294. UINT64 C1;
  295. UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
  296. UINT128 fstar;
  297. UINT128 P128;
  298. // check for NaN or Infinity
  299. if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
  300. // set invalid flag
  301. *pfpsf |= INVALID_EXCEPTION;
  302. // return Integer Indefinite
  303. res = 0x80000000;
  304. BID_RETURN (res);
  305. }
  306. // unpack x
  307. x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
  308. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  309. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  310. x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
  311. C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  312. if (C1 > 9999999999999999ull) { // non-canonical
  313. x_exp = 0;
  314. C1 = 0;
  315. }
  316. } else {
  317. x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
  318. C1 = x & MASK_BINARY_SIG1;
  319. }
  320. // check for zeros (possibly from non-canonical values)
  321. if (C1 == 0x0ull) {
  322. // x is 0
  323. res = 0x00000000;
  324. BID_RETURN (res);
  325. }
  326. // x is not special and is not zero
  327. // q = nr. of decimal digits in x (1 <= q <= 54)
  328. // determine first the nr. of bits in x
  329. if (C1 >= 0x0020000000000000ull) { // x >= 2^53
  330. // split the 64-bit value in two 32-bit halves to avoid rounding errors
  331. if (C1 >= 0x0000000100000000ull) { // x >= 2^32
  332. tmp1.d = (double) (C1 >> 32); // exact conversion
  333. x_nr_bits =
  334. 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  335. } else { // x < 2^32
  336. tmp1.d = (double) C1; // exact conversion
  337. x_nr_bits =
  338. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  339. }
  340. } else { // if x < 2^53
  341. tmp1.d = (double) C1; // exact conversion
  342. x_nr_bits =
  343. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  344. }
  345. q = nr_digits[x_nr_bits - 1].digits;
  346. if (q == 0) {
  347. q = nr_digits[x_nr_bits - 1].digits1;
  348. if (C1 >= nr_digits[x_nr_bits - 1].threshold_lo)
  349. q++;
  350. }
  351. exp = x_exp - 398; // unbiased exponent
  352. if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
  353. // set invalid flag
  354. *pfpsf |= INVALID_EXCEPTION;
  355. // return Integer Indefinite
  356. res = 0x80000000;
  357. BID_RETURN (res);
  358. } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
  359. // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
  360. // so x rounded to an integer may or may not fit in a signed 32-bit int
  361. // the cases that do not fit are identified here; the ones that fit
  362. // fall through and will be handled with other cases further,
  363. // under '1 <= q + exp <= 10'
  364. if (x_sign) { // if n < 0 and q + exp = 10
  365. // if n < -2^31 - 1/2 then n is too large
  366. // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31+1/2
  367. // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x500000005, 1<=q<=16
  368. // <=> C * 10^(11-q) > 0x500000005, 1<=q<=16
  369. if (q <= 11) {
  370. // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
  371. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  372. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  373. if (tmp64 > 0x500000005ull) {
  374. // set invalid flag
  375. *pfpsf |= INVALID_EXCEPTION;
  376. // return Integer Indefinite
  377. res = 0x80000000;
  378. BID_RETURN (res);
  379. }
  380. // else cases that can be rounded to a 32-bit int fall through
  381. // to '1 <= q + exp <= 10'
  382. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  383. // C * 10^(11-q) > 0x500000005 <=>
  384. // C > 0x500000005 * 10^(q-11) where 1 <= q - 11 <= 5
  385. // (scale 2^31+1/2 up)
  386. // Note: 0x500000005*10^(q-11) has q-1 or q digits, where q <= 16
  387. tmp64 = 0x500000005ull * ten2k64[q - 11];
  388. if (C1 > tmp64) {
  389. // set invalid flag
  390. *pfpsf |= INVALID_EXCEPTION;
  391. // return Integer Indefinite
  392. res = 0x80000000;
  393. BID_RETURN (res);
  394. }
  395. // else cases that can be rounded to a 32-bit int fall through
  396. // to '1 <= q + exp <= 10'
  397. }
  398. } else { // if n > 0 and q + exp = 10
  399. // if n >= 2^31 - 1/2 then n is too large
  400. // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31-1/2
  401. // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x4fffffffb, 1<=q<=16
  402. // <=> C * 10^(11-q) >= 0x4fffffffb, 1<=q<=16
  403. if (q <= 11) {
  404. // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
  405. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  406. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  407. if (tmp64 >= 0x4fffffffbull) {
  408. // set invalid flag
  409. *pfpsf |= INVALID_EXCEPTION;
  410. // return Integer Indefinite
  411. res = 0x80000000;
  412. BID_RETURN (res);
  413. }
  414. // else cases that can be rounded to a 32-bit int fall through
  415. // to '1 <= q + exp <= 10'
  416. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  417. // C * 10^(11-q) >= 0x4fffffffb <=>
  418. // C >= 0x4fffffffb * 10^(q-11) where 1 <= q - 11 <= 5
  419. // (scale 2^31-1/2 up)
  420. // Note: 0x4fffffffb*10^(q-11) has q-1 or q digits, where q <= 16
  421. tmp64 = 0x4fffffffbull * ten2k64[q - 11];
  422. if (C1 >= tmp64) {
  423. // set invalid flag
  424. *pfpsf |= INVALID_EXCEPTION;
  425. // return Integer Indefinite
  426. res = 0x80000000;
  427. BID_RETURN (res);
  428. }
  429. // else cases that can be rounded to a 32-bit int fall through
  430. // to '1 <= q + exp <= 10'
  431. }
  432. }
  433. }
  434. // n is not too large to be converted to int32: -2^31 - 1/2 < n < 2^31 - 1/2
  435. // Note: some of the cases tested for above fall through to this point
  436. if ((q + exp) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
  437. // set inexact flag
  438. *pfpsf |= INEXACT_EXCEPTION;
  439. // return 0
  440. res = 0x00000000;
  441. BID_RETURN (res);
  442. } else if ((q + exp) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
  443. // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
  444. // res = 0
  445. // else
  446. // res = +/-1
  447. ind = q - 1;
  448. if (C1 <= midpoint64[ind]) {
  449. res = 0x00000000; // return 0
  450. } else if (x_sign) { // n < 0
  451. res = 0xffffffff; // return -1
  452. } else { // n > 0
  453. res = 0x00000001; // return +1
  454. }
  455. // set inexact flag
  456. *pfpsf |= INEXACT_EXCEPTION;
  457. } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
  458. // -2^31-1/2 <= x <= -1 or 1 <= x < 2^31-1/2 so x can be rounded
  459. // to nearest to a 32-bit signed integer
  460. if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
  461. ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
  462. // chop off ind digits from the lower part of C1
  463. // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
  464. C1 = C1 + midpoint64[ind - 1];
  465. // calculate C* and f*
  466. // C* is actually floor(C*) in this case
  467. // C* and f* need shifting and masking, as shown by
  468. // shiftright128[] and maskhigh128[]
  469. // 1 <= x <= 15
  470. // kx = 10^(-x) = ten2mk64[ind - 1]
  471. // C* = (C1 + 1/2 * 10^x) * 10^(-x)
  472. // the approximation of 10^(-x) was rounded up to 54 bits
  473. __mul_64x64_to_128MACH (P128, C1, ten2mk64[ind - 1]);
  474. Cstar = P128.w[1];
  475. fstar.w[1] = P128.w[1] & maskhigh128[ind - 1];
  476. fstar.w[0] = P128.w[0];
  477. // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
  478. // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
  479. // if (0 < f* < 10^(-x)) then the result is a midpoint
  480. // if floor(C*) is even then C* = floor(C*) - logical right
  481. // shift; C* has p decimal digits, correct by Prop. 1)
  482. // else if floor(C*) is odd C* = floor(C*)-1 (logical right
  483. // shift; C* has p decimal digits, correct by Pr. 1)
  484. // else
  485. // C* = floor(C*) (logical right shift; C has p decimal digits,
  486. // correct by Property 1)
  487. // n = C* * 10^(e+x)
  488. // shift right C* by Ex-64 = shiftright128[ind]
  489. shift = shiftright128[ind - 1]; // 0 <= shift <= 39
  490. Cstar = Cstar >> shift;
  491. // determine inexactness of the rounding of C*
  492. // if (0 < f* - 1/2 < 10^(-x)) then
  493. // the result is exact
  494. // else // if (f* - 1/2 > T*) then
  495. // the result is inexact
  496. if (ind - 1 <= 2) {
  497. if (fstar.w[0] > 0x8000000000000000ull) {
  498. // f* > 1/2 and the result may be exact
  499. tmp64 = fstar.w[0] - 0x8000000000000000ull; // f* - 1/2
  500. if ((tmp64 > ten2mk128trunc[ind - 1].w[1])) {
  501. // ten2mk128trunc[ind -1].w[1] is identical to
  502. // ten2mk128[ind -1].w[1]
  503. // set the inexact flag
  504. *pfpsf |= INEXACT_EXCEPTION;
  505. } // else the result is exact
  506. } else { // the result is inexact; f2* <= 1/2
  507. // set the inexact flag
  508. *pfpsf |= INEXACT_EXCEPTION;
  509. }
  510. } else { // if 3 <= ind - 1 <= 14
  511. if (fstar.w[1] > onehalf128[ind - 1] ||
  512. (fstar.w[1] == onehalf128[ind - 1] && fstar.w[0])) {
  513. // f2* > 1/2 and the result may be exact
  514. // Calculate f2* - 1/2
  515. tmp64 = fstar.w[1] - onehalf128[ind - 1];
  516. if (tmp64 || fstar.w[0] > ten2mk128trunc[ind - 1].w[1]) {
  517. // ten2mk128trunc[ind -1].w[1] is identical to
  518. // ten2mk128[ind -1].w[1]
  519. // set the inexact flag
  520. *pfpsf |= INEXACT_EXCEPTION;
  521. } // else the result is exact
  522. } else { // the result is inexact; f2* <= 1/2
  523. // set the inexact flag
  524. *pfpsf |= INEXACT_EXCEPTION;
  525. }
  526. }
  527. // if the result was a midpoint it was rounded away from zero, so
  528. // it will need a correction
  529. // check for midpoints
  530. if ((fstar.w[1] == 0) && fstar.w[0]
  531. && (fstar.w[0] <= ten2mk128trunc[ind - 1].w[1])) {
  532. // ten2mk128trunc[ind -1].w[1] is identical to
  533. // ten2mk128[ind -1].w[1]
  534. // the result is a midpoint; round to nearest
  535. if (Cstar & 0x01) { // Cstar is odd; MP in [EVEN, ODD]
  536. // if floor(C*) is odd C = floor(C*) - 1; the result >= 1
  537. Cstar--; // Cstar is now even
  538. } // else MP in [ODD, EVEN]
  539. }
  540. if (x_sign)
  541. res = -Cstar;
  542. else
  543. res = Cstar;
  544. } else if (exp == 0) {
  545. // 1 <= q <= 10
  546. // res = +/-C (exact)
  547. if (x_sign)
  548. res = -C1;
  549. else
  550. res = C1;
  551. } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
  552. // res = +/-C * 10^exp (exact)
  553. if (x_sign)
  554. res = -C1 * ten2k64[exp];
  555. else
  556. res = C1 * ten2k64[exp];
  557. }
  558. }
  559. BID_RETURN (res);
  560. }
  561. /*****************************************************************************
  562. * BID64_to_int32_floor
  563. ****************************************************************************/
  564. #if DECIMAL_CALL_BY_REFERENCE
  565. void
  566. bid64_to_int32_floor (int *pres,
  567. UINT64 *
  568. px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  569. _EXC_INFO_PARAM) {
  570. UINT64 x = *px;
  571. #else
  572. int
  573. bid64_to_int32_floor (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  574. _EXC_INFO_PARAM) {
  575. #endif
  576. int res;
  577. UINT64 x_sign;
  578. UINT64 x_exp;
  579. int exp; // unbiased exponent
  580. // Note: C1 represents x_significand (UINT64)
  581. UINT64 tmp64;
  582. BID_UI64DOUBLE tmp1;
  583. unsigned int x_nr_bits;
  584. int q, ind, shift;
  585. UINT64 C1;
  586. UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
  587. UINT128 fstar;
  588. UINT128 P128;
  589. // check for NaN or Infinity
  590. if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
  591. // set invalid flag
  592. *pfpsf |= INVALID_EXCEPTION;
  593. // return Integer Indefinite
  594. res = 0x80000000;
  595. BID_RETURN (res);
  596. }
  597. // unpack x
  598. x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
  599. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  600. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  601. x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
  602. C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  603. if (C1 > 9999999999999999ull) { // non-canonical
  604. x_exp = 0;
  605. C1 = 0;
  606. }
  607. } else {
  608. x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
  609. C1 = x & MASK_BINARY_SIG1;
  610. }
  611. // check for zeros (possibly from non-canonical values)
  612. if (C1 == 0x0ull) {
  613. // x is 0
  614. res = 0x00000000;
  615. BID_RETURN (res);
  616. }
  617. // x is not special and is not zero
  618. // q = nr. of decimal digits in x (1 <= q <= 54)
  619. // determine first the nr. of bits in x
  620. if (C1 >= 0x0020000000000000ull) { // x >= 2^53
  621. // split the 64-bit value in two 32-bit halves to avoid rounding errors
  622. if (C1 >= 0x0000000100000000ull) { // x >= 2^32
  623. tmp1.d = (double) (C1 >> 32); // exact conversion
  624. x_nr_bits =
  625. 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  626. } else { // x < 2^32
  627. tmp1.d = (double) C1; // exact conversion
  628. x_nr_bits =
  629. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  630. }
  631. } else { // if x < 2^53
  632. tmp1.d = (double) C1; // exact conversion
  633. x_nr_bits =
  634. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  635. }
  636. q = nr_digits[x_nr_bits - 1].digits;
  637. if (q == 0) {
  638. q = nr_digits[x_nr_bits - 1].digits1;
  639. if (C1 >= nr_digits[x_nr_bits - 1].threshold_lo)
  640. q++;
  641. }
  642. exp = x_exp - 398; // unbiased exponent
  643. if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
  644. // set invalid flag
  645. *pfpsf |= INVALID_EXCEPTION;
  646. // return Integer Indefinite
  647. res = 0x80000000;
  648. BID_RETURN (res);
  649. } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
  650. // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
  651. // so x rounded to an integer may or may not fit in a signed 32-bit int
  652. // the cases that do not fit are identified here; the ones that fit
  653. // fall through and will be handled with other cases further,
  654. // under '1 <= q + exp <= 10'
  655. if (x_sign) { // if n < 0 and q + exp = 10
  656. // if n < -2^31 then n is too large
  657. // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31
  658. // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x500000000, 1<=q<=16
  659. // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
  660. if (q <= 11) {
  661. // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
  662. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  663. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  664. if (tmp64 > 0x500000000ull) {
  665. // set invalid flag
  666. *pfpsf |= INVALID_EXCEPTION;
  667. // return Integer Indefinite
  668. res = 0x80000000;
  669. BID_RETURN (res);
  670. }
  671. // else cases that can be rounded to a 32-bit int fall through
  672. // to '1 <= q + exp <= 10'
  673. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  674. // C * 10^(11-q) > 0x500000000 <=>
  675. // C > 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
  676. // (scale 2^31+1 up)
  677. // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
  678. tmp64 = 0x500000000ull * ten2k64[q - 11];
  679. if (C1 > tmp64) {
  680. // set invalid flag
  681. *pfpsf |= INVALID_EXCEPTION;
  682. // return Integer Indefinite
  683. res = 0x80000000;
  684. BID_RETURN (res);
  685. }
  686. // else cases that can be rounded to a 32-bit int fall through
  687. // to '1 <= q + exp <= 10'
  688. }
  689. } else { // if n > 0 and q + exp = 10
  690. // if n >= 2^31 then n is too large
  691. // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
  692. // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
  693. // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
  694. if (q <= 11) {
  695. // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
  696. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  697. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  698. if (tmp64 >= 0x500000000ull) {
  699. // set invalid flag
  700. *pfpsf |= INVALID_EXCEPTION;
  701. // return Integer Indefinite
  702. res = 0x80000000;
  703. BID_RETURN (res);
  704. }
  705. // else cases that can be rounded to a 32-bit int fall through
  706. // to '1 <= q + exp <= 10'
  707. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  708. // C * 10^(11-q) >= 0x500000000 <=>
  709. // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
  710. // (scale 2^31-1 up)
  711. // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
  712. tmp64 = 0x500000000ull * ten2k64[q - 11];
  713. if (C1 >= tmp64) {
  714. // set invalid flag
  715. *pfpsf |= INVALID_EXCEPTION;
  716. // return Integer Indefinite
  717. res = 0x80000000;
  718. BID_RETURN (res);
  719. }
  720. // else cases that can be rounded to a 32-bit int fall through
  721. // to '1 <= q + exp <= 10'
  722. }
  723. }
  724. }
  725. // n is not too large to be converted to int32: -2^31 <= n < 2^31
  726. // Note: some of the cases tested for above fall through to this point
  727. if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
  728. // return -1 or 0
  729. if (x_sign)
  730. res = 0xffffffff;
  731. else
  732. res = 0x00000000;
  733. BID_RETURN (res);
  734. } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
  735. // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
  736. // to nearest to a 32-bit signed integer
  737. if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
  738. ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
  739. // chop off ind digits from the lower part of C1
  740. // C1 fits in 64 bits
  741. // calculate C* and f*
  742. // C* is actually floor(C*) in this case
  743. // C* and f* need shifting and masking, as shown by
  744. // shiftright128[] and maskhigh128[]
  745. // 1 <= x <= 15
  746. // kx = 10^(-x) = ten2mk64[ind - 1]
  747. // C* = C1 * 10^(-x)
  748. // the approximation of 10^(-x) was rounded up to 54 bits
  749. __mul_64x64_to_128MACH (P128, C1, ten2mk64[ind - 1]);
  750. Cstar = P128.w[1];
  751. fstar.w[1] = P128.w[1] & maskhigh128[ind - 1];
  752. fstar.w[0] = P128.w[0];
  753. // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
  754. // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
  755. // C* = floor(C*) (logical right shift; C has p decimal digits,
  756. // correct by Property 1)
  757. // n = C* * 10^(e+x)
  758. // shift right C* by Ex-64 = shiftright128[ind]
  759. shift = shiftright128[ind - 1]; // 0 <= shift <= 39
  760. Cstar = Cstar >> shift;
  761. // determine inexactness of the rounding of C*
  762. // if (0 < f* < 10^(-x)) then
  763. // the result is exact
  764. // else // if (f* > T*) then
  765. // the result is inexact
  766. if (ind - 1 <= 2) {
  767. if (fstar.w[0] > ten2mk128trunc[ind - 1].w[1]) {
  768. // ten2mk128trunc[ind -1].w[1] is identical to
  769. // ten2mk128[ind -1].w[1]
  770. if (x_sign) { // negative and inexact
  771. Cstar++;
  772. }
  773. } // else the result is exact
  774. } else { // if 3 <= ind - 1 <= 14
  775. if (fstar.w[1] || fstar.w[0] > ten2mk128trunc[ind - 1].w[1]) {
  776. // ten2mk128trunc[ind -1].w[1] is identical to
  777. // ten2mk128[ind -1].w[1]
  778. if (x_sign) { // negative and inexact
  779. Cstar++;
  780. }
  781. } // else the result is exact
  782. }
  783. if (x_sign)
  784. res = -Cstar;
  785. else
  786. res = Cstar;
  787. } else if (exp == 0) {
  788. // 1 <= q <= 10
  789. // res = +/-C (exact)
  790. if (x_sign)
  791. res = -C1;
  792. else
  793. res = C1;
  794. } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
  795. // res = +/-C * 10^exp (exact)
  796. if (x_sign)
  797. res = -C1 * ten2k64[exp];
  798. else
  799. res = C1 * ten2k64[exp];
  800. }
  801. }
  802. BID_RETURN (res);
  803. }
  804. /*****************************************************************************
  805. * BID64_to_int32_xfloor
  806. ****************************************************************************/
  807. #if DECIMAL_CALL_BY_REFERENCE
  808. void
  809. bid64_to_int32_xfloor (int *pres,
  810. UINT64 *
  811. px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  812. _EXC_INFO_PARAM) {
  813. UINT64 x = *px;
  814. #else
  815. int
  816. bid64_to_int32_xfloor (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  817. _EXC_INFO_PARAM) {
  818. #endif
  819. int res;
  820. UINT64 x_sign;
  821. UINT64 x_exp;
  822. int exp; // unbiased exponent
  823. // Note: C1 represents x_significand (UINT64)
  824. UINT64 tmp64;
  825. BID_UI64DOUBLE tmp1;
  826. unsigned int x_nr_bits;
  827. int q, ind, shift;
  828. UINT64 C1;
  829. UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
  830. UINT128 fstar;
  831. UINT128 P128;
  832. // check for NaN or Infinity
  833. if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
  834. // set invalid flag
  835. *pfpsf |= INVALID_EXCEPTION;
  836. // return Integer Indefinite
  837. res = 0x80000000;
  838. BID_RETURN (res);
  839. }
  840. // unpack x
  841. x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
  842. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  843. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  844. x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
  845. C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  846. if (C1 > 9999999999999999ull) { // non-canonical
  847. x_exp = 0;
  848. C1 = 0;
  849. }
  850. } else {
  851. x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
  852. C1 = x & MASK_BINARY_SIG1;
  853. }
  854. // check for zeros (possibly from non-canonical values)
  855. if (C1 == 0x0ull) {
  856. // x is 0
  857. res = 0x00000000;
  858. BID_RETURN (res);
  859. }
  860. // x is not special and is not zero
  861. // q = nr. of decimal digits in x (1 <= q <= 54)
  862. // determine first the nr. of bits in x
  863. if (C1 >= 0x0020000000000000ull) { // x >= 2^53
  864. // split the 64-bit value in two 32-bit halves to avoid rounding errors
  865. if (C1 >= 0x0000000100000000ull) { // x >= 2^32
  866. tmp1.d = (double) (C1 >> 32); // exact conversion
  867. x_nr_bits =
  868. 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  869. } else { // x < 2^32
  870. tmp1.d = (double) C1; // exact conversion
  871. x_nr_bits =
  872. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  873. }
  874. } else { // if x < 2^53
  875. tmp1.d = (double) C1; // exact conversion
  876. x_nr_bits =
  877. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  878. }
  879. q = nr_digits[x_nr_bits - 1].digits;
  880. if (q == 0) {
  881. q = nr_digits[x_nr_bits - 1].digits1;
  882. if (C1 >= nr_digits[x_nr_bits - 1].threshold_lo)
  883. q++;
  884. }
  885. exp = x_exp - 398; // unbiased exponent
  886. if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
  887. // set invalid flag
  888. *pfpsf |= INVALID_EXCEPTION;
  889. // return Integer Indefinite
  890. res = 0x80000000;
  891. BID_RETURN (res);
  892. } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
  893. // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
  894. // so x rounded to an integer may or may not fit in a signed 32-bit int
  895. // the cases that do not fit are identified here; the ones that fit
  896. // fall through and will be handled with other cases further,
  897. // under '1 <= q + exp <= 10'
  898. if (x_sign) { // if n < 0 and q + exp = 10
  899. // if n < -2^31 then n is too large
  900. // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31
  901. // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x500000000, 1<=q<=16
  902. // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
  903. if (q <= 11) {
  904. // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
  905. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  906. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  907. if (tmp64 > 0x500000000ull) {
  908. // set invalid flag
  909. *pfpsf |= INVALID_EXCEPTION;
  910. // return Integer Indefinite
  911. res = 0x80000000;
  912. BID_RETURN (res);
  913. }
  914. // else cases that can be rounded to a 32-bit int fall through
  915. // to '1 <= q + exp <= 10'
  916. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  917. // C * 10^(11-q) > 0x500000000 <=>
  918. // C > 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
  919. // (scale 2^31+1 up)
  920. // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
  921. tmp64 = 0x500000000ull * ten2k64[q - 11];
  922. if (C1 > tmp64) {
  923. // set invalid flag
  924. *pfpsf |= INVALID_EXCEPTION;
  925. // return Integer Indefinite
  926. res = 0x80000000;
  927. BID_RETURN (res);
  928. }
  929. // else cases that can be rounded to a 32-bit int fall through
  930. // to '1 <= q + exp <= 10'
  931. }
  932. } else { // if n > 0 and q + exp = 10
  933. // if n >= 2^31 then n is too large
  934. // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
  935. // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
  936. // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
  937. if (q <= 11) {
  938. // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
  939. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  940. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  941. if (tmp64 >= 0x500000000ull) {
  942. // set invalid flag
  943. *pfpsf |= INVALID_EXCEPTION;
  944. // return Integer Indefinite
  945. res = 0x80000000;
  946. BID_RETURN (res);
  947. }
  948. // else cases that can be rounded to a 32-bit int fall through
  949. // to '1 <= q + exp <= 10'
  950. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  951. // C * 10^(11-q) >= 0x500000000 <=>
  952. // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
  953. // (scale 2^31-1 up)
  954. // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
  955. tmp64 = 0x500000000ull * ten2k64[q - 11];
  956. if (C1 >= tmp64) {
  957. // set invalid flag
  958. *pfpsf |= INVALID_EXCEPTION;
  959. // return Integer Indefinite
  960. res = 0x80000000;
  961. BID_RETURN (res);
  962. }
  963. // else cases that can be rounded to a 32-bit int fall through
  964. // to '1 <= q + exp <= 10'
  965. }
  966. }
  967. }
  968. // n is not too large to be converted to int32: -2^31 <= n < 2^31
  969. // Note: some of the cases tested for above fall through to this point
  970. if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
  971. // set inexact flag
  972. *pfpsf |= INEXACT_EXCEPTION;
  973. // return -1 or 0
  974. if (x_sign)
  975. res = 0xffffffff;
  976. else
  977. res = 0x00000000;
  978. BID_RETURN (res);
  979. } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
  980. // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
  981. // to nearest to a 32-bit signed integer
  982. if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
  983. ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
  984. // chop off ind digits from the lower part of C1
  985. // C1 fits in 64 bits
  986. // calculate C* and f*
  987. // C* is actually floor(C*) in this case
  988. // C* and f* need shifting and masking, as shown by
  989. // shiftright128[] and maskhigh128[]
  990. // 1 <= x <= 15
  991. // kx = 10^(-x) = ten2mk64[ind - 1]
  992. // C* = C1 * 10^(-x)
  993. // the approximation of 10^(-x) was rounded up to 54 bits
  994. __mul_64x64_to_128MACH (P128, C1, ten2mk64[ind - 1]);
  995. Cstar = P128.w[1];
  996. fstar.w[1] = P128.w[1] & maskhigh128[ind - 1];
  997. fstar.w[0] = P128.w[0];
  998. // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
  999. // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
  1000. // C* = floor(C*) (logical right shift; C has p decimal digits,
  1001. // correct by Property 1)
  1002. // n = C* * 10^(e+x)
  1003. // shift right C* by Ex-64 = shiftright128[ind]
  1004. shift = shiftright128[ind - 1]; // 0 <= shift <= 39
  1005. Cstar = Cstar >> shift;
  1006. // determine inexactness of the rounding of C*
  1007. // if (0 < f* < 10^(-x)) then
  1008. // the result is exact
  1009. // else // if (f* > T*) then
  1010. // the result is inexact
  1011. if (ind - 1 <= 2) {
  1012. if (fstar.w[0] > ten2mk128trunc[ind - 1].w[1]) {
  1013. // ten2mk128trunc[ind -1].w[1] is identical to
  1014. // ten2mk128[ind -1].w[1]
  1015. if (x_sign) { // negative and inexact
  1016. Cstar++;
  1017. }
  1018. // set the inexact flag
  1019. *pfpsf |= INEXACT_EXCEPTION;
  1020. } // else the result is exact
  1021. } else { // if 3 <= ind - 1 <= 14
  1022. if (fstar.w[1] || fstar.w[0] > ten2mk128trunc[ind - 1].w[1]) {
  1023. // ten2mk128trunc[ind -1].w[1] is identical to
  1024. // ten2mk128[ind -1].w[1]
  1025. if (x_sign) { // negative and inexact
  1026. Cstar++;
  1027. }
  1028. // set the inexact flag
  1029. *pfpsf |= INEXACT_EXCEPTION;
  1030. } // else the result is exact
  1031. }
  1032. if (x_sign)
  1033. res = -Cstar;
  1034. else
  1035. res = Cstar;
  1036. } else if (exp == 0) {
  1037. // 1 <= q <= 10
  1038. // res = +/-C (exact)
  1039. if (x_sign)
  1040. res = -C1;
  1041. else
  1042. res = C1;
  1043. } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
  1044. // res = +/-C * 10^exp (exact)
  1045. if (x_sign)
  1046. res = -C1 * ten2k64[exp];
  1047. else
  1048. res = C1 * ten2k64[exp];
  1049. }
  1050. }
  1051. BID_RETURN (res);
  1052. }
  1053. /*****************************************************************************
  1054. * BID64_to_int32_ceil
  1055. ****************************************************************************/
  1056. #if DECIMAL_CALL_BY_REFERENCE
  1057. void
  1058. bid64_to_int32_ceil (int *pres,
  1059. UINT64 *
  1060. px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1061. _EXC_INFO_PARAM) {
  1062. UINT64 x = *px;
  1063. #else
  1064. int
  1065. bid64_to_int32_ceil (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1066. _EXC_INFO_PARAM) {
  1067. #endif
  1068. int res;
  1069. UINT64 x_sign;
  1070. UINT64 x_exp;
  1071. int exp; // unbiased exponent
  1072. // Note: C1 represents x_significand (UINT64)
  1073. UINT64 tmp64;
  1074. BID_UI64DOUBLE tmp1;
  1075. unsigned int x_nr_bits;
  1076. int q, ind, shift;
  1077. UINT64 C1;
  1078. UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
  1079. UINT128 fstar;
  1080. UINT128 P128;
  1081. // check for NaN or Infinity
  1082. if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
  1083. // set invalid flag
  1084. *pfpsf |= INVALID_EXCEPTION;
  1085. // return Integer Indefinite
  1086. res = 0x80000000;
  1087. BID_RETURN (res);
  1088. }
  1089. // unpack x
  1090. x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
  1091. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1092. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1093. x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
  1094. C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1095. if (C1 > 9999999999999999ull) { // non-canonical
  1096. x_exp = 0;
  1097. C1 = 0;
  1098. }
  1099. } else {
  1100. x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
  1101. C1 = x & MASK_BINARY_SIG1;
  1102. }
  1103. // check for zeros (possibly from non-canonical values)
  1104. if (C1 == 0x0ull) {
  1105. // x is 0
  1106. res = 0x00000000;
  1107. BID_RETURN (res);
  1108. }
  1109. // x is not special and is not zero
  1110. // q = nr. of decimal digits in x (1 <= q <= 54)
  1111. // determine first the nr. of bits in x
  1112. if (C1 >= 0x0020000000000000ull) { // x >= 2^53
  1113. // split the 64-bit value in two 32-bit halves to avoid rounding errors
  1114. if (C1 >= 0x0000000100000000ull) { // x >= 2^32
  1115. tmp1.d = (double) (C1 >> 32); // exact conversion
  1116. x_nr_bits =
  1117. 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  1118. } else { // x < 2^32
  1119. tmp1.d = (double) C1; // exact conversion
  1120. x_nr_bits =
  1121. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  1122. }
  1123. } else { // if x < 2^53
  1124. tmp1.d = (double) C1; // exact conversion
  1125. x_nr_bits =
  1126. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  1127. }
  1128. q = nr_digits[x_nr_bits - 1].digits;
  1129. if (q == 0) {
  1130. q = nr_digits[x_nr_bits - 1].digits1;
  1131. if (C1 >= nr_digits[x_nr_bits - 1].threshold_lo)
  1132. q++;
  1133. }
  1134. exp = x_exp - 398; // unbiased exponent
  1135. if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
  1136. // set invalid flag
  1137. *pfpsf |= INVALID_EXCEPTION;
  1138. // return Integer Indefinite
  1139. res = 0x80000000;
  1140. BID_RETURN (res);
  1141. } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
  1142. // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
  1143. // so x rounded to an integer may or may not fit in a signed 32-bit int
  1144. // the cases that do not fit are identified here; the ones that fit
  1145. // fall through and will be handled with other cases further,
  1146. // under '1 <= q + exp <= 10'
  1147. if (x_sign) { // if n < 0 and q + exp = 10
  1148. // if n <= -2^31 - 1 then n is too large
  1149. // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
  1150. // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
  1151. // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
  1152. if (q <= 11) {
  1153. // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
  1154. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  1155. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  1156. if (tmp64 >= 0x50000000aull) {
  1157. // set invalid flag
  1158. *pfpsf |= INVALID_EXCEPTION;
  1159. // return Integer Indefinite
  1160. res = 0x80000000;
  1161. BID_RETURN (res);
  1162. }
  1163. // else cases that can be rounded to a 32-bit int fall through
  1164. // to '1 <= q + exp <= 10'
  1165. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  1166. // C * 10^(11-q) >= 0x50000000a <=>
  1167. // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
  1168. // (scale 2^31+1 up)
  1169. // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
  1170. tmp64 = 0x50000000aull * ten2k64[q - 11];
  1171. if (C1 >= tmp64) {
  1172. // set invalid flag
  1173. *pfpsf |= INVALID_EXCEPTION;
  1174. // return Integer Indefinite
  1175. res = 0x80000000;
  1176. BID_RETURN (res);
  1177. }
  1178. // else cases that can be rounded to a 32-bit int fall through
  1179. // to '1 <= q + exp <= 10'
  1180. }
  1181. } else { // if n > 0 and q + exp = 10
  1182. // if n > 2^31 - 1 then n is too large
  1183. // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31 - 1
  1184. // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x4fffffff6, 1<=q<=16
  1185. // <=> C * 10^(11-q) > 0x4fffffff6, 1<=q<=16
  1186. if (q <= 11) {
  1187. // Note: C * 10^(11-q) has 10 or 11 digits; 0x4fffffff6 has 11 digits
  1188. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  1189. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  1190. if (tmp64 > 0x4fffffff6ull) {
  1191. // set invalid flag
  1192. *pfpsf |= INVALID_EXCEPTION;
  1193. // return Integer Indefinite
  1194. res = 0x80000000;
  1195. BID_RETURN (res);
  1196. }
  1197. // else cases that can be rounded to a 32-bit int fall through
  1198. // to '1 <= q + exp <= 10'
  1199. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  1200. // C * 10^(11-q) > 0x4fffffff6 <=>
  1201. // C > 0x4fffffff6 * 10^(q-11) where 1 <= q - 11 <= 5
  1202. // (scale 2^31-1 up)
  1203. // Note: 0x4fffffff6*10^(q-11) has q-1 or q digits, where q <= 16
  1204. tmp64 = 0x4fffffff6ull * ten2k64[q - 11];
  1205. if (C1 > tmp64) {
  1206. // set invalid flag
  1207. *pfpsf |= INVALID_EXCEPTION;
  1208. // return Integer Indefinite
  1209. res = 0x80000000;
  1210. BID_RETURN (res);
  1211. }
  1212. // else cases that can be rounded to a 32-bit int fall through
  1213. // to '1 <= q + exp <= 10'
  1214. }
  1215. }
  1216. }
  1217. // n is not too large to be converted to int32: -2^31 - 1 < n <= 2^31 - 1
  1218. // Note: some of the cases tested for above fall through to this point
  1219. if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
  1220. // return 0 or 1
  1221. if (x_sign)
  1222. res = 0x00000000;
  1223. else
  1224. res = 0x00000001;
  1225. BID_RETURN (res);
  1226. } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
  1227. // -2^31-1 < x <= -1 or 1 <= x <= 2^31-1 so x can be rounded
  1228. // to nearest to a 32-bit signed integer
  1229. if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
  1230. ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
  1231. // chop off ind digits from the lower part of C1
  1232. // C1 fits in 64 bits
  1233. // calculate C* and f*
  1234. // C* is actually floor(C*) in this case
  1235. // C* and f* need shifting and masking, as shown by
  1236. // shiftright128[] and maskhigh128[]
  1237. // 1 <= x <= 15
  1238. // kx = 10^(-x) = ten2mk64[ind - 1]
  1239. // C* = C1 * 10^(-x)
  1240. // the approximation of 10^(-x) was rounded up to 54 bits
  1241. __mul_64x64_to_128MACH (P128, C1, ten2mk64[ind - 1]);
  1242. Cstar = P128.w[1];
  1243. fstar.w[1] = P128.w[1] & maskhigh128[ind - 1];
  1244. fstar.w[0] = P128.w[0];
  1245. // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
  1246. // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
  1247. // C* = floor(C*) (logical right shift; C has p decimal digits,
  1248. // correct by Property 1)
  1249. // n = C* * 10^(e+x)
  1250. // shift right C* by Ex-64 = shiftright128[ind]
  1251. shift = shiftright128[ind - 1]; // 0 <= shift <= 39
  1252. Cstar = Cstar >> shift;
  1253. // determine inexactness of the rounding of C*
  1254. // if (0 < f* < 10^(-x)) then
  1255. // the result is exact
  1256. // else // if (f* > T*) then
  1257. // the result is inexact
  1258. if (ind - 1 <= 2) {
  1259. if (fstar.w[0] > ten2mk128trunc[ind - 1].w[1]) {
  1260. // ten2mk128trunc[ind -1].w[1] is identical to
  1261. // ten2mk128[ind -1].w[1]
  1262. if (!x_sign) { // positive and inexact
  1263. Cstar++;
  1264. }
  1265. } // else the result is exact
  1266. } else { // if 3 <= ind - 1 <= 14
  1267. if (fstar.w[1] || fstar.w[0] > ten2mk128trunc[ind - 1].w[1]) {
  1268. // ten2mk128trunc[ind -1].w[1] is identical to
  1269. // ten2mk128[ind -1].w[1]
  1270. if (!x_sign) { // positive and inexact
  1271. Cstar++;
  1272. }
  1273. } // else the result is exact
  1274. }
  1275. if (x_sign)
  1276. res = -Cstar;
  1277. else
  1278. res = Cstar;
  1279. } else if (exp == 0) {
  1280. // 1 <= q <= 10
  1281. // res = +/-C (exact)
  1282. if (x_sign)
  1283. res = -C1;
  1284. else
  1285. res = C1;
  1286. } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
  1287. // res = +/-C * 10^exp (exact)
  1288. if (x_sign)
  1289. res = -C1 * ten2k64[exp];
  1290. else
  1291. res = C1 * ten2k64[exp];
  1292. }
  1293. }
  1294. BID_RETURN (res);
  1295. }
  1296. /*****************************************************************************
  1297. * BID64_to_int32_xceil
  1298. ****************************************************************************/
  1299. #if DECIMAL_CALL_BY_REFERENCE
  1300. void
  1301. bid64_to_int32_xceil (int *pres,
  1302. UINT64 *
  1303. px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1304. _EXC_INFO_PARAM) {
  1305. UINT64 x = *px;
  1306. #else
  1307. int
  1308. bid64_to_int32_xceil (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1309. _EXC_INFO_PARAM) {
  1310. #endif
  1311. int res;
  1312. UINT64 x_sign;
  1313. UINT64 x_exp;
  1314. int exp; // unbiased exponent
  1315. // Note: C1 represents x_significand (UINT64)
  1316. UINT64 tmp64;
  1317. BID_UI64DOUBLE tmp1;
  1318. unsigned int x_nr_bits;
  1319. int q, ind, shift;
  1320. UINT64 C1;
  1321. UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
  1322. UINT128 fstar;
  1323. UINT128 P128;
  1324. // check for NaN or Infinity
  1325. if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
  1326. // set invalid flag
  1327. *pfpsf |= INVALID_EXCEPTION;
  1328. // return Integer Indefinite
  1329. res = 0x80000000;
  1330. BID_RETURN (res);
  1331. }
  1332. // unpack x
  1333. x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
  1334. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1335. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1336. x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
  1337. C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1338. if (C1 > 9999999999999999ull) { // non-canonical
  1339. x_exp = 0;
  1340. C1 = 0;
  1341. }
  1342. } else {
  1343. x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
  1344. C1 = x & MASK_BINARY_SIG1;
  1345. }
  1346. // check for zeros (possibly from non-canonical values)
  1347. if (C1 == 0x0ull) {
  1348. // x is 0
  1349. res = 0x00000000;
  1350. BID_RETURN (res);
  1351. }
  1352. // x is not special and is not zero
  1353. // q = nr. of decimal digits in x (1 <= q <= 54)
  1354. // determine first the nr. of bits in x
  1355. if (C1 >= 0x0020000000000000ull) { // x >= 2^53
  1356. // split the 64-bit value in two 32-bit halves to avoid rounding errors
  1357. if (C1 >= 0x0000000100000000ull) { // x >= 2^32
  1358. tmp1.d = (double) (C1 >> 32); // exact conversion
  1359. x_nr_bits =
  1360. 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  1361. } else { // x < 2^32
  1362. tmp1.d = (double) C1; // exact conversion
  1363. x_nr_bits =
  1364. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  1365. }
  1366. } else { // if x < 2^53
  1367. tmp1.d = (double) C1; // exact conversion
  1368. x_nr_bits =
  1369. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  1370. }
  1371. q = nr_digits[x_nr_bits - 1].digits;
  1372. if (q == 0) {
  1373. q = nr_digits[x_nr_bits - 1].digits1;
  1374. if (C1 >= nr_digits[x_nr_bits - 1].threshold_lo)
  1375. q++;
  1376. }
  1377. exp = x_exp - 398; // unbiased exponent
  1378. if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
  1379. // set invalid flag
  1380. *pfpsf |= INVALID_EXCEPTION;
  1381. // return Integer Indefinite
  1382. res = 0x80000000;
  1383. BID_RETURN (res);
  1384. } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
  1385. // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
  1386. // so x rounded to an integer may or may not fit in a signed 32-bit int
  1387. // the cases that do not fit are identified here; the ones that fit
  1388. // fall through and will be handled with other cases further,
  1389. // under '1 <= q + exp <= 10'
  1390. if (x_sign) { // if n < 0 and q + exp = 10
  1391. // if n <= -2^31 - 1 then n is too large
  1392. // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
  1393. // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
  1394. // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
  1395. if (q <= 11) {
  1396. // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
  1397. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  1398. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  1399. if (tmp64 >= 0x50000000aull) {
  1400. // set invalid flag
  1401. *pfpsf |= INVALID_EXCEPTION;
  1402. // return Integer Indefinite
  1403. res = 0x80000000;
  1404. BID_RETURN (res);
  1405. }
  1406. // else cases that can be rounded to a 32-bit int fall through
  1407. // to '1 <= q + exp <= 10'
  1408. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  1409. // C * 10^(11-q) >= 0x50000000a <=>
  1410. // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
  1411. // (scale 2^31+1 up)
  1412. // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
  1413. tmp64 = 0x50000000aull * ten2k64[q - 11];
  1414. if (C1 >= tmp64) {
  1415. // set invalid flag
  1416. *pfpsf |= INVALID_EXCEPTION;
  1417. // return Integer Indefinite
  1418. res = 0x80000000;
  1419. BID_RETURN (res);
  1420. }
  1421. // else cases that can be rounded to a 32-bit int fall through
  1422. // to '1 <= q + exp <= 10'
  1423. }
  1424. } else { // if n > 0 and q + exp = 10
  1425. // if n > 2^31 - 1 then n is too large
  1426. // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31 - 1
  1427. // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x4fffffff6, 1<=q<=16
  1428. // <=> C * 10^(11-q) > 0x4fffffff6, 1<=q<=16
  1429. if (q <= 11) {
  1430. // Note: C * 10^(11-q) has 10 or 11 digits; 0x4fffffff6 has 11 digits
  1431. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  1432. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  1433. if (tmp64 > 0x4fffffff6ull) {
  1434. // set invalid flag
  1435. *pfpsf |= INVALID_EXCEPTION;
  1436. // return Integer Indefinite
  1437. res = 0x80000000;
  1438. BID_RETURN (res);
  1439. }
  1440. // else cases that can be rounded to a 32-bit int fall through
  1441. // to '1 <= q + exp <= 10'
  1442. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  1443. // C * 10^(11-q) > 0x4fffffff6 <=>
  1444. // C > 0x4fffffff6 * 10^(q-11) where 1 <= q - 11 <= 5
  1445. // (scale 2^31-1 up)
  1446. // Note: 0x4fffffff6*10^(q-11) has q-1 or q digits, where q <= 16
  1447. tmp64 = 0x4fffffff6ull * ten2k64[q - 11];
  1448. if (C1 > tmp64) {
  1449. // set invalid flag
  1450. *pfpsf |= INVALID_EXCEPTION;
  1451. // return Integer Indefinite
  1452. res = 0x80000000;
  1453. BID_RETURN (res);
  1454. }
  1455. // else cases that can be rounded to a 32-bit int fall through
  1456. // to '1 <= q + exp <= 10'
  1457. }
  1458. }
  1459. }
  1460. // n is not too large to be converted to int32: -2^31 - 1 < n <= 2^31 - 1
  1461. // Note: some of the cases tested for above fall through to this point
  1462. if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
  1463. // set inexact flag
  1464. *pfpsf |= INEXACT_EXCEPTION;
  1465. // return 0 or 1
  1466. if (x_sign)
  1467. res = 0x00000000;
  1468. else
  1469. res = 0x00000001;
  1470. BID_RETURN (res);
  1471. } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
  1472. // -2^31-1 < x <= -1 or 1 <= x <= 2^31-1 so x can be rounded
  1473. // to nearest to a 32-bit signed integer
  1474. if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
  1475. ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
  1476. // chop off ind digits from the lower part of C1
  1477. // C1 fits in 64 bits
  1478. // calculate C* and f*
  1479. // C* is actually floor(C*) in this case
  1480. // C* and f* need shifting and masking, as shown by
  1481. // shiftright128[] and maskhigh128[]
  1482. // 1 <= x <= 15
  1483. // kx = 10^(-x) = ten2mk64[ind - 1]
  1484. // C* = C1 * 10^(-x)
  1485. // the approximation of 10^(-x) was rounded up to 54 bits
  1486. __mul_64x64_to_128MACH (P128, C1, ten2mk64[ind - 1]);
  1487. Cstar = P128.w[1];
  1488. fstar.w[1] = P128.w[1] & maskhigh128[ind - 1];
  1489. fstar.w[0] = P128.w[0];
  1490. // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
  1491. // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
  1492. // C* = floor(C*) (logical right shift; C has p decimal digits,
  1493. // correct by Property 1)
  1494. // n = C* * 10^(e+x)
  1495. // shift right C* by Ex-64 = shiftright128[ind]
  1496. shift = shiftright128[ind - 1]; // 0 <= shift <= 39
  1497. Cstar = Cstar >> shift;
  1498. // determine inexactness of the rounding of C*
  1499. // if (0 < f* < 10^(-x)) then
  1500. // the result is exact
  1501. // else // if (f* > T*) then
  1502. // the result is inexact
  1503. if (ind - 1 <= 2) {
  1504. if (fstar.w[0] > ten2mk128trunc[ind - 1].w[1]) {
  1505. // ten2mk128trunc[ind -1].w[1] is identical to
  1506. // ten2mk128[ind -1].w[1]
  1507. if (!x_sign) { // positive and inexact
  1508. Cstar++;
  1509. }
  1510. // set the inexact flag
  1511. *pfpsf |= INEXACT_EXCEPTION;
  1512. } // else the result is exact
  1513. } else { // if 3 <= ind - 1 <= 14
  1514. if (fstar.w[1] || fstar.w[0] > ten2mk128trunc[ind - 1].w[1]) {
  1515. // ten2mk128trunc[ind -1].w[1] is identical to
  1516. // ten2mk128[ind -1].w[1]
  1517. if (!x_sign) { // positive and inexact
  1518. Cstar++;
  1519. }
  1520. // set the inexact flag
  1521. *pfpsf |= INEXACT_EXCEPTION;
  1522. } // else the result is exact
  1523. }
  1524. if (x_sign)
  1525. res = -Cstar;
  1526. else
  1527. res = Cstar;
  1528. } else if (exp == 0) {
  1529. // 1 <= q <= 10
  1530. // res = +/-C (exact)
  1531. if (x_sign)
  1532. res = -C1;
  1533. else
  1534. res = C1;
  1535. } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
  1536. // res = +/-C * 10^exp (exact)
  1537. if (x_sign)
  1538. res = -C1 * ten2k64[exp];
  1539. else
  1540. res = C1 * ten2k64[exp];
  1541. }
  1542. }
  1543. BID_RETURN (res);
  1544. }
  1545. /*****************************************************************************
  1546. * BID64_to_int32_int
  1547. ****************************************************************************/
  1548. #if DECIMAL_CALL_BY_REFERENCE
  1549. void
  1550. bid64_to_int32_int (int *pres,
  1551. UINT64 *
  1552. px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1553. _EXC_INFO_PARAM) {
  1554. UINT64 x = *px;
  1555. #else
  1556. int
  1557. bid64_to_int32_int (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1558. _EXC_INFO_PARAM) {
  1559. #endif
  1560. int res;
  1561. UINT64 x_sign;
  1562. UINT64 x_exp;
  1563. int exp; // unbiased exponent
  1564. // Note: C1 represents x_significand (UINT64)
  1565. UINT64 tmp64;
  1566. BID_UI64DOUBLE tmp1;
  1567. unsigned int x_nr_bits;
  1568. int q, ind, shift;
  1569. UINT64 C1;
  1570. UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
  1571. UINT128 P128;
  1572. // check for NaN or Infinity
  1573. if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
  1574. // set invalid flag
  1575. *pfpsf |= INVALID_EXCEPTION;
  1576. // return Integer Indefinite
  1577. res = 0x80000000;
  1578. BID_RETURN (res);
  1579. }
  1580. // unpack x
  1581. x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
  1582. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1583. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1584. x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
  1585. C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1586. if (C1 > 9999999999999999ull) { // non-canonical
  1587. x_exp = 0;
  1588. C1 = 0;
  1589. }
  1590. } else {
  1591. x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
  1592. C1 = x & MASK_BINARY_SIG1;
  1593. }
  1594. // check for zeros (possibly from non-canonical values)
  1595. if (C1 == 0x0ull) {
  1596. // x is 0
  1597. res = 0x00000000;
  1598. BID_RETURN (res);
  1599. }
  1600. // x is not special and is not zero
  1601. // q = nr. of decimal digits in x (1 <= q <= 54)
  1602. // determine first the nr. of bits in x
  1603. if (C1 >= 0x0020000000000000ull) { // x >= 2^53
  1604. // split the 64-bit value in two 32-bit halves to avoid rounding errors
  1605. if (C1 >= 0x0000000100000000ull) { // x >= 2^32
  1606. tmp1.d = (double) (C1 >> 32); // exact conversion
  1607. x_nr_bits =
  1608. 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  1609. } else { // x < 2^32
  1610. tmp1.d = (double) C1; // exact conversion
  1611. x_nr_bits =
  1612. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  1613. }
  1614. } else { // if x < 2^53
  1615. tmp1.d = (double) C1; // exact conversion
  1616. x_nr_bits =
  1617. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  1618. }
  1619. q = nr_digits[x_nr_bits - 1].digits;
  1620. if (q == 0) {
  1621. q = nr_digits[x_nr_bits - 1].digits1;
  1622. if (C1 >= nr_digits[x_nr_bits - 1].threshold_lo)
  1623. q++;
  1624. }
  1625. exp = x_exp - 398; // unbiased exponent
  1626. if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
  1627. // set invalid flag
  1628. *pfpsf |= INVALID_EXCEPTION;
  1629. // return Integer Indefinite
  1630. res = 0x80000000;
  1631. BID_RETURN (res);
  1632. } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
  1633. // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
  1634. // so x rounded to an integer may or may not fit in a signed 32-bit int
  1635. // the cases that do not fit are identified here; the ones that fit
  1636. // fall through and will be handled with other cases further,
  1637. // under '1 <= q + exp <= 10'
  1638. if (x_sign) { // if n < 0 and q + exp = 10
  1639. // if n <= -2^31 - 1 then n is too large
  1640. // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
  1641. // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
  1642. // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
  1643. if (q <= 11) {
  1644. // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
  1645. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  1646. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  1647. if (tmp64 >= 0x50000000aull) {
  1648. // set invalid flag
  1649. *pfpsf |= INVALID_EXCEPTION;
  1650. // return Integer Indefinite
  1651. res = 0x80000000;
  1652. BID_RETURN (res);
  1653. }
  1654. // else cases that can be rounded to a 32-bit int fall through
  1655. // to '1 <= q + exp <= 10'
  1656. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  1657. // C * 10^(11-q) >= 0x50000000a <=>
  1658. // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
  1659. // (scale 2^31+1 up)
  1660. // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
  1661. tmp64 = 0x50000000aull * ten2k64[q - 11];
  1662. if (C1 >= tmp64) {
  1663. // set invalid flag
  1664. *pfpsf |= INVALID_EXCEPTION;
  1665. // return Integer Indefinite
  1666. res = 0x80000000;
  1667. BID_RETURN (res);
  1668. }
  1669. // else cases that can be rounded to a 32-bit int fall through
  1670. // to '1 <= q + exp <= 10'
  1671. }
  1672. } else { // if n > 0 and q + exp = 10
  1673. // if n >= 2^31 then n is too large
  1674. // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
  1675. // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
  1676. // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
  1677. if (q <= 11) {
  1678. // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
  1679. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  1680. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  1681. if (tmp64 >= 0x500000000ull) {
  1682. // set invalid flag
  1683. *pfpsf |= INVALID_EXCEPTION;
  1684. // return Integer Indefinite
  1685. res = 0x80000000;
  1686. BID_RETURN (res);
  1687. }
  1688. // else cases that can be rounded to a 32-bit int fall through
  1689. // to '1 <= q + exp <= 10'
  1690. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  1691. // C * 10^(11-q) >= 0x500000000 <=>
  1692. // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
  1693. // (scale 2^31-1 up)
  1694. // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
  1695. tmp64 = 0x500000000ull * ten2k64[q - 11];
  1696. if (C1 >= tmp64) {
  1697. // set invalid flag
  1698. *pfpsf |= INVALID_EXCEPTION;
  1699. // return Integer Indefinite
  1700. res = 0x80000000;
  1701. BID_RETURN (res);
  1702. }
  1703. // else cases that can be rounded to a 32-bit int fall through
  1704. // to '1 <= q + exp <= 10'
  1705. }
  1706. }
  1707. }
  1708. // n is not too large to be converted to int32: -2^31 - 1 < n < 2^31
  1709. // Note: some of the cases tested for above fall through to this point
  1710. if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
  1711. // return 0
  1712. res = 0x00000000;
  1713. BID_RETURN (res);
  1714. } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
  1715. // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
  1716. // to nearest to a 32-bit signed integer
  1717. if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
  1718. ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
  1719. // chop off ind digits from the lower part of C1
  1720. // C1 fits in 64 bits
  1721. // calculate C* and f*
  1722. // C* is actually floor(C*) in this case
  1723. // C* and f* need shifting and masking, as shown by
  1724. // shiftright128[] and maskhigh128[]
  1725. // 1 <= x <= 15
  1726. // kx = 10^(-x) = ten2mk64[ind - 1]
  1727. // C* = C1 * 10^(-x)
  1728. // the approximation of 10^(-x) was rounded up to 54 bits
  1729. __mul_64x64_to_128MACH (P128, C1, ten2mk64[ind - 1]);
  1730. Cstar = P128.w[1];
  1731. // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
  1732. // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
  1733. // C* = floor(C*) (logical right shift; C has p decimal digits,
  1734. // correct by Property 1)
  1735. // n = C* * 10^(e+x)
  1736. // shift right C* by Ex-64 = shiftright128[ind]
  1737. shift = shiftright128[ind - 1]; // 0 <= shift <= 39
  1738. Cstar = Cstar >> shift;
  1739. if (x_sign)
  1740. res = -Cstar;
  1741. else
  1742. res = Cstar;
  1743. } else if (exp == 0) {
  1744. // 1 <= q <= 10
  1745. // res = +/-C (exact)
  1746. if (x_sign)
  1747. res = -C1;
  1748. else
  1749. res = C1;
  1750. } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
  1751. // res = +/-C * 10^exp (exact)
  1752. if (x_sign)
  1753. res = -C1 * ten2k64[exp];
  1754. else
  1755. res = C1 * ten2k64[exp];
  1756. }
  1757. }
  1758. BID_RETURN (res);
  1759. }
  1760. /*****************************************************************************
  1761. * BID64_to_int32_xint
  1762. ****************************************************************************/
  1763. #if DECIMAL_CALL_BY_REFERENCE
  1764. void
  1765. bid64_to_int32_xint (int *pres,
  1766. UINT64 *
  1767. px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1768. _EXC_INFO_PARAM) {
  1769. UINT64 x = *px;
  1770. #else
  1771. int
  1772. bid64_to_int32_xint (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  1773. _EXC_INFO_PARAM) {
  1774. #endif
  1775. int res;
  1776. UINT64 x_sign;
  1777. UINT64 x_exp;
  1778. int exp; // unbiased exponent
  1779. // Note: C1 represents x_significand (UINT64)
  1780. UINT64 tmp64;
  1781. BID_UI64DOUBLE tmp1;
  1782. unsigned int x_nr_bits;
  1783. int q, ind, shift;
  1784. UINT64 C1;
  1785. UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
  1786. UINT128 fstar;
  1787. UINT128 P128;
  1788. // check for NaN or Infinity
  1789. if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
  1790. // set invalid flag
  1791. *pfpsf |= INVALID_EXCEPTION;
  1792. // return Integer Indefinite
  1793. res = 0x80000000;
  1794. BID_RETURN (res);
  1795. }
  1796. // unpack x
  1797. x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
  1798. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  1799. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  1800. x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
  1801. C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  1802. if (C1 > 9999999999999999ull) { // non-canonical
  1803. x_exp = 0;
  1804. C1 = 0;
  1805. }
  1806. } else {
  1807. x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
  1808. C1 = x & MASK_BINARY_SIG1;
  1809. }
  1810. // check for zeros (possibly from non-canonical values)
  1811. if (C1 == 0x0ull) {
  1812. // x is 0
  1813. res = 0x00000000;
  1814. BID_RETURN (res);
  1815. }
  1816. // x is not special and is not zero
  1817. // q = nr. of decimal digits in x (1 <= q <= 54)
  1818. // determine first the nr. of bits in x
  1819. if (C1 >= 0x0020000000000000ull) { // x >= 2^53
  1820. // split the 64-bit value in two 32-bit halves to avoid rounding errors
  1821. if (C1 >= 0x0000000100000000ull) { // x >= 2^32
  1822. tmp1.d = (double) (C1 >> 32); // exact conversion
  1823. x_nr_bits =
  1824. 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  1825. } else { // x < 2^32
  1826. tmp1.d = (double) C1; // exact conversion
  1827. x_nr_bits =
  1828. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  1829. }
  1830. } else { // if x < 2^53
  1831. tmp1.d = (double) C1; // exact conversion
  1832. x_nr_bits =
  1833. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  1834. }
  1835. q = nr_digits[x_nr_bits - 1].digits;
  1836. if (q == 0) {
  1837. q = nr_digits[x_nr_bits - 1].digits1;
  1838. if (C1 >= nr_digits[x_nr_bits - 1].threshold_lo)
  1839. q++;
  1840. }
  1841. exp = x_exp - 398; // unbiased exponent
  1842. if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
  1843. // set invalid flag
  1844. *pfpsf |= INVALID_EXCEPTION;
  1845. // return Integer Indefinite
  1846. res = 0x80000000;
  1847. BID_RETURN (res);
  1848. } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
  1849. // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
  1850. // so x rounded to an integer may or may not fit in a signed 32-bit int
  1851. // the cases that do not fit are identified here; the ones that fit
  1852. // fall through and will be handled with other cases further,
  1853. // under '1 <= q + exp <= 10'
  1854. if (x_sign) { // if n < 0 and q + exp = 10
  1855. // if n <= -2^31 - 1 then n is too large
  1856. // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
  1857. // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
  1858. // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
  1859. if (q <= 11) {
  1860. // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
  1861. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  1862. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  1863. if (tmp64 >= 0x50000000aull) {
  1864. // set invalid flag
  1865. *pfpsf |= INVALID_EXCEPTION;
  1866. // return Integer Indefinite
  1867. res = 0x80000000;
  1868. BID_RETURN (res);
  1869. }
  1870. // else cases that can be rounded to a 32-bit int fall through
  1871. // to '1 <= q + exp <= 10'
  1872. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  1873. // C * 10^(11-q) >= 0x50000000a <=>
  1874. // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
  1875. // (scale 2^31+1 up)
  1876. // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
  1877. tmp64 = 0x50000000aull * ten2k64[q - 11];
  1878. if (C1 >= tmp64) {
  1879. // set invalid flag
  1880. *pfpsf |= INVALID_EXCEPTION;
  1881. // return Integer Indefinite
  1882. res = 0x80000000;
  1883. BID_RETURN (res);
  1884. }
  1885. // else cases that can be rounded to a 32-bit int fall through
  1886. // to '1 <= q + exp <= 10'
  1887. }
  1888. } else { // if n > 0 and q + exp = 10
  1889. // if n >= 2^31 then n is too large
  1890. // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
  1891. // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
  1892. // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
  1893. if (q <= 11) {
  1894. // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
  1895. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  1896. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  1897. if (tmp64 >= 0x500000000ull) {
  1898. // set invalid flag
  1899. *pfpsf |= INVALID_EXCEPTION;
  1900. // return Integer Indefinite
  1901. res = 0x80000000;
  1902. BID_RETURN (res);
  1903. }
  1904. // else cases that can be rounded to a 32-bit int fall through
  1905. // to '1 <= q + exp <= 10'
  1906. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  1907. // C * 10^(11-q) >= 0x500000000 <=>
  1908. // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
  1909. // (scale 2^31-1 up)
  1910. // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
  1911. tmp64 = 0x500000000ull * ten2k64[q - 11];
  1912. if (C1 >= tmp64) {
  1913. // set invalid flag
  1914. *pfpsf |= INVALID_EXCEPTION;
  1915. // return Integer Indefinite
  1916. res = 0x80000000;
  1917. BID_RETURN (res);
  1918. }
  1919. // else cases that can be rounded to a 32-bit int fall through
  1920. // to '1 <= q + exp <= 10'
  1921. }
  1922. }
  1923. }
  1924. // n is not too large to be converted to int32: -2^31 - 1 < n < 2^31
  1925. // Note: some of the cases tested for above fall through to this point
  1926. if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
  1927. // set inexact flag
  1928. *pfpsf |= INEXACT_EXCEPTION;
  1929. // return 0
  1930. res = 0x00000000;
  1931. BID_RETURN (res);
  1932. } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
  1933. // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
  1934. // to nearest to a 32-bit signed integer
  1935. if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
  1936. ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
  1937. // chop off ind digits from the lower part of C1
  1938. // C1 fits in 64 bits
  1939. // calculate C* and f*
  1940. // C* is actually floor(C*) in this case
  1941. // C* and f* need shifting and masking, as shown by
  1942. // shiftright128[] and maskhigh128[]
  1943. // 1 <= x <= 15
  1944. // kx = 10^(-x) = ten2mk64[ind - 1]
  1945. // C* = C1 * 10^(-x)
  1946. // the approximation of 10^(-x) was rounded up to 54 bits
  1947. __mul_64x64_to_128MACH (P128, C1, ten2mk64[ind - 1]);
  1948. Cstar = P128.w[1];
  1949. fstar.w[1] = P128.w[1] & maskhigh128[ind - 1];
  1950. fstar.w[0] = P128.w[0];
  1951. // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
  1952. // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
  1953. // C* = floor(C*) (logical right shift; C has p decimal digits,
  1954. // correct by Property 1)
  1955. // n = C* * 10^(e+x)
  1956. // shift right C* by Ex-64 = shiftright128[ind]
  1957. shift = shiftright128[ind - 1]; // 0 <= shift <= 39
  1958. Cstar = Cstar >> shift;
  1959. // determine inexactness of the rounding of C*
  1960. // if (0 < f* < 10^(-x)) then
  1961. // the result is exact
  1962. // else // if (f* > T*) then
  1963. // the result is inexact
  1964. if (ind - 1 <= 2) {
  1965. if (fstar.w[0] > ten2mk128trunc[ind - 1].w[1]) {
  1966. // ten2mk128trunc[ind -1].w[1] is identical to
  1967. // ten2mk128[ind -1].w[1]
  1968. // set the inexact flag
  1969. *pfpsf |= INEXACT_EXCEPTION;
  1970. } // else the result is exact
  1971. } else { // if 3 <= ind - 1 <= 14
  1972. if (fstar.w[1] || fstar.w[0] > ten2mk128trunc[ind - 1].w[1]) {
  1973. // ten2mk128trunc[ind -1].w[1] is identical to
  1974. // ten2mk128[ind -1].w[1]
  1975. // set the inexact flag
  1976. *pfpsf |= INEXACT_EXCEPTION;
  1977. } // else the result is exact
  1978. }
  1979. if (x_sign)
  1980. res = -Cstar;
  1981. else
  1982. res = Cstar;
  1983. } else if (exp == 0) {
  1984. // 1 <= q <= 10
  1985. // res = +/-C (exact)
  1986. if (x_sign)
  1987. res = -C1;
  1988. else
  1989. res = C1;
  1990. } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
  1991. // res = +/-C * 10^exp (exact)
  1992. if (x_sign)
  1993. res = -C1 * ten2k64[exp];
  1994. else
  1995. res = C1 * ten2k64[exp];
  1996. }
  1997. }
  1998. BID_RETURN (res);
  1999. }
  2000. /*****************************************************************************
  2001. * BID64_to_int32_rninta
  2002. ****************************************************************************/
  2003. #if DECIMAL_CALL_BY_REFERENCE
  2004. void
  2005. bid64_to_int32_rninta (int *pres,
  2006. UINT64 *
  2007. px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  2008. _EXC_INFO_PARAM) {
  2009. UINT64 x = *px;
  2010. #else
  2011. int
  2012. bid64_to_int32_rninta (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  2013. _EXC_INFO_PARAM) {
  2014. #endif
  2015. int res;
  2016. UINT64 x_sign;
  2017. UINT64 x_exp;
  2018. int exp; // unbiased exponent
  2019. // Note: C1 represents x_significand (UINT64)
  2020. UINT64 tmp64;
  2021. BID_UI64DOUBLE tmp1;
  2022. unsigned int x_nr_bits;
  2023. int q, ind, shift;
  2024. UINT64 C1;
  2025. UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
  2026. UINT128 P128;
  2027. // check for NaN or Infinity
  2028. if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
  2029. // set invalid flag
  2030. *pfpsf |= INVALID_EXCEPTION;
  2031. // return Integer Indefinite
  2032. res = 0x80000000;
  2033. BID_RETURN (res);
  2034. }
  2035. // unpack x
  2036. x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
  2037. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  2038. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  2039. x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
  2040. C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  2041. if (C1 > 9999999999999999ull) { // non-canonical
  2042. x_exp = 0;
  2043. C1 = 0;
  2044. }
  2045. } else {
  2046. x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
  2047. C1 = x & MASK_BINARY_SIG1;
  2048. }
  2049. // check for zeros (possibly from non-canonical values)
  2050. if (C1 == 0x0ull) {
  2051. // x is 0
  2052. res = 0x00000000;
  2053. BID_RETURN (res);
  2054. }
  2055. // x is not special and is not zero
  2056. // q = nr. of decimal digits in x (1 <= q <= 54)
  2057. // determine first the nr. of bits in x
  2058. if (C1 >= 0x0020000000000000ull) { // x >= 2^53
  2059. // split the 64-bit value in two 32-bit halves to avoid rounding errors
  2060. if (C1 >= 0x0000000100000000ull) { // x >= 2^32
  2061. tmp1.d = (double) (C1 >> 32); // exact conversion
  2062. x_nr_bits =
  2063. 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  2064. } else { // x < 2^32
  2065. tmp1.d = (double) C1; // exact conversion
  2066. x_nr_bits =
  2067. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  2068. }
  2069. } else { // if x < 2^53
  2070. tmp1.d = (double) C1; // exact conversion
  2071. x_nr_bits =
  2072. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  2073. }
  2074. q = nr_digits[x_nr_bits - 1].digits;
  2075. if (q == 0) {
  2076. q = nr_digits[x_nr_bits - 1].digits1;
  2077. if (C1 >= nr_digits[x_nr_bits - 1].threshold_lo)
  2078. q++;
  2079. }
  2080. exp = x_exp - 398; // unbiased exponent
  2081. if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
  2082. // set invalid flag
  2083. *pfpsf |= INVALID_EXCEPTION;
  2084. // return Integer Indefinite
  2085. res = 0x80000000;
  2086. BID_RETURN (res);
  2087. } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
  2088. // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
  2089. // so x rounded to an integer may or may not fit in a signed 32-bit int
  2090. // the cases that do not fit are identified here; the ones that fit
  2091. // fall through and will be handled with other cases further,
  2092. // under '1 <= q + exp <= 10'
  2093. if (x_sign) { // if n < 0 and q + exp = 10
  2094. // if n <= -2^31 - 1/2 then n is too large
  2095. // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1/2
  2096. // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000005, 1<=q<=16
  2097. // <=> C * 10^(11-q) >= 0x500000005, 1<=q<=16
  2098. if (q <= 11) {
  2099. // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
  2100. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  2101. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  2102. if (tmp64 >= 0x500000005ull) {
  2103. // set invalid flag
  2104. *pfpsf |= INVALID_EXCEPTION;
  2105. // return Integer Indefinite
  2106. res = 0x80000000;
  2107. BID_RETURN (res);
  2108. }
  2109. // else cases that can be rounded to a 32-bit int fall through
  2110. // to '1 <= q + exp <= 10'
  2111. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  2112. // C * 10^(11-q) >= 0x500000005 <=>
  2113. // C >= 0x500000005 * 10^(q-11) where 1 <= q - 11 <= 5
  2114. // (scale 2^31+1/2 up)
  2115. // Note: 0x500000005*10^(q-11) has q-1 or q digits, where q <= 16
  2116. tmp64 = 0x500000005ull * ten2k64[q - 11];
  2117. if (C1 >= tmp64) {
  2118. // set invalid flag
  2119. *pfpsf |= INVALID_EXCEPTION;
  2120. // return Integer Indefinite
  2121. res = 0x80000000;
  2122. BID_RETURN (res);
  2123. }
  2124. // else cases that can be rounded to a 32-bit int fall through
  2125. // to '1 <= q + exp <= 10'
  2126. }
  2127. } else { // if n > 0 and q + exp = 10
  2128. // if n >= 2^31 - 1/2 then n is too large
  2129. // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31-1/2
  2130. // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x4fffffffb, 1<=q<=16
  2131. // <=> C * 10^(11-q) >= 0x4fffffffb, 1<=q<=16
  2132. if (q <= 11) {
  2133. // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
  2134. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  2135. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  2136. if (tmp64 >= 0x4fffffffbull) {
  2137. // set invalid flag
  2138. *pfpsf |= INVALID_EXCEPTION;
  2139. // return Integer Indefinite
  2140. res = 0x80000000;
  2141. BID_RETURN (res);
  2142. }
  2143. // else cases that can be rounded to a 32-bit int fall through
  2144. // to '1 <= q + exp <= 10'
  2145. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  2146. // C * 10^(11-q) >= 0x4fffffffb <=>
  2147. // C >= 0x4fffffffb * 10^(q-11) where 1 <= q - 11 <= 5
  2148. // (scale 2^31-1/2 up)
  2149. // Note: 0x4fffffffb*10^(q-11) has q-1 or q digits, where q <= 16
  2150. tmp64 = 0x4fffffffbull * ten2k64[q - 11];
  2151. if (C1 >= tmp64) {
  2152. // set invalid flag
  2153. *pfpsf |= INVALID_EXCEPTION;
  2154. // return Integer Indefinite
  2155. res = 0x80000000;
  2156. BID_RETURN (res);
  2157. }
  2158. // else cases that can be rounded to a 32-bit int fall through
  2159. // to '1 <= q + exp <= 10'
  2160. }
  2161. }
  2162. }
  2163. // n is not too large to be converted to int32: -2^31 - 1/2 < n < 2^31 - 1/2
  2164. // Note: some of the cases tested for above fall through to this point
  2165. if ((q + exp) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
  2166. // return 0
  2167. res = 0x00000000;
  2168. BID_RETURN (res);
  2169. } else if ((q + exp) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
  2170. // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
  2171. // res = 0
  2172. // else
  2173. // res = +/-1
  2174. ind = q - 1;
  2175. if (C1 < midpoint64[ind]) {
  2176. res = 0x00000000; // return 0
  2177. } else if (x_sign) { // n < 0
  2178. res = 0xffffffff; // return -1
  2179. } else { // n > 0
  2180. res = 0x00000001; // return +1
  2181. }
  2182. } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
  2183. // -2^31-1/2 <= x <= -1 or 1 <= x < 2^31-1/2 so x can be rounded
  2184. // to nearest away to a 32-bit signed integer
  2185. if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
  2186. ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
  2187. // chop off ind digits from the lower part of C1
  2188. // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
  2189. C1 = C1 + midpoint64[ind - 1];
  2190. // calculate C* and f*
  2191. // C* is actually floor(C*) in this case
  2192. // C* and f* need shifting and masking, as shown by
  2193. // shiftright128[] and maskhigh128[]
  2194. // 1 <= x <= 15
  2195. // kx = 10^(-x) = ten2mk64[ind - 1]
  2196. // C* = (C1 + 1/2 * 10^x) * 10^(-x)
  2197. // the approximation of 10^(-x) was rounded up to 54 bits
  2198. __mul_64x64_to_128MACH (P128, C1, ten2mk64[ind - 1]);
  2199. Cstar = P128.w[1];
  2200. // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
  2201. // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
  2202. // C* = floor(C*)-1 (logical right shift; C* has p decimal digits,
  2203. // correct by Pr. 1)
  2204. // n = C* * 10^(e+x)
  2205. // shift right C* by Ex-64 = shiftright128[ind]
  2206. shift = shiftright128[ind - 1]; // 0 <= shift <= 39
  2207. Cstar = Cstar >> shift;
  2208. // if the result was a midpoint it was rounded away from zero
  2209. if (x_sign)
  2210. res = -Cstar;
  2211. else
  2212. res = Cstar;
  2213. } else if (exp == 0) {
  2214. // 1 <= q <= 10
  2215. // res = +/-C (exact)
  2216. if (x_sign)
  2217. res = -C1;
  2218. else
  2219. res = C1;
  2220. } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
  2221. // res = +/-C * 10^exp (exact)
  2222. if (x_sign)
  2223. res = -C1 * ten2k64[exp];
  2224. else
  2225. res = C1 * ten2k64[exp];
  2226. }
  2227. }
  2228. BID_RETURN (res);
  2229. }
  2230. /*****************************************************************************
  2231. * BID64_to_int32_xrninta
  2232. ****************************************************************************/
  2233. #if DECIMAL_CALL_BY_REFERENCE
  2234. void
  2235. bid64_to_int32_xrninta (int *pres,
  2236. UINT64 *
  2237. px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  2238. _EXC_INFO_PARAM) {
  2239. UINT64 x = *px;
  2240. #else
  2241. int
  2242. bid64_to_int32_xrninta (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
  2243. _EXC_INFO_PARAM) {
  2244. #endif
  2245. int res;
  2246. UINT64 x_sign;
  2247. UINT64 x_exp;
  2248. int exp; // unbiased exponent
  2249. // Note: C1 represents x_significand (UINT64)
  2250. UINT64 tmp64;
  2251. BID_UI64DOUBLE tmp1;
  2252. unsigned int x_nr_bits;
  2253. int q, ind, shift;
  2254. UINT64 C1;
  2255. UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
  2256. UINT128 fstar;
  2257. UINT128 P128;
  2258. // check for NaN or Infinity
  2259. if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
  2260. // set invalid flag
  2261. *pfpsf |= INVALID_EXCEPTION;
  2262. // return Integer Indefinite
  2263. res = 0x80000000;
  2264. BID_RETURN (res);
  2265. }
  2266. // unpack x
  2267. x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
  2268. // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
  2269. if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
  2270. x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
  2271. C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
  2272. if (C1 > 9999999999999999ull) { // non-canonical
  2273. x_exp = 0;
  2274. C1 = 0;
  2275. }
  2276. } else {
  2277. x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
  2278. C1 = x & MASK_BINARY_SIG1;
  2279. }
  2280. // check for zeros (possibly from non-canonical values)
  2281. if (C1 == 0x0ull) {
  2282. // x is 0
  2283. res = 0x00000000;
  2284. BID_RETURN (res);
  2285. }
  2286. // x is not special and is not zero
  2287. // q = nr. of decimal digits in x (1 <= q <= 54)
  2288. // determine first the nr. of bits in x
  2289. if (C1 >= 0x0020000000000000ull) { // x >= 2^53
  2290. // split the 64-bit value in two 32-bit halves to avoid rounding errors
  2291. if (C1 >= 0x0000000100000000ull) { // x >= 2^32
  2292. tmp1.d = (double) (C1 >> 32); // exact conversion
  2293. x_nr_bits =
  2294. 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  2295. } else { // x < 2^32
  2296. tmp1.d = (double) C1; // exact conversion
  2297. x_nr_bits =
  2298. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  2299. }
  2300. } else { // if x < 2^53
  2301. tmp1.d = (double) C1; // exact conversion
  2302. x_nr_bits =
  2303. 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
  2304. }
  2305. q = nr_digits[x_nr_bits - 1].digits;
  2306. if (q == 0) {
  2307. q = nr_digits[x_nr_bits - 1].digits1;
  2308. if (C1 >= nr_digits[x_nr_bits - 1].threshold_lo)
  2309. q++;
  2310. }
  2311. exp = x_exp - 398; // unbiased exponent
  2312. if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
  2313. // set invalid flag
  2314. *pfpsf |= INVALID_EXCEPTION;
  2315. // return Integer Indefinite
  2316. res = 0x80000000;
  2317. BID_RETURN (res);
  2318. } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
  2319. // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
  2320. // so x rounded to an integer may or may not fit in a signed 32-bit int
  2321. // the cases that do not fit are identified here; the ones that fit
  2322. // fall through and will be handled with other cases further,
  2323. // under '1 <= q + exp <= 10'
  2324. if (x_sign) { // if n < 0 and q + exp = 10
  2325. // if n <= -2^31 - 1/2 then n is too large
  2326. // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1/2
  2327. // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000005, 1<=q<=16
  2328. // <=> C * 10^(11-q) >= 0x500000005, 1<=q<=16
  2329. if (q <= 11) {
  2330. // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
  2331. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  2332. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  2333. if (tmp64 >= 0x500000005ull) {
  2334. // set invalid flag
  2335. *pfpsf |= INVALID_EXCEPTION;
  2336. // return Integer Indefinite
  2337. res = 0x80000000;
  2338. BID_RETURN (res);
  2339. }
  2340. // else cases that can be rounded to a 32-bit int fall through
  2341. // to '1 <= q + exp <= 10'
  2342. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  2343. // C * 10^(11-q) >= 0x500000005 <=>
  2344. // C >= 0x500000005 * 10^(q-11) where 1 <= q - 11 <= 5
  2345. // (scale 2^31+1/2 up)
  2346. // Note: 0x500000005*10^(q-11) has q-1 or q digits, where q <= 16
  2347. tmp64 = 0x500000005ull * ten2k64[q - 11];
  2348. if (C1 >= tmp64) {
  2349. // set invalid flag
  2350. *pfpsf |= INVALID_EXCEPTION;
  2351. // return Integer Indefinite
  2352. res = 0x80000000;
  2353. BID_RETURN (res);
  2354. }
  2355. // else cases that can be rounded to a 32-bit int fall through
  2356. // to '1 <= q + exp <= 10'
  2357. }
  2358. } else { // if n > 0 and q + exp = 10
  2359. // if n >= 2^31 - 1/2 then n is too large
  2360. // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31-1/2
  2361. // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x4fffffffb, 1<=q<=16
  2362. // <=> C * 10^(11-q) >= 0x4fffffffb, 1<=q<=16
  2363. if (q <= 11) {
  2364. // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
  2365. tmp64 = C1 * ten2k64[11 - q]; // C scaled up to 11-digit int
  2366. // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
  2367. if (tmp64 >= 0x4fffffffbull) {
  2368. // set invalid flag
  2369. *pfpsf |= INVALID_EXCEPTION;
  2370. // return Integer Indefinite
  2371. res = 0x80000000;
  2372. BID_RETURN (res);
  2373. }
  2374. // else cases that can be rounded to a 32-bit int fall through
  2375. // to '1 <= q + exp <= 10'
  2376. } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
  2377. // C * 10^(11-q) >= 0x4fffffffb <=>
  2378. // C >= 0x4fffffffb * 10^(q-11) where 1 <= q - 11 <= 5
  2379. // (scale 2^31-1/2 up)
  2380. // Note: 0x4fffffffb*10^(q-11) has q-1 or q digits, where q <= 16
  2381. tmp64 = 0x4fffffffbull * ten2k64[q - 11];
  2382. if (C1 >= tmp64) {
  2383. // set invalid flag
  2384. *pfpsf |= INVALID_EXCEPTION;
  2385. // return Integer Indefinite
  2386. res = 0x80000000;
  2387. BID_RETURN (res);
  2388. }
  2389. // else cases that can be rounded to a 32-bit int fall through
  2390. // to '1 <= q + exp <= 10'
  2391. }
  2392. }
  2393. }
  2394. // n is not too large to be converted to int32: -2^31 - 1/2 < n < 2^31 - 1/2
  2395. // Note: some of the cases tested for above fall through to this point
  2396. if ((q + exp) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
  2397. // set inexact flag
  2398. *pfpsf |= INEXACT_EXCEPTION;
  2399. // return 0
  2400. res = 0x00000000;
  2401. BID_RETURN (res);
  2402. } else if ((q + exp) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
  2403. // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
  2404. // res = 0
  2405. // else
  2406. // res = +/-1
  2407. ind = q - 1;
  2408. if (C1 < midpoint64[ind]) {
  2409. res = 0x00000000; // return 0
  2410. } else if (x_sign) { // n < 0
  2411. res = 0xffffffff; // return -1
  2412. } else { // n > 0
  2413. res = 0x00000001; // return +1
  2414. }
  2415. // set inexact flag
  2416. *pfpsf |= INEXACT_EXCEPTION;
  2417. } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
  2418. // -2^31-1/2 <= x <= -1 or 1 <= x < 2^31-1/2 so x can be rounded
  2419. // to nearest away to a 32-bit signed integer
  2420. if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
  2421. ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
  2422. // chop off ind digits from the lower part of C1
  2423. // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
  2424. C1 = C1 + midpoint64[ind - 1];
  2425. // calculate C* and f*
  2426. // C* is actually floor(C*) in this case
  2427. // C* and f* need shifting and masking, as shown by
  2428. // shiftright128[] and maskhigh128[]
  2429. // 1 <= x <= 15
  2430. // kx = 10^(-x) = ten2mk64[ind - 1]
  2431. // C* = (C1 + 1/2 * 10^x) * 10^(-x)
  2432. // the approximation of 10^(-x) was rounded up to 54 bits
  2433. __mul_64x64_to_128MACH (P128, C1, ten2mk64[ind - 1]);
  2434. Cstar = P128.w[1];
  2435. fstar.w[1] = P128.w[1] & maskhigh128[ind - 1];
  2436. fstar.w[0] = P128.w[0];
  2437. // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
  2438. // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
  2439. // C* = floor(C*)-1 (logical right shift; C* has p decimal digits,
  2440. // correct by Pr. 1)
  2441. // n = C* * 10^(e+x)
  2442. // shift right C* by Ex-64 = shiftright128[ind]
  2443. shift = shiftright128[ind - 1]; // 0 <= shift <= 39
  2444. Cstar = Cstar >> shift;
  2445. // determine inexactness of the rounding of C*
  2446. // if (0 < f* - 1/2 < 10^(-x)) then
  2447. // the result is exact
  2448. // else // if (f* - 1/2 > T*) then
  2449. // the result is inexact
  2450. if (ind - 1 <= 2) {
  2451. if (fstar.w[0] > 0x8000000000000000ull) {
  2452. // f* > 1/2 and the result may be exact
  2453. tmp64 = fstar.w[0] - 0x8000000000000000ull; // f* - 1/2
  2454. if ((tmp64 > ten2mk128trunc[ind - 1].w[1])) {
  2455. // ten2mk128trunc[ind -1].w[1] is identical to
  2456. // ten2mk128[ind -1].w[1]
  2457. // set the inexact flag
  2458. *pfpsf |= INEXACT_EXCEPTION;
  2459. } // else the result is exact
  2460. } else { // the result is inexact; f2* <= 1/2
  2461. // set the inexact flag
  2462. *pfpsf |= INEXACT_EXCEPTION;
  2463. }
  2464. } else { // if 3 <= ind - 1 <= 14
  2465. if (fstar.w[1] > onehalf128[ind - 1] ||
  2466. (fstar.w[1] == onehalf128[ind - 1] && fstar.w[0])) {
  2467. // f2* > 1/2 and the result may be exact
  2468. // Calculate f2* - 1/2
  2469. tmp64 = fstar.w[1] - onehalf128[ind - 1];
  2470. if (tmp64 || fstar.w[0] > ten2mk128trunc[ind - 1].w[1]) {
  2471. // ten2mk128trunc[ind -1].w[1] is identical to
  2472. // ten2mk128[ind -1].w[1]
  2473. // set the inexact flag
  2474. *pfpsf |= INEXACT_EXCEPTION;
  2475. } // else the result is exact
  2476. } else { // the result is inexact; f2* <= 1/2
  2477. // set the inexact flag
  2478. *pfpsf |= INEXACT_EXCEPTION;
  2479. }
  2480. }
  2481. // if the result was a midpoint it was rounded away from zero
  2482. if (x_sign)
  2483. res = -Cstar;
  2484. else
  2485. res = Cstar;
  2486. } else if (exp == 0) {
  2487. // 1 <= q <= 10
  2488. // res = +/-C (exact)
  2489. if (x_sign)
  2490. res = -C1;
  2491. else
  2492. res = C1;
  2493. } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
  2494. // res = +/-C * 10^exp (exact)
  2495. if (x_sign)
  2496. res = -C1 * ten2k64[exp];
  2497. else
  2498. res = C1 * ten2k64[exp];
  2499. }
  2500. }
  2501. BID_RETURN (res);
  2502. }