bid2dpd_dpd2bid.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* Copyright (C) 2007-2013 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. #undef IN_LIBGCC2
  19. #include "bid-dpd.h"
  20. /* get full 64x64bit product */
  21. #define __mul_64x64_to_128(P, CX, CY) \
  22. { \
  23. UINT64 CXH, CXL, CYH,CYL,PL,PH,PM,PM2; \
  24. CXH = (CX) >> 32; \
  25. CXL = (UINT32)(CX); \
  26. CYH = (CY) >> 32; \
  27. CYL = (UINT32)(CY); \
  28. \
  29. PM = CXH*CYL; \
  30. PH = CXH*CYH; \
  31. PL = CXL*CYL; \
  32. PM2 = CXL*CYH; \
  33. PH += (PM>>32); \
  34. PM = (UINT64)((UINT32)PM)+PM2+(PL>>32); \
  35. \
  36. (P).w[1] = PH + (PM>>32); \
  37. (P).w[0] = (PM<<32)+(UINT32)PL; \
  38. }
  39. /* add 64-bit value to 128-bit */
  40. #define __add_128_64(R128, A128, B64) \
  41. { \
  42. UINT64 R64H; \
  43. R64H = (A128).w[1]; \
  44. (R128).w[0] = (B64) + (A128).w[0]; \
  45. if((R128).w[0] < (B64)) R64H ++; \
  46. (R128).w[1] = R64H; \
  47. }
  48. /* add 128-bit value to 128-bit (assume no carry-out) */
  49. #define __add_128_128(R128, A128, B128) \
  50. { \
  51. UINT128 Q128; \
  52. Q128.w[1] = (A128).w[1]+(B128).w[1]; \
  53. Q128.w[0] = (B128).w[0] + (A128).w[0]; \
  54. if(Q128.w[0] < (B128).w[0]) Q128.w[1] ++; \
  55. (R128).w[1] = Q128.w[1]; \
  56. (R128).w[0] = Q128.w[0]; \
  57. }
  58. #define __mul_128x128_high(Q, A, B) \
  59. { \
  60. UINT128 ALBL, ALBH, AHBL, AHBH, QM, QM2; \
  61. \
  62. __mul_64x64_to_128(ALBH, (A).w[0], (B).w[1]); \
  63. __mul_64x64_to_128(AHBL, (B).w[0], (A).w[1]); \
  64. __mul_64x64_to_128(ALBL, (A).w[0], (B).w[0]); \
  65. __mul_64x64_to_128(AHBH, (A).w[1],(B).w[1]); \
  66. \
  67. __add_128_128(QM, ALBH, AHBL); \
  68. __add_128_64(QM2, QM, ALBL.w[1]); \
  69. __add_128_64((Q), AHBH, QM2.w[1]); \
  70. }
  71. #include "bid2dpd_dpd2bid.h"
  72. static const unsigned int dm103[] =
  73. { 0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 11000 };
  74. void _bid_to_dpd32 (_Decimal32 *, _Decimal32 *);
  75. void
  76. _bid_to_dpd32 (_Decimal32 *pres, _Decimal32 *px) {
  77. unsigned int sign, coefficient_x, exp, dcoeff;
  78. unsigned int b2, b1, b0, b01, res;
  79. _Decimal32 x = *px;
  80. sign = (x & 0x80000000);
  81. if ((x & 0x60000000ul) == 0x60000000ul) {
  82. /* special encodings */
  83. if ((x & 0x78000000ul) == 0x78000000ul) {
  84. *pres = x; /* NaN or Infinity */
  85. return;
  86. }
  87. /* coefficient */
  88. coefficient_x = (x & 0x001ffffful) | 0x00800000ul;
  89. if (coefficient_x >= 10000000) coefficient_x = 0;
  90. /* get exponent */
  91. exp = (x >> 21) & 0xff;
  92. } else {
  93. exp = (x >> 23) & 0xff;
  94. coefficient_x = (x & 0x007ffffful);
  95. }
  96. b01 = coefficient_x / 1000;
  97. b2 = coefficient_x - 1000 * b01;
  98. b0 = b01 / 1000;
  99. b1 = b01 - 1000 * b0;
  100. dcoeff = b2d[b2] | b2d2[b1];
  101. if (b0 >= 8) { /* is b0 8 or 9? */
  102. res = sign | ((0x600 | ((exp >> 6) << 7) |
  103. ((b0 & 1) << 6) | (exp & 0x3f)) << 20) | dcoeff;
  104. } else { /* else b0 is 0..7 */
  105. res = sign | ((((exp >> 6) << 9) | (b0 << 6) |
  106. (exp & 0x3f)) << 20) | dcoeff;
  107. }
  108. *pres = res;
  109. }
  110. void _dpd_to_bid32 (_Decimal32 *, _Decimal32 *);
  111. void
  112. _dpd_to_bid32 (_Decimal32 *pres, _Decimal32 *px) {
  113. unsigned int r;
  114. unsigned int sign, exp, bcoeff;
  115. UINT64 trailing;
  116. unsigned int d0, d1, d2;
  117. _Decimal32 x = *px;
  118. sign = (x & 0x80000000);
  119. trailing = (x & 0x000fffff);
  120. if ((x & 0x78000000) == 0x78000000) {
  121. *pres = x;
  122. return;
  123. } else { /* normal number */
  124. if ((x & 0x60000000) == 0x60000000) { /* G0..G1 = 11 -> d0 = 8 + G4 */
  125. d0 = d2b3[((x >> 26) & 1) | 8]; /* d0 = (comb & 0x0100 ? 9 : 8); */
  126. exp = (x >> 27) & 3; /* exp leading bits are G2..G3 */
  127. } else {
  128. d0 = d2b3[(x >> 26) & 0x7];
  129. exp = (x >> 29) & 3; /* exp loading bits are G0..G1 */
  130. }
  131. d1 = d2b2[(trailing >> 10) & 0x3ff];
  132. d2 = d2b[(trailing) & 0x3ff];
  133. bcoeff = d2 + d1 + d0;
  134. exp = (exp << 6) + ((x >> 20) & 0x3f);
  135. if (bcoeff < (1 << 23)) {
  136. r = exp;
  137. r <<= 23;
  138. r |= (bcoeff | sign);
  139. } else {
  140. r = exp;
  141. r <<= 21;
  142. r |= (sign | 0x60000000ul);
  143. /* add coeff, without leading bits */
  144. r |= (((unsigned int) bcoeff) & 0x1fffff);
  145. }
  146. }
  147. *pres = r;
  148. }
  149. void _bid_to_dpd64 (_Decimal64 *, _Decimal64 *);
  150. void
  151. _bid_to_dpd64 (_Decimal64 *pres, _Decimal64 *px) {
  152. UINT64 res;
  153. UINT64 sign, comb, exp, B34, B01;
  154. UINT64 d103, D61;
  155. UINT64 b0, b2, b3, b5;
  156. unsigned int b1, b4;
  157. UINT64 bcoeff;
  158. UINT64 dcoeff;
  159. unsigned int yhi, ylo;
  160. _Decimal64 x = *px;
  161. sign = (x & 0x8000000000000000ull);
  162. comb = (x & 0x7ffc000000000000ull) >> 51;
  163. if ((comb & 0xf00) == 0xf00) {
  164. *pres = x;
  165. return;
  166. } else { /* Normal number */
  167. if ((comb & 0xc00) == 0xc00) { /* G0..G1 = 11 -> exp is G2..G11 */
  168. exp = (comb) & 0x3ff;
  169. bcoeff = (x & 0x0007ffffffffffffull) | 0x0020000000000000ull;
  170. } else {
  171. exp = (comb >> 2) & 0x3ff;
  172. bcoeff = (x & 0x001fffffffffffffull);
  173. }
  174. D61 = 2305843009ull; /* Floor(2^61 / 10^9) */
  175. /* Multiply the binary coefficient by ceil(2^64 / 1000), and take the upper
  176. 64-bits in order to compute a division by 1000. */
  177. yhi = (D61 * (UINT64)(bcoeff >> (UINT64)27)) >> (UINT64)34;
  178. ylo = bcoeff - 1000000000ull * yhi;
  179. if (ylo >= 1000000000) {
  180. ylo = ylo - 1000000000;
  181. yhi = yhi + 1;
  182. }
  183. d103 = 0x4189374c;
  184. B34 = ((UINT64) ylo * d103) >> (32 + 8);
  185. B01 = ((UINT64) yhi * d103) >> (32 + 8);
  186. b5 = ylo - B34 * 1000;
  187. b2 = yhi - B01 * 1000;
  188. b3 = ((UINT64) B34 * d103) >> (32 + 8);
  189. b0 = ((UINT64) B01 * d103) >> (32 + 8);
  190. b4 = (unsigned int) B34 - (unsigned int) b3 *1000;
  191. b1 = (unsigned int) B01 - (unsigned int) dm103[b0];
  192. dcoeff = b2d[b5] | b2d2[b4] | b2d3[b3] | b2d4[b2] | b2d5[b1];
  193. if (b0 >= 8) /* is b0 8 or 9? */
  194. res = sign | ((0x1800 | ((exp >> 8) << 9) | ((b0 & 1) << 8) |
  195. (exp & 0xff)) << 50) | dcoeff;
  196. else /* else b0 is 0..7 */
  197. res = sign | ((((exp >> 8) << 11) | (b0 << 8) |
  198. (exp & 0xff)) << 50) | dcoeff;
  199. }
  200. *pres = res;
  201. }
  202. void _dpd_to_bid64 (_Decimal64 *, _Decimal64 *);
  203. void
  204. _dpd_to_bid64 (_Decimal64 *pres, _Decimal64 *px) {
  205. UINT64 res;
  206. UINT64 sign, comb, exp;
  207. UINT64 trailing;
  208. UINT64 d0, d1, d2;
  209. unsigned int d3, d4, d5;
  210. UINT64 bcoeff, mask;
  211. _Decimal64 x = *px;
  212. sign = (x & 0x8000000000000000ull);
  213. comb = (x & 0x7ffc000000000000ull) >> 50;
  214. trailing = (x & 0x0003ffffffffffffull);
  215. if ((comb & 0x1e00) == 0x1e00) {
  216. if ((comb & 0x1f00) == 0x1f00) { /* G0..G4 = 11111 -> NaN */
  217. if (comb & 0x0100) { /* G5 = 1 -> sNaN */
  218. *pres = x;
  219. } else { /* G5 = 0 -> qNaN */
  220. *pres = x;
  221. }
  222. } else { /*if ((comb & 0x1e00) == 0x1e00); G0..G4 = 11110 -> INF */
  223. *pres = x;
  224. }
  225. return;
  226. } else { /* normal number */
  227. if ((comb & 0x1800) == 0x1800) { /* G0..G1 = 11 -> d0 = 8 + G4 */
  228. d0 = d2b6[((comb >> 8) & 1) | 8]; /* d0 = (comb & 0x0100 ? 9 : 8); */
  229. exp = (comb & 0x600) >> 1; /* exp = (comb & 0x0400 ? 1 : 0) * 0x200 +
  230. (comb & 0x0200 ? 1 : 0) * 0x100; exp leading bits are G2..G3 */
  231. } else {
  232. d0 = d2b6[(comb >> 8) & 0x7];
  233. exp = (comb & 0x1800) >> 3; /* exp = (comb & 0x1000 ? 1 : 0) * 0x200 +
  234. (comb & 0x0800 ? 1 : 0) * 0x100; exp loading bits are G0..G1 */
  235. }
  236. d1 = d2b5[(trailing >> 40) & 0x3ff];
  237. d2 = d2b4[(trailing >> 30) & 0x3ff];
  238. d3 = d2b3[(trailing >> 20) & 0x3ff];
  239. d4 = d2b2[(trailing >> 10) & 0x3ff];
  240. d5 = d2b[(trailing) & 0x3ff];
  241. bcoeff = (d5 + d4 + d3) + d2 + d1 + d0;
  242. exp += (comb & 0xff);
  243. mask = 1;
  244. mask <<= 53;
  245. if (bcoeff < mask) { /* check whether coefficient fits in 10*5+3 bits */
  246. res = exp;
  247. res <<= 53;
  248. res |= (bcoeff | sign);
  249. *pres = res;
  250. return;
  251. }
  252. /* special format */
  253. res = (exp << 51) | (sign | 0x6000000000000000ull);
  254. /* add coeff, without leading bits */
  255. mask = (mask >> 2) - 1;
  256. bcoeff &= mask;
  257. res |= bcoeff;
  258. }
  259. *pres = res;
  260. }
  261. void _bid_to_dpd128 (_Decimal128 *, _Decimal128 *);
  262. void
  263. _bid_to_dpd128 (_Decimal128 *pres, _Decimal128 *px) {
  264. UINT128 res;
  265. UINT128 sign;
  266. unsigned int comb;
  267. UINT128 bcoeff;
  268. UINT128 dcoeff;
  269. UINT128 BH, d1018, BT2, BT1;
  270. UINT64 exp, BL, d109;
  271. UINT64 d106, d103;
  272. UINT64 k1, k2, k4, k5, k7, k8, k10, k11;
  273. unsigned int BHH32, BLL32, BHL32, BLH32, k0, k3, k6, k9, amount;
  274. _Decimal128 x = *px;
  275. sign.w[1] = (x.w[1] & 0x8000000000000000ull);
  276. sign.w[0] = 0;
  277. comb = (x.w[1] /*& 0x7fffc00000000000ull */ ) >> 46;
  278. exp = 0;
  279. if ((comb & 0x1e000) == 0x1e000) {
  280. if ((comb & 0x1f000) == 0x1f000) { /* G0..G4 = 11111 -> NaN */
  281. if (comb & 0x01000) { /* G5 = 1 -> sNaN */
  282. res = x;
  283. } else { /* G5 = 0 -> qNaN */
  284. res = x;
  285. }
  286. } else { /* G0..G4 = 11110 -> INF */
  287. res = x;
  288. }
  289. } else { /* normal number */
  290. exp = ((x.w[1] & 0x7fff000000000000ull) >> 49) & 0x3fff;
  291. bcoeff.w[1] = (x.w[1] & 0x0001ffffffffffffull);
  292. bcoeff.w[0] = x.w[0];
  293. d1018 = reciprocals10_128[18];
  294. __mul_128x128_high (BH, bcoeff, d1018);
  295. amount = recip_scale[18];
  296. BH.w[0] = (BH.w[0] >> amount) | (BH.w[1] << (64 - amount));
  297. BL = bcoeff.w[0] - BH.w[0] * 1000000000000000000ull;
  298. d109 = reciprocals10_64[9];
  299. __mul_64x64_to_128 (BT1, BH.w[0], d109);
  300. BHH32 = (unsigned int) (BT1.w[1] >> short_recip_scale[9]);
  301. BHL32 = (unsigned int) BH.w[0] - BHH32 * 1000000000;
  302. __mul_64x64_to_128 (BT2, BL, d109);
  303. BLH32 = (unsigned int) (BT2.w[1] >> short_recip_scale[9]);
  304. BLL32 = (unsigned int) BL - BLH32 * 1000000000;
  305. d106 = 0x431BDE83;
  306. d103 = 0x4189374c;
  307. k0 = ((UINT64) BHH32 * d106) >> (32 + 18);
  308. BHH32 -= (unsigned int) k0 *1000000;
  309. k1 = ((UINT64) BHH32 * d103) >> (32 + 8);
  310. k2 = BHH32 - (unsigned int) k1 *1000;
  311. k3 = ((UINT64) BHL32 * d106) >> (32 + 18);
  312. BHL32 -= (unsigned int) k3 *1000000;
  313. k4 = ((UINT64) BHL32 * d103) >> (32 + 8);
  314. k5 = BHL32 - (unsigned int) k4 *1000;
  315. k6 = ((UINT64) BLH32 * d106) >> (32 + 18);
  316. BLH32 -= (unsigned int) k6 *1000000;
  317. k7 = ((UINT64) BLH32 * d103) >> (32 + 8);
  318. k8 = BLH32 - (unsigned int) k7 *1000;
  319. k9 = ((UINT64) BLL32 * d106) >> (32 + 18);
  320. BLL32 -= (unsigned int) k9 *1000000;
  321. k10 = ((UINT64) BLL32 * d103) >> (32 + 8);
  322. k11 = BLL32 - (unsigned int) k10 *1000;
  323. dcoeff.w[1] = (b2d[k5] >> 4) | (b2d[k4] << 6) | (b2d[k3] << 16) |
  324. (b2d[k2] << 26) | (b2d[k1] << 36);
  325. dcoeff.w[0] = b2d[k11] | (b2d[k10] << 10) | (b2d[k9] << 20) |
  326. (b2d[k8] << 30) | (b2d[k7] << 40) | (b2d[k6] << 50) | (b2d[k5] << 60);
  327. res.w[0] = dcoeff.w[0];
  328. if (k0 >= 8) {
  329. res.w[1] = sign.w[1] | ((0x18000 | ((exp >> 12) << 13) |
  330. ((k0 & 1) << 12) | (exp & 0xfff)) << 46) | dcoeff.w[1];
  331. } else {
  332. res.w[1] = sign.w[1] | ((((exp >> 12) << 15) | (k0 << 12) |
  333. (exp & 0xfff)) << 46) | dcoeff.w[1];
  334. }
  335. }
  336. *pres = res;
  337. }
  338. void _dpd_to_bid128 (_Decimal128 *, _Decimal128 *);
  339. void
  340. _dpd_to_bid128 (_Decimal128 *pres, _Decimal128 *px) {
  341. UINT128 res;
  342. UINT128 sign;
  343. UINT64 exp, comb;
  344. UINT128 trailing;
  345. UINT64 d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11;
  346. UINT128 bcoeff;
  347. UINT64 tl, th;
  348. _Decimal128 x = *px;
  349. sign.w[1] = (x.w[1] & 0x8000000000000000ull);
  350. sign.w[0] = 0;
  351. comb = (x.w[1] & 0x7fffc00000000000ull) >> 46;
  352. trailing.w[1] = x.w[1];
  353. trailing.w[0] = x.w[0];
  354. if ((comb & 0x1e000) == 0x1e000) {
  355. if ((comb & 0x1f000) == 0x1f000) { /* G0..G4 = 11111 -> NaN */
  356. if (comb & 0x01000) { /* G5 = 1 -> sNaN */
  357. *pres = x;
  358. } else { /* G5 = 0 -> qNaN */
  359. *pres = x;
  360. }
  361. } else { /* G0..G4 = 11110 -> INF */
  362. *pres = x;
  363. }
  364. return;
  365. } else { /* Normal number */
  366. if ((comb & 0x18000) == 0x18000) { /* G0..G1 = 11 -> d0 = 8 + G4 */
  367. d0 = d2b6[8 + ((comb & 0x01000) >> 12)];
  368. exp = (comb & 0x06000) >> 1; /* exp leading bits are G2..G3 */
  369. } else {
  370. d0 = d2b6[((comb & 0x07000) >> 12)];
  371. exp = (comb & 0x18000) >> 3; /* exp loading bits are G0..G1 */
  372. }
  373. d11 = d2b[(trailing.w[0]) & 0x3ff];
  374. d10 = d2b2[(trailing.w[0] >> 10) & 0x3ff];
  375. d9 = d2b3[(trailing.w[0] >> 20) & 0x3ff];
  376. d8 = d2b4[(trailing.w[0] >> 30) & 0x3ff];
  377. d7 = d2b5[(trailing.w[0] >> 40) & 0x3ff];
  378. d6 = d2b6[(trailing.w[0] >> 50) & 0x3ff];
  379. d5 = d2b[(trailing.w[0] >> 60) | ((trailing.w[1] & 0x3f) << 4)];
  380. d4 = d2b2[(trailing.w[1] >> 6) & 0x3ff];
  381. d3 = d2b3[(trailing.w[1] >> 16) & 0x3ff];
  382. d2 = d2b4[(trailing.w[1] >> 26) & 0x3ff];
  383. d1 = d2b5[(trailing.w[1] >> 36) & 0x3ff];
  384. tl = d11 + d10 + d9 + d8 + d7 + d6;
  385. th = d5 + d4 + d3 + d2 + d1 + d0;
  386. __mul_64x64_to_128 (bcoeff, th, 1000000000000000000ull);
  387. __add_128_64 (bcoeff, bcoeff, tl);
  388. exp += (comb & 0xfff);
  389. res.w[0] = bcoeff.w[0];
  390. res.w[1] = (exp << 49) | sign.w[1] | bcoeff.w[1];
  391. }
  392. *pres = res;
  393. }