#![no_std]
#![doc(html_root_url = "https://docs.rs/dragonbox/0.1.4")]
#![allow(
clippy::bool_to_int_with_if,
clippy::cast_lossless,
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
clippy::comparison_chain,
clippy::doc_markdown,
clippy::expl_impl_clone_on_copy,
clippy::if_not_else,
clippy::items_after_statements,
clippy::manual_range_contains,
clippy::must_use_candidate,
clippy::never_loop,
clippy::ptr_as_ptr,
clippy::shadow_unrelated,
clippy::similar_names,
clippy::too_many_lines,
clippy::toplevel_ref_arg,
clippy::unreadable_literal,
clippy::unusual_byte_groupings
)]
mod buffer;
mod cache;
mod div;
mod log;
mod to_chars;
mod wuint;
use crate::buffer::Sealed;
use crate::cache::EntryTypeExt as _;
use core::mem::MaybeUninit;
pub struct Buffer {
bytes: [MaybeUninit<u8>; to_chars::MAX_OUTPUT_STRING_LENGTH],
}
pub trait Float: Sealed {}
const SIGNIFICAND_BITS: usize = 52;
const EXPONENT_BITS: usize = 11;
const MIN_EXPONENT: i32 = -1022;
const EXPONENT_BIAS: i32 = -1023;
type CarrierUint = u64;
type SignedSignificand = i64;
const CARRIER_BITS: usize = 64;
const fn extract_exponent_bits(u: CarrierUint) -> u32 {
const EXPONENT_BITS_MASK: u32 = (1 << EXPONENT_BITS) - 1;
(u >> SIGNIFICAND_BITS) as u32 & EXPONENT_BITS_MASK
}
const fn remove_exponent_bits(u: CarrierUint, exponent_bits: u32) -> SignedSignificand {
(u ^ ((exponent_bits as CarrierUint) << SIGNIFICAND_BITS)) as SignedSignificand
}
const fn remove_sign_bit_and_shift(s: SignedSignificand) -> CarrierUint {
(s as CarrierUint) << 1
}
const fn is_nonzero(u: CarrierUint) -> bool {
(u << 1) != 0
}
const fn is_negative(s: SignedSignificand) -> bool {
s < 0
}
const fn has_even_significand_bits(s: SignedSignificand) -> bool {
s % 2 == 0
}
const fn compute_power32<const K: u32>(a: u32) -> u32 {
let mut p = 1;
let mut i = 0;
while i < K {
p *= a;
i += 1;
}
p
}
const fn compute_power64<const K: u32>(a: u64) -> u64 {
let mut p = 1;
let mut i = 0;
while i < K {
p *= a;
i += 1;
}
p
}
const fn count_factors<const A: usize>(mut n: usize) -> u32 {
let mut c = 0;
while n % A == 0 {
n /= A;
c += 1;
}
c
}
fn break_rounding_tie(significand: &mut u64) {
*significand = if *significand % 2 == 0 {
*significand
} else {
*significand - 1
};
}
fn divide_by_pow10<const N: u32, const MAX_POW2: i32, const MAX_POW5: i32>(n: u64) -> u64 {
assert!(MAX_POW2 + (log::floor_log2_pow10(MAX_POW5) - MAX_POW5) < 64);
if N == 3
&& MAX_POW2 + (log::floor_log2_pow10(N as i32 + MAX_POW5) - (N as i32 + MAX_POW5)) < 70
{
wuint::umul128_upper64(n, 0x8312_6e97_8d4f_df3c) >> 9
} else {
struct Divisor<const N: u32>;
impl<const N: u32> Divisor<N> {
const VALUE: u64 = compute_power64::<N>(10);
}
n / Divisor::<N>::VALUE
}
}
struct Decimal {
significand: u64,
exponent: i32,
}
const KAPPA: u32 = 2;
fn compute_nearest_normal(
two_fc: CarrierUint,
exponent: i32,
has_even_significand_bits: bool,
) -> Decimal {
let minus_k = log::floor_log10_pow2(exponent) - KAPPA as i32;
let ref cache = unsafe { cache::get(-minus_k) };
let beta_minus_1 = exponent + log::floor_log2_pow10(-minus_k);
let deltai = compute_delta(cache, beta_minus_1);
let two_fr = two_fc | 1;
let zi = compute_mul(two_fr << beta_minus_1, cache);
const BIG_DIVISOR: u32 = compute_power32::<{ KAPPA + 1 }>(10);
const SMALL_DIVISOR: u32 = compute_power32::<KAPPA>(10);
let mut significand = divide_by_pow10::<
{ KAPPA + 1 },
{ SIGNIFICAND_BITS as i32 + KAPPA as i32 + 2 },
{ KAPPA as i32 + 1 },
>(zi);
let mut r = (zi - BIG_DIVISOR as u64 * significand) as u32;
'small_divisor_case_label: loop {
if r > deltai {
break 'small_divisor_case_label;
} else if r < deltai {
if r == 0
&& !has_even_significand_bits
&& is_product_integer_fc_pm_half(two_fr, exponent, minus_k)
{
significand -= 1;
r = BIG_DIVISOR;
break 'small_divisor_case_label;
}
} else {
let two_fl = two_fc - 1;
if (!has_even_significand_bits
|| !is_product_integer_fc_pm_half(two_fl, exponent, minus_k))
&& !compute_mul_parity(two_fl, cache, beta_minus_1)
{
break 'small_divisor_case_label;
}
}
let exponent = minus_k + KAPPA as i32 + 1;
return Decimal {
significand,
exponent,
};
}
significand *= 10;
let exponent = minus_k + KAPPA as i32;
let mut dist = r - (deltai / 2) + (SMALL_DIVISOR / 2);
let approx_y_parity = ((dist ^ (SMALL_DIVISOR / 2)) & 1) != 0;
let divisible_by_10_to_the_kappa = div::check_divisibility_and_divide_by_pow10(&mut dist);
significand += dist as CarrierUint;
if divisible_by_10_to_the_kappa {
if compute_mul_parity(two_fc, cache, beta_minus_1) != approx_y_parity {
significand -= 1;
} else {
if is_product_integer_fc(two_fc, exponent, minus_k) {
break_rounding_tie(&mut significand);
}
}
}
Decimal {
significand,
exponent,
}
}
fn compute_nearest_shorter(exponent: i32) -> Decimal {
let minus_k = log::floor_log10_pow2_minus_log10_4_over_3(exponent);
let beta_minus_1 = exponent + log::floor_log2_pow10(-minus_k);
let ref cache = unsafe { cache::get(-minus_k) };
let mut xi = compute_left_endpoint_for_shorter_interval_case(cache, beta_minus_1);
let zi = compute_right_endpoint_for_shorter_interval_case(cache, beta_minus_1);
if !is_left_endpoint_integer_shorter_interval(exponent) {
xi += 1;
}
let significand = zi / 10;
if significand * 10 >= xi {
return Decimal {
significand,
exponent: minus_k + 1,
};
}
let mut significand = compute_round_up_for_shorter_interval_case(cache, beta_minus_1);
let exponent = minus_k;
const SHORTER_INTERVAL_TIE_LOWER_THRESHOLD: i32 =
-log::floor_log5_pow2_minus_log5_3(SIGNIFICAND_BITS as i32 + 4)
- 2
- SIGNIFICAND_BITS as i32;
const SHORTER_INTERVAL_TIE_UPPER_THRESHOLD: i32 =
-log::floor_log5_pow2(SIGNIFICAND_BITS as i32 + 2) - 2 - SIGNIFICAND_BITS as i32;
if exponent >= SHORTER_INTERVAL_TIE_LOWER_THRESHOLD
&& exponent <= SHORTER_INTERVAL_TIE_UPPER_THRESHOLD
{
break_rounding_tie(&mut significand);
} else if significand < xi {
significand += 1;
}
Decimal {
significand,
exponent,
}
}
fn compute_mul(u: CarrierUint, cache: &cache::EntryType) -> CarrierUint {
wuint::umul192_upper64(u, *cache)
}
fn compute_delta(cache: &cache::EntryType, beta_minus_1: i32) -> u32 {
(cache.high() >> ((CARRIER_BITS - 1) as i32 - beta_minus_1)) as u32
}
fn compute_mul_parity(two_f: CarrierUint, cache: &cache::EntryType, beta_minus_1: i32) -> bool {
debug_assert!(beta_minus_1 >= 1);
debug_assert!(beta_minus_1 < 64);
((wuint::umul192_middle64(two_f, *cache) >> (64 - beta_minus_1)) & 1) != 0
}
fn compute_left_endpoint_for_shorter_interval_case(
cache: &cache::EntryType,
beta_minus_1: i32,
) -> CarrierUint {
(cache.high() - (cache.high() >> (SIGNIFICAND_BITS + 2)))
>> ((CARRIER_BITS - SIGNIFICAND_BITS - 1) as i32 - beta_minus_1)
}
fn compute_right_endpoint_for_shorter_interval_case(
cache: &cache::EntryType,
beta_minus_1: i32,
) -> CarrierUint {
(cache.high() + (cache.high() >> (SIGNIFICAND_BITS + 2)))
>> ((CARRIER_BITS - SIGNIFICAND_BITS - 1) as i32 - beta_minus_1)
}
fn compute_round_up_for_shorter_interval_case(
cache: &cache::EntryType,
beta_minus_1: i32,
) -> CarrierUint {
((cache.high() >> ((CARRIER_BITS - SIGNIFICAND_BITS - 2) as i32 - beta_minus_1)) + 1) / 2
}
const MAX_POWER_OF_FACTOR_OF_5: i32 = log::floor_log5_pow2(SIGNIFICAND_BITS as i32 + 2);
const DIVISIBILITY_CHECK_BY_5_THRESHOLD: i32 =
log::floor_log2_pow10(MAX_POWER_OF_FACTOR_OF_5 + KAPPA as i32 + 1);
fn is_product_integer_fc_pm_half(two_f: CarrierUint, exponent: i32, minus_k: i32) -> bool {
const CASE_FC_PM_HALF_LOWER_THRESHOLD: i32 =
-(KAPPA as i32) - log::floor_log5_pow2(KAPPA as i32);
const CASE_FC_PM_HALF_UPPER_THRESHOLD: i32 = log::floor_log2_pow10(KAPPA as i32 + 1);
if exponent < CASE_FC_PM_HALF_LOWER_THRESHOLD {
false
}
else if exponent <= CASE_FC_PM_HALF_UPPER_THRESHOLD {
true
}
else if exponent > DIVISIBILITY_CHECK_BY_5_THRESHOLD {
false
} else {
unsafe {
div::divisible_by_power_of_5::<{ MAX_POWER_OF_FACTOR_OF_5 as usize + 1 }>(
two_f,
minus_k as u32,
)
}
}
}
fn is_product_integer_fc(two_f: CarrierUint, exponent: i32, minus_k: i32) -> bool {
const CASE_FC_LOWER_THRESHOLD: i32 =
-(KAPPA as i32) - 1 - log::floor_log5_pow2(KAPPA as i32 + 1);
const CASE_FC_UPPER_THRESHOLD: i32 = log::floor_log2_pow10(KAPPA as i32 + 1);
if exponent > DIVISIBILITY_CHECK_BY_5_THRESHOLD {
false
} else if exponent > CASE_FC_UPPER_THRESHOLD {
unsafe {
div::divisible_by_power_of_5::<{ MAX_POWER_OF_FACTOR_OF_5 as usize + 1 }>(
two_f,
minus_k as u32,
)
}
}
else if exponent >= CASE_FC_LOWER_THRESHOLD {
true
}
else {
div::divisible_by_power_of_2(two_f, (minus_k - exponent + 1) as u32)
}
}
const fn floor_log2(mut n: u64) -> i32 {
let mut count = -1;
while n != 0 {
count += 1;
n >>= 1;
}
count
}
fn is_left_endpoint_integer_shorter_interval(exponent: i32) -> bool {
const CASE_SHORTER_INTERVAL_LEFT_ENDPOINT_LOWER_THRESHOLD: i32 = 2;
const CASE_SHORTER_INTERVAL_LEFT_ENDPOINT_UPPER_THRESHOLD: i32 = 2 + floor_log2(
compute_power64::<{ count_factors::<5>((1 << (SIGNIFICAND_BITS + 2)) - 1) + 1 }>(10) / 3,
);
exponent >= CASE_SHORTER_INTERVAL_LEFT_ENDPOINT_LOWER_THRESHOLD
&& exponent <= CASE_SHORTER_INTERVAL_LEFT_ENDPOINT_UPPER_THRESHOLD
}
fn to_decimal(x: f64) -> Decimal {
let br = x.to_bits();
let exponent_bits = extract_exponent_bits(br);
let signed_significand_bits = remove_exponent_bits(br, exponent_bits);
let mut two_fc = remove_sign_bit_and_shift(signed_significand_bits);
let mut exponent = exponent_bits as i32;
if exponent != 0 {
exponent += EXPONENT_BIAS - SIGNIFICAND_BITS as i32;
if two_fc == 0 {
return compute_nearest_shorter(exponent);
}
two_fc |= 1 << (SIGNIFICAND_BITS + 1);
}
else {
exponent = MIN_EXPONENT - SIGNIFICAND_BITS as i32;
}
compute_nearest_normal(
two_fc,
exponent,
has_even_significand_bits(signed_significand_bits),
)
}