1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
/* Copyright (C) 2007-2013 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
#include "bid_internal.h"
/*****************************************************************************
* BID64_round_integral_exact
****************************************************************************/
#if DECIMAL_CALL_BY_REFERENCE
void
bid64_from_int32 (UINT64 * pres,
int *px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
int x = *px;
#else
UINT64
bid64_from_int32 (int x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
#endif
UINT64 res;
// if integer is negative, put the absolute value
// in the lowest 32bits of the result
if ((x & SIGNMASK32) == SIGNMASK32) {
// negative int32
x = ~x + 1; // 2's complement of x
res = (unsigned int) x | 0xb1c0000000000000ull;
// (exp << 53)) = biased exp. is 0
} else { // positive int32
res = x | 0x31c0000000000000ull; // (exp << 53)) = biased exp. is 0
}
BID_RETURN (res);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid64_from_uint32 (UINT64 * pres, unsigned int *px
_EXC_MASKS_PARAM _EXC_INFO_PARAM) {
unsigned int x = *px;
#else
UINT64
bid64_from_uint32 (unsigned int x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
#endif
UINT64 res;
res = x | 0x31c0000000000000ull; // (exp << 53)) = biased exp. is 0
BID_RETURN (res);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid64_from_int64 (UINT64 * pres, SINT64 * px
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
SINT64 x = *px;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT64
bid64_from_int64 (SINT64 x
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
UINT64 res;
UINT64 x_sign, C;
unsigned int q, ind;
int incr_exp = 0;
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0;
int is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0;
x_sign = x & 0x8000000000000000ull;
// if the integer is negative, use the absolute value
if (x_sign)
C = ~((UINT64) x) + 1;
else
C = x;
if (C <= BID64_SIG_MAX) { // |C| <= 10^16-1 and the result is exact
if (C < 0x0020000000000000ull) { // C < 2^53
res = x_sign | 0x31c0000000000000ull | C;
} else { // C >= 2^53
res =
x_sign | 0x6c70000000000000ull | (C & 0x0007ffffffffffffull);
}
} else { // |C| >= 10^16 and the result may be inexact
// the smallest |C| is 10^16 which has 17 decimal digits
// the largest |C| is 0x8000000000000000 = 9223372036854775808 w/ 19 digits
if (C < 0x16345785d8a0000ull) { // x < 10^17
q = 17;
ind = 1; // number of digits to remove for q = 17
} else if (C < 0xde0b6b3a7640000ull) { // C < 10^18
q = 18;
ind = 2; // number of digits to remove for q = 18
} else { // C < 10^19
q = 19;
ind = 3; // number of digits to remove for q = 19
}
// overflow and underflow are not possible
// Note: performace can be improved by inlining this call
round64_2_18 ( // will work for 19 digits too if C fits in 64 bits
q, ind, C, &res, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint);
if (incr_exp)
ind++;
// set the inexact flag
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint ||
is_midpoint_lt_even || is_midpoint_gt_even)
*pfpsf |= INEXACT_EXCEPTION;
// general correction from RN to RA, RM, RP, RZ; result uses ind for exp
if (rnd_mode != ROUNDING_TO_NEAREST) {
if ((!x_sign
&& ((rnd_mode == ROUNDING_UP && is_inexact_lt_midpoint)
||
((rnd_mode == ROUNDING_TIES_AWAY
|| rnd_mode == ROUNDING_UP) && is_midpoint_gt_even)))
|| (x_sign
&& ((rnd_mode == ROUNDING_DOWN && is_inexact_lt_midpoint)
||
((rnd_mode == ROUNDING_TIES_AWAY
|| rnd_mode == ROUNDING_DOWN)
&& is_midpoint_gt_even)))) {
res = res + 1;
if (res == 0x002386f26fc10000ull) { // res = 10^16 => rounding overflow
res = 0x00038d7ea4c68000ull; // 10^15
ind = ind + 1;
}
} else if ((is_midpoint_lt_even || is_inexact_gt_midpoint) &&
((x_sign && (rnd_mode == ROUNDING_UP ||
rnd_mode == ROUNDING_TO_ZERO)) ||
(!x_sign && (rnd_mode == ROUNDING_DOWN ||
rnd_mode == ROUNDING_TO_ZERO)))) {
res = res - 1;
// check if we crossed into the lower decade
if (res == 0x00038d7ea4c67fffull) { // 10^15 - 1
res = 0x002386f26fc0ffffull; // 10^16 - 1
ind = ind - 1;
}
} else {
; // exact, the result is already correct
}
}
if (res < 0x0020000000000000ull) { // res < 2^53
res = x_sign | (((UINT64) ind + 398) << 53) | res;
} else { // res >= 2^53
res =
x_sign | 0x6000000000000000ull | (((UINT64) ind + 398) << 51) |
(res & 0x0007ffffffffffffull);
}
}
BID_RETURN (res);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid64_from_uint64 (UINT64 * pres, UINT64 * px
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT64 x = *px;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT64
bid64_from_uint64 (UINT64 x
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
UINT64 res;
UINT128 x128, res128;
unsigned int q, ind;
int incr_exp = 0;
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0;
int is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0;
if (x <= BID64_SIG_MAX) { // x <= 10^16-1 and the result is exact
if (x < 0x0020000000000000ull) { // x < 2^53
res = 0x31c0000000000000ull | x;
} else { // x >= 2^53
res = 0x6c70000000000000ull | (x & 0x0007ffffffffffffull);
}
} else { // x >= 10^16 and the result may be inexact
// the smallest x is 10^16 which has 17 decimal digits
// the largest x is 0xffffffffffffffff = 18446744073709551615 w/ 20 digits
if (x < 0x16345785d8a0000ull) { // x < 10^17
q = 17;
ind = 1; // number of digits to remove for q = 17
} else if (x < 0xde0b6b3a7640000ull) { // x < 10^18
q = 18;
ind = 2; // number of digits to remove for q = 18
} else if (x < 0x8ac7230489e80000ull) { // x < 10^19
q = 19;
ind = 3; // number of digits to remove for q = 19
} else { // x < 10^20
q = 20;
ind = 4; // number of digits to remove for q = 20
}
// overflow and underflow are not possible
// Note: performace can be improved by inlining this call
if (q <= 19) {
round64_2_18 ( // will work for 20 digits too if x fits in 64 bits
q, ind, x, &res, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint);
} else { // q = 20
x128.w[1] = 0x0;
x128.w[0] = x;
round128_19_38 (q, ind, x128, &res128, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint);
res = res128.w[0]; // res.w[1] is 0
}
if (incr_exp)
ind++;
// set the inexact flag
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint ||
is_midpoint_lt_even || is_midpoint_gt_even)
*pfpsf |= INEXACT_EXCEPTION;
// general correction from RN to RA, RM, RP, RZ; result uses ind for exp
if (rnd_mode != ROUNDING_TO_NEAREST) {
if ((rnd_mode == ROUNDING_UP && is_inexact_lt_midpoint) ||
((rnd_mode == ROUNDING_TIES_AWAY || rnd_mode == ROUNDING_UP)
&& is_midpoint_gt_even)) {
res = res + 1;
if (res == 0x002386f26fc10000ull) { // res = 10^16 => rounding overflow
res = 0x00038d7ea4c68000ull; // 10^15
ind = ind + 1;
}
} else if ((is_midpoint_lt_even || is_inexact_gt_midpoint) &&
(rnd_mode == ROUNDING_DOWN ||
rnd_mode == ROUNDING_TO_ZERO)) {
res = res - 1;
// check if we crossed into the lower decade
if (res == 0x00038d7ea4c67fffull) { // 10^15 - 1
res = 0x002386f26fc0ffffull; // 10^16 - 1
ind = ind - 1;
}
} else {
; // exact, the result is already correct
}
}
if (res < 0x0020000000000000ull) { // res < 2^53
res = (((UINT64) ind + 398) << 53) | res;
} else { // res >= 2^53
res = 0x6000000000000000ull | (((UINT64) ind + 398) << 51) |
(res & 0x0007ffffffffffffull);
}
}
BID_RETURN (res);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid128_from_int32 (UINT128 * pres,
int *px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
int x = *px;
#else
UINT128
bid128_from_int32 (int x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
#endif
UINT128 res;
// if integer is negative, use the absolute value
if ((x & SIGNMASK32) == SIGNMASK32) {
res.w[HIGH_128W] = 0xb040000000000000ull;
res.w[LOW_128W] = ~((unsigned int) x) + 1; // 2's complement of x
} else {
res.w[HIGH_128W] = 0x3040000000000000ull;
res.w[LOW_128W] = (unsigned int) x;
}
BID_RETURN (res);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid128_from_uint32 (UINT128 * pres, unsigned int *px
_EXC_MASKS_PARAM _EXC_INFO_PARAM) {
unsigned int x = *px;
#else
UINT128
bid128_from_uint32 (unsigned int x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
#endif
UINT128 res;
res.w[HIGH_128W] = 0x3040000000000000ull;
res.w[LOW_128W] = x;
BID_RETURN (res);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid128_from_int64 (UINT128 * pres, SINT64 * px
_EXC_MASKS_PARAM _EXC_INFO_PARAM) {
SINT64 x = *px;
#else
UINT128
bid128_from_int64 (SINT64 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
#endif
UINT128 res;
// if integer is negative, use the absolute value
if ((x & SIGNMASK64) == SIGNMASK64) {
res.w[HIGH_128W] = 0xb040000000000000ull;
res.w[LOW_128W] = ~x + 1; // 2's complement of x
} else {
res.w[HIGH_128W] = 0x3040000000000000ull;
res.w[LOW_128W] = x;
}
BID_RETURN (res);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid128_from_uint64 (UINT128 * pres, UINT64 * px
_EXC_MASKS_PARAM _EXC_INFO_PARAM) {
UINT64 x = *px;
#else
UINT128
bid128_from_uint64 (UINT64 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
#endif
UINT128 res;
res.w[HIGH_128W] = 0x3040000000000000ull;
res.w[LOW_128W] = x;
BID_RETURN (res);
}
|