Skip to content

Commit

Permalink
add new float functionality, rewrite casting code
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacholt100 committed Dec 18, 2024
1 parent 1eaa169 commit 159f7ad
Show file tree
Hide file tree
Showing 72 changed files with 4,396 additions and 3,154 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ exclude = ["src/float/*", "src/tests", "TODO.txt", "lit-parser/*"] # TODO: make
[features]
default = []
nightly = []
float = []
serde = ["dep:serde", "serde-big-array"]
numtraits = ["num-integer", "num-traits"]

[dependencies]
num-integer = { version = "0.1", optional = true, default-features = false }
num-traits = { version = "0.2", optional = true, default-features = false }
num-traits = { version = "0.2.19", optional = true, default-features = false }
serde = { version = "1.0", features = ["derive"], optional = true, default-features = false }
serde-big-array = { version = "0.5", optional = true, default-features = false }
rand = { version = "0.8", features = ["min_const_gen"], optional = true, default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ The `valuable` feature enables the [`Valuable`](https://docs.rs/valuable/latest/

### Nightly features

Activating the `nightly` feature will enable the `from_be_bytes`, `from_le_bytes`, `from_ne_bytes`, `to_be_bytes`, `to_le_bytes` and `to_ne_bytes` methods on `bnum`'s unsigned and signed integers and will make the `unchecked_...` methods `const`. This comes at the cost of only being able to compile on nightly. The nightly features that this uses are [`generic_const_exprs`](https://github.com/rust-lang/rust/issues/76560), [`const_trait_impl`](https://github.com/rust-lang/rust/issues/67792), [`effects`](https://github.com/rust-lang/rust/issues/102090) and [`const_option`](https://github.com/rust-lang/rust/issues/67441).
Activating the `nightly` feature will enable the `from_be_bytes`, `from_le_bytes`, `from_ne_bytes`, `to_be_bytes`, `to_le_bytes` and `to_ne_bytes` methods on `bnum`'s unsigned and signed integers and will make the `unchecked_...` methods `const`. This comes at the cost of only being able to compile on nightly. The nightly features that this uses are [`generic_const_exprs`](https://github.com/rust-lang/rust/issues/76560), [`const_trait_impl`](https://github.com/rust-lang/rust/issues/67792) and [`const_option`](https://github.com/rust-lang/rust/issues/67441).

## Testing

Expand Down
40 changes: 27 additions & 13 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ Floats:
* Conversions from and to:
* primitive floats
* bnum floats
* FromStr trait
* FromStr trait: REMEMBER: the num_traits crate has a general from_str_radix method for floats, could use if stuck
* Display, debug, upper exp, lower exp traits
* Transcendental functions:
* exp
* exp2
* exp_m1
* ln
* ln_1p
* log
* log2
* log10
Expand All @@ -21,8 +23,6 @@ Floats:
* atan
* atan2
* sin_cos
* exp_m1
* ln_1p
* sinh
* cosh
* tanh
Expand All @@ -31,14 +31,19 @@ Floats:
* atanh
* to_degrees
* to_radians
* powf
* gamma
* ln_gamma
* Other functions:
* mul_add
* midpoint
* recip
* Optimised division algorithm depending on size of mantissa
* recip
* Optimised multiplication algorithm depending on size of mantissa
* Fused multiply add
* Constants:
* DIGITS
* MIN_10_EXP
* MAX_10_EXP
* DIGITS. For this, we can divide MB by f64::LOG2_10 and take floor (roughly speaking).
* MIN_10_EXP. For this, we can divide MIN_EXP by f64::LOG2_10, and take floor (roughly speaking)
* MAX_10_EXP. For this, we can divide MAX_EXP by f64::LOG2_10, and take floor (roughly speaking)
* Maths constants:
* E
* FRAC_1_PI
Expand All @@ -62,11 +67,20 @@ Floats:
* From/TryFrom trait for ints, other floats
* Float type aliases from IEEE standard: f16, f32, f64, f80, f128. (Include f32 and f64 as allows const methods which aren't available on the primitives)
* Serde
* Rand
* num_traits::{Bounded, Float, FloatConst, AsPrimitive, FromPrimitive, ToPrimitive, One, Zero, Inv, MulAdd, MulAddAssign, Pow, Signed, Euclid, Num}
* Rand:
* gen_range stuff
* num_traits::{Bounded, Float, FloatConst, FloatCore, AsPrimitive, FromPrimitive, ToPrimitive, ConstZero, ConstOne, One, Zero, FromBytes, ToBytes, Inv, MulAdd, MulAddAssign, Pow, Signed, Euclid, Num}
* Division algorithm which doesn't need where clause

Ints:
Faster mulitplication algorithm for larger integers
Faster division algorithms for larger integers
Update serde to use decimal string instead of struct debug - but CHECK that all serde options serialise primitive ints as decimal strings
* Use new Rust const capabilities to write more idiomatic code (e.g. we don't need the option_expect! macro anymore). Note though that maybe we should wait a bit to keep the MSRV not too recent
* Faster mulitplication algorithm for larger integers
* Faster division algorithms for larger integers
* Update serde to use decimal string instead of struct debug - but CHECK that all serde options serialise primitive ints as decimal strings
* FromBytes and ToBytes num_traits traits - only when can implement without "unconstrained generic constant" error

Other stuff:
* Think about removing BTryFrom and just implementing TryFrom (no From for now), then can use CastFrom/As trait for Result-less conversions
* Replace bitors, bitands, shifts, masks etc. with more efficient implementations (e.g. using set_bit, flip_bit, one-less-than-power-of-two methods, methods for efficiently generating masks/getting certain range of bits of integer)
* Consider putting floats and signed integers behind optional features (which are enabled by default)
* Add 16 bit and 32 bit width types to the test widths, so test u16, u32, f16, f32 as well (just make the digit sizes that are too wide not do anything for those tests)
7 changes: 6 additions & 1 deletion bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ license = "MIT OR Apache-2.0"

[dev-dependencies]
criterion = { version = "0.5" }
bnum = { path = "../", features = ["rand", "nightly"] }
bnum = { path = "../", features = ["rand", "nightly", "float"] }
num-traits = "0.2"
uint = "0.9"
rand = { version = "0.8", features = ["std", "std_rng"] }
paste = "1.0"

[[bench]]
name = "float"
# path = "benches/float/mod.rs"
harness = false

[[bench]]
name = "benchmark"
harness = false
Expand Down
Loading

0 comments on commit 159f7ad

Please sign in to comment.