| 1 |
/* Copyright (C) 2007-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
This file is part of GCC. |
| 4 |
|
| 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 3, or (at your option) any later |
| 8 |
version. |
| 9 |
|
| 10 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
for more details. |
| 14 |
|
| 15 |
Under Section 7 of GPL version 3, you are granted additional |
| 16 |
permissions described in the GCC Runtime Library Exception, version |
| 17 |
3.1, as published by the Free Software Foundation. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License and |
| 20 |
a copy of the GCC Runtime Library Exception along with this program; |
| 21 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 22 |
<http://www.gnu.org/licenses/>. */ |
| 23 |
|
| 24 |
#include "bid_internal.h" |
| 25 |
|
| 26 |
/* |
| 27 |
* Takes a BID32 as input and converts it to a BID64 and returns it. |
| 28 |
*/ |
| 29 |
TYPE0_FUNCTION_ARGTYPE1_NORND (UINT64, bid32_to_bid64, UINT32, x) |
| 30 |
|
| 31 |
UINT64 res; |
| 32 |
UINT32 sign_x; |
| 33 |
int exponent_x; |
| 34 |
UINT32 coefficient_x; |
| 35 |
|
| 36 |
if (!unpack_BID32 (&sign_x, &exponent_x, &coefficient_x, x)) { |
| 37 |
// Inf, NaN, 0 |
| 38 |
if (((x) & 0x78000000) == 0x78000000) { |
| 39 |
if (((x) & 0x7e000000) == 0x7e000000) { // sNaN |
| 40 |
#ifdef SET_STATUS_FLAGS |
| 41 |
__set_status_flags (pfpsf, INVALID_EXCEPTION); |
| 42 |
#endif |
| 43 |
} |
| 44 |
res = (coefficient_x & 0x000fffff); |
| 45 |
res *= 1000000000; |
| 46 |
res |= ((((UINT64) coefficient_x) << 32) & 0xfc00000000000000ull); |
| 47 |
|
| 48 |
BID_RETURN (res); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
res = |
| 53 |
very_fast_get_BID64_small_mantissa (((UINT64) sign_x) << 32, |
| 54 |
exponent_x + |
| 55 |
DECIMAL_EXPONENT_BIAS - |
| 56 |
DECIMAL_EXPONENT_BIAS_32, |
| 57 |
(UINT64) coefficient_x); |
| 58 |
BID_RETURN (res); |
| 59 |
} // convert_bid32_to_bid64 |
| 60 |
|
| 61 |
|
| 62 |
/* |
| 63 |
* Takes a BID64 as input and converts it to a BID32 and returns it. |
| 64 |
*/ |
| 65 |
#if DECIMAL_CALL_BY_REFERENCE |
| 66 |
|
| 67 |
void |
| 68 |
bid64_to_bid32 (UINT32 * pres, |
| 69 |
UINT64 * |
| 70 |
px _RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 71 |
_EXC_INFO_PARAM) { |
| 72 |
UINT64 x; |
| 73 |
#else |
| 74 |
|
| 75 |
UINT32 |
| 76 |
bid64_to_bid32 (UINT64 x _RND_MODE_PARAM _EXC_FLAGS_PARAM |
| 77 |
_EXC_MASKS_PARAM _EXC_INFO_PARAM) { |
| 78 |
#endif |
| 79 |
UINT128 Q; |
| 80 |
UINT64 sign_x, coefficient_x, remainder_h, carry, Stemp; |
| 81 |
UINT32 res; |
| 82 |
UINT64 t64; |
| 83 |
int_float tempx; |
| 84 |
int exponent_x, bin_expon_cx, extra_digits, rmode = 0, amount; |
| 85 |
unsigned status = 0; |
| 86 |
|
| 87 |
#if DECIMAL_CALL_BY_REFERENCE |
| 88 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 89 |
_IDEC_round rnd_mode = *prnd_mode; |
| 90 |
#endif |
| 91 |
x = *px; |
| 92 |
#endif |
| 93 |
|
| 94 |
// unpack arguments, check for NaN or Infinity, 0 |
| 95 |
if (!unpack_BID64 (&sign_x, &exponent_x, &coefficient_x, x)) { |
| 96 |
if (((x) & 0x7800000000000000ull) == 0x7800000000000000ull) { |
| 97 |
t64 = (coefficient_x & 0x0003ffffffffffffull); |
| 98 |
res = t64/1000000000ull; |
| 99 |
//res = (coefficient_x & 0x0003ffffffffffffull); |
| 100 |
//res /= 1000000000ull; |
| 101 |
res |= ((coefficient_x >> 32) & 0xfc000000); |
| 102 |
#ifdef SET_STATUS_FLAGS |
| 103 |
if ((x & SNAN_MASK64) == SNAN_MASK64) // sNaN |
| 104 |
__set_status_flags (pfpsf, INVALID_EXCEPTION); |
| 105 |
#endif |
| 106 |
BID_RETURN (res); |
| 107 |
} |
| 108 |
exponent_x = |
| 109 |
exponent_x - DECIMAL_EXPONENT_BIAS + DECIMAL_EXPONENT_BIAS_32; |
| 110 |
if (exponent_x < 0) |
| 111 |
exponent_x = 0; |
| 112 |
if (exponent_x > DECIMAL_MAX_EXPON_32) |
| 113 |
exponent_x = DECIMAL_MAX_EXPON_32; |
| 114 |
res = (sign_x >> 32) | (exponent_x << 23); |
| 115 |
BID_RETURN (res); |
| 116 |
} |
| 117 |
|
| 118 |
exponent_x = |
| 119 |
exponent_x - DECIMAL_EXPONENT_BIAS + DECIMAL_EXPONENT_BIAS_32; |
| 120 |
|
| 121 |
// check number of digits |
| 122 |
if (coefficient_x >= 10000000) { |
| 123 |
tempx.d = (float) coefficient_x; |
| 124 |
bin_expon_cx = ((tempx.i >> 23) & 0xff) - 0x7f; |
| 125 |
extra_digits = estimate_decimal_digits[bin_expon_cx] - 7; |
| 126 |
// add test for range |
| 127 |
if (coefficient_x >= power10_index_binexp[bin_expon_cx]) |
| 128 |
extra_digits++; |
| 129 |
|
| 130 |
#ifndef IEEE_ROUND_NEAREST_TIES_AWAY |
| 131 |
#ifndef IEEE_ROUND_NEAREST |
| 132 |
rmode = rnd_mode; |
| 133 |
if (sign_x && (unsigned) (rmode - 1) < 2) |
| 134 |
rmode = 3 - rmode; |
| 135 |
#else |
| 136 |
rmode = 0; |
| 137 |
#endif |
| 138 |
#else |
| 139 |
rmode = 0; |
| 140 |
#endif |
| 141 |
|
| 142 |
exponent_x += extra_digits; |
| 143 |
if ((exponent_x < 0) && (exponent_x + MAX_FORMAT_DIGITS_32 >= 0)) { |
| 144 |
status = UNDERFLOW_EXCEPTION; |
| 145 |
extra_digits -= exponent_x; |
| 146 |
exponent_x = 0; |
| 147 |
} |
| 148 |
coefficient_x += round_const_table[rmode][extra_digits]; |
| 149 |
__mul_64x64_to_128 (Q, coefficient_x, |
| 150 |
reciprocals10_64[extra_digits]); |
| 151 |
|
| 152 |
// now get P/10^extra_digits: shift Q_high right by M[extra_digits]-128 |
| 153 |
amount = short_recip_scale[extra_digits]; |
| 154 |
|
| 155 |
coefficient_x = Q.w[1] >> amount; |
| 156 |
|
| 157 |
#ifndef IEEE_ROUND_NEAREST_TIES_AWAY |
| 158 |
#ifndef IEEE_ROUND_NEAREST |
| 159 |
if (rmode == 0) //ROUNDING_TO_NEAREST |
| 160 |
#endif |
| 161 |
if (coefficient_x & 1) { |
| 162 |
// check whether fractional part of initial_P/10^extra_digits |
| 163 |
// is exactly .5 |
| 164 |
|
| 165 |
// get remainder |
| 166 |
remainder_h = Q.w[1] << (64 - amount); |
| 167 |
|
| 168 |
if (!remainder_h && (Q.w[0] < reciprocals10_64[extra_digits])) |
| 169 |
coefficient_x--; |
| 170 |
} |
| 171 |
#endif |
| 172 |
|
| 173 |
#ifdef SET_STATUS_FLAGS |
| 174 |
|
| 175 |
{ |
| 176 |
status |= INEXACT_EXCEPTION; |
| 177 |
// get remainder |
| 178 |
remainder_h = Q.w[1] << (64 - amount); |
| 179 |
|
| 180 |
switch (rmode) { |
| 181 |
case ROUNDING_TO_NEAREST: |
| 182 |
case ROUNDING_TIES_AWAY: |
| 183 |
// test whether fractional part is 0 |
| 184 |
if (remainder_h == 0x8000000000000000ull |
| 185 |
&& (Q.w[0] < reciprocals10_64[extra_digits])) |
| 186 |
status = EXACT_STATUS; |
| 187 |
break; |
| 188 |
case ROUNDING_DOWN: |
| 189 |
case ROUNDING_TO_ZERO: |
| 190 |
if (!remainder_h && (Q.w[0] < reciprocals10_64[extra_digits])) |
| 191 |
status = EXACT_STATUS; |
| 192 |
break; |
| 193 |
default: |
| 194 |
// round up |
| 195 |
__add_carry_out (Stemp, carry, Q.w[0], |
| 196 |
reciprocals10_64[extra_digits]); |
| 197 |
if ((remainder_h >> (64 - amount)) + carry >= |
| 198 |
(((UINT64) 1) << amount)) |
| 199 |
status = EXACT_STATUS; |
| 200 |
} |
| 201 |
|
| 202 |
if (status != EXACT_STATUS) |
| 203 |
__set_status_flags (pfpsf, status); |
| 204 |
} |
| 205 |
|
| 206 |
#endif |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
res = |
| 211 |
get_BID32 ((UINT32) (sign_x >> 32), |
| 212 |
exponent_x, coefficient_x, rnd_mode, pfpsf); |
| 213 |
BID_RETURN (res); |
| 214 |
|
| 215 |
} |