annotate libdecnumber/bid/bid2dpd_dpd2bid.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1 /* Copyright (C) 2007-2020 Free Software Foundation, Inc.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 This file is part of GCC.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 GCC is free software; you can redistribute it and/or modify it under
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 the terms of the GNU General Public License as published by the Free
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 Software Foundation; either version 3, or (at your option) any later
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 version.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 for more details.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 Under Section 7 of GPL version 3, you are granted additional
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 permissions described in the GCC Runtime Library Exception, version
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 3.1, as published by the Free Software Foundation.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 You should have received a copy of the GNU General Public License and
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 a copy of the GCC Runtime Library Exception along with this program;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 <http://www.gnu.org/licenses/>. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 #undef IN_LIBGCC2
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 #include "bid-dpd.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 /* get full 64x64bit product */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 #define __mul_64x64_to_128(P, CX, CY) \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 { \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 UINT64 CXH, CXL, CYH,CYL,PL,PH,PM,PM2; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 CXH = (CX) >> 32; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 CXL = (UINT32)(CX); \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 CYH = (CY) >> 32; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 CYL = (UINT32)(CY); \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 PM = CXH*CYL; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 PH = CXH*CYH; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 PL = CXL*CYL; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 PM2 = CXL*CYH; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 PH += (PM>>32); \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 PM = (UINT64)((UINT32)PM)+PM2+(PL>>32); \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 (P).w[1] = PH + (PM>>32); \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 (P).w[0] = (PM<<32)+(UINT32)PL; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 /* add 64-bit value to 128-bit */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 #define __add_128_64(R128, A128, B64) \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 { \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 UINT64 R64H; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 R64H = (A128).w[1]; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 (R128).w[0] = (B64) + (A128).w[0]; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 if((R128).w[0] < (B64)) R64H ++; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 (R128).w[1] = R64H; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 /* add 128-bit value to 128-bit (assume no carry-out) */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 #define __add_128_128(R128, A128, B128) \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 { \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 UINT128 Q128; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 Q128.w[1] = (A128).w[1]+(B128).w[1]; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 Q128.w[0] = (B128).w[0] + (A128).w[0]; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 if(Q128.w[0] < (B128).w[0]) Q128.w[1] ++; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 (R128).w[1] = Q128.w[1]; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 (R128).w[0] = Q128.w[0]; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 #define __mul_128x128_high(Q, A, B) \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 { \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 UINT128 ALBL, ALBH, AHBL, AHBH, QM, QM2; \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 __mul_64x64_to_128(ALBH, (A).w[0], (B).w[1]); \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 __mul_64x64_to_128(AHBL, (B).w[0], (A).w[1]); \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 __mul_64x64_to_128(ALBL, (A).w[0], (B).w[0]); \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 __mul_64x64_to_128(AHBH, (A).w[1],(B).w[1]); \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 __add_128_128(QM, ALBH, AHBL); \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 __add_128_64(QM2, QM, ALBL.w[1]); \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 __add_128_64((Q), AHBH, QM2.w[1]); \
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
81
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 #include "bid2dpd_dpd2bid.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
83
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 static const unsigned int dm103[] =
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 { 0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 11000 };
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 void _bid_to_dpd32 (_Decimal32 *, _Decimal32 *);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 _bid_to_dpd32 (_Decimal32 *pres, _Decimal32 *px) {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 unsigned int sign, coefficient_x, exp, dcoeff;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 unsigned int b2, b1, b0, b01, res;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 _Decimal32 x = *px;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
94
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 sign = (x & 0x80000000);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 if ((x & 0x60000000ul) == 0x60000000ul) {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 /* special encodings */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 if ((x & 0x78000000ul) == 0x78000000ul) {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 *pres = x; /* NaN or Infinity */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 return;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 /* coefficient */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 coefficient_x = (x & 0x001ffffful) | 0x00800000ul;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 if (coefficient_x >= 10000000) coefficient_x = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 /* get exponent */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 exp = (x >> 21) & 0xff;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 } else {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 exp = (x >> 23) & 0xff;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 coefficient_x = (x & 0x007ffffful);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 b01 = coefficient_x / 1000;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 b2 = coefficient_x - 1000 * b01;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 b0 = b01 / 1000;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 b1 = b01 - 1000 * b0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 dcoeff = b2d[b2] | b2d2[b1];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 if (b0 >= 8) { /* is b0 8 or 9? */
111
kono
parents: 0
diff changeset
117 res = sign | ((0x600 | ((exp >> 6) << 7) |
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 ((b0 & 1) << 6) | (exp & 0x3f)) << 20) | dcoeff;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 } else { /* else b0 is 0..7 */
111
kono
parents: 0
diff changeset
120 res = sign | ((((exp >> 6) << 9) | (b0 << 6) |
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 (exp & 0x3f)) << 20) | dcoeff;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 *pres = res;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
125
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 void _dpd_to_bid32 (_Decimal32 *, _Decimal32 *);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
127
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 _dpd_to_bid32 (_Decimal32 *pres, _Decimal32 *px) {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 unsigned int r;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 unsigned int sign, exp, bcoeff;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 UINT64 trailing;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 unsigned int d0, d1, d2;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 _Decimal32 x = *px;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
135
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 sign = (x & 0x80000000);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 trailing = (x & 0x000fffff);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 if ((x & 0x78000000) == 0x78000000) {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 *pres = x;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 return;
111
kono
parents: 0
diff changeset
141 }
kono
parents: 0
diff changeset
142 /* normal number */
kono
parents: 0
diff changeset
143 if ((x & 0x60000000) == 0x60000000) { /* G0..G1 = 11 -> d0 = 8 + G4 */
kono
parents: 0
diff changeset
144 d0 = d2b3[((x >> 26) & 1) | 8]; /* d0 = (comb & 0x0100 ? 9 : 8); */
kono
parents: 0
diff changeset
145 exp = (x >> 27) & 3; /* exp leading bits are G2..G3 */
kono
parents: 0
diff changeset
146 } else {
kono
parents: 0
diff changeset
147 d0 = d2b3[(x >> 26) & 0x7];
kono
parents: 0
diff changeset
148 exp = (x >> 29) & 3; /* exp loading bits are G0..G1 */
kono
parents: 0
diff changeset
149 }
kono
parents: 0
diff changeset
150 d1 = d2b2[(trailing >> 10) & 0x3ff];
kono
parents: 0
diff changeset
151 d2 = d2b[(trailing) & 0x3ff];
kono
parents: 0
diff changeset
152 bcoeff = d2 + d1 + d0;
kono
parents: 0
diff changeset
153 exp = (exp << 6) + ((x >> 20) & 0x3f);
kono
parents: 0
diff changeset
154 if (bcoeff < (1 << 23)) {
kono
parents: 0
diff changeset
155 r = exp;
kono
parents: 0
diff changeset
156 r <<= 23;
kono
parents: 0
diff changeset
157 r |= (bcoeff | sign);
kono
parents: 0
diff changeset
158 } else {
kono
parents: 0
diff changeset
159 r = exp;
kono
parents: 0
diff changeset
160 r <<= 21;
kono
parents: 0
diff changeset
161 r |= (sign | 0x60000000ul);
kono
parents: 0
diff changeset
162 /* add coeff, without leading bits */
kono
parents: 0
diff changeset
163 r |= (((unsigned int) bcoeff) & 0x1fffff);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
164 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
165 *pres = r;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
167
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 void _bid_to_dpd64 (_Decimal64 *, _Decimal64 *);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
169
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
170 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 _bid_to_dpd64 (_Decimal64 *pres, _Decimal64 *px) {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 UINT64 res;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 UINT64 sign, comb, exp, B34, B01;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 UINT64 d103, D61;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
175 UINT64 b0, b2, b3, b5;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 unsigned int b1, b4;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 UINT64 bcoeff;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
178 UINT64 dcoeff;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 unsigned int yhi, ylo;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 _Decimal64 x = *px;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
181
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 sign = (x & 0x8000000000000000ull);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 comb = (x & 0x7ffc000000000000ull) >> 51;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 if ((comb & 0xf00) == 0xf00) {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 *pres = x;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 return;
111
kono
parents: 0
diff changeset
187 }
kono
parents: 0
diff changeset
188 /* Normal number */
kono
parents: 0
diff changeset
189 if ((comb & 0xc00) == 0xc00) { /* G0..G1 = 11 -> exp is G2..G11 */
kono
parents: 0
diff changeset
190 exp = (comb) & 0x3ff;
kono
parents: 0
diff changeset
191 bcoeff = (x & 0x0007ffffffffffffull) | 0x0020000000000000ull;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
192 if (bcoeff >= 10000000000000000ull)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
193 bcoeff = 0;
111
kono
parents: 0
diff changeset
194 } else {
kono
parents: 0
diff changeset
195 exp = (comb >> 2) & 0x3ff;
kono
parents: 0
diff changeset
196 bcoeff = (x & 0x001fffffffffffffull);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 }
111
kono
parents: 0
diff changeset
198 D61 = 2305843009ull; /* Floor(2^61 / 10^9) */
kono
parents: 0
diff changeset
199 /* Multiply the binary coefficient by ceil(2^64 / 1000), and take the upper
kono
parents: 0
diff changeset
200 64-bits in order to compute a division by 1000. */
kono
parents: 0
diff changeset
201 yhi = (D61 * (UINT64)(bcoeff >> (UINT64)27)) >> (UINT64)34;
kono
parents: 0
diff changeset
202 ylo = bcoeff - 1000000000ull * yhi;
kono
parents: 0
diff changeset
203 if (ylo >= 1000000000) {
kono
parents: 0
diff changeset
204 ylo = ylo - 1000000000;
kono
parents: 0
diff changeset
205 yhi = yhi + 1;
kono
parents: 0
diff changeset
206 }
kono
parents: 0
diff changeset
207 d103 = 0x4189374c;
kono
parents: 0
diff changeset
208 B34 = ((UINT64) ylo * d103) >> (32 + 8);
kono
parents: 0
diff changeset
209 B01 = ((UINT64) yhi * d103) >> (32 + 8);
kono
parents: 0
diff changeset
210 b5 = ylo - B34 * 1000;
kono
parents: 0
diff changeset
211 b2 = yhi - B01 * 1000;
kono
parents: 0
diff changeset
212 b3 = ((UINT64) B34 * d103) >> (32 + 8);
kono
parents: 0
diff changeset
213 b0 = ((UINT64) B01 * d103) >> (32 + 8);
kono
parents: 0
diff changeset
214 b4 = (unsigned int) B34 - (unsigned int) b3 *1000;
kono
parents: 0
diff changeset
215 b1 = (unsigned int) B01 - (unsigned int) dm103[b0];
kono
parents: 0
diff changeset
216 dcoeff = b2d[b5] | b2d2[b4] | b2d3[b3] | b2d4[b2] | b2d5[b1];
kono
parents: 0
diff changeset
217 if (b0 >= 8) /* is b0 8 or 9? */
kono
parents: 0
diff changeset
218 res = sign | ((0x1800 | ((exp >> 8) << 9) | ((b0 & 1) << 8) |
kono
parents: 0
diff changeset
219 (exp & 0xff)) << 50) | dcoeff;
kono
parents: 0
diff changeset
220 else /* else b0 is 0..7 */
kono
parents: 0
diff changeset
221 res = sign | ((((exp >> 8) << 11) | (b0 << 8) |
kono
parents: 0
diff changeset
222 (exp & 0xff)) << 50) | dcoeff;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
223 *pres = res;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
225
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
226 void _dpd_to_bid64 (_Decimal64 *, _Decimal64 *);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
227
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
228 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
229 _dpd_to_bid64 (_Decimal64 *pres, _Decimal64 *px) {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 UINT64 res;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 UINT64 sign, comb, exp;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
232 UINT64 trailing;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
233 UINT64 d0, d1, d2;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 unsigned int d3, d4, d5;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 UINT64 bcoeff, mask;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 _Decimal64 x = *px;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
237
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 sign = (x & 0x8000000000000000ull);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
239 comb = (x & 0x7ffc000000000000ull) >> 50;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
240 trailing = (x & 0x0003ffffffffffffull);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
241 if ((comb & 0x1e00) == 0x1e00) {
111
kono
parents: 0
diff changeset
242 *pres = x;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 return;
111
kono
parents: 0
diff changeset
244 }
kono
parents: 0
diff changeset
245 /* normal number */
kono
parents: 0
diff changeset
246 if ((comb & 0x1800) == 0x1800) { /* G0..G1 = 11 -> d0 = 8 + G4 */
kono
parents: 0
diff changeset
247 d0 = d2b6[((comb >> 8) & 1) | 8]; /* d0 = (comb & 0x0100 ? 9 : 8); */
kono
parents: 0
diff changeset
248 exp = (comb & 0x600) >> 1; /* exp = (comb & 0x0400 ? 1 : 0) * 0x200 +
kono
parents: 0
diff changeset
249 (comb & 0x0200 ? 1 : 0) * 0x100; exp leading bits are G2..G3 */
kono
parents: 0
diff changeset
250 } else {
kono
parents: 0
diff changeset
251 d0 = d2b6[(comb >> 8) & 0x7];
kono
parents: 0
diff changeset
252 exp = (comb & 0x1800) >> 3; /* exp = (comb & 0x1000 ? 1 : 0) * 0x200 +
kono
parents: 0
diff changeset
253 (comb & 0x0800 ? 1 : 0) * 0x100; exp loading bits are G0..G1 */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
254 }
111
kono
parents: 0
diff changeset
255 d1 = d2b5[(trailing >> 40) & 0x3ff];
kono
parents: 0
diff changeset
256 d2 = d2b4[(trailing >> 30) & 0x3ff];
kono
parents: 0
diff changeset
257 d3 = d2b3[(trailing >> 20) & 0x3ff];
kono
parents: 0
diff changeset
258 d4 = d2b2[(trailing >> 10) & 0x3ff];
kono
parents: 0
diff changeset
259 d5 = d2b[(trailing) & 0x3ff];
kono
parents: 0
diff changeset
260 bcoeff = (d5 + d4 + d3) + d2 + d1 + d0;
kono
parents: 0
diff changeset
261 exp += (comb & 0xff);
kono
parents: 0
diff changeset
262 mask = 1;
kono
parents: 0
diff changeset
263 mask <<= 53;
kono
parents: 0
diff changeset
264 if (bcoeff < mask) { /* check whether coefficient fits in 10*5+3 bits */
kono
parents: 0
diff changeset
265 res = exp;
kono
parents: 0
diff changeset
266 res <<= 53;
kono
parents: 0
diff changeset
267 res |= (bcoeff | sign);
kono
parents: 0
diff changeset
268 *pres = res;
kono
parents: 0
diff changeset
269 return;
kono
parents: 0
diff changeset
270 }
kono
parents: 0
diff changeset
271 /* special format */
kono
parents: 0
diff changeset
272 res = (exp << 51) | (sign | 0x6000000000000000ull);
kono
parents: 0
diff changeset
273 /* add coeff, without leading bits */
kono
parents: 0
diff changeset
274 mask = (mask >> 2) - 1;
kono
parents: 0
diff changeset
275 bcoeff &= mask;
kono
parents: 0
diff changeset
276 res |= bcoeff;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
277 *pres = res;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
278 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
279
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
280 void _bid_to_dpd128 (_Decimal128 *, _Decimal128 *);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
281
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
282 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
283 _bid_to_dpd128 (_Decimal128 *pres, _Decimal128 *px) {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
284 UINT128 res;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
285 UINT128 sign;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
286 unsigned int comb;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
287 UINT128 bcoeff;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
288 UINT128 dcoeff;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
289 UINT128 BH, d1018, BT2, BT1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
290 UINT64 exp, BL, d109;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
291 UINT64 d106, d103;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
292 UINT64 k1, k2, k4, k5, k7, k8, k10, k11;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 unsigned int BHH32, BLL32, BHL32, BLH32, k0, k3, k6, k9, amount;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
294 _Decimal128 x = *px;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
295
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
296 sign.w[1] = (x.w[1] & 0x8000000000000000ull);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
297 sign.w[0] = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
298 comb = (x.w[1] /*& 0x7fffc00000000000ull */ ) >> 46;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
299 exp = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
300 if ((comb & 0x1e000) == 0x1e000) {
111
kono
parents: 0
diff changeset
301 res = x;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
302 } else { /* normal number */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
303 if ((comb & 0x18000) == 0x18000) {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
304 /* Noncanonical significand (prepending 8 or 9 to any 110-bit
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
305 trailing significand field produces a value above 10^34). */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
306 exp = (comb & 0x7fff) >> 1;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
307 bcoeff.w[1] = 0;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
308 bcoeff.w[0] = 0;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
309 } else {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
310 exp = ((x.w[1] & 0x7fff000000000000ull) >> 49) & 0x3fff;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
311 bcoeff.w[1] = (x.w[1] & 0x0001ffffffffffffull);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
312 bcoeff.w[0] = x.w[0];
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
313 if (bcoeff.w[1] > 0x1ed09bead87c0ull
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
314 || (bcoeff.w[1] == 0x1ed09bead87c0ull
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
315 && bcoeff.w[0] >= 0x378d8e6400000000ull)) {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
316 bcoeff.w[1] = 0;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
317 bcoeff.w[0] = 0;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
318 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
319 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
320 d1018 = reciprocals10_128[18];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
321 __mul_128x128_high (BH, bcoeff, d1018);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
322 amount = recip_scale[18];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
323 BH.w[0] = (BH.w[0] >> amount) | (BH.w[1] << (64 - amount));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
324 BL = bcoeff.w[0] - BH.w[0] * 1000000000000000000ull;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
325 d109 = reciprocals10_64[9];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
326 __mul_64x64_to_128 (BT1, BH.w[0], d109);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
327 BHH32 = (unsigned int) (BT1.w[1] >> short_recip_scale[9]);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
328 BHL32 = (unsigned int) BH.w[0] - BHH32 * 1000000000;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
329 __mul_64x64_to_128 (BT2, BL, d109);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
330 BLH32 = (unsigned int) (BT2.w[1] >> short_recip_scale[9]);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
331 BLL32 = (unsigned int) BL - BLH32 * 1000000000;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
332 d106 = 0x431BDE83;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
333 d103 = 0x4189374c;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
334 k0 = ((UINT64) BHH32 * d106) >> (32 + 18);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
335 BHH32 -= (unsigned int) k0 *1000000;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
336 k1 = ((UINT64) BHH32 * d103) >> (32 + 8);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
337 k2 = BHH32 - (unsigned int) k1 *1000;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
338 k3 = ((UINT64) BHL32 * d106) >> (32 + 18);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
339 BHL32 -= (unsigned int) k3 *1000000;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
340 k4 = ((UINT64) BHL32 * d103) >> (32 + 8);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
341 k5 = BHL32 - (unsigned int) k4 *1000;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 k6 = ((UINT64) BLH32 * d106) >> (32 + 18);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
343 BLH32 -= (unsigned int) k6 *1000000;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
344 k7 = ((UINT64) BLH32 * d103) >> (32 + 8);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
345 k8 = BLH32 - (unsigned int) k7 *1000;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
346 k9 = ((UINT64) BLL32 * d106) >> (32 + 18);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
347 BLL32 -= (unsigned int) k9 *1000000;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
348 k10 = ((UINT64) BLL32 * d103) >> (32 + 8);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
349 k11 = BLL32 - (unsigned int) k10 *1000;
111
kono
parents: 0
diff changeset
350 dcoeff.w[1] = (b2d[k5] >> 4) | (b2d[k4] << 6) | (b2d[k3] << 16) |
kono
parents: 0
diff changeset
351 (b2d[k2] << 26) | (b2d[k1] << 36);
kono
parents: 0
diff changeset
352 dcoeff.w[0] = b2d[k11] | (b2d[k10] << 10) | (b2d[k9] << 20) |
kono
parents: 0
diff changeset
353 (b2d[k8] << 30) | (b2d[k7] << 40) | (b2d[k6] << 50) | (b2d[k5] << 60);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
354 res.w[0] = dcoeff.w[0];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
355 if (k0 >= 8) {
111
kono
parents: 0
diff changeset
356 res.w[1] = sign.w[1] | ((0x18000 | ((exp >> 12) << 13) |
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
357 ((k0 & 1) << 12) | (exp & 0xfff)) << 46) | dcoeff.w[1];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
358 } else {
111
kono
parents: 0
diff changeset
359 res.w[1] = sign.w[1] | ((((exp >> 12) << 15) | (k0 << 12) |
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
360 (exp & 0xfff)) << 46) | dcoeff.w[1];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
361 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
362 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
363 *pres = res;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
364 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
365
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
366 void _dpd_to_bid128 (_Decimal128 *, _Decimal128 *);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
367
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
368 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
369 _dpd_to_bid128 (_Decimal128 *pres, _Decimal128 *px) {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
370 UINT128 res;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
371 UINT128 sign;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
372 UINT64 exp, comb;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
373 UINT128 trailing;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
374 UINT64 d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
375 UINT128 bcoeff;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
376 UINT64 tl, th;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
377 _Decimal128 x = *px;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
378
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
379 sign.w[1] = (x.w[1] & 0x8000000000000000ull);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
380 sign.w[0] = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
381 comb = (x.w[1] & 0x7fffc00000000000ull) >> 46;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
382 trailing.w[1] = x.w[1];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
383 trailing.w[0] = x.w[0];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
384 if ((comb & 0x1e000) == 0x1e000) {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
385 *pres = x;
111
kono
parents: 0
diff changeset
386 return;
kono
parents: 0
diff changeset
387 }
kono
parents: 0
diff changeset
388 if ((comb & 0x18000) == 0x18000) { /* G0..G1 = 11 -> d0 = 8 + G4 */
kono
parents: 0
diff changeset
389 d0 = d2b6[8 + ((comb & 0x01000) >> 12)];
kono
parents: 0
diff changeset
390 exp = (comb & 0x06000) >> 1; /* exp leading bits are G2..G3 */
kono
parents: 0
diff changeset
391 } else {
kono
parents: 0
diff changeset
392 d0 = d2b6[((comb & 0x07000) >> 12)];
kono
parents: 0
diff changeset
393 exp = (comb & 0x18000) >> 3; /* exp loading bits are G0..G1 */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
394 }
111
kono
parents: 0
diff changeset
395 d11 = d2b[(trailing.w[0]) & 0x3ff];
kono
parents: 0
diff changeset
396 d10 = d2b2[(trailing.w[0] >> 10) & 0x3ff];
kono
parents: 0
diff changeset
397 d9 = d2b3[(trailing.w[0] >> 20) & 0x3ff];
kono
parents: 0
diff changeset
398 d8 = d2b4[(trailing.w[0] >> 30) & 0x3ff];
kono
parents: 0
diff changeset
399 d7 = d2b5[(trailing.w[0] >> 40) & 0x3ff];
kono
parents: 0
diff changeset
400 d6 = d2b6[(trailing.w[0] >> 50) & 0x3ff];
kono
parents: 0
diff changeset
401 d5 = d2b[(trailing.w[0] >> 60) | ((trailing.w[1] & 0x3f) << 4)];
kono
parents: 0
diff changeset
402 d4 = d2b2[(trailing.w[1] >> 6) & 0x3ff];
kono
parents: 0
diff changeset
403 d3 = d2b3[(trailing.w[1] >> 16) & 0x3ff];
kono
parents: 0
diff changeset
404 d2 = d2b4[(trailing.w[1] >> 26) & 0x3ff];
kono
parents: 0
diff changeset
405 d1 = d2b5[(trailing.w[1] >> 36) & 0x3ff];
kono
parents: 0
diff changeset
406 tl = d11 + d10 + d9 + d8 + d7 + d6;
kono
parents: 0
diff changeset
407 th = d5 + d4 + d3 + d2 + d1 + d0;
kono
parents: 0
diff changeset
408 __mul_64x64_to_128 (bcoeff, th, 1000000000000000000ull);
kono
parents: 0
diff changeset
409 __add_128_64 (bcoeff, bcoeff, tl);
kono
parents: 0
diff changeset
410 exp += (comb & 0xfff);
kono
parents: 0
diff changeset
411 res.w[0] = bcoeff.w[0];
kono
parents: 0
diff changeset
412 res.w[1] = (exp << 49) | sign.w[1] | bcoeff.w[1];
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
413 *pres = res;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
414 }