Forgot ChangeLog of part1 pr25561.
[official-gcc.git] / libgcc / config / libbid / bid32_to_bid128.c
blobed8e7f9847c41152c9ff07987c1bd29333983b9d
1 /* Copyright (C) 2007 Free Software Foundation, Inc.
3 This file is part of GCC.
5 GCC is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
8 version.
10 In addition to the permissions in the GNU General Public License, the
11 Free Software Foundation gives you unlimited permission to link the
12 compiled version of this file into combinations with other programs,
13 and to distribute those combinations without any restriction coming
14 from the use of this file. (The General Public License restrictions
15 do apply in other respects; for example, they cover modification of
16 the file, and distribution when not linked into a combine
17 executable.)
19 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
20 WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
24 You should have received a copy of the GNU General Public License
25 along with GCC; see the file COPYING. If not, write to the Free
26 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
27 02110-1301, USA. */
29 #define BID_128RES
30 #include "bid_internal.h"
32 /*
33 * Takes a BID32 as input and converts it to a BID128 and returns it.
35 TYPE0_FUNCTION_ARGTYPE1_NORND (UINT128, bid32_to_bid128, UINT32, x)
37 UINT128 new_coeff, res;
38 UINT32 sign_x;
39 int exponent_x;
40 UINT32 coefficient_x;
42 if (!unpack_BID32 (&sign_x, &exponent_x, &coefficient_x, x)) {
43 if (((x) & 0x78000000) == 0x78000000) {
44 #ifdef SET_STATUS_FLAGS
45 if (((x) & 0x7e000000) == 0x7e000000) // sNaN
46 __set_status_flags (pfpsf, INVALID_EXCEPTION);
47 #endif
48 res.w[0] = (coefficient_x & 0x000fffff);
49 __mul_64x128_low (res, res.w[0], power10_table_128[27]);
50 res.w[1] |=
51 ((((UINT64) coefficient_x) << 32) & 0xfc00000000000000ull);
53 BID_RETURN (res);
57 new_coeff.w[0] = coefficient_x;
58 new_coeff.w[1] = 0;
59 get_BID128_very_fast (&res, ((UINT64) sign_x) << 32,
60 exponent_x + DECIMAL_EXPONENT_BIAS_128 -
61 DECIMAL_EXPONENT_BIAS_32, new_coeff);
62 BID_RETURN (res);
63 } // convert_bid32_to_bid128
67 * Takes a BID128 as input and converts it to a BID32 and returns it.
69 #if DECIMAL_CALL_BY_REFERENCE
71 void
72 bid128_to_bid32 (UINT32 * pres,
73 UINT128 *
74 px _RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
75 _EXC_INFO_PARAM) {
76 UINT128 x = *px;
77 #else
79 UINT32
80 bid128_to_bid32 (UINT128 x _RND_MODE_PARAM _EXC_FLAGS_PARAM
81 _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
82 #endif
83 UINT128 CX, T128, TP128, Qh, Ql, Qh1, Stemp, Tmp, Tmp1, CX1;
84 UINT64 sign_x, carry, cy;
85 SINT64 D;
86 UINT32 res;
87 int_float f64, fx;
88 int exponent_x, extra_digits, amount, bin_expon_cx, uf_check = 0;
89 unsigned rmode, status;
91 #if DECIMAL_CALL_BY_REFERENCE
92 #if !DECIMAL_GLOBAL_ROUNDING
93 _IDEC_round rnd_mode = *prnd_mode;
94 #endif
95 #endif
97 BID_SWAP128 (x);
98 // unpack arguments, check for NaN or Infinity or 0
99 if (!unpack_BID128_value (&sign_x, &exponent_x, &CX, x)) {
100 if (((x.w[1]) & 0x7800000000000000ull) == 0x7800000000000000ull) {
101 Tmp.w[1] = (CX.w[1] & 0x00003fffffffffffull);
102 Tmp.w[0] = CX.w[0];
103 TP128 = reciprocals10_128[27];
104 __mul_128x128_full (Qh, Ql, Tmp, TP128);
105 amount = recip_scale[27] - 64;
106 res = ((CX.w[1] >> 32) & 0xfc000000) | (Qh.w[1] >> amount);
107 #ifdef SET_STATUS_FLAGS
108 if ((x.w[1] & SNAN_MASK64) == SNAN_MASK64) // sNaN
109 __set_status_flags (pfpsf, INVALID_EXCEPTION);
110 #endif
111 BID_RETURN_VAL (res);
113 // x is 0
114 exponent_x =
115 exponent_x - DECIMAL_EXPONENT_BIAS_128 + DECIMAL_EXPONENT_BIAS_32;
116 if (exponent_x < 0)
117 exponent_x = 0;
118 if (exponent_x > DECIMAL_MAX_EXPON_32)
119 exponent_x = DECIMAL_MAX_EXPON_32;
120 res = (sign_x >> 32) | (exponent_x << 23);
121 BID_RETURN_VAL (res);
125 if (CX.w[1] || (CX.w[0] >= 10000000)) {
126 // find number of digits in coefficient
127 // 2^64
128 f64.i = 0x5f800000;
129 // fx ~ CX
130 fx.d = (float) CX.w[1] * f64.d + (float) CX.w[0];
131 bin_expon_cx = ((fx.i >> 23) & 0xff) - 0x7f;
132 extra_digits = estimate_decimal_digits[bin_expon_cx] - 7;
133 // scale = 38-estimate_decimal_digits[bin_expon_cx];
134 D = CX.w[1] - power10_index_binexp_128[bin_expon_cx].w[1];
135 if (D > 0
136 || (!D
137 && CX.w[0] >= power10_index_binexp_128[bin_expon_cx].w[0]))
138 extra_digits++;
140 exponent_x += extra_digits;
142 #ifndef IEEE_ROUND_NEAREST_TIES_AWAY
143 #ifndef IEEE_ROUND_NEAREST
144 rmode = rnd_mode;
145 if (sign_x && (unsigned) (rmode - 1) < 2)
146 rmode = 3 - rmode;
147 #else
148 rmode = 0;
149 #endif
150 #else
151 rmode = 0;
152 #endif
153 if (exponent_x <
154 DECIMAL_EXPONENT_BIAS_128 - DECIMAL_EXPONENT_BIAS_32) {
155 uf_check = 1;
156 if (-extra_digits + exponent_x - DECIMAL_EXPONENT_BIAS_128 +
157 DECIMAL_EXPONENT_BIAS_32 + 35 >= 0) {
158 if (exponent_x ==
159 DECIMAL_EXPONENT_BIAS_128 - DECIMAL_EXPONENT_BIAS_32 - 1) {
160 T128 = round_const_table_128[rmode][extra_digits];
161 __add_carry_out (CX1.w[0], carry, T128.w[0], CX.w[0]);
162 CX1.w[1] = CX.w[1] + T128.w[1] + carry;
163 if (__unsigned_compare_ge_128
164 (CX1, power10_table_128[extra_digits + 7]))
165 uf_check = 0;
167 extra_digits =
168 extra_digits + DECIMAL_EXPONENT_BIAS_128 -
169 DECIMAL_EXPONENT_BIAS_32 - exponent_x;
170 exponent_x =
171 DECIMAL_EXPONENT_BIAS_128 - DECIMAL_EXPONENT_BIAS_32;
172 } else
173 rmode = ROUNDING_TO_ZERO;
176 T128 = round_const_table_128[rmode][extra_digits];
177 __add_carry_out (CX.w[0], carry, T128.w[0], CX.w[0]);
178 CX.w[1] = CX.w[1] + T128.w[1] + carry;
180 TP128 = reciprocals10_128[extra_digits];
181 __mul_128x128_full (Qh, Ql, CX, TP128);
182 amount = recip_scale[extra_digits];
184 if (amount >= 64) {
185 CX.w[0] = Qh.w[1] >> (amount - 64);
186 CX.w[1] = 0;
187 } else {
188 __shr_128 (CX, Qh, amount);
191 #ifndef IEEE_ROUND_NEAREST_TIES_AWAY
192 #ifndef IEEE_ROUND_NEAREST
193 if (!(rnd_mode))
194 #endif
195 if (CX.w[0] & 1) {
196 // check whether fractional part of initial_P/10^ed1 is exactly .5
198 // get remainder
199 __shl_128_long (Qh1, Qh, (128 - amount));
201 if (!Qh1.w[1] && !Qh1.w[0]
202 && (Ql.w[1] < reciprocals10_128[extra_digits].w[1]
203 || (Ql.w[1] == reciprocals10_128[extra_digits].w[1]
204 && Ql.w[0] < reciprocals10_128[extra_digits].w[0]))) {
205 CX.w[0]--;
208 #endif
212 status = INEXACT_EXCEPTION;
213 // get remainder
214 __shl_128_long (Qh1, Qh, (128 - amount));
216 switch (rmode) {
217 case ROUNDING_TO_NEAREST:
218 case ROUNDING_TIES_AWAY:
219 // test whether fractional part is 0
220 if (Qh1.w[1] == 0x8000000000000000ull && (!Qh1.w[0])
221 && (Ql.w[1] < reciprocals10_128[extra_digits].w[1]
222 || (Ql.w[1] == reciprocals10_128[extra_digits].w[1]
223 && Ql.w[0] < reciprocals10_128[extra_digits].w[0])))
224 status = EXACT_STATUS;
225 break;
226 case ROUNDING_DOWN:
227 case ROUNDING_TO_ZERO:
228 if ((!Qh1.w[1]) && (!Qh1.w[0])
229 && (Ql.w[1] < reciprocals10_128[extra_digits].w[1]
230 || (Ql.w[1] == reciprocals10_128[extra_digits].w[1]
231 && Ql.w[0] < reciprocals10_128[extra_digits].w[0])))
232 status = EXACT_STATUS;
233 break;
234 default:
235 // round up
236 __add_carry_out (Stemp.w[0], cy, Ql.w[0],
237 reciprocals10_128[extra_digits].w[0]);
238 __add_carry_in_out (Stemp.w[1], carry, Ql.w[1],
239 reciprocals10_128[extra_digits].w[1], cy);
240 __shr_128_long (Qh, Qh1, (128 - amount));
241 Tmp.w[0] = 1;
242 Tmp.w[1] = 0;
243 __shl_128_long (Tmp1, Tmp, amount);
244 Qh.w[0] += carry;
245 if (Qh.w[0] < carry)
246 Qh.w[1]++;
247 if (__unsigned_compare_ge_128 (Qh, Tmp1))
248 status = EXACT_STATUS;
251 if (status != EXACT_STATUS) {
252 if (uf_check) {
253 status |= UNDERFLOW_EXCEPTION;
255 #ifdef SET_STATUS_FLAGS
256 __set_status_flags (pfpsf, status);
257 #endif
263 res =
264 get_BID32 ((UINT32) (sign_x >> 32),
265 exponent_x - DECIMAL_EXPONENT_BIAS_128 +
266 DECIMAL_EXPONENT_BIAS_32, CX.w[0], rnd_mode, pfpsf);
267 BID_RETURN_VAL (res);