gcc/libgcc/config/libbid/bid_round.c
H.J. Lu b2a00c8984 Makefile.in (dfp-filenames): Replace decimal_globals...
libgcc/

2007-09-27  H.J. Lu  <hongjiu.lu@intel.com>

	* Makefile.in (dfp-filenames): Replace decimal_globals,
	decimal_data, binarydecimal and convert_data with
	bid_decimal_globals, bid_decimal_data, bid_binarydecimal
	and bid_convert_data, respectively.

libgcc/config/libbid/

2007-09-27  H.J. Lu  <hongjiu.lu@intel.com>

	* bid128_fromstring.c: Removed.

	* bid_dpd.c: New from libbid 2007-09-26.
	* bid128_to_int16.c: Likewise.
	* bid128_to_int8.c: Likewise.
	* bid128_to_uint8.c: Likewise.
	* bid128_to_uint16.c: Likewise.
	* bid64_to_int16.c: Likewise.
	* bid64_to_int8.c: Likewise.
	* bid64_to_uint16.c: Likewise.
	* bid64_to_uint8.c: Likewise.

	* bid128_2_str.h: Updated from libbid 2007-09-26.
	* bid128_2_str_macros.h: Likewise.
	* bid128_2_str_tables.c: Likewise.
	* bid128_add.c: Likewise.
	* bid128.c: Likewise.
	* bid128_compare.c: Likewise.
	* bid128_div.c: Likewise.
	* bid128_fma.c: Likewise.
	* bid128_logb.c: Likewise.
	* bid128_minmax.c: Likewise.
	* bid128_mul.c: Likewise.
	* bid128_next.c: Likewise.
	* bid128_noncomp.c: Likewise.
	* bid128_quantize.c: Likewise.
	* bid128_rem.c: Likewise.
	* bid128_round_integral.c: Likewise.
	* bid128_scalb.c: Likewise.
	* bid128_sqrt.c: Likewise.
	* bid128_string.c: Likewise.
	* bid128_to_int32.c: Likewise.
	* bid128_to_int64.c: Likewise.
	* bid128_to_uint32.c: Likewise.
	* bid128_to_uint64.c: Likewise.
	* bid32_to_bid128.c: Likewise.
	* bid32_to_bid64.c: Likewise.
	* bid64_add.c: Likewise.
	* bid64_compare.c: Likewise.
	* bid64_div.c: Likewise.
	* bid64_fma.c: Likewise.
	* bid64_logb.c: Likewise.
	* bid64_minmax.c: Likewise.
	* bid64_mul.c: Likewise.
	* bid64_next.c: Likewise.
	* bid64_noncomp.c: Likewise.
	* bid64_quantize.c: Likewise.
	* bid64_rem.c: Likewise.
	* bid64_round_integral.c: Likewise.
	* bid64_scalb.c: Likewise.
	* bid64_sqrt.c: Likewise.
	* bid64_string.c: Likewise.
	* bid64_to_bid128.c: Likewise.
	* bid64_to_int32.c: Likewise.
	* bid64_to_int64.c: Likewise.
	* bid64_to_uint32.c: Likewise.
	* bid64_to_uint64.c: Likewise.
	* bid_b2d.h: Likewise.
	* bid_binarydecimal.c: Likewise.
	* bid_conf.h: Likewise.
	* bid_convert_data.c: Likewise.
	* bid_decimal_data.c: Likewise.
	* bid_decimal_globals.c: Likewise.
	* bid_div_macros.h: Likewise.
	* bid_flag_operations.c: Likewise.
	* bid_from_int.c: Likewise.
	* bid_functions.h: Likewise.
	* bid_gcc_intrinsics.h: Likewise.
	* bid_inline_add.h: Likewise.
	* bid_internal.h: Likewise.
	* bid_round.c: Likewise.
	* bid_sqrt_macros.h: Likewise.
	* _addsub_dd.c: Likewise.
	* _addsub_sd.c: Likewise.
	* _addsub_td.c: Likewise.
	* _dd_to_df.c: Likewise.
	* _dd_to_di.c: Likewise.
	* _dd_to_sd.c: Likewise.
	* _dd_to_sf.c: Likewise.
	* _dd_to_si.c: Likewise.
	* _dd_to_td.c: Likewise.
	* _dd_to_tf.c: Likewise.
	* _dd_to_udi.c: Likewise.
	* _dd_to_usi.c: Likewise.
	* _dd_to_xf.c: Likewise.
	* _df_to_dd.c: Likewise.
	* _df_to_sd.c: Likewise.
	* _df_to_td.c: Likewise.
	* _di_to_dd.c: Likewise.
	* _di_to_sd.c: Likewise.
	* _di_to_td.c: Likewise.
	* _div_dd.c: Likewise.
	* _div_sd.c: Likewise.
	* _div_td.c: Likewise.
	* _eq_dd.c: Likewise.
	* _eq_sd.c: Likewise.
	* _eq_td.c: Likewise.
	* _ge_dd.c: Likewise.
	* _ge_sd.c: Likewise.
	* _ge_td.c: Likewise.
	* _gt_dd.c: Likewise.
	* _gt_sd.c: Likewise.
	* _gt_td.c: Likewise.
	* _isinfd128.c: Likewise.
	* _isinfd32.c: Likewise.
	* _isinfd64.c: Likewise.
	* _le_dd.c: Likewise.
	* _le_sd.c: Likewise.
	* _le_td.c: Likewise.
	* _lt_dd.c: Likewise.
	* _lt_sd.c: Likewise.
	* _lt_td.c: Likewise.
	* _mul_dd.c: Likewise.
	* _mul_sd.c: Likewise.
	* _mul_td.c: Likewise.
	* _ne_dd.c: Likewise.
	* _ne_sd.c: Likewise.
	* _ne_td.c: Likewise.
	* _sd_to_dd.c: Likewise.
	* _sd_to_df.c: Likewise.
	* _sd_to_di.c: Likewise.
	* _sd_to_sf.c: Likewise.
	* _sd_to_si.c: Likewise.
	* _sd_to_td.c: Likewise.
	* _sd_to_tf.c: Likewise.
	* _sd_to_udi.c: Likewise.
	* _sd_to_usi.c: Likewise.
	* _sd_to_xf.c: Likewise.
	* _sf_to_dd.c: Likewise.
	* _sf_to_sd.c: Likewise.
	* _sf_to_td.c: Likewise.
	* _si_to_dd.c: Likewise.
	* _si_to_sd.c: Likewise.
	* _si_to_td.c: Likewise.
	* _td_to_dd.c: Likewise.
	* _td_to_df.c: Likewise.
	* _td_to_di.c: Likewise.
	* _td_to_sd.c: Likewise.
	* _td_to_sf.c: Likewise.
	* _td_to_si.c: Likewise.
	* _td_to_tf.c: Likewise.
	* _td_to_udi.c: Likewise.
	* _td_to_usi.c: Likewise.
	* _td_to_xf.c: Likewise.
	* _tf_to_dd.c: Likewise.
	* _tf_to_sd.c: Likewise.
	* _tf_to_td.c: Likewise.
	* _udi_to_dd.c: Likewise.
	* _udi_to_sd.c: Likewise.
	* _udi_to_td.c: Likewise.
	* _unord_dd.c: Likewise.
	* _unord_sd.c: Likewise.
	* _unord_td.c: Likewise.
	* _usi_to_dd.c: Likewise.
	* _usi_to_sd.c: Likewise.
	* _usi_to_td.c: Likewise.
	* _xf_to_dd.c: Likewise.
	* _xf_to_sd.c: Likewise.
	* _xf_to_td.c: Likewise.

