Skip to content

Commit

Permalink
Merge pull request #14 from carlosga/13-bid128_to_intxxx
Browse files Browse the repository at this point in the history
Closes #13
  • Loading branch information
carlosga authored Feb 3, 2024
2 parents 20e337a + bcb8091 commit ed9c438
Show file tree
Hide file tree
Showing 37 changed files with 17,767 additions and 134 deletions.
2 changes: 2 additions & 0 deletions src/bid128_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ pub (crate) fn bid128qd_sub(x: &BID_UINT128, y: BID_UINT64, rnd_mode: u32, pfpsf
/// BID128 sub
/////////////////////////////////////

/// Decimal floating-point subtraction
pub (crate) fn bid128_sub(x: &BID_UINT128, y: &BID_UINT128, rnd_mode: u32, pfpsf: &mut _IDEC_flags) -> BID_UINT128 {
let mut y: BID_UINT128 = *y;
if (y.w[BID_HIGH_128W] & MASK_NAN) != MASK_NAN {
Expand All @@ -144,6 +145,7 @@ pub (crate) fn bid128_sub(x: &BID_UINT128, y: &BID_UINT128, rnd_mode: u32, pfpsf
/// BID128 add
/////////////////////////////////////

/// Decimal floating-point addition
pub (crate) fn bid128_add(x: &BID_UINT128, y: &BID_UINT128, rnd_mode: u32, pfpsf: &mut _IDEC_flags) -> BID_UINT128 {
let mut res: BID_UINT128 = BID_UINT128 { w: [0xbaddbaddbaddbaddu64, 0xbaddbaddbaddbaddu64] };
let mut x_sign: BID_UINT64;
Expand Down
16 changes: 16 additions & 0 deletions src/bid128_compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use crate::constants::{MASK_INF, MASK_NAN, MASK_SIGN, MASK_SNAN};
use crate::core::StatusFlags;
use crate::d128::{_IDEC_flags, BID_UINT128, BID_UINT192, BID_UINT256};

/// Compare 128-bit decimal floating-point numbers for specified relation;
/// do not signal invalid exception for quiet NaNs
pub (crate) fn bid128_quiet_equal(x: &BID_UINT128, y: &BID_UINT128, pfpsf: &mut _IDEC_flags) -> bool {
let mut res: bool;
let mut exp_x: i32;
Expand Down Expand Up @@ -150,6 +152,8 @@ pub (crate) fn bid128_quiet_equal(x: &BID_UINT128, y: &BID_UINT128, pfpsf: &mut
res
}

/// Compare 128-bit decimal floating-point numbers for specified relation;
/// do not signal invalid exception for quiet NaNs
pub (crate) fn bid128_quiet_greater(x: &BID_UINT128, y: &BID_UINT128, pfpsf: &mut _IDEC_flags) -> bool {
let res: bool;
let exp_x: i32;
Expand Down Expand Up @@ -385,6 +389,8 @@ pub (crate) fn bid128_quiet_greater(x: &BID_UINT128, y: &BID_UINT128, pfpsf: &mu
res
}

/// Compare 128-bit decimal floating-point numbers for specified relation;
/// do not signal invalid exception for quiet NaNs
pub (crate) fn bid128_quiet_greater_equal(x: &BID_UINT128, y: &BID_UINT128, pfpsf: &mut _IDEC_flags) -> bool {
let res: bool;
let exp_x: i32;
Expand Down Expand Up @@ -616,6 +622,8 @@ pub (crate) fn bid128_quiet_greater_equal(x: &BID_UINT128, y: &BID_UINT128, pfps
}
}

/// Compare 128-bit decimal floating-point numbers for specified relation;
/// do not signal invalid exception for quiet NaNs
pub (crate) fn bid128_quiet_greater_unordered(x: &BID_UINT128, y: &BID_UINT128, pfpsf: &mut _IDEC_flags) -> bool {
let res: bool;
let exp_x: i32;
Expand Down Expand Up @@ -849,6 +857,8 @@ pub (crate) fn bid128_quiet_greater_unordered(x: &BID_UINT128, y: &BID_UINT128,
}
}

/// Compare 128-bit decimal floating-point numbers for specified relation;
/// do not signal invalid exception for quiet NaNs
pub (crate) fn bid128_quiet_less(x: &BID_UINT128, y: &BID_UINT128, pfpsf: &mut _IDEC_flags) -> bool {
let res: bool;
let exp_x: i32;
Expand Down Expand Up @@ -1082,6 +1092,8 @@ pub (crate) fn bid128_quiet_less(x: &BID_UINT128, y: &BID_UINT128, pfpsf: &mut _
}
}

/// Compare 128-bit decimal floating-point numbers for specified relation;
/// do not signal invalid exception for quiet NaNs
pub (crate) fn bid128_quiet_less_equal(x: &BID_UINT128, y: &BID_UINT128, pfpsf: &mut _IDEC_flags) -> bool {
let res: bool;
let exp_x: i32;
Expand Down Expand Up @@ -1314,6 +1326,8 @@ pub (crate) fn bid128_quiet_less_equal(x: &BID_UINT128, y: &BID_UINT128, pfpsf:
}
}

/// Compare 128-bit decimal floating-point numbers for specified relation;
/// do not signal invalid exception for quiet NaNs
pub (crate) fn bid128_quiet_less_unordered(x: &BID_UINT128, y: &BID_UINT128, pfpsf: &mut _IDEC_flags) -> bool {
let res: bool;
let exp_x: i32;
Expand Down Expand Up @@ -1546,6 +1560,8 @@ pub (crate) fn bid128_quiet_less_unordered(x: &BID_UINT128, y: &BID_UINT128, pfp
}
}

/// Compare 128-bit decimal floating-point numbers for specified relation;
/// do not signal invalid exception for quiet NaNs
pub (crate) fn bid128_quiet_not_equal(x: &BID_UINT128, y: &BID_UINT128, pfpsf: &mut _IDEC_flags) -> bool {
let res: bool;
let mut exp_x: i32;
Expand Down
1 change: 1 addition & 0 deletions src/bid128_div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::constants::{DECIMAL_EXPONENT_BIAS_128, DECIMAL_MAX_EXPON_128, QUIET_M
use crate::core::{RoundingMode, StatusFlags};
use crate::d128::{_IDEC_flags, BID_SINT64, BID_UI32FLOAT, BID_UINT128, BID_UINT256, BID_UINT32, BID_UINT64};

/// Decimal floating-point division
pub (crate) fn bid128_div(x: &BID_UINT128, y: &BID_UINT128, rnd_mode: u32, pfpsf: &mut _IDEC_flags) -> BID_UINT128 {
let mut CA4: BID_UINT256 = BID_UINT256::default();
let mut CA4r: BID_UINT256 = BID_UINT256::default();
Expand Down
2 changes: 1 addition & 1 deletion src/bid128_fma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::d128::{_IDEC_flags, BID_SINT64, BID_UI64DOUBLE, BID_UINT128, BID_UINT
// BID128 fma x * y + z
//////////////////////////////////////////////

pub(crate) fn bid_rounding_correction(
pub (crate) fn bid_rounding_correction(
rnd_mode: u32,
is_inexact_lt_midpoint: bool,
is_inexact_gt_midpoint: bool,
Expand Down
2 changes: 1 addition & 1 deletion src/bid128_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub (crate) fn bid128qd_mul(x: &BID_UINT128, y: &BID_UINT64, rnd_mode: u32, pfps
bid128_mul(x, &y1, rnd_mode, pfpsf)
}

/// bid128_mul stands for bid128qq_mul
/// Decimal floating-point multiplication
pub (crate) fn bid128_mul(x: &BID_UINT128, y: &BID_UINT128, rnd_mode: u32, pfpsf: &mut _IDEC_flags) -> BID_UINT128 {
let z: BID_UINT128 = BID_UINT128 { w: [0x0000000000000000u64, 0x5ffe000000000000u64] };
let x_sign: BID_UINT64;
Expand Down
70 changes: 35 additions & 35 deletions src/bid128_noncomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@
use crate::bid_conf::BID_SWAP128;

use crate::bid128::{bid_nr_digits, bid_ten2k128, bid_ten2k64};
use crate::bid128_string::bid128_from_string;
use crate::bid_conf::{BID_HIGH_128W, BID_LOW_128W};
use crate::bid_internal::{__mul_128x128_to_256, __mul_64x128_to_192};
use crate::constants::*;
use crate::core::ClassTypes;
use crate::d128::{BID_UI64DOUBLE, BID_UINT128, BID_UINT192, BID_UINT256, BID_UINT64};
use crate::core::{ClassTypes, DEFAULT_ROUNDING_MODE};
use crate::d128::{_IDEC_flags, BID_UI64DOUBLE, BID_UINT128, BID_UINT192, BID_UINT256, BID_UINT64};

/// Return true if and only if x has negative sign
pub (crate) fn bid128_isSigned(x: &BID_UINT128) -> bool {
(x.w[BID_HIGH_128W] & MASK_SIGN) == MASK_SIGN
}

/// return 1 iff x is not zero, nor NaN nor subnormal nor infinity
/// Return true if and only if x is normal (not zero, subnormal, infinite, or NaN)
pub (crate) fn bid128_isNormal(x: &BID_UINT128) -> bool {
let x_exp: BID_UINT64;
let C1_hi: BID_UINT64;
Expand Down Expand Up @@ -116,7 +118,7 @@ pub (crate) fn bid128_isNormal(x: &BID_UINT128) -> bool {
if exp + q <= -6143 { false } else { true }
}

// return 1 iff x is not zero, nor NaN nor normal nor infinity
/// Return true if and only if x is subnormal
pub (crate) fn bid128_isSubnormal(x: &BID_UINT128) -> bool {
let x_exp: BID_UINT64;
let C1_hi: BID_UINT64;
Expand Down Expand Up @@ -186,10 +188,12 @@ pub (crate) fn bid128_isSubnormal(x: &BID_UINT128) -> bool {
// if (exp + q <= -6143) { 1 } else { 0 }
}

/// Return true if and only if x is zero, subnormal or normal (not infinite or NaN)
pub (crate) fn bid128_isFinite(x: &BID_UINT128) -> bool {
(x.w[BID_HIGH_128W] & MASK_INF) != MASK_INF
}

/// Return true if and only if x is +0 or -0
pub (crate) fn bid128_isZero(x: &BID_UINT128) -> bool {
let mut sig_x: BID_UINT128 = BID_UINT128::default();

Expand All @@ -213,15 +217,17 @@ pub (crate) fn bid128_isZero(x: &BID_UINT128) -> bool {
false
}

/// Return true if and only if x is infinite
pub (crate) fn bid128_isInf(x: &BID_UINT128) -> bool {
((x.w[BID_HIGH_128W] & MASK_INF) == MASK_INF) && ((x.w[BID_HIGH_128W] & MASK_NAN) != MASK_NAN)
}

/// Return true if and only if x is a signaling NaN
pub (crate) fn bid128_isSignaling(x: &BID_UINT128) -> bool {
(x.w[BID_HIGH_128W] & MASK_SNAN) == MASK_SNAN
}

/// return 1 iff x is a canonical number ,infinity, or NaN.
/// Return true if and only if x is a finite number, infinity, or NaN that is canonical
pub (crate) fn bid128_isCanonical(x: &BID_UINT128) -> bool {
let mut sig_x: BID_UINT128 = BID_UINT128::default();

Expand Down Expand Up @@ -258,36 +264,40 @@ pub (crate) fn bid128_isCanonical(x: &BID_UINT128) -> bool {
}
}

/// Return true if and only if x is a NaN
pub (crate) fn bid128_isNaN(x: &BID_UINT128) -> bool {
(x.w[BID_HIGH_128W] & MASK_NAN) == MASK_NAN
}

/// copies a floating-point operand x to destination y, with no change
/// Copies a decimal floating-point operand x to a destination in the same format, with no change
pub (crate) fn bid128_copy(x: &BID_UINT128) -> BID_UINT128 {
*x
}

/// copies a floating-point operand x to destination y, reversing the sign
/// Copies a 128-bit decimal floating-point operand x to a destination in the same format, reversing the sign
pub (crate) fn bid128_negate(x: &BID_UINT128) -> BID_UINT128 {
let mut res = *x;
res.w[BID_HIGH_128W] ^= MASK_SIGN;
res
}

/// copies a floating-point operand x to destination y, changing the sign to positive
/// Copies a 128-bit decimal floating-point operand x to a destination in the same format, changing the sign to positive
pub (crate) fn bid128_abs(x: &BID_UINT128) -> BID_UINT128 {
let mut res = *x;
res.w[BID_HIGH_128W] &= !MASK_SIGN;
res
}

/// copies operand x to destination in the same format as x, but with the sign of y
/// Copies a 128-bit decimal floating-point operand x to a destination in the same format as x, but with the sign of y
pub (crate) fn bid128_copySign(x: &BID_UINT128, y: &BID_UINT128) -> BID_UINT128 {
let mut res = *x;
res.w[BID_HIGH_128W] = (x.w[BID_HIGH_128W] & !MASK_SIGN) | y.w[BID_HIGH_128W] & MASK_SIGN;
res
}

/// Tells which of the following ten classes x falls into (details in the IEEE Standard 754-2008):
/// signalingNaN, quietNaN, negativeInfinity, negativeNormal, negativeSubnormal, negativeZero, positiveZero,
/// positiveSubnormal, positiveNormal, positiveInfinity
pub (crate) fn bid128_class(x: &BID_UINT128) -> ClassTypes {
let sig_x_prime256: BID_UINT256;
let sig_x_prime192: BID_UINT192;
Expand Down Expand Up @@ -352,9 +362,9 @@ pub (crate) fn bid128_class(x: &BID_UINT128) -> ClassTypes {
if (x.w[1] & MASK_SIGN) == MASK_SIGN { ClassTypes::negativeNormal } else { ClassTypes::positiveNormal }
}

/// true if the exponents of x and y are the same, false otherwise.
/// The special cases of sameQuantum(NaN, NaN) and sameQuantum(Inf, Inf) are true
/// If exactly one operand is infinite or exactly one operand is NaN, then false
/// sameQuantum(x, y) is true if the exponents of x and y are the same, and false otherwise;
/// sameQuantum(NaN, NaN) and sameQuantum(inf, inf) are true;
/// if exactly one operand is infinite or exactly one operand is NaN, sameQuantum is false
pub (crate) fn bid128_sameQuantum(x: &BID_UINT128, y: &BID_UINT128) -> bool {
let x_exp: BID_UINT64;
let y_exp: BID_UINT64;
Expand Down Expand Up @@ -393,6 +403,7 @@ pub (crate) fn bid128_sameQuantum(x: &BID_UINT128, y: &BID_UINT128) -> bool {
x_exp == y_exp
}

/// Return true if x and y are ordered (see the IEEE Standard 754-2008)
pub (crate) fn bid128_totalOrder(x: &BID_UINT128, y: &BID_UINT128) -> bool {
let mut exp_x: i32;
let mut exp_y: i32;
Expand Down Expand Up @@ -677,6 +688,7 @@ pub (crate) fn bid128_totalOrder(x: &BID_UINT128, y: &BID_UINT128) -> bool {
|| (sig_n_prime192.w[1] == sig_x.w[1] && sig_n_prime192.w[0] > sig_x.w[0])) ^ ((x.w[1] & MASK_SIGN) == MASK_SIGN)
}

/// Return true if the absolute values of x and y are ordered (see the IEEE Standard 754-2008)
pub (crate) fn bid128_totalOrderMag(x: &BID_UINT128, y: &BID_UINT128) -> bool {
let mut exp_x: i32;
let mut exp_y: i32;
Expand Down Expand Up @@ -903,47 +915,35 @@ pub (crate) fn bid128_totalOrderMag(x: &BID_UINT128, y: &BID_UINT128) -> bool {
|| (sig_n_prime192.w[1] == sig_x.w[1] && sig_n_prime192.w[0] > sig_x.w[0])
}

/// Return the radix b of the format of x, 2 or 10
pub (crate) fn bid128_radix(_: &BID_UINT128) -> i32 {
// // dummy test
// (x.w[BID_LOW_128W]) { 10 } else { 10 }
10
}

pub (crate) fn bid128_inf() -> BID_UINT128 {
let mut res = BID_UINT128::default();
let mut res: BID_UINT128 = BID_UINT128::default();
res.w[BID_HIGH_128W] = 0x7800000000000000u64; // +inf
res.w[BID_LOW_128W] = 0x0000000000000000u64;
res
}

// TODO: bid128_from_string
/*
pub (crate) bid128_nan(tagp: string) -> BID_UINT128 {
let res: BID_UINT128 = BID_UINT128::default();
let x: BID_UINT128 = BID_UINT128::default();
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = BID_ROUNDING_TO_NEAREST;
#endif
#if !DECIMAL_GLOBAL_EXCEPTION_FLAGS
unsigned int fpsf;
unsigned int *pfpsf = &fpsf;
#endif
pub (crate) fn bid128_nan(tagp: &str, pfpsf: &mut _IDEC_flags) -> BID_UINT128 {
let mut res: BID_UINT128 = BID_UINT128::default();

res.w[BID_HIGH_128W] = 0x7c00000000000000u64; // +QNaN
res.w[BID_LOW_128W] = 0x0000000000000000u64;

if (!tagp) {
if tagp.is_empty() {
return res;
}

bid128_from_string(&x, &tagp _RND_MODE_ARG _EXC_FLAGS_ARG);
// bid128_from_string(&x, &tagp);
let mut x: BID_UINT128 = bid128_from_string(tagp, DEFAULT_ROUNDING_MODE, pfpsf);

x.w[BID_HIGH_128W] = x.w[BID_HIGH_128W] & 0x0000cfffffffffffu64; // valid values fit in 110 bits=46+64
res.w[BID_HIGH_128W] = res.w[BID_HIGH_128W] | x.w[BID_HIGH_128W];
res.w[BID_LOW_128W] = x.w[BID_LOW_128W];
x.w[BID_HIGH_128W] &= 0x0000cfffffffffffu64; // valid values fit in 110 bits=46+64
res.w[BID_HIGH_128W] |= x.w[BID_HIGH_128W];
res.w[BID_LOW_128W] = x.w[BID_LOW_128W];

return res;
}
*/
res
}
1 change: 1 addition & 0 deletions src/bid128_rem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::constants::{INFINITY_MASK64, NAN_MASK64, QUIET_MASK64, SNAN_MASK64};
use crate::core::StatusFlags;
use crate::d128::{_IDEC_flags, BID_SINT64, BID_UI32FLOAT, BID_UINT128, BID_UINT256, BID_UINT64};

/// Decimal floating-point remainder
pub (crate) fn bid128_rem(x: &BID_UINT128, y: &BID_UINT128, pfpsf: &mut _IDEC_flags) -> BID_UINT128 {
let mut P256: BID_UINT256 = BID_UINT256::default();
let mut CX: BID_UINT128 = BID_UINT128::default();
Expand Down
4 changes: 4 additions & 0 deletions src/bid128_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// BID128_to_string
//////////////////////////////////////////////


use crate::bid128::{bid_char_table2, bid_char_table3};
use crate::bid128_2_str_macros::*;
use crate::bid128_2_str_tables::mod10_18_tbl;
Expand All @@ -26,6 +27,7 @@ const MAX_FORMAT_DIGITS_128: usize = 34;
const MAX_STRING_DIGITS_128: usize = 100;
const MAX_SEARCH: usize = MAX_STRING_DIGITS_128 - MAX_FORMAT_DIGITS_128 - 1;

/// Convert 128-bit decimal floating-point value (binary encoding) to string format
pub (crate) fn bid128_to_string(x: &BID_UINT128) -> String {
let x_sign: BID_UINT64;
let mut x_exp: BID_UINT64;
Expand Down Expand Up @@ -206,6 +208,8 @@ pub (crate) fn bid128_to_string(x: &BID_UINT128) -> String {
str
}

/// Convert a decimal floating-point value represented in string format
/// (decimal character sequence) to 128-bit decimal floating-point format (binary encoding)
pub (crate) fn bid128_from_string(str: &str, rnd_mode: u32, pfpsf: &mut _IDEC_flags) -> BID_UINT128 {
let mut CX: BID_UINT128 = BID_UINT128::default();
let mut res: BID_UINT128 = BID_UINT128::default();
Expand Down
Loading

0 comments on commit ed9c438

Please sign in to comment.