Skip to content

Commit

Permalink
bid128_quantum
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosga committed Feb 9, 2024
1 parent fd2e654 commit 50f3963
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/bid128_quantum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* ---------------------------------------------------------------------------------------------------*/
/* Port of the Intel Decimal Floating-Point Math Library decimal128 type to Rust. */
/* decmathlib-rs - Copyright (C) 2023-2024 Carlos Guzmán Álvarez */
/* -------------------------------------------------------------------------------------------------- */
/* Licensed under the MIT license. See LICENSE file in the project root for fu64 license information. */
/* -------------------------------------------------------------------------------------------------- */
/* Intel® Decimal Floating-Point Math Library - Copyright (c) 2018, Intel Corp. */
/* -------------------------------------------------------------------------------------------------- */

use crate::constants::{MASK_ANY_INF, MASK_INF, MASK_STEERING_BITS, NAN_MASK64, QUIET_MASK64};
use crate::d128::{BID_UINT128, BID_UINT64};

/// The quantumdN functions compute the quantum of a finite argument.
/// If x is infinite, the result is +Inf. If x is NaN, the result is NaN.
pub (crate) fn bid128_quantum(x: &BID_UINT128) -> BID_UINT128 {
let mut res: BID_UINT128 = BID_UINT128::default();

// If x is infinite, the result is +Inf. If x is NaN, the result is NaN
if (x.w[1] & MASK_ANY_INF) == MASK_INF {
res.w[1] = 0x7800000000000000u64;
res.w[0] = 0x0000000000000000u64;
return res;
} else if (x.w[1] & NAN_MASK64) == NAN_MASK64 {
res.w[1] = x.w[1] & QUIET_MASK64;
return res;
}

// Extract exponent
let int_exp = if (x.w[1] & MASK_STEERING_BITS) == MASK_STEERING_BITS {
(((x.w[1] >> 47) & 0x3fff) as i32) - 6176
} else {
(((x.w[1] >> 49) & 0x3fff) as i32) - 6176
};

// Form 10^new_exponent*1
res.w[1] = (((int_exp as i64) << 49 ) + 0x3040000000000000i64) as BID_UINT64;
res.w[0] = 0x0000000000000001u64;

return res;
}
7 changes: 7 additions & 0 deletions src/d128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::bid128_nexttoward::bid128_nexttoward;
use crate::bid128_noncomp::*;
use crate::bid128_quantexp::bid128_quantexp;
use crate::bid128_quantize::bid128_quantize;
use crate::bid128_quantum::bid128_quantum;
use crate::bid128_rem::bid128_rem;
use crate::bid128_scalbln::bid128_scalbln;
use crate::bid128_scalbn::bid128_scalbn;
Expand Down Expand Up @@ -389,6 +390,12 @@ impl d128 {
bid128_quantize(x, y, rnd_mode.unwrap_or(DEFAULT_ROUNDING_MODE), pfpsf)
}

/// The quantumdN functions compute the quantum of a finite argument.
/// If x is infinite, the result is +Inf. If x is NaN, the result is NaN.
pub fn quantum(&self) -> Self {
bid128_quantum(self)
}

/// Returns x * 10^N
pub fn scalbn(&self, n: i32, rnd_mode: Option<u32>, pfpsf: &mut _IDEC_flags) -> Self {
bid128_scalbn(self, n, rnd_mode.unwrap_or(DEFAULT_ROUNDING_MODE), pfpsf)
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod bid128_nexttoward;
mod bid128_noncomp;
mod bid128_quantexp;
mod bid128_quantize;
mod bid128_quantum;
mod bid128_rem;
mod bid128_round_integral;
mod bid128_scalbln;
Expand Down
26 changes: 26 additions & 0 deletions tests/bid128_quantum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* ---------------------------------------------------------------------------------------------------*/
/* Port of the Intel Decimal Floating-Point Math Library decimal128 type to Rust. */
/* decmathlib-rs - Copyright (C) 2023-2024 Carlos Guzmán Álvarez */
/* -------------------------------------------------------------------------------------------------- */
/* Licensed under the MIT license. See LICENSE file in the project root for full license information. */
/* -------------------------------------------------------------------------------------------------- */
/* Intel® Decimal Floating-Point Math Library - Copyright (c) 2018, Intel Corp. */
/* -------------------------------------------------------------------------------------------------- */

mod common;

dec_test!(bid128_quantum_001, bid128_quantum, 0x304C0000000000000000000000003039u128, 0x304C0000000000000000000000000001u128);
dec_test!(bid128_quantum_002, bid128_quantum, 0xB04C0000000000000000000000003039u128, 0x304C0000000000000000000000000001u128);
dec_test!(bid128_quantum_003, bid128_quantum, 0x302C0000000000000000000000003039u128, 0x302C0000000000000000000000000001u128);
dec_test!(bid128_quantum_004, bid128_quantum, 0xB02C0000000000000000000000003039u128, 0x302C0000000000000000000000000001u128);
dec_test!(bid128_quantum_005, bid128_quantum, 0x30360000000000000000000000003039u128, 0x30360000000000000000000000000001u128);
dec_test!(bid128_quantum_006, bid128_quantum, 0xB0360000000000000000000000003039u128, 0x30360000000000000000000000000001u128);
dec_test!(bid128_quantum_007, bid128_quantum, 0x303E000000000000000000000001E23Au128, 0x303E0000000000000000000000000001u128);
dec_test!(bid128_quantum_008, bid128_quantum, 0x5FFE314DC6448D9338C15B0A00000000u128, 0x5FFE0000000000000000000000000001u128);
dec_test!(bid128_quantum_009, bid128_quantum, 0xDFFE314DC6448D9338C15B0A00000000u128, 0x5FFE0000000000000000000000000001u128);
dec_test!(bid128_quantum_010, bid128_quantum, 0x30400000000000000000000000000000u128, 0x30400000000000000000000000000001u128);
dec_test!(bid128_quantum_011, bid128_quantum, 0xB0400000000000000000000000000000u128, 0x30400000000000000000000000000001u128);
dec_test!(bid128_quantum_012, bid128_quantum, 0x00000000000000000000000000000001u128, 0x00000000000000000000000000000001u128);
dec_test!(bid128_quantum_013, bid128_quantum, 0x80000000000000000000000000000001u128, 0x00000000000000000000000000000001u128);
dec_test!(bid128_quantum_014, bid128_quantum, 0x00000000000000000000000000000000u128, 0x00000000000000000000000000000001u128);
dec_test!(bid128_quantum_015, bid128_quantum, 0x78000000000000000000000000000000u128, 0x78000000000000000000000000000000u128);
11 changes: 11 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,17 @@ macro_rules! dec_test {
}
};

($name:ident, bid128_quantum, $input1:expr, $exp:expr) => {
#[test]
fn $name() {
let dec1 = decmathlib_rs::d128::d128::from($input1);
let exp = decmathlib_rs::d128::d128::from($exp);
let res1 = dec1.quantum();

assert_eq!(exp, res1);
}
};

($name:ident, bid128_negate, $input1:expr, $exp:expr) => {
#[test]
fn $name() {
Expand Down

0 comments on commit 50f3963

Please sign in to comment.