bid2dpd_dpd2bid.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* Copyright (C) 2007-2018 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. }
  124. /* normal number */
  125. if ((x & 0x60000000) == 0x60000000) { /* G0..G1 = 11 -> d0 = 8 + G4 */
  126. d0 = d2b3[((x >> 26) & 1) | 8]; /* d0 = (comb & 0x0100 ? 9 : 8); */
  127. exp = (x >> 27) & 3; /* exp leading bits are G2..G3 */
  128. } else {
  129. d0 = d2b3[(x >> 26) & 0x7];
  130. exp = (x >> 29) & 3; /* exp loading bits are G0..G1 */
  131. }
  132. d1 = d2b2[(trailing >> 10) & 0x3ff];
  133. d2 = d2b[(trailing) & 0x3ff];
  134. bcoeff = d2 + d1 + d0;
  135. exp = (exp << 6) + ((x >> 20) & 0x3f);
  136. if (bcoeff < (1 << 23)) {
  137. r = exp;
  138. r <<= 23;
  139. r |= (bcoeff | sign);
  140. } else {
  141. r = exp;
  142. r <<= 21;
  143. r |= (sign | 0x60000000ul);
  144. /* add coeff, without leading bits */
  145. r |= (((unsigned int) bcoeff) & 0x1fffff);
  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. }
  167. /* Normal number */
  168. if ((comb & 0xc00) == 0xc00) { /* G0..G1 = 11 -> exp is G2..G11 */
  169. exp = (comb) & 0x3ff;
  170. bcoeff = (x & 0x0007ffffffffffffull) | 0x0020000000000000ull;
  171. } else {
  172. exp = (comb >> 2) & 0x3ff;
  173. bcoeff = (x & 0x001fffffffffffffull);
  174. }
  175. D61 = 2305843009ull; /* Floor(2^61 / 10^9) */
  176. /* Multiply the binary coefficient by ceil(2^64 / 1000), and take the upper
  177. 64-bits in order to compute a division by 1000. */
  178. yhi = (D61 * (UINT64)(bcoeff >> (UINT64)27)) >> (UINT64)34;
  179. ylo = bcoeff - 1000000000ull * yhi;
  180. if (ylo >= 1000000000) {
  181. ylo = ylo - 1000000000;
  182. yhi = yhi + 1;
  183. }
  184. d103 = 0x4189374c;
  185. B34 = ((UINT64) ylo * d103) >> (32 + 8);
  186. B01 = ((UINT64) yhi * d103) >> (32 + 8);
  187. b5 = ylo - B34 * 1000;
  188. b2 = yhi - B01 * 1000;
  189. b3 = ((UINT64) B34 * d103) >> (32 + 8);
  190. b0 = ((UINT64) B01 * d103) >> (32 + 8);
  191. b4 = (unsigned int) B34 - (unsigned int) b3 *1000;
  192. b1 = (unsigned int) B01 - (unsigned int) dm103[b0];
  193. dcoeff = b2d[b5] | b2d2[b4] | b2d3[b3] | b2d4[b2] | b2d5[b1];
  194. if (b0 >= 8) /* is b0 8 or 9? */
  195. res = sign | ((0x1800 | ((exp >> 8) << 9) | ((b0 & 1) << 8) |
  196. (exp & 0xff)) << 50) | dcoeff;
  197. else /* else b0 is 0..7 */
  198. res = sign | ((((exp >> 8) << 11) | (b0 << 8) |
  199. (exp & 0xff)) << 50) | dcoeff;
  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. *pres = x;
  217. return;
  218. }
  219. /* normal number */
  220. if ((comb & 0x1800) == 0x1800) { /* G0..G1 = 11 -> d0 = 8 + G4 */
  221. d0 = d2b6[((comb >> 8) & 1) | 8]; /* d0 = (comb & 0x0100 ? 9 : 8); */
  222. exp = (comb & 0x600) >> 1; /* exp = (comb & 0x0400 ? 1 : 0) * 0x200 +
  223. (comb & 0x0200 ? 1 : 0) * 0x100; exp leading bits are G2..G3 */
  224. } else {
  225. d0 = d2b6[(comb >> 8) & 0x7];
  226. exp = (comb & 0x1800) >> 3; /* exp = (comb & 0x1000 ? 1 : 0) * 0x200 +
  227. (comb & 0x0800 ? 1 : 0) * 0x100; exp loading bits are G0..G1 */
  228. }
  229. d1 = d2b5[(trailing >> 40) & 0x3ff];
  230. d2 = d2b4[(trailing >> 30) & 0x3ff];
  231. d3 = d2b3[(trailing >> 20) & 0x3ff];
  232. d4 = d2b2[(trailing >> 10) & 0x3ff];
  233. d5 = d2b[(trailing) & 0x3ff];
  234. bcoeff = (d5 + d4 + d3) + d2 + d1 + d0;
  235. exp += (comb & 0xff);
  236. mask = 1;
  237. mask <<= 53;
  238. if (bcoeff < mask) { /* check whether coefficient fits in 10*5+3 bits */
  239. res = exp;
  240. res <<= 53;
  241. res |= (bcoeff | sign);
  242. *pres = res;
  243. return;
  244. }
  245. /* special format */
  246. res = (exp << 51) | (sign | 0x6000000000000000ull);
  247. /* add coeff, without leading bits */
  248. mask = (mask >> 2) - 1;
  249. bcoeff &= mask;
  250. res |= bcoeff;
  251. *pres = res;
  252. }
  253. void _bid_to_dpd128 (_Decimal128 *, _Decimal128 *);
  254. void
  255. _bid_to_dpd128 (_Decimal128 *pres, _Decimal128 *px) {
  256. UINT128 res;
  257. UINT128 sign;
  258. unsigned int comb;
  259. UINT128 bcoeff;
  260. UINT128 dcoeff;
  261. UINT128 BH, d1018, BT2, BT1;
  262. UINT64 exp, BL, d109;
  263. UINT64 d106, d103;
  264. UINT64 k1, k2, k4, k5, k7, k8, k10, k11;
  265. unsigned int BHH32, BLL32, BHL32, BLH32, k0, k3, k6, k9, amount;
  266. _Decimal128 x = *px;
  267. sign.w[1] = (x.w[1] & 0x8000000000000000ull);
  268. sign.w[0] = 0;
  269. comb = (x.w[1] /*& 0x7fffc00000000000ull */ ) >> 46;
  270. exp = 0;
  271. if ((comb & 0x1e000) == 0x1e000) {
  272. res = x;
  273. } else { /* normal number */
  274. exp = ((x.w[1] & 0x7fff000000000000ull) >> 49) & 0x3fff;
  275. bcoeff.w[1] = (x.w[1] & 0x0001ffffffffffffull);
  276. bcoeff.w[0] = x.w[0];
  277. d1018 = reciprocals10_128[18];
  278. __mul_128x128_high (BH, bcoeff, d1018);
  279. amount = recip_scale[18];
  280. BH.w[0] = (BH.w[0] >> amount) | (BH.w[1] << (64 - amount));
  281. BL = bcoeff.w[0] - BH.w[0] * 1000000000000000000ull;
  282. d109 = reciprocals10_64[9];
  283. __mul_64x64_to_128 (BT1, BH.w[0], d109);
  284. BHH32 = (unsigned int) (BT1.w[1] >> short_recip_scale[9]);
  285. BHL32 = (unsigned int) BH.w[0] - BHH32 * 1000000000;
  286. __mul_64x64_to_128 (BT2, BL, d109);
  287. BLH32 = (unsigned int) (BT2.w[1] >> short_recip_scale[9]);
  288. BLL32 = (unsigned int) BL - BLH32 * 1000000000;
  289. d106 = 0x431BDE83;
  290. d103 = 0x4189374c;
  291. k0 = ((UINT64) BHH32 * d106) >> (32 + 18);
  292. BHH32 -= (unsigned int) k0 *1000000;
  293. k1 = ((UINT64) BHH32 * d103) >> (32 + 8);
  294. k2 = BHH32 - (unsigned int) k1 *1000;
  295. k3 = ((UINT64) BHL32 * d106) >> (32 + 18);
  296. BHL32 -= (unsigned int) k3 *1000000;
  297. k4 = ((UINT64) BHL32 * d103) >> (32 + 8);
  298. k5 = BHL32 - (unsigned int) k4 *1000;
  299. k6 = ((UINT64) BLH32 * d106) >> (32 + 18);
  300. BLH32 -= (unsigned int) k6 *1000000;
  301. k7 = ((UINT64) BLH32 * d103) >> (32 + 8);
  302. k8 = BLH32 - (unsigned int) k7 *1000;
  303. k9 = ((UINT64) BLL32 * d106) >> (32 + 18);
  304. BLL32 -= (unsigned int) k9 *1000000;
  305. k10 = ((UINT64) BLL32 * d103) >> (32 + 8);
  306. k11 = BLL32 - (unsigned int) k10 *1000;
  307. dcoeff.w[1] = (b2d[k5] >> 4) | (b2d[k4] << 6) | (b2d[k3] << 16) |
  308. (b2d[k2] << 26) | (b2d[k1] << 36);
  309. dcoeff.w[0] = b2d[k11] | (b2d[k10] << 10) | (b2d[k9] << 20) |
  310. (b2d[k8] << 30) | (b2d[k7] << 40) | (b2d[k6] << 50) | (b2d[k5] << 60);
  311. res.w[0] = dcoeff.w[0];
  312. if (k0 >= 8) {
  313. res.w[1] = sign.w[1] | ((0x18000 | ((exp >> 12) << 13) |
  314. ((k0 & 1) << 12) | (exp & 0xfff)) << 46) | dcoeff.w[1];
  315. } else {
  316. res.w[1] = sign.w[1] | ((((exp >> 12) << 15) | (k0 << 12) |
  317. (exp & 0xfff)) << 46) | dcoeff.w[1];
  318. }
  319. }
  320. *pres = res;
  321. }
  322. void _dpd_to_bid128 (_Decimal128 *, _Decimal128 *);
  323. void
  324. _dpd_to_bid128 (_Decimal128 *pres, _Decimal128 *px) {
  325. UINT128 res;
  326. UINT128 sign;
  327. UINT64 exp, comb;
  328. UINT128 trailing;
  329. UINT64 d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11;
  330. UINT128 bcoeff;
  331. UINT64 tl, th;
  332. _Decimal128 x = *px;
  333. sign.w[1] = (x.w[1] & 0x8000000000000000ull);
  334. sign.w[0] = 0;
  335. comb = (x.w[1] & 0x7fffc00000000000ull) >> 46;
  336. trailing.w[1] = x.w[1];
  337. trailing.w[0] = x.w[0];
  338. if ((comb & 0x1e000) == 0x1e000) {
  339. *pres = x;
  340. return;
  341. }
  342. if ((comb & 0x18000) == 0x18000) { /* G0..G1 = 11 -> d0 = 8 + G4 */
  343. d0 = d2b6[8 + ((comb & 0x01000) >> 12)];
  344. exp = (comb & 0x06000) >> 1; /* exp leading bits are G2..G3 */
  345. } else {
  346. d0 = d2b6[((comb & 0x07000) >> 12)];
  347. exp = (comb & 0x18000) >> 3; /* exp loading bits are G0..G1 */
  348. }
  349. d11 = d2b[(trailing.w[0]) & 0x3ff];
  350. d10 = d2b2[(trailing.w[0] >> 10) & 0x3ff];
  351. d9 = d2b3[(trailing.w[0] >> 20) & 0x3ff];
  352. d8 = d2b4[(trailing.w[0] >> 30) & 0x3ff];
  353. d7 = d2b5[(trailing.w[0] >> 40) & 0x3ff];
  354. d6 = d2b6[(trailing.w[0] >> 50) & 0x3ff];
  355. d5 = d2b[(trailing.w[0] >> 60) | ((trailing.w[1] & 0x3f) << 4)];
  356. d4 = d2b2[(trailing.w[1] >> 6) & 0x3ff];
  357. d3 = d2b3[(trailing.w[1] >> 16) & 0x3ff];
  358. d2 = d2b4[(trailing.w[1] >> 26) & 0x3ff];
  359. d1 = d2b5[(trailing.w[1] >> 36) & 0x3ff];
  360. tl = d11 + d10 + d9 + d8 + d7 + d6;
  361. th = d5 + d4 + d3 + d2 + d1 + d0;
  362. __mul_64x64_to_128 (bcoeff, th, 1000000000000000000ull);
  363. __add_128_64 (bcoeff, bcoeff, tl);
  364. exp += (comb & 0xfff);
  365. res.w[0] = bcoeff.w[0];
  366. res.w[1] = (exp << 49) | sign.w[1] | bcoeff.w[1];
  367. *pres = res;
  368. }