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
29 #include "bid_internal.h"
31 #define MAX_FORMAT_DIGITS 16
32 #define DECIMAL_EXPONENT_BIAS 398
33 #define MAX_DECIMAL_EXPONENT 767
35 #if DECIMAL_CALL_BY_REFERENCE
38 bid64_quantize (UINT64 * pres, UINT64 * px,
40 py _RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
46 bid64_quantize (UINT64 x,
47 UINT64 y _RND_MODE_PARAM _EXC_FLAGS_PARAM
48 _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
51 UINT64 sign_x, sign_y, coefficient_x, coefficient_y, remainder_h, C64,
53 UINT64 tmp, carry, res;
55 int exponent_x, exponent_y, digits_x, extra_digits, amount, amount2;
56 int expon_diff, total_digits, bin_expon_cx;
57 unsigned rmode, status;
59 #if DECIMAL_CALL_BY_REFERENCE
60 #if !DECIMAL_GLOBAL_ROUNDING
61 _IDEC_round rnd_mode = *prnd_mode;
67 valid_x = unpack_BID64 (&sign_x, &exponent_x, &coefficient_x, x);
68 // unpack arguments, check for NaN or Infinity
69 if (!unpack_BID64 (&sign_y, &exponent_y, &coefficient_y, y)) {
71 #ifdef SET_STATUS_FLAGS
72 if ((x & SNAN_MASK64) == SNAN_MASK64) // y is sNaN
73 __set_status_flags (pfpsf, INVALID_EXCEPTION);
77 if (((coefficient_x << 1) == 0xf000000000000000ull)
78 && ((coefficient_y << 1) == 0xf000000000000000ull)) {
83 if ((y & 0x7800000000000000ull) == 0x7800000000000000ull) {
84 #ifdef SET_STATUS_FLAGS
85 if (((y & 0x7e00000000000000ull) == 0x7e00000000000000ull) // sNaN
86 || (((y & 0x7c00000000000000ull) == 0x7800000000000000ull) && //Inf
87 ((x & 0x7c00000000000000ull) < 0x7800000000000000ull)))
88 __set_status_flags (pfpsf, INVALID_EXCEPTION);
90 if ((y & NAN_MASK64) != NAN_MASK64)
92 if ((x & NAN_MASK64) != NAN_MASK64) {
93 res = 0x7c00000000000000ull | (coefficient_y & QUIET_MASK64);
94 if (((y & NAN_MASK64) != NAN_MASK64) && ((x & NAN_MASK64) == 0x7800000000000000ull))
100 // unpack arguments, check for NaN or Infinity
102 // x is Inf. or NaN or 0
105 if ((x & 0x7800000000000000ull) == 0x7800000000000000ull) {
106 #ifdef SET_STATUS_FLAGS
107 if (((x & 0x7e00000000000000ull) == 0x7e00000000000000ull) // sNaN
108 || ((x & 0x7c00000000000000ull) == 0x7800000000000000ull)) //Inf
109 __set_status_flags (pfpsf, INVALID_EXCEPTION);
111 if ((x & NAN_MASK64) != NAN_MASK64)
113 res = 0x7c00000000000000ull | (coefficient_x & QUIET_MASK64);
117 res = very_fast_get_BID64_small_mantissa (sign_x, exponent_y, 0);
120 // get number of decimal digits in coefficient_x
121 tempx.d = (float) coefficient_x;
122 bin_expon_cx = ((tempx.i >> 23) & 0xff) - 0x7f;
123 digits_x = estimate_decimal_digits[bin_expon_cx];
124 if (coefficient_x >= power10_table_128[digits_x].w[0])
127 expon_diff = exponent_x - exponent_y;
128 total_digits = digits_x + expon_diff;
130 // check range of scaled coefficient
131 if ((UINT32) (total_digits + 1) <= 17) {
132 if (expon_diff >= 0) {
133 coefficient_x *= power10_table_128[expon_diff].w[0];
134 res = very_fast_get_BID64 (sign_x, exponent_y, coefficient_x);
137 // must round off -expon_diff digits
138 extra_digits = -expon_diff;
139 #ifndef IEEE_ROUND_NEAREST_TIES_AWAY
140 #ifndef IEEE_ROUND_NEAREST
142 if (sign_x && (unsigned) (rmode - 1) < 2)
150 coefficient_x += round_const_table[rmode][extra_digits];
152 // get P*(2^M[extra_digits])/10^extra_digits
153 __mul_64x64_to_128 (CT, coefficient_x,
154 reciprocals10_64[extra_digits]);
156 // now get P/10^extra_digits: shift C64 right by M[extra_digits]-128
157 amount = short_recip_scale[extra_digits];
158 C64 = CT.w[1] >> amount;
159 #ifndef IEEE_ROUND_NEAREST_TIES_AWAY
160 #ifndef IEEE_ROUND_NEAREST
164 // check whether fractional part of initial_P/10^extra_digits
166 // this is the same as fractional part of
167 // (initial_P + 0.5*10^extra_digits)/10^extra_digits is exactly zero
170 amount2 = 64 - amount;
173 remainder_h >>= amount2;
174 remainder_h = remainder_h & CT.w[1];
176 // test whether fractional part is 0
177 if (!remainder_h && (CT.w[0] < reciprocals10_64[extra_digits])) {
183 #ifdef SET_STATUS_FLAGS
184 status = INEXACT_EXCEPTION;
186 remainder_h = CT.w[1] << (64 - amount);
188 case ROUNDING_TO_NEAREST:
189 case ROUNDING_TIES_AWAY:
190 // test whether fractional part is 0
191 if ((remainder_h == 0x8000000000000000ull)
192 && (CT.w[0] < reciprocals10_64[extra_digits]))
193 status = EXACT_STATUS;
196 case ROUNDING_TO_ZERO:
197 if (!remainder_h && (CT.w[0] < reciprocals10_64[extra_digits]))
198 status = EXACT_STATUS;
199 //if(!C64 && rmode==ROUNDING_DOWN) sign_s=sign_y;
203 __add_carry_out (tmp, carry, CT.w[0],
204 reciprocals10_64[extra_digits]);
205 if ((remainder_h >> (64 - amount)) + carry >=
206 (((UINT64) 1) << amount))
207 status = EXACT_STATUS;
210 __set_status_flags (pfpsf, status);
213 res = very_fast_get_BID64_small_mantissa (sign_x, exponent_y, C64);
217 if (total_digits < 0) {
218 #ifdef SET_STATUS_FLAGS
219 __set_status_flags (pfpsf, INEXACT_EXCEPTION);
222 #ifndef IEEE_ROUND_NEAREST_TIES_AWAY
223 #ifndef IEEE_ROUND_NEAREST
225 if (sign_x && (unsigned) (rmode - 1) < 2)
227 if (rmode == ROUNDING_UP)
231 res = very_fast_get_BID64_small_mantissa (sign_x, exponent_y, C64);
234 // else more than 16 digits in coefficient
235 #ifdef SET_STATUS_FLAGS
236 __set_status_flags (pfpsf, INVALID_EXCEPTION);
238 res = 0x7c00000000000000ull;