2007-09-27  H.J. Lu  <hongjiu.lu@intel.com>

	* b2d.h: Renamed to ...
	* bid_b2d.h: This.

	* bid128_to_string.c: Renamed to ...
	* bid128_string.c: This.

	* bid_intrinsics.h: Renamed to ...
	* bid_gcc_intrinsics.h: This.

	* bid_string.c: Renamed to ...
	* bid64_string.c: This.

	* binarydecimal.c: Renamed to ...
	* bid_decimal_globals.c: This.

	* convert_data.c: Renamed to ...
	* bid_convert_data.c: This.

	* decimal_data.c: Renamed to ...
	* bid_decimal_data.c: This.

	* decimal_globals.c: Renamed to ...
	* bid_decimal_globals.c: This.

	* div_macros.h: Renamed to ...
	* bid_div_macros.h: This.

	* inline_bid_add.h: Renamed to ...
	* bid_inline_add.h: This.

	* sqrt_macros.h: Renamed to ...
	* bid_sqrt_macros.h: This.

From-SVN: r128841
2007-09-27 10:47:23 -07:00

1055 lines
39 KiB
C

/* Copyright (C) 2007 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 2, or (at your option) any later
version.
In addition to the permissions in the GNU General Public License, the
Free Software Foundation gives you unlimited permission to link the
compiled version of this file into combinations with other programs,
and to distribute those combinations without any restriction coming
from the use of this file. (The General Public License restrictions
do apply in other respects; for example, they cover modification of
the file, and distribution when not linked into a combine
executable.)
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.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING. If not, write to the Free
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA. */
/*****************************************************************************
*
* BID64 encoding:
* ****************************************
* 63 62 53 52 0
* |---|------------------|--------------|
* | S | Biased Exp (E) | Coeff (c) |
* |---|------------------|--------------|
*
* bias = 398
* number = (-1)^s * 10^(E-398) * c
* coefficient range - 0 to (2^53)-1
* COEFF_MAX = 2^53-1 = 9007199254740991
*
*****************************************************************************
*
* BID128 encoding:
* 1-bit sign
* 14-bit biased exponent in [0x21, 0x3020] = [33, 12320]
* unbiased exponent in [-6176, 6111]; exponent bias = 6176
* 113-bit unsigned binary integer coefficient (49-bit high + 64-bit low)
* Note: 10^34-1 ~ 2^112.945555... < 2^113 => coefficient fits in 113 bits
*
* Note: assume invalid encodings are not passed to this function
*
* Round a number C with q decimal digits, represented as a binary integer
* to q - x digits. Six different routines are provided for different values
* of q. The maximum value of q used in the library is q = 3 * P - 1 where
* P = 16 or P = 34 (so q <= 111 decimal digits).
* The partitioning is based on the following, where Kx is the scaled
* integer representing the value of 10^(-x) rounded up to a number of bits
* sufficient to ensure correct rounding:
*
* --------------------------------------------------------------------------
* q x max. value of a max number min. number
* of bits in C of bits in Kx
* --------------------------------------------------------------------------
*
* GROUP 1: 64 bits
* round64_2_18 ()
*
* 2 [1,1] 10^1 - 1 < 2^3.33 4 4
* ... ... ... ... ...
* 18 [1,17] 10^18 - 1 < 2^59.80 60 61
*
* GROUP 2: 128 bits
* round128_19_38 ()
*
* 19 [1,18] 10^19 - 1 < 2^63.11 64 65
* 20 [1,19] 10^20 - 1 < 2^66.44 67 68
* ... ... ... ... ...
* 38 [1,37] 10^38 - 1 < 2^126.24 127 128
*
* GROUP 3: 192 bits
* round192_39_57 ()
*
* 39 [1,38] 10^39 - 1 < 2^129.56 130 131
* ... ... ... ... ...
* 57 [1,56] 10^57 - 1 < 2^189.35 190 191
*
* GROUP 4: 256 bits
* round256_58_76 ()
*
* 58 [1,57] 10^58 - 1 < 2^192.68 193 194
* ... ... ... ... ...
* 76 [1,75] 10^76 - 1 < 2^252.47 253 254
*
* GROUP 5: 320 bits
* round320_77_96 ()
*
* 77 [1,76] 10^77 - 1 < 2^255.79 256 257
* 78 [1,77] 10^78 - 1 < 2^259.12 260 261
* ... ... ... ... ...
* 96 [1,95] 10^96 - 1 < 2^318.91 319 320
*
* GROUP 6: 384 bits
* round384_97_115 ()
*
* 97 [1,96] 10^97 - 1 < 2^322.23 323 324
* ... ... ... ... ...
* 115 [1,114] 10^115 - 1 < 2^382.03 383 384
*
****************************************************************************/
#include "bid_internal.h"
void
round64_2_18 (int q,
int x,
UINT64 C,
UINT64 * ptr_Cstar,
int *incr_exp,
int *ptr_is_midpoint_lt_even,
int *ptr_is_midpoint_gt_even,
int *ptr_is_inexact_lt_midpoint,
int *ptr_is_inexact_gt_midpoint) {
UINT128 P128;
UINT128 fstar;
UINT64 Cstar;
UINT64 tmp64;
int shift;
int ind;
// Note:
// In round128_2_18() positive numbers with 2 <= q <= 18 will be
// rounded to nearest only for 1 <= x <= 3:
// x = 1 or x = 2 when q = 17
// x = 2 or x = 3 when q = 18
// However, for generality and possible uses outside the frame of IEEE 754R
// this implementation works for 1 <= x <= q - 1
// assume *ptr_is_midpoint_lt_even, *ptr_is_midpoint_gt_even,
// *ptr_is_inexact_lt_midpoint, and *ptr_is_inexact_gt_midpoint are
// initialized to 0 by the caller
// round a number C with q decimal digits, 2 <= q <= 18
// to q - x digits, 1 <= x <= 17
// C = C + 1/2 * 10^x where the result C fits in 64 bits
// (because the largest value is 999999999999999999 + 50000000000000000 =
// 0x0e92596fd628ffff, which fits in 60 bits)
ind = x - 1; // 0 <= ind <= 16
C = C + midpoint64[ind];
// kx ~= 10^(-x), kx = Kx64[ind] * 2^(-Ex), 0 <= ind <= 16
// P128 = (C + 1/2 * 10^x) * kx * 2^Ex = (C + 1/2 * 10^x) * Kx
// the approximation kx of 10^(-x) was rounded up to 64 bits
__mul_64x64_to_128MACH (P128, C, Kx64[ind]);
// calculate C* = floor (P128) and f*
// Cstar = P128 >> Ex
// fstar = low Ex bits of P128
shift = Ex64m64[ind]; // in [3, 56]
Cstar = P128.w[1] >> shift;
fstar.w[1] = P128.w[1] & mask64[ind];
fstar.w[0] = P128.w[0];
// the top Ex bits of 10^(-x) are T* = ten2mxtrunc64[ind], e.g.
// if x=1, T*=ten2mxtrunc64[0]=0xcccccccccccccccc
// if (0 < f* < 10^(-x)) then the result is a midpoint
// if floor(C*) is even then C* = floor(C*) - logical right
// shift; C* has q - x decimal digits, correct by Prop. 1)
// else if floor(C*) is odd C* = floor(C*)-1 (logical right
// shift; C* has q - x decimal digits, correct by Pr. 1)
// else
// C* = floor(C*) (logical right shift; C has q - x decimal digits,
// correct by Property 1)
// in the caling function n = C* * 10^(e+x)
// determine inexactness of the rounding of C*
// if (0 < f* - 1/2 < 10^(-x)) then
// the result is exact
// else // if (f* - 1/2 > T*) then
// the result is inexact
if (fstar.w[1] > half64[ind] ||
(fstar.w[1] == half64[ind] && fstar.w[0])) {
// f* > 1/2 and the result may be exact
// Calculate f* - 1/2
tmp64 = fstar.w[1] - half64[ind];
if (tmp64 || fstar.w[0] > ten2mxtrunc64[ind]) { // f* - 1/2 > 10^(-x)
*ptr_is_inexact_lt_midpoint = 1;
} // else the result is exact
} else { // the result is inexact; f2* <= 1/2
*ptr_is_inexact_gt_midpoint = 1;
}
// check for midpoints (could do this before determining inexactness)
if (fstar.w[1] == 0 && fstar.w[0] <= ten2mxtrunc64[ind]) {
// the result is a midpoint
if (Cstar & 0x01) { // Cstar is odd; MP in [EVEN, ODD]
// if floor(C*) is odd C = floor(C*) - 1; the result may be 0
Cstar--; // Cstar is now even
*ptr_is_midpoint_gt_even = 1;
*ptr_is_inexact_lt_midpoint = 0;
*ptr_is_inexact_gt_midpoint = 0;
} else { // else MP in [ODD, EVEN]
*ptr_is_midpoint_lt_even = 1;
*ptr_is_inexact_lt_midpoint = 0;
*ptr_is_inexact_gt_midpoint = 0;
}
}
// check for rounding overflow, which occurs if Cstar = 10^(q-x)
ind = q - x; // 1 <= ind <= q - 1
if (Cstar == ten2k64[ind]) { // if Cstar = 10^(q-x)
Cstar = ten2k64[ind - 1]; // Cstar = 10^(q-x-1)
*incr_exp = 1;
} else { // 10^33 <= Cstar <= 10^34 - 1
*incr_exp = 0;
}
*ptr_Cstar = Cstar;
}
void
round128_19_38 (int q,
int x,
UINT128 C,
UINT128 * ptr_Cstar,
int *incr_exp,
int *ptr_is_midpoint_lt_even,
int *ptr_is_midpoint_gt_even,
int *ptr_is_inexact_lt_midpoint,
int *ptr_is_inexact_gt_midpoint) {
UINT256 P256;
UINT256 fstar;
UINT128 Cstar;
UINT64 tmp64;
int shift;
int ind;
// Note:
// In round128_19_38() positive numbers with 19 <= q <= 38 will be
// rounded to nearest only for 1 <= x <= 23:
// x = 3 or x = 4 when q = 19
// x = 4 or x = 5 when q = 20
// ...
// x = 18 or x = 19 when q = 34
// x = 1 or x = 2 or x = 19 or x = 20 when q = 35
// x = 2 or x = 3 or x = 20 or x = 21 when q = 36
// x = 3 or x = 4 or x = 21 or x = 22 when q = 37
// x = 4 or x = 5 or x = 22 or x = 23 when q = 38
// However, for generality and possible uses outside the frame of IEEE 754R
// this implementation works for 1 <= x <= q - 1
// assume *ptr_is_midpoint_lt_even, *ptr_is_midpoint_gt_even,
// *ptr_is_inexact_lt_midpoint, and *ptr_is_inexact_gt_midpoint are
// initialized to 0 by the caller
// round a number C with q decimal digits, 19 <= q <= 38
// to q - x digits, 1 <= x <= 37
// C = C + 1/2 * 10^x where the result C fits in 128 bits
// (because the largest value is 99999999999999999999999999999999999999 +
// 5000000000000000000000000000000000000 =
// 0x4efe43b0c573e7e68a043d8fffffffff, which fits is 127 bits)
ind = x - 1; // 0 <= ind <= 36
if (ind <= 18) { // if 0 <= ind <= 18
tmp64 = C.w[0];
C.w[0] = C.w[0] + midpoint64[ind];
if (C.w[0] < tmp64)
C.w[1]++;
} else { // if 19 <= ind <= 37
tmp64 = C.w[0];
C.w[0] = C.w[0] + midpoint128[ind - 19].w[0];
if (C.w[0] < tmp64) {
C.w[1]++;
}
C.w[1] = C.w[1] + midpoint128[ind - 19].w[1];
}
// kx ~= 10^(-x), kx = Kx128[ind] * 2^(-Ex), 0 <= ind <= 36
// P256 = (C + 1/2 * 10^x) * kx * 2^Ex = (C + 1/2 * 10^x) * Kx
// the approximation kx of 10^(-x) was rounded up to 128 bits
__mul_128x128_to_256 (P256, C, Kx128[ind]);
// calculate C* = floor (P256) and f*
// Cstar = P256 >> Ex
// fstar = low Ex bits of P256
shift = Ex128m128[ind]; // in [2, 63] but have to consider two cases
if (ind <= 18) { // if 0 <= ind <= 18
Cstar.w[0] = (P256.w[2] >> shift) | (P256.w[3] << (64 - shift));
Cstar.w[1] = (P256.w[3] >> shift);
fstar.w[0] = P256.w[0];
fstar.w[1] = P256.w[1];
fstar.w[2] = P256.w[2] & mask128[ind];
fstar.w[3] = 0x0ULL;
} else { // if 19 <= ind <= 37
Cstar.w[0] = P256.w[3] >> shift;
Cstar.w[1] = 0x0ULL;
fstar.w[0] = P256.w[0];
fstar.w[1] = P256.w[1];
fstar.w[2] = P256.w[2];
fstar.w[3] = P256.w[3] & mask128[ind];
}
// the top Ex bits of 10^(-x) are T* = ten2mxtrunc64[ind], e.g.
// if x=1, T*=ten2mxtrunc128[0]=0xcccccccccccccccccccccccccccccccc
// if (0 < f* < 10^(-x)) then the result is a midpoint
// if floor(C*) is even then C* = floor(C*) - logical right
// shift; C* has q - x decimal digits, correct by Prop. 1)
// else if floor(C*) is odd C* = floor(C*)-1 (logical right
// shift; C* has q - x decimal digits, correct by Pr. 1)
// else
// C* = floor(C*) (logical right shift; C has q - x decimal digits,
// correct by Property 1)
// in the caling function n = C* * 10^(e+x)
// determine inexactness of the rounding of C*
// if (0 < f* - 1/2 < 10^(-x)) then
// the result is exact
// else // if (f* - 1/2 > T*) then
// the result is inexact
if (ind <= 18) { // if 0 <= ind <= 18
if (fstar.w[2] > half128[ind] ||
(fstar.w[2] == half128[ind] && (fstar.w[1] || fstar.w[0]))) {
// f* > 1/2 and the result may be exact
// Calculate f* - 1/2
tmp64 = fstar.w[2] - half128[ind];
if (tmp64 || fstar.w[1] > ten2mxtrunc128[ind].w[1] || (fstar.w[1] == ten2mxtrunc128[ind].w[1] && fstar.w[0] > ten2mxtrunc128[ind].w[0])) { // f* - 1/2 > 10^(-x)
*ptr_is_inexact_lt_midpoint = 1;
} // else the result is exact
} else { // the result is inexact; f2* <= 1/2
*ptr_is_inexact_gt_midpoint = 1;
}
} else { // if 19 <= ind <= 37
if (fstar.w[3] > half128[ind] || (fstar.w[3] == half128[ind] &&
(fstar.w[2] || fstar.w[1]
|| fstar.w[0]))) {
// f* > 1/2 and the result may be exact
// Calculate f* - 1/2
tmp64 = fstar.w[3] - half128[ind];
if (tmp64 || fstar.w[2] || fstar.w[1] > ten2mxtrunc128[ind].w[1] || (fstar.w[1] == ten2mxtrunc128[ind].w[1] && fstar.w[0] > ten2mxtrunc128[ind].w[0])) { // f* - 1/2 > 10^(-x)
*ptr_is_inexact_lt_midpoint = 1;
} // else the result is exact
} else { // the result is inexact; f2* <= 1/2
*ptr_is_inexact_gt_midpoint = 1;
}
}
// check for midpoints (could do this before determining inexactness)
if (fstar.w[3] == 0 && fstar.w[2] == 0 &&
(fstar.w[1] < ten2mxtrunc128[ind].w[1] ||
(fstar.w[1] == ten2mxtrunc128[ind].w[1] &&
fstar.w[0] <= ten2mxtrunc128[ind].w[0]))) {
// the result is a midpoint
if (Cstar.w[0] & 0x01) { // Cstar is odd; MP in [EVEN, ODD]
// if floor(C*) is odd C = floor(C*) - 1; the result may be 0
Cstar.w[0]--; // Cstar is now even
if (Cstar.w[0] == 0xffffffffffffffffULL) {
Cstar.w[1]--;
}
*ptr_is_midpoint_gt_even = 1;
*ptr_is_inexact_lt_midpoint = 0;
*ptr_is_inexact_gt_midpoint = 0;
} else { // else MP in [ODD, EVEN]
*ptr_is_midpoint_lt_even = 1;
*ptr_is_inexact_lt_midpoint = 0;
*ptr_is_inexact_gt_midpoint = 0;
}
}
// check for rounding overflow, which occurs if Cstar = 10^(q-x)
ind = q - x; // 1 <= ind <= q - 1
if (ind <= 19) {
if (Cstar.w[1] == 0x0ULL && Cstar.w[0] == ten2k64[ind]) {
// if Cstar = 10^(q-x)
Cstar.w[0] = ten2k64[ind - 1]; // Cstar = 10^(q-x-1)
*incr_exp = 1;
} else {
*incr_exp = 0;
}
} else if (ind == 20) {
// if ind = 20
if (Cstar.w[1] == ten2k128[0].w[1]
&& Cstar.w[0] == ten2k128[0].w[0]) {
// if Cstar = 10^(q-x)
Cstar.w[0] = ten2k64[19]; // Cstar = 10^(q-x-1)
Cstar.w[1] = 0x0ULL;
*incr_exp = 1;
} else {
*incr_exp = 0;
}
} else { // if 21 <= ind <= 37
if (Cstar.w[1] == ten2k128[ind - 20].w[1] &&
Cstar.w[0] == ten2k128[ind - 20].w[0]) {
// if Cstar = 10^(q-x)
Cstar.w[0] = ten2k128[ind - 21].w[0]; // Cstar = 10^(q-x-1)
Cstar.w[1] = ten2k128[ind - 21].w[1];
*incr_exp = 1;
} else {
*incr_exp = 0;
}
}
ptr_Cstar->w[1] = Cstar.w[1];
ptr_Cstar->w[0] = Cstar.w[0];
}
void
round192_39_57 (int q,
int x,
UINT192 C,
UINT192 * ptr_Cstar,
int *incr_exp,
int *ptr_is_midpoint_lt_even,
int *ptr_is_midpoint_gt_even,
int *ptr_is_inexact_lt_midpoint,
int *ptr_is_inexact_gt_midpoint) {
UINT384 P384;
UINT384 fstar;
UINT192 Cstar;
UINT64 tmp64;
int shift;
int ind;
// Note:
// In round192_39_57() positive numbers with 39 <= q <= 57 will be
// rounded to nearest only for 5 <= x <= 42:
// x = 23 or x = 24 or x = 5 or x = 6 when q = 39
// x = 24 or x = 25 or x = 6 or x = 7 when q = 40
// ...
// x = 41 or x = 42 or x = 23 or x = 24 when q = 57
// However, for generality and possible uses outside the frame of IEEE 754R
// this implementation works for 1 <= x <= q - 1
// assume *ptr_is_midpoint_lt_even, *ptr_is_midpoint_gt_even,
// *ptr_is_inexact_lt_midpoint, and *ptr_is_inexact_gt_midpoint are
// initialized to 0 by the caller
// round a number C with q decimal digits, 39 <= q <= 57
// to q - x digits, 1 <= x <= 56
// C = C + 1/2 * 10^x where the result C fits in 192 bits
// (because the largest value is
// 999999999999999999999999999999999999999999999999999999999 +
// 50000000000000000000000000000000000000000000000000000000 =
// 0x2ad282f212a1da846afdaf18c034ff09da7fffffffffffff, which fits in 190 bits)
ind = x - 1; // 0 <= ind <= 55
if (ind <= 18) { // if 0 <= ind <= 18
tmp64 = C.w[0];
C.w[0] = C.w[0] + midpoint64[ind];
if (C.w[0] < tmp64) {
C.w[1]++;
if (C.w[1] == 0x0) {
C.w[2]++;
}
}
} else if (ind <= 37) { // if 19 <= ind <= 37
tmp64 = C.w[0];
C.w[0] = C.w[0] + midpoint128[ind - 19].w[0];
if (C.w[0] < tmp64) {
C.w[1]++;
if (C.w[1] == 0x0) {
C.w[2]++;
}
}
tmp64 = C.w[1];
C.w[1] = C.w[1] + midpoint128[ind - 19].w[1];
if (C.w[1] < tmp64) {
C.w[2]++;
}
} else { // if 38 <= ind <= 57 (actually ind <= 55)
tmp64 = C.w[0];
C.w[0] = C.w[0] + midpoint192[ind - 38].w[0];
if (C.w[0] < tmp64) {
C.w[1]++;
if (C.w[1] == 0x0ull) {
C.w[2]++;
}
}
tmp64 = C.w[1];
C.w[1] = C.w[1] + midpoint192[ind - 38].w[1];
if (C.w[1] < tmp64) {
C.w[2]++;
}
C.w[2] = C.w[2] + midpoint192[ind - 38].w[2];
}
// kx ~= 10^(-x), kx = Kx192[ind] * 2^(-Ex), 0 <= ind <= 55
// P384 = (C + 1/2 * 10^x) * kx * 2^Ex = (C + 1/2 * 10^x) * Kx
// the approximation kx of 10^(-x) was rounded up to 192 bits
__mul_192x192_to_384 (P384, C, Kx192[ind]);
// calculate C* = floor (P384) and f*
// Cstar = P384 >> Ex
// fstar = low Ex bits of P384
shift = Ex192m192[ind]; // in [1, 63] but have to consider three cases
if (ind <= 18) { // if 0 <= ind <= 18
Cstar.w[2] = (P384.w[5] >> shift);
Cstar.w[1] = (P384.w[5] << (64 - shift)) | (P384.w[4] >> shift);
Cstar.w[0] = (P384.w[4] << (64 - shift)) | (P384.w[3] >> shift);
fstar.w[5] = 0x0ULL;
fstar.w[4] = 0x0ULL;
fstar.w[3] = P384.w[3] & mask192[ind];
fstar.w[2] = P384.w[2];
fstar.w[1] = P384.w[1];
fstar.w[0] = P384.w[0];
} else if (ind <= 37) { // if 19 <= ind <= 37
Cstar.w[2] = 0x0ULL;
Cstar.w[1] = P384.w[5] >> shift;
Cstar.w[0] = (P384.w[5] << (64 - shift)) | (P384.w[4] >> shift);
fstar.w[5] = 0x0ULL;
fstar.w[4] = P384.w[4] & mask192[ind];
fstar.w[3] = P384.w[3];
fstar.w[2] = P384.w[2];
fstar.w[1] = P384.w[1];
fstar.w[0] = P384.w[0];
} else { // if 38 <= ind <= 57
Cstar.w[2] = 0x0ULL;
Cstar.w[1] = 0x0ULL;
Cstar.w[0] = P384.w[5] >> shift;
fstar.w[5] = P384.w[5] & mask192[ind];
fstar.w[4] = P384.w[4];
fstar.w[3] = P384.w[3];
fstar.w[2] = P384.w[2];
fstar.w[1] = P384.w[1];
fstar.w[0] = P384.w[0];
}
// the top Ex bits of 10^(-x) are T* = ten2mxtrunc192[ind], e.g. if x=1,
// T*=ten2mxtrunc192[0]=0xcccccccccccccccccccccccccccccccccccccccccccccccc
// if (0 < f* < 10^(-x)) then the result is a midpoint
// if floor(C*) is even then C* = floor(C*) - logical right
// shift; C* has q - x decimal digits, correct by Prop. 1)
// else if floor(C*) is odd C* = floor(C*)-1 (logical right
// shift; C* has q - x decimal digits, correct by Pr. 1)
// else
// C* = floor(C*) (logical right shift; C has q - x decimal digits,
// correct by Property 1)
// in the caling function n = C* * 10^(e+x)
// determine inexactness of the rounding of C*
// if (0 < f* - 1/2 < 10^(-x)) then
// the result is exact
// else // if (f* - 1/2 > T*) then
// the result is inexact
if (ind <= 18) { // if 0 <= ind <= 18
if (fstar.w[3] > half192[ind] || (fstar.w[3] == half192[ind] &&
(fstar.w[2] || fstar.w[1]
|| fstar.w[0]))) {
// f* > 1/2 and the result may be exact
// Calculate f* - 1/2
tmp64 = fstar.w[3] - half192[ind];
if (tmp64 || fstar.w[2] > ten2mxtrunc192[ind].w[2] || (fstar.w[2] == ten2mxtrunc192[ind].w[2] && fstar.w[1] > ten2mxtrunc192[ind].w[1]) || (fstar.w[2] == ten2mxtrunc192[ind].w[2] && fstar.w[1] == ten2mxtrunc192[ind].w[1] && fstar.w[0] > ten2mxtrunc192[ind].w[0])) { // f* - 1/2 > 10^(-x)
*ptr_is_inexact_lt_midpoint = 1;
} // else the result is exact
} else { // the result is inexact; f2* <= 1/2
*ptr_is_inexact_gt_midpoint = 1;
}
} else if (ind <= 37) { // if 19 <= ind <= 37
if (fstar.w[4] > half192[ind] || (fstar.w[4] == half192[ind] &&
(fstar.w[3] || fstar.w[2]
|| fstar.w[1] || fstar.w[0]))) {
// f* > 1/2 and the result may be exact
// Calculate f* - 1/2
tmp64 = fstar.w[4] - half192[ind];
if (tmp64 || fstar.w[3] || fstar.w[2] > ten2mxtrunc192[ind].w[2] || (fstar.w[2] == ten2mxtrunc192[ind].w[2] && fstar.w[1] > ten2mxtrunc192[ind].w[1]) || (fstar.w[2] == ten2mxtrunc192[ind].w[2] && fstar.w[1] == ten2mxtrunc192[ind].w[1] && fstar.w[0] > ten2mxtrunc192[ind].w[0])) { // f* - 1/2 > 10^(-x)
*ptr_is_inexact_lt_midpoint = 1;
} // else the result is exact
} else { // the result is inexact; f2* <= 1/2
*ptr_is_inexact_gt_midpoint = 1;
}
} else { // if 38 <= ind <= 55
if (fstar.w[5] > half192[ind] || (fstar.w[5] == half192[ind] &&
(fstar.w[4] || fstar.w[3]
|| fstar.w[2] || fstar.w[1]
|| fstar.w[0]))) {
// f* > 1/2 and the result may be exact
// Calculate f* - 1/2
tmp64 = fstar.w[5] - half192[ind];
if (tmp64 || fstar.w[4] || fstar.w[3] || fstar.w[2] > ten2mxtrunc192[ind].w[2] || (fstar.w[2] == ten2mxtrunc192[ind].w[2] && fstar.w[1] > ten2mxtrunc192[ind].w[1]) || (fstar.w[2] == ten2mxtrunc192[ind].w[2] && fstar.w[1] == ten2mxtrunc192[ind].w[1] && fstar.w[0] > ten2mxtrunc192[ind].w[0])) { // f* - 1/2 > 10^(-x)
*ptr_is_inexact_lt_midpoint = 1;
} // else the result is exact
} else { // the result is inexact; f2* <= 1/2
*ptr_is_inexact_gt_midpoint = 1;
}
}
// check for midpoints (could do this before determining inexactness)
if (fstar.w[5] == 0 && fstar.w[4] == 0 && fstar.w[3] == 0 &&
(fstar.w[2] < ten2mxtrunc192[ind].w[2] ||
(fstar.w[2] == ten2mxtrunc192[ind].w[2] &&
fstar.w[1] < ten2mxtrunc192[ind].w[1]) ||
(fstar.w[2] == ten2mxtrunc192[ind].w[2] &&
fstar.w[1] == ten2mxtrunc192[ind].w[1] &&
fstar.w[0] <= ten2mxtrunc192[ind].w[0]))) {
// the result is a midpoint
if (Cstar.w[0] & 0x01) { // Cstar is odd; MP in [EVEN, ODD]
// if floor(C*) is odd C = floor(C*) - 1; the result may be 0
Cstar.w[0]--; // Cstar is now even
if (Cstar.w[0] == 0xffffffffffffffffULL) {
Cstar.w[1]--;
if (Cstar.w[1] == 0xffffffffffffffffULL) {
Cstar.w[2]--;
}
}
*ptr_is_midpoint_gt_even = 1;
*ptr_is_inexact_lt_midpoint = 0;
*ptr_is_inexact_gt_midpoint = 0;
} else { // else MP in [ODD, EVEN]
*ptr_is_midpoint_lt_even = 1;
*ptr_is_inexact_lt_midpoint = 0;
*ptr_is_inexact_gt_midpoint = 0;
}
}
// check for rounding overflow, which occurs if Cstar = 10^(q-x)
ind = q - x; // 1 <= ind <= q - 1
if (ind <= 19) {
if (Cstar.w[2] == 0x0ULL && Cstar.w[1] == 0x0ULL &&
Cstar.w[0] == ten2k64[ind]) {
// if Cstar = 10^(q-x)
Cstar.w[0] = ten2k64[ind - 1]; // Cstar = 10^(q-x-1)
*incr_exp = 1;
} else {
*incr_exp = 0;
}
} else if (ind == 20) {
// if ind = 20
if (Cstar.w[2] == 0x0ULL && Cstar.w[1] == ten2k128[0].w[1] &&
Cstar.w[0] == ten2k128[0].w[0]) {
// if Cstar = 10^(q-x)
Cstar.w[0] = ten2k64[19]; // Cstar = 10^(q-x-1)
Cstar.w[1] = 0x0ULL;
*incr_exp = 1;
} else {
*incr_exp = 0;
}
} else if (ind <= 38) { // if 21 <= ind <= 38
if (Cstar.w[2] == 0x0ULL && Cstar.w[1] == ten2k128[ind - 20].w[1] &&
Cstar.w[0] == ten2k128[ind - 20].w[0]) {
// if Cstar = 10^(q-x)
Cstar.w[0] = ten2k128[ind - 21].w[0]; // Cstar = 10^(q-x-1)
Cstar.w[1] = ten2k128[ind - 21].w[1];
*incr_exp = 1;
} else {
*incr_exp = 0;
}
} else if (ind == 39) {
if (Cstar.w[2] == ten2k256[0].w[2] && Cstar.w[1] == ten2k256[0].w[1]
&& Cstar.w[0] == ten2k256[0].w[0]) {
// if Cstar = 10^(q-x)
Cstar.w[0] = ten2k128[18].w[0]; // Cstar = 10^(q-x-1)
Cstar.w[1] = ten2k128[18].w[1];
Cstar.w[2] = 0x0ULL;
*incr_exp = 1;
} else {
*incr_exp = 0;
}
} else { // if 40 <= ind <= 56
if (Cstar.w[2] == ten2k256[ind - 39].w[2] &&
Cstar.w[1] == ten2k256[ind - 39].w[1] &&
Cstar.w[0] == ten2k256[ind - 39].w[0]) {
// if Cstar = 10^(q-x)
Cstar.w[0] = ten2k256[ind - 40].w[0]; // Cstar = 10^(q-x-1)
Cstar.w[1] = ten2k256[ind - 40].w[1];
Cstar.w[2] = ten2k256[ind - 40].w[2];
*incr_exp = 1;
} else {
*incr_exp = 0;
}
}
ptr_Cstar->w[2] = Cstar.w[2];
ptr_Cstar->w[1] = Cstar.w[1];
ptr_Cstar->w[0] = Cstar.w[0];
}
void
round256_58_76 (int q,
int x,
UINT256 C,
UINT256 * ptr_Cstar,
int *incr_exp,
int *ptr_is_midpoint_lt_even,
int *ptr_is_midpoint_gt_even,
int *ptr_is_inexact_lt_midpoint,
int *ptr_is_inexact_gt_midpoint) {
UINT512 P512;
UINT512 fstar;
UINT256 Cstar;
UINT64 tmp64;
int shift;
int ind;
// Note:
// In round256_58_76() positive numbers with 58 <= q <= 76 will be
// rounded to nearest only for 24 <= x <= 61:
// x = 42 or x = 43 or x = 24 or x = 25 when q = 58
// x = 43 or x = 44 or x = 25 or x = 26 when q = 59
// ...
// x = 60 or x = 61 or x = 42 or x = 43 when q = 76
// However, for generality and possible uses outside the frame of IEEE 754R
// this implementation works for 1 <= x <= q - 1
// assume *ptr_is_midpoint_lt_even, *ptr_is_midpoint_gt_even,
// *ptr_is_inexact_lt_midpoint, and *ptr_is_inexact_gt_midpoint are
// initialized to 0 by the caller
// round a number C with q decimal digits, 58 <= q <= 76
// to q - x digits, 1 <= x <= 75
// C = C + 1/2 * 10^x where the result C fits in 256 bits
// (because the largest value is 9999999999999999999999999999999999999999
// 999999999999999999999999999999999999 + 500000000000000000000000000
// 000000000000000000000000000000000000000000000000 =
// 0x1736ca15d27a56cae15cf0e7b403d1f2bd6ebb0a50dc83ffffffffffffffffff,
// which fits in 253 bits)
ind = x - 1; // 0 <= ind <= 74
if (ind <= 18) { // if 0 <= ind <= 18
tmp64 = C.w[0];
C.w[0] = C.w[0] + midpoint64[ind];
if (C.w[0] < tmp64) {
C.w[1]++;
if (C.w[1] == 0x0) {
C.w[2]++;
if (C.w[2] == 0x0) {
C.w[3]++;
}
}
}
} else if (ind <= 37) { // if 19 <= ind <= 37
tmp64 = C.w[0];
C.w[0] = C.w[0] + midpoint128[ind - 19].w[0];
if (C.w[0] < tmp64) {
C.w[1]++;
if (C.w[1] == 0x0) {
C.w[2]++;
if (C.w[2] == 0x0) {
C.w[3]++;
}
}
}
tmp64 = C.w[1];
C.w[1] = C.w[1] + midpoint128[ind - 19].w[1];
if (C.w[1] < tmp64) {
C.w[2]++;
if (C.w[2] == 0x0) {
C.w[3]++;
}
}
} else if (ind <= 57) { // if 38 <= ind <= 57
tmp64 = C.w[0];
C.w[0] = C.w[0] + midpoint192[ind - 38].w[0];
if (C.w[0] < tmp64) {
C.w[1]++;
if (C.w[1] == 0x0ull) {
C.w[2]++;
if (C.w[2] == 0x0) {
C.w[3]++;
}
}
}
tmp64 = C.w[1];
C.w[1] = C.w[1] + midpoint192[ind - 38].w[1];
if (C.w[1] < tmp64) {
C.w[2]++;
if (C.w[2] == 0x0) {
C.w[3]++;
}
}
tmp64 = C.w[2];
C.w[2] = C.w[2] + midpoint192[ind - 38].w[2];
if (C.w[2] < tmp64) {
C.w[3]++;
}
} else { // if 58 <= ind <= 76 (actually 58 <= ind <= 74)
tmp64 = C.w[0];
C.w[0] = C.w[0] + midpoint256[ind - 58].w[0];
if (C.w[0] < tmp64) {
C.w[1]++;
if (C.w[1] == 0x0ull) {
C.w[2]++;
if (C.w[2] == 0x0) {
C.w[3]++;
}
}
}
tmp64 = C.w[1];
C.w[1] = C.w[1] + midpoint256[ind - 58].w[1];
if (C.w[1] < tmp64) {
C.w[2]++;
if (C.w[2] == 0x0) {
C.w[3]++;
}
}
tmp64 = C.w[2];
C.w[2] = C.w[2] + midpoint256[ind - 58].w[2];
if (C.w[2] < tmp64) {
C.w[3]++;
}
C.w[3] = C.w[3] + midpoint256[ind - 58].w[3];
}
// kx ~= 10^(-x), kx = Kx256[ind] * 2^(-Ex), 0 <= ind <= 74
// P512 = (C + 1/2 * 10^x) * kx * 2^Ex = (C + 1/2 * 10^x) * Kx
// the approximation kx of 10^(-x) was rounded up to 192 bits
__mul_256x256_to_512 (P512, C, Kx256[ind]);
// calculate C* = floor (P512) and f*
// Cstar = P512 >> Ex
// fstar = low Ex bits of P512
shift = Ex256m256[ind]; // in [0, 63] but have to consider four cases
if (ind <= 18) { // if 0 <= ind <= 18
Cstar.w[3] = (P512.w[7] >> shift);
Cstar.w[2] = (P512.w[7] << (64 - shift)) | (P512.w[6] >> shift);
Cstar.w[1] = (P512.w[6] << (64 - shift)) | (P512.w[5] >> shift);
Cstar.w[0] = (P512.w[5] << (64 - shift)) | (P512.w[4] >> shift);
fstar.w[7] = 0x0ULL;
fstar.w[6] = 0x0ULL;
fstar.w[5] = 0x0ULL;
fstar.w[4] = P512.w[4] & mask256[ind];
fstar.w[3] = P512.w[3];
fstar.w[2] = P512.w[2];
fstar.w[1] = P512.w[1];
fstar.w[0] = P512.w[0];
} else if (ind <= 37) { // if 19 <= ind <= 37
Cstar.w[3] = 0x0ULL;
Cstar.w[2] = P512.w[7] >> shift;
Cstar.w[1] = (P512.w[7] << (64 - shift)) | (P512.w[6] >> shift);
Cstar.w[0] = (P512.w[6] << (64 - shift)) | (P512.w[5] >> shift);
fstar.w[7] = 0x0ULL;
fstar.w[6] = 0x0ULL;
fstar.w[5] = P512.w[5] & mask256[ind];
fstar.w[4] = P512.w[4];
fstar.w[3] = P512.w[3];
fstar.w[2] = P512.w[2];
fstar.w[1] = P512.w[1];
fstar.w[0] = P512.w[0];
} else if (ind <= 56) { // if 38 <= ind <= 56
Cstar.w[3] = 0x0ULL;
Cstar.w[2] = 0x0ULL;
Cstar.w[1] = P512.w[7] >> shift;
Cstar.w[0] = (P512.w[7] << (64 - shift)) | (P512.w[6] >> shift);
fstar.w[7] = 0x0ULL;
fstar.w[6] = P512.w[6] & mask256[ind];
fstar.w[5] = P512.w[5];
fstar.w[4] = P512.w[4];
fstar.w[3] = P512.w[3];
fstar.w[2] = P512.w[2];
fstar.w[1] = P512.w[1];
fstar.w[0] = P512.w[0];
} else if (ind == 57) {
Cstar.w[3] = 0x0ULL;
Cstar.w[2] = 0x0ULL;
Cstar.w[1] = 0x0ULL;
Cstar.w[0] = P512.w[7];
fstar.w[7] = 0x0ULL;
fstar.w[6] = P512.w[6];
fstar.w[5] = P512.w[5];
fstar.w[4] = P512.w[4];
fstar.w[3] = P512.w[3];
fstar.w[2] = P512.w[2];
fstar.w[1] = P512.w[1];
fstar.w[0] = P512.w[0];
} else { // if 58 <= ind <= 74
Cstar.w[3] = 0x0ULL;
Cstar.w[2] = 0x0ULL;
Cstar.w[1] = 0x0ULL;
Cstar.w[0] = P512.w[7] >> shift;
fstar.w[7] = P512.w[7] & mask256[ind];
fstar.w[6] = P512.w[6];
fstar.w[5] = P512.w[5];
fstar.w[4] = P512.w[4];
fstar.w[3] = P512.w[3];
fstar.w[2] = P512.w[2];
fstar.w[1] = P512.w[1];
fstar.w[0] = P512.w[0];
}
// the top Ex bits of 10^(-x) are T* = ten2mxtrunc256[ind], e.g. if x=1,
// T*=ten2mxtrunc256[0]=
// 0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
// if (0 < f* < 10^(-x)) then the result is a midpoint
// if floor(C*) is even then C* = floor(C*) - logical right
// shift; C* has q - x decimal digits, correct by Prop. 1)
// else if floor(C*) is odd C* = floor(C*)-1 (logical right
// shift; C* has q - x decimal digits, correct by Pr. 1)
// else
// C* = floor(C*) (logical right shift; C has q - x decimal digits,
// correct by Property 1)
// in the caling function n = C* * 10^(e+x)
// determine inexactness of the rounding of C*
// if (0 < f* - 1/2 < 10^(-x)) then
// the result is exact
// else // if (f* - 1/2 > T*) then
// the result is inexact
if (ind <= 18) { // if 0 <= ind <= 18
if (fstar.w[4] > half256[ind] || (fstar.w[4] == half256[ind] &&
(fstar.w[3] || fstar.w[2]
|| fstar.w[1] || fstar.w[0]))) {
// f* > 1/2 and the result may be exact
// Calculate f* - 1/2
tmp64 = fstar.w[4] - half256[ind];
if (tmp64 || fstar.w[3] > ten2mxtrunc256[ind].w[2] || (fstar.w[3] == ten2mxtrunc256[ind].w[3] && fstar.w[2] > ten2mxtrunc256[ind].w[2]) || (fstar.w[3] == ten2mxtrunc256[ind].w[3] && fstar.w[2] == ten2mxtrunc256[ind].w[2] && fstar.w[1] > ten2mxtrunc256[ind].w[1]) || (fstar.w[3] == ten2mxtrunc256[ind].w[3] && fstar.w[2] == ten2mxtrunc256[ind].w[2] && fstar.w[1] == ten2mxtrunc256[ind].w[1] && fstar.w[0] > ten2mxtrunc256[ind].w[0])) { // f* - 1/2 > 10^(-x)
*ptr_is_inexact_lt_midpoint = 1;
} // else the result is exact
} else { // the result is inexact; f2* <= 1/2
*ptr_is_inexact_gt_midpoint = 1;
}
} else if (ind <= 37) { // if 19 <= ind <= 37
if (fstar.w[5] > half256[ind] || (fstar.w[5] == half256[ind] &&
(fstar.w[4] || fstar.w[3]
|| fstar.w[2] || fstar.w[1]
|| fstar.w[0]))) {
// f* > 1/2 and the result may be exact
// Calculate f* - 1/2
tmp64 = fstar.w[5] - half256[ind];
if (tmp64 || fstar.w[4] || fstar.w[3] > ten2mxtrunc256[ind].w[3] || (fstar.w[3] == ten2mxtrunc256[ind].w[3] && fstar.w[2] > ten2mxtrunc256[ind].w[2]) || (fstar.w[3] == ten2mxtrunc256[ind].w[3] && fstar.w[2] == ten2mxtrunc256[ind].w[2] && fstar.w[1] > ten2mxtrunc256[ind].w[1]) || (fstar.w[3] == ten2mxtrunc256[ind].w[3] && fstar.w[2] == ten2mxtrunc256[ind].w[2] && fstar.w[1] == ten2mxtrunc256[ind].w[1] && fstar.w[0] > ten2mxtrunc256[ind].w[0])) { // f* - 1/2 > 10^(-x)
*ptr_is_inexact_lt_midpoint = 1;
} // else the result is exact
} else { // the result is inexact; f2* <= 1/2
*ptr_is_inexact_gt_midpoint = 1;
}
} else if (ind <= 57) { // if 38 <= ind <= 57
if (fstar.w[6] > half256[ind] || (fstar.w[6] == half256[ind] &&
(fstar.w[5] || fstar.w[4]
|| fstar.w[3] || fstar.w[2]
|| fstar.w[1] || fstar.w[0]))) {
// f* > 1/2 and the result may be exact
// Calculate f* - 1/2
tmp64 = fstar.w[6] - half256[ind];
if (tmp64 || fstar.w[5] || fstar.w[4] || fstar.w[3] > ten2mxtrunc256[ind].w[3] || (fstar.w[3] == ten2mxtrunc256[ind].w[3] && fstar.w[2] > ten2mxtrunc256[ind].w[2]) || (fstar.w[3] == ten2mxtrunc256[ind].w[3] && fstar.w[2] == ten2mxtrunc256[ind].w[2] && fstar.w[1] > ten2mxtrunc256[ind].w[1]) || (fstar.w[3] == ten2mxtrunc256[ind].w[3] && fstar.w[2] == ten2mxtrunc256[ind].w[2] && fstar.w[1] == ten2mxtrunc256[ind].w[1] && fstar.w[0] > ten2mxtrunc256[ind].w[0])) { // f* - 1/2 > 10^(-x)
*ptr_is_inexact_lt_midpoint = 1;
} // else the result is exact
} else { // the result is inexact; f2* <= 1/2
*ptr_is_inexact_gt_midpoint = 1;
}
} else { // if 58 <= ind <= 74
if (fstar.w[7] > half256[ind] || (fstar.w[7] == half256[ind] &&
(fstar.w[6] || fstar.w[5]
|| fstar.w[4] || fstar.w[3]
|| fstar.w[2] || fstar.w[1]
|| fstar.w[0]))) {
// f* > 1/2 and the result may be exact
// Calculate f* - 1/2
tmp64 = fstar.w[7] - half256[ind];
if (tmp64 || fstar.w[6] || fstar.w[5] || fstar.w[4] || fstar.w[3] > ten2mxtrunc256[ind].w[3] || (fstar.w[3] == ten2mxtrunc256[ind].w[3] && fstar.w[2] > ten2mxtrunc256[ind].w[2]) || (fstar.w[3] == ten2mxtrunc256[ind].w[3] && fstar.w[2] == ten2mxtrunc256[ind].w[2] && fstar.w[1] > ten2mxtrunc256[ind].w[1]) || (fstar.w[3] == ten2mxtrunc256[ind].w[3] && fstar.w[2] == ten2mxtrunc256[ind].w[2] && fstar.w[1] == ten2mxtrunc256[ind].w[1] && fstar.w[0] > ten2mxtrunc256[ind].w[0])) { // f* - 1/2 > 10^(-x)
*ptr_is_inexact_lt_midpoint = 1;
} // else the result is exact
} else { // the result is inexact; f2* <= 1/2
*ptr_is_inexact_gt_midpoint = 1;
}
}
// check for midpoints (could do this before determining inexactness)
if (fstar.w[7] == 0 && fstar.w[6] == 0 &&
fstar.w[5] == 0 && fstar.w[4] == 0 &&
(fstar.w[3] < ten2mxtrunc256[ind].w[3] ||
(fstar.w[3] == ten2mxtrunc256[ind].w[3] &&
fstar.w[2] < ten2mxtrunc256[ind].w[2]) ||
(fstar.w[3] == ten2mxtrunc256[ind].w[3] &&
fstar.w[2] == ten2mxtrunc256[ind].w[2] &&
fstar.w[1] < ten2mxtrunc256[ind].w[1]) ||
(fstar.w[3] == ten2mxtrunc256[ind].w[3] &&
fstar.w[2] == ten2mxtrunc256[ind].w[2] &&
fstar.w[1] == ten2mxtrunc256[ind].w[1] &&
fstar.w[0] <= ten2mxtrunc256[ind].w[0]))) {
// the result is a midpoint
if (Cstar.w[0] & 0x01) { // Cstar is odd; MP in [EVEN, ODD]
// if floor(C*) is odd C = floor(C*) - 1; the result may be 0
Cstar.w[0]--; // Cstar is now even
if (Cstar.w[0] == 0xffffffffffffffffULL) {
Cstar.w[1]--;
if (Cstar.w[1] == 0xffffffffffffffffULL) {
Cstar.w[2]--;
if (Cstar.w[2] == 0xffffffffffffffffULL) {
Cstar.w[3]--;
}
}
}
*ptr_is_midpoint_gt_even = 1;
*ptr_is_inexact_lt_midpoint = 0;
*ptr_is_inexact_gt_midpoint = 0;
} else { // else MP in [ODD, EVEN]
*ptr_is_midpoint_lt_even = 1;
*ptr_is_inexact_lt_midpoint = 0;
*ptr_is_inexact_gt_midpoint = 0;
}
}
// check for rounding overflow, which occurs if Cstar = 10^(q-x)
ind = q - x; // 1 <= ind <= q - 1
if (ind <= 19) {
if (Cstar.w[3] == 0x0ULL && Cstar.w[2] == 0x0ULL &&
Cstar.w[1] == 0x0ULL && Cstar.w[0] == ten2k64[ind]) {
// if Cstar = 10^(q-x)
Cstar.w[0] = ten2k64[ind - 1]; // Cstar = 10^(q-x-1)
*incr_exp = 1;
} else {
*incr_exp = 0;
}
} else if (ind == 20) {
// if ind = 20
if (Cstar.w[3] == 0x0ULL && Cstar.w[2] == 0x0ULL &&
Cstar.w[1] == ten2k128[0].w[1]
&& Cstar.w[0] == ten2k128[0].w[0]) {
// if Cstar = 10^(q-x)
Cstar.w[0] = ten2k64[19]; // Cstar = 10^(q-x-1)
Cstar.w[1] = 0x0ULL;
*incr_exp = 1;
} else {
*incr_exp = 0;
}
} else if (ind <= 38) { // if 21 <= ind <= 38
if (Cstar.w[3] == 0x0ULL && Cstar.w[2] == 0x0ULL &&
Cstar.w[1] == ten2k128[ind - 20].w[1] &&
Cstar.w[0] == ten2k128[ind - 20].w[0]) {
// if Cstar = 10^(q-x)
Cstar.w[0] = ten2k128[ind - 21].w[0]; // Cstar = 10^(q-x-1)
Cstar.w[1] = ten2k128[ind - 21].w[1];
*incr_exp = 1;
} else {
*incr_exp = 0;
}
} else if (ind == 39) {
if (Cstar.w[3] == 0x0ULL && Cstar.w[2] == ten2k256[0].w[2] &&
Cstar.w[1] == ten2k256[0].w[1]
&& Cstar.w[0] == ten2k256[0].w[0]) {
// if Cstar = 10^(q-x)
Cstar.w[0] = ten2k128[18].w[0]; // Cstar = 10^(q-x-1)
Cstar.w[1] = ten2k128[18].w[1];
Cstar.w[2] = 0x0ULL;
*incr_exp = 1;
} else {
*incr_exp = 0;
}
} else if (ind <= 57) { // if 40 <= ind <= 57
if (Cstar.w[3] == 0x0ULL && Cstar.w[2] == ten2k256[ind - 39].w[2] &&
Cstar.w[1] == ten2k256[ind - 39].w[1] &&
Cstar.w[0] == ten2k256[ind - 39].w[0]) {
// if Cstar = 10^(q-x)
Cstar.w[0] = ten2k256[ind - 40].w[0]; // Cstar = 10^(q-x-1)
Cstar.w[1] = ten2k256[ind - 40].w[1];
Cstar.w[2] = ten2k256[ind - 40].w[2];
*incr_exp = 1;
} else {
*incr_exp = 0;
}
// else if (ind == 58) is not needed becauae we do not have ten2k192[] yet
} else { // if 58 <= ind <= 77 (actually 58 <= ind <= 74)
if (Cstar.w[3] == ten2k256[ind - 39].w[3] &&
Cstar.w[2] == ten2k256[ind - 39].w[2] &&
Cstar.w[1] == ten2k256[ind - 39].w[1] &&
Cstar.w[0] == ten2k256[ind - 39].w[0]) {
// if Cstar = 10^(q-x)
Cstar.w[0] = ten2k256[ind - 40].w[0]; // Cstar = 10^(q-x-1)
Cstar.w[1] = ten2k256[ind - 40].w[1];
Cstar.w[2] = ten2k256[ind - 40].w[2];
Cstar.w[3] = ten2k256[ind - 40].w[3];
*incr_exp = 1;
} else {
*incr_exp = 0;
}
}
ptr_Cstar->w[3] = Cstar.w[3];
ptr_Cstar->w[2] = Cstar.w[2];
ptr_Cstar->w[1] = Cstar.w[1];
ptr_Cstar->w[0] = Cstar.w[0];
}