1 /* Copyright (C) 2007-2014 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 3, or (at your option) any later
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
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.
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/>. */
25 #include "bid_div_macros.h"
28 BID128_FUNCTION_ARG2_NORND_CUSTOMRESTYPE (UINT128
, bid128_rem
, x
, y
)
31 UINT128 CX
, CY
, CX2
, CQ
, CR
, T
, CXS
, P128
, res
;
32 UINT64 sign_x
, sign_y
, valid_y
;
35 int exponent_x
, exponent_y
, diff_expon
, bin_expon_cx
, scale
,
38 // unpack arguments, check for NaN or Infinity
40 valid_y
= unpack_BID128_value (&sign_y
, &exponent_y
, &CY
, y
);
42 if (!unpack_BID128_value (&sign_x
, &exponent_x
, &CX
, x
)) {
43 #ifdef SET_STATUS_FLAGS
44 if ((y
.w
[1] & SNAN_MASK64
) == SNAN_MASK64
) // y is sNaN
45 __set_status_flags (pfpsf
, INVALID_EXCEPTION
);
48 if ((x
.w
[1] & 0x7c00000000000000ull
) == 0x7c00000000000000ull
) {
49 #ifdef SET_STATUS_FLAGS
50 if ((x
.w
[1] & SNAN_MASK64
) == SNAN_MASK64
) // y is sNaN
51 __set_status_flags (pfpsf
, INVALID_EXCEPTION
);
53 res
.w
[1] = CX
.w
[1] & QUIET_MASK64
;
58 if ((x
.w
[1] & 0x7800000000000000ull
) == 0x7800000000000000ull
) {
60 if (((y
.w
[1] & 0x7c00000000000000ull
) != 0x7c00000000000000ull
))
63 #ifdef SET_STATUS_FLAGS
65 __set_status_flags (pfpsf
, INVALID_EXCEPTION
);
67 res
.w
[1] = 0x7c00000000000000ull
;
74 if ((!CY
.w
[1]) && (!CY
.w
[0])) {
75 #ifdef SET_STATUS_FLAGS
77 __set_status_flags (pfpsf
, INVALID_EXCEPTION
);
80 res
.w
[1] = 0x7c00000000000000ull
;
84 if (valid_y
|| ((y
.w
[1] & NAN_MASK64
) == INFINITY_MASK64
)) {
86 if ((exponent_x
> exponent_y
)
87 && ((y
.w
[1] & NAN_MASK64
) != INFINITY_MASK64
))
88 exponent_x
= exponent_y
;
90 res
.w
[1] = sign_x
| (((UINT64
) exponent_x
) << 49);
99 if ((y
.w
[1] & 0x7c00000000000000ull
) == 0x7c00000000000000ull
) {
100 #ifdef SET_STATUS_FLAGS
101 if ((y
.w
[1] & SNAN_MASK64
) == SNAN_MASK64
) // y is sNaN
102 __set_status_flags (pfpsf
, INVALID_EXCEPTION
);
104 res
.w
[1] = CY
.w
[1] & QUIET_MASK64
;
109 if ((y
.w
[1] & 0x7800000000000000ull
) == 0x7800000000000000ull
) {
116 #ifdef SET_STATUS_FLAGS
118 __set_status_flags (pfpsf
, INVALID_EXCEPTION
);
120 res
.w
[1] = 0x7c00000000000000ull
;
125 diff_expon
= exponent_x
- exponent_y
;
127 if (diff_expon
<= 0) {
128 diff_expon
= -diff_expon
;
130 if (diff_expon
> 34) {
131 // |x|<|y| in this case
135 // set exponent of y to exponent_x, scale coefficient_y
136 T
= power10_table_128
[diff_expon
];
137 __mul_128x128_to_256 (P256
, CY
, T
);
139 if (P256
.w
[2] || P256
.w
[3]) {
140 // |x|<|y| in this case
145 CX2
.w
[1] = (CX
.w
[1] << 1) | (CX
.w
[0] >> 63);
146 CX2
.w
[0] = CX
.w
[0] << 1;
147 if (__unsigned_compare_ge_128 (P256
, CX2
)) {
148 // |x|<|y| in this case
153 P128
.w
[0] = P256
.w
[0];
154 P128
.w
[1] = P256
.w
[1];
155 __div_128_by_128 (&CQ
, &CR
, CX
, P128
);
157 CX2
.w
[1] = (CR
.w
[1] << 1) | (CR
.w
[0] >> 63);
158 CX2
.w
[0] = CR
.w
[0] << 1;
159 if ((__unsigned_compare_gt_128 (CX2
, P256
))
160 || (CX2
.w
[1] == P256
.w
[1] && CX2
.w
[0] == P256
.w
[0]
162 __sub_128_128 (CR
, P256
, CR
);
163 sign_x
^= 0x8000000000000000ull
;
166 get_BID128_very_fast (&res
, sign_x
, exponent_x
, CR
);
176 while (diff_expon
> 0) {
177 // get number of digits in CX and scale=38-digits
179 fx
.d
= (float) CX
.w
[1] * f64
.d
+ (float) CX
.w
[0];
180 bin_expon_cx
= ((fx
.i
>> 23) & 0xff) - 0x7f;
181 scale
= scale0
- estimate_decimal_digits
[bin_expon_cx
];
182 // scale = 38-estimate_decimal_digits[bin_expon_cx];
183 D
= CX
.w
[1] - power10_index_binexp_128
[bin_expon_cx
].w
[1];
185 || (!D
&& CX
.w
[0] >= power10_index_binexp_128
[bin_expon_cx
].w
[0]))
188 if (diff_expon
>= scale
)
195 T
= power10_table_128
[scale
];
196 __mul_128x128_low (CXS
, CX
, T
);
198 __div_128_by_128 (&CQ
, &CX
, CXS
, CY
);
200 // check for remainder == 0
201 if (!CX
.w
[1] && !CX
.w
[0]) {
202 get_BID128_very_fast (&res
, sign_x
, exponent_y
, CX
);
207 CX2
.w
[1] = (CX
.w
[1] << 1) | (CX
.w
[0] >> 63);
208 CX2
.w
[0] = CX
.w
[0] << 1;
209 if ((__unsigned_compare_gt_128 (CX2
, CY
))
210 || (CX2
.w
[1] == CY
.w
[1] && CX2
.w
[0] == CY
.w
[0] && (CQ
.w
[0] & 1))) {
211 __sub_128_128 (CX
, CY
, CX
);
212 sign_x
^= 0x8000000000000000ull
;
215 get_BID128_very_fast (&res
, sign_x
, exponent_y
, CX
);