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
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
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
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
30 #include "bid_internal.h"
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
;
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
);
48 res
.w
[0] = (coefficient_x
& 0x000fffff);
49 __mul_64x128_low (res
, res
.w
[0], power10_table_128
[27]);
51 ((((UINT64
) coefficient_x
) << 32) & 0xfc00000000000000ull
);
57 new_coeff
.w
[0] = coefficient_x
;
59 get_BID128_very_fast (&res
, ((UINT64
) sign_x
) << 32,
60 exponent_x
+ DECIMAL_EXPONENT_BIAS_128
-
61 DECIMAL_EXPONENT_BIAS_32
, new_coeff
);
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
72 bid128_to_bid32 (UINT32
* pres
,
74 px _RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
80 bid128_to_bid32 (UINT128 x _RND_MODE_PARAM _EXC_FLAGS_PARAM
81 _EXC_MASKS_PARAM _EXC_INFO_PARAM
) {
83 UINT128 CX
, T128
, TP128
, Qh
, Ql
, Qh1
, Stemp
, Tmp
, Tmp1
, CX1
;
84 UINT64 sign_x
, carry
, cy
;
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
;
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
);
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
);
111 BID_RETURN_VAL (res
);
115 exponent_x
- DECIMAL_EXPONENT_BIAS_128
+ DECIMAL_EXPONENT_BIAS_32
;
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
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];
137 && CX
.w
[0] >= power10_index_binexp_128
[bin_expon_cx
].w
[0]))
140 exponent_x
+= extra_digits
;
142 #ifndef IEEE_ROUND_NEAREST_TIES_AWAY
143 #ifndef IEEE_ROUND_NEAREST
145 if (sign_x
&& (unsigned) (rmode
- 1) < 2)
154 DECIMAL_EXPONENT_BIAS_128
- DECIMAL_EXPONENT_BIAS_32
) {
156 if (-extra_digits
+ exponent_x
- DECIMAL_EXPONENT_BIAS_128
+
157 DECIMAL_EXPONENT_BIAS_32
+ 35 >= 0) {
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]))
168 extra_digits
+ DECIMAL_EXPONENT_BIAS_128
-
169 DECIMAL_EXPONENT_BIAS_32
- exponent_x
;
171 DECIMAL_EXPONENT_BIAS_128
- DECIMAL_EXPONENT_BIAS_32
;
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
];
185 CX
.w
[0] = Qh
.w
[1] >> (amount
- 64);
188 __shr_128 (CX
, Qh
, amount
);
191 #ifndef IEEE_ROUND_NEAREST_TIES_AWAY
192 #ifndef IEEE_ROUND_NEAREST
196 // check whether fractional part of initial_P/10^ed1 is exactly .5
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]))) {
212 status
= INEXACT_EXCEPTION
;
214 __shl_128_long (Qh1
, Qh
, (128 - amount
));
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
;
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
;
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
));
243 __shl_128_long (Tmp1
, Tmp
, amount
);
247 if (__unsigned_compare_ge_128 (Qh
, Tmp1
))
248 status
= EXACT_STATUS
;
251 if (status
!= EXACT_STATUS
) {
253 status
|= UNDERFLOW_EXCEPTION
;
255 #ifdef SET_STATUS_FLAGS
256 __set_status_flags (pfpsf
, status
);
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
);