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_div_macros.h"
33 BID128_FUNCTION_ARG2_NORND_CUSTOMRESTYPE (UINT128, bid128_rem, x, y)
36 UINT128 CX, CY, CX2, CQ, CR, T, CXS, P128, res;
37 UINT64 sign_x, sign_y, valid_y;
40 int exponent_x, exponent_y, diff_expon, bin_expon_cx, scale,
43 // unpack arguments, check for NaN or Infinity
45 valid_y = unpack_BID128_value (&sign_y, &exponent_y, &CY, y);
47 if (!unpack_BID128_value (&sign_x, &exponent_x, &CX, x)) {
48 #ifdef SET_STATUS_FLAGS
49 if ((y.w[1] & SNAN_MASK64) == SNAN_MASK64) // y is sNaN
50 __set_status_flags (pfpsf, INVALID_EXCEPTION);
53 if ((x.w[1] & 0x7c00000000000000ull) == 0x7c00000000000000ull) {
54 #ifdef SET_STATUS_FLAGS
55 if ((x.w[1] & SNAN_MASK64) == SNAN_MASK64) // y is sNaN
56 __set_status_flags (pfpsf, INVALID_EXCEPTION);
58 res.w[1] = CX.w[1] & QUIET_MASK64;
63 if ((x.w[1] & 0x7800000000000000ull) == 0x7800000000000000ull) {
65 if (((y.w[1] & 0x7c00000000000000ull) != 0x7c00000000000000ull))
68 #ifdef SET_STATUS_FLAGS
70 __set_status_flags (pfpsf, INVALID_EXCEPTION);
72 res.w[1] = 0x7c00000000000000ull;
79 if ((!CY.w[1]) && (!CY.w[0])) {
80 #ifdef SET_STATUS_FLAGS
82 __set_status_flags (pfpsf, INVALID_EXCEPTION);
85 res.w[1] = 0x7c00000000000000ull;
89 if (valid_y || ((y.w[1] & NAN_MASK64) == INFINITY_MASK64)) {
91 if ((exponent_x > exponent_y)
92 && ((y.w[1] & NAN_MASK64) != INFINITY_MASK64))
93 exponent_x = exponent_y;
95 res.w[1] = sign_x | (((UINT64) exponent_x) << 49);
104 if ((y.w[1] & 0x7c00000000000000ull) == 0x7c00000000000000ull) {
105 #ifdef SET_STATUS_FLAGS
106 if ((y.w[1] & SNAN_MASK64) == SNAN_MASK64) // y is sNaN
107 __set_status_flags (pfpsf, INVALID_EXCEPTION);
109 res.w[1] = CY.w[1] & QUIET_MASK64;
114 if ((y.w[1] & 0x7800000000000000ull) == 0x7800000000000000ull) {
121 #ifdef SET_STATUS_FLAGS
123 __set_status_flags (pfpsf, INVALID_EXCEPTION);
125 res.w[1] = 0x7c00000000000000ull;
130 diff_expon = exponent_x - exponent_y;
132 if (diff_expon <= 0) {
133 diff_expon = -diff_expon;
135 if (diff_expon > 34) {
136 // |x|<|y| in this case
140 // set exponent of y to exponent_x, scale coefficient_y
141 T = power10_table_128[diff_expon];
142 __mul_128x128_to_256 (P256, CY, T);
144 if (P256.w[2] || P256.w[3]) {
145 // |x|<|y| in this case
150 CX2.w[1] = (CX.w[1] << 1) | (CX.w[0] >> 63);
151 CX2.w[0] = CX.w[0] << 1;
152 if (__unsigned_compare_ge_128 (P256, CX2)) {
153 // |x|<|y| in this case
158 P128.w[0] = P256.w[0];
159 P128.w[1] = P256.w[1];
160 __div_128_by_128 (&CQ, &CR, CX, P128);
162 CX2.w[1] = (CR.w[1] << 1) | (CR.w[0] >> 63);
163 CX2.w[0] = CR.w[0] << 1;
164 if ((__unsigned_compare_gt_128 (CX2, P256))
165 || (CX2.w[1] == P256.w[1] && CX2.w[0] == P256.w[0]
167 __sub_128_128 (CR, P256, CR);
168 sign_x ^= 0x8000000000000000ull;
171 get_BID128_very_fast (&res, sign_x, exponent_x, CR);
181 while (diff_expon > 0) {
182 // get number of digits in CX and scale=38-digits
184 fx.d = (float) CX.w[1] * f64.d + (float) CX.w[0];
185 bin_expon_cx = ((fx.i >> 23) & 0xff) - 0x7f;
186 scale = scale0 - estimate_decimal_digits[bin_expon_cx];
187 // scale = 38-estimate_decimal_digits[bin_expon_cx];
188 D = CX.w[1] - power10_index_binexp_128[bin_expon_cx].w[1];
190 || (!D && CX.w[0] >= power10_index_binexp_128[bin_expon_cx].w[0]))
193 if (diff_expon >= scale)
200 T = power10_table_128[scale];
201 __mul_128x128_low (CXS, CX, T);
203 __div_128_by_128 (&CQ, &CX, CXS, CY);
205 // check for remainder == 0
206 if (!CX.w[1] && !CX.w[0]) {
207 get_BID128_very_fast (&res, sign_x, exponent_y, CX);
212 CX2.w[1] = (CX.w[1] << 1) | (CX.w[0] >> 63);
213 CX2.w[0] = CX.w[0] << 1;
214 if ((__unsigned_compare_gt_128 (CX2, CY))
215 || (CX2.w[1] == CY.w[1] && CX2.w[0] == CY.w[0] && (CQ.w[0] & 1))) {
216 __sub_128_128 (CX, CY, CX);
217 sign_x ^= 0x8000000000000000ull;
220 get_BID128_very_fast (&res, sign_x, exponent_y, CX);