Skip to content

Commit

Permalink
Renamed binary feature to power_of_two.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexhuszagh committed May 3, 2021
1 parent 70b2867 commit 7010efd
Show file tree
Hide file tree
Showing 35 changed files with 166 additions and 141 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ format = ["lexical-core/format"]
# Use the optimized Grisu3 implementation from dtoa (not recommended).
grisu3 = ["lexical-core/grisu3"]
# Add support for parsing and writing power-of-two float and integer strings.
binary = ["lexical-core/binary"]
power_of_two = ["lexical-core/power_of_two"]
# Add support for parsing and writing non-decimal float and integer strings.
radix = ["lexical-core/radix"]
# Allow custom rounding schemes, at the cost of slower performance.
Expand Down
10 changes: 5 additions & 5 deletions ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ if [ ! -z $NO_FEATURES ]; then
else
LEXICAL_FEATURES=(
"rounding"
"rounding,binary"
"rounding,power_of_two"
"rounding,radix"
"grisu3"
"ryu"
"format"
"format,binary"
"format,power_of_two"
"format,radix"
)
CORE_FEATURES=("${LEXICAL_FEATURES[@]}")
Expand All @@ -78,16 +78,16 @@ check() {

# Need to test a few permutations just to ensure everything compiles.
features=(
"binary"
"format"
"radix"
"power_of_two"
"rounding"
"format,binary"
"format,radix"
"format,rounding"
"format,power_of_two"
"radix,rounding"
"format,binary,rounding"
"format,radix,rounding"
"format,rounding,power_of_two"
)

# Iterate over all features.
Expand Down
2 changes: 1 addition & 1 deletion lexical-benchmark/lexical/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ default = ["no_alloc", "ryu", "std"]
no_alloc = ["lexical-core/no_alloc"]
format = ["lexical-core/format"]
grisu3 = ["lexical-core/grisu3"]
binary = ["lexical-core/binary"]
power_of_two = ["lexical-core/power_of_two"]
radix = ["lexical-core/radix"]
rounding = ["lexical-core/rounding"]
ryu = ["lexical-core/ryu"]
Expand Down
2 changes: 1 addition & 1 deletion lexical-capi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ format = ["lexical-core/format"]
# Use the optimized Grisu3 implementation from dtoa (not recommended).
grisu3 = ["lexical-core/grisu3"]
# Add support for parsing and writing power-of-two float and integer strings.
binary = ["lexical-core/binary"]
power_of_two = ["lexical-core/power_of_two"]
# Add support for parsing and writing non-decimal float and integer strings.
radix = ["lexical-core/radix"]
# Allow custom rounding schemes, at the cost of slower performance.
Expand Down
4 changes: 2 additions & 2 deletions lexical-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ format = []
# Use the optimized Grisu3 implementation from dtoa (not recommended).
grisu3 = ["dtoa"]
# Add support for parsing and writing power-of-two float and integer strings.
binary = []
power_of_two = []
# Add support for parsing and writing non-decimal float and integer strings.
radix = ["binary"]
radix = ["power_of_two"]
# Allow custom rounding schemes, at the cost of slower performance.
rounding = []
# Use the `std` library.
Expand Down
28 changes: 14 additions & 14 deletions lexical-core/src/atof/algorithm/correct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ where

/// Detect if a float representation is exactly halfway after truncation.
#[inline(always)]
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
fn is_halfway<F: FloatType>(mantissa: u64) -> bool {
// Get the leading and trailing zeros from the least-significant bit.
let bit_length: i32 = 64 - mantissa.leading_zeros().as_i32();
Expand All @@ -112,7 +112,7 @@ fn is_halfway<F: FloatType>(mantissa: u64) -> bool {

/// Detect if a float representation is odd after truncation.
#[inline(always)]
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
fn is_odd<F: FloatType>(mantissa: u64) -> bool {
// Get the leading and trailing zeros from the least-significant bit.
let bit_length: i32 = 64 - mantissa.leading_zeros().as_i32();
Expand All @@ -135,7 +135,7 @@ fn is_odd<F: FloatType>(mantissa: u64) -> bool {
/// This works since multiplying by the exponent will not affect the
/// mantissa unless the exponent is denormal, which will cause truncation
/// regardless.
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
fn pow2_fast_path<F>(mantissa: u64, radix: u32, radix_log2: i32, exponent: i32) -> F
where
F: FloatType,
Expand Down Expand Up @@ -361,7 +361,7 @@ where
// POW2

/// Parse power-of-two radix string to native float.
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
fn pow2_to_native<'a, F, Data>(
mut data: Data,
bytes: &'a [u8],
Expand Down Expand Up @@ -454,12 +454,12 @@ where
F: FloatType,
Data: FastDataInterface<'a>,
{
#[cfg(not(feature = "binary"))]
#[cfg(not(feature = "power_of_two"))]
{
pown_to_native(data, bytes, radix, incorrect, lossy, sign, rounding)
}

#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
{
let pow2_exp = log2(radix);
match pow2_exp {
Expand Down Expand Up @@ -526,7 +526,7 @@ mod tests {
}

#[test]
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
fn is_odd_test() {
// Variant of b1000000000000000000000001, a halfway value for f32.
assert!(is_odd::<f32>(0x1000002));
Expand Down Expand Up @@ -565,7 +565,7 @@ mod tests {
}

#[test]
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
fn is_halfway_test() {
// Variant of b1000000000000000000000001, a halfway value for f32.
assert!(is_halfway::<f32>(0x1000001));
Expand Down Expand Up @@ -602,7 +602,7 @@ mod tests {
}

#[test]
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
fn float_pow2_fast_path() {
// Everything is valid.
let mantissa = 1 << 63;
Expand All @@ -617,7 +617,7 @@ mod tests {
}

#[test]
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
fn double_pow2_fast_path_test() {
// Everything is valid.
let mantissa = 1 << 63;
Expand Down Expand Up @@ -848,7 +848,7 @@ mod tests {
fn atod_test() {
let atod10 = move |x| f64::from_lexical_partial(x);

#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
let atod2 = move |x| {
let options = ParseFloatOptions::binary();
f64::from_lexical_partial_with_options(x, &options)
Expand Down Expand Up @@ -929,10 +929,10 @@ mod tests {
// Rounding error
// Adapted from:
// https://www.exploringbinary.com/glibc-strtod-incorrectly-converts-2-to-the-negative-1075/
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
assert_eq!(Ok((5e-324, 14)), atod2(b"1^-10000110010"));

#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
assert_eq!(Ok((0.0, 14)), atod2(b"1^-10000110011"));
assert_eq!(Ok((0.0, 1077)), atod10(b"0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024703282292062327208828439643411068618252990130716238221279284125033775363510437593264991818081799618989828234772285886546332835517796989819938739800539093906315035659515570226392290858392449105184435931802849936536152500319370457678249219365623669863658480757001585769269903706311928279558551332927834338409351978015531246597263579574622766465272827220056374006485499977096599470454020828166226237857393450736339007967761930577506740176324673600968951340535537458516661134223766678604162159680461914467291840300530057530849048765391711386591646239524912623653881879636239373280423891018672348497668235089863388587925628302755995657524455507255189313690836254779186948667994968324049705821028513185451396213837722826145437693412532098591327667236328125"));

Expand Down Expand Up @@ -972,7 +972,7 @@ mod tests {
assert_eq!(Ok((38652960461239320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0, 310)), atod10(b"38652960461239320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0"));

// Round-trip for base2.
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
{
assert_eq!(
Ok((f64::from_bits(0x3bcd261840000000), 33)),
Expand Down
4 changes: 2 additions & 2 deletions lexical-core/src/atof/algorithm/powers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod large;
mod small;

// Always export, since it's required for the fast-path algorithm.
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
mod small64_binary;
mod small64_decimal;
#[cfg(feature = "radix")]
Expand All @@ -15,7 +15,7 @@ cfg_if! {
if #[cfg(limb_width_32)] {
mod large32_decimal;
mod small32_decimal;
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
mod small32_binary;
cfg_if! {
if #[cfg(feature = "radix")] {
Expand Down
16 changes: 8 additions & 8 deletions lexical-core/src/atof/algorithm/powers/small.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::util::Limb;
use static_assertions::const_assert;

#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
use super::small64_binary;
use super::small64_decimal;
#[cfg(feature = "radix")]
Expand All @@ -12,13 +12,13 @@ use super::small64_radix;
cfg_if! {
if #[cfg(limb_width_32)] {
use super::small32_decimal::*;
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
use super::small32_binary::*;
#[cfg(feature = "radix")]
use super::small32_radix::*;
} else {
use super::small64_decimal::*;
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
use super::small64_binary::*;
#[cfg(feature = "radix")]
use super::small64_radix::*;
Expand All @@ -29,7 +29,7 @@ const_assert!(POW5[1] / POW5[0] == 5);
const_assert!(POW10[1] / POW10[0] == 10);

cfg_if! {
if #[cfg(feature = "binary")] {
if #[cfg(feature = "power_of_two")] {
// Ensure our small powers are valid.
const_assert!(POW2[1] / POW2[0] == 2);
const_assert!(POW4[1] / POW4[0] == 4);
Expand Down Expand Up @@ -77,7 +77,7 @@ const_assert!(POW36[1] / POW36[0] == 36);
/// Get the correct small power from the radix.
#[inline]
pub(crate) fn get_small_powers(radix: u32) -> &'static [Limb] {
#[cfg(not(feature = "binary"))]
#[cfg(not(feature = "power_of_two"))]
{
match radix {
5 => &POW5,
Expand All @@ -86,7 +86,7 @@ pub(crate) fn get_small_powers(radix: u32) -> &'static [Limb] {
}
}

#[cfg(all(feature = "binary", not(feature = "radix")))]
#[cfg(all(feature = "power_of_two", not(feature = "radix")))]
{
match radix {
2 => &POW2,
Expand Down Expand Up @@ -146,7 +146,7 @@ pub(crate) fn get_small_powers(radix: u32) -> &'static [Limb] {
/// Get the correct 64-bit small power from the radix.
#[inline]
pub(crate) fn get_small_powers_64(radix: u32) -> &'static [u64] {
#[cfg(not(feature = "binary"))]
#[cfg(not(feature = "power_of_two"))]
{
match radix {
5 => &small64_decimal::POW5,
Expand All @@ -155,7 +155,7 @@ pub(crate) fn get_small_powers_64(radix: u32) -> &'static [u64] {
}
}

#[cfg(all(feature = "binary", not(feature = "radix")))]
#[cfg(all(feature = "power_of_two", not(feature = "radix")))]
{
match radix {
2 => &small64_binary::POW2,
Expand Down
6 changes: 3 additions & 3 deletions lexical-core/src/atof/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ where
Ok((F::INFINITY, iter.as_ptr()))
} else {
// Not infinity, may be valid with a different radix.
if cfg!(feature = "binary") {
if cfg!(feature = "power_of_two") {
algorithm::to_native::<F, Data>(data, bytes, sign, radix, incorrect, lossy, rounding)
} else {
Err((ErrorCode::InvalidDigit, bytes.as_ptr()))
Expand Down Expand Up @@ -101,7 +101,7 @@ where
Ok((F::NAN, iter.as_ptr()))
} else {
// Not NaN, may be valid with a different radix.
if cfg!(feature = "binary") {
if cfg!(feature = "power_of_two") {
algorithm::to_native::<F, Data>(data, bytes, sign, radix, incorrect, lossy, rounding)
} else {
Err((ErrorCode::InvalidDigit, bytes.as_ptr()))
Expand Down Expand Up @@ -721,7 +721,7 @@ mod tests {
}

#[test]
#[cfg(all(feature = "binary", feature = "rounding"))]
#[cfg(all(feature = "power_of_two", feature = "rounding"))]
fn special_rounding_binary_test() {
// Each one of these pairs is halfway, and we can detect the
// rounding schemes from this.
Expand Down
6 changes: 3 additions & 3 deletions lexical-core/src/atoi/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ from_lexical_with_options!(atoi_with_options, i128);
mod tests {
use crate::error::*;
use crate::traits::*;
#[cfg(any(feature = "format", feature = "binary"))]
#[cfg(any(feature = "format", feature = "power_of_two"))]
use crate::util::*;

#[cfg(feature = "property_tests")]
Expand Down Expand Up @@ -447,7 +447,7 @@ mod tests {
}

#[test]
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
fn i32_binary_test() {
let options = ParseIntegerOptions::binary();
assert_eq!(i32::from_lexical_with_options(b"11", &options), Ok(3));
Expand All @@ -457,7 +457,7 @@ mod tests {
#[cfg(feature = "property_tests")]
proptest! {
#[test]
#[cfg(feature = "binary")]
#[cfg(feature = "power_of_two")]
fn i32_binary_roundtrip_display_proptest(i in i32::MIN..i32::MAX) {
let mut buffer = new_buffer();
let write_opts = WriteIntegerOptions::binary();
Expand Down
2 changes: 1 addition & 1 deletion lexical-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) const U128_FORMATTED_SIZE_DECIMAL: usize = 39;
// Since we're declaring a variable on the stack, and our power-of-two
// alignment dramatically improved atoi performance, do it.
cfg_if! {
if #[cfg(feature = "binary")] {
if #[cfg(feature = "power_of_two")] {
// Use 256, actually, since we seem to have memory issues with f64.
// Clearly not sufficient memory allocated for non-decimal values.
pub(crate) const I8_FORMATTED_SIZE: usize = 16;
Expand Down
Loading

0 comments on commit 7010efd

Please sign in to comment.