bid64_to_uint64.c 80 KB

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