require rustc 1.43.0
Will probably bump requirement to 1.44.0 before releasing 1.0.0 so that {from,to}_{le,be,ne}_bytes can be const fn.
This commit is contained in:
parent
48a763c01f
commit
ff0874a1f4
|
@ -22,7 +22,7 @@ install:
|
|||
- curl -sSf -o rustup-init.exe https://win.rustup.rs
|
||||
- rustup-init.exe -y --default-host %TARGET% --default-toolchain none --no-modify-path
|
||||
- rustup --version
|
||||
- rustup toolchain install --profile minimal beta-%TARGET% 1.39.0-%TARGET%
|
||||
- rustup toolchain install --profile minimal beta-%TARGET% 1.43.0-%TARGET%
|
||||
- rustup component add --toolchain beta-%TARGET% rustfmt clippy
|
||||
|
||||
build: false
|
||||
|
@ -51,7 +51,7 @@ test_script:
|
|||
- cargo +beta-%TARGET% check --all-targets --features fail-on-warnings
|
||||
- cargo +beta-%TARGET% test --release --features "fail-on-warnings az f16 serde"
|
||||
- cargo +beta-%TARGET% fmt -- --check
|
||||
- cargo +1.39.0-%TARGET% test --lib --features "fail-on-warnings az f16 serde"
|
||||
- cargo +1.43.0-%TARGET% test --lib --features "fail-on-warnings az f16 serde"
|
||||
|
||||
after_test:
|
||||
- bash -c "if [ -d $USERPROFILE/.cargo/registry/cache ]; then cd $USERPROFILE/.cargo/registry; for c in cache/*/*.crate; do s=src/${c#cache/}; if [ ! -e ${s/.crate/} ]; then rm -v $c; fi; done; find cache -name \*.crate | sort; fi"
|
||||
|
|
|
@ -9,7 +9,7 @@ before_script:
|
|||
- getconf LONG_BIT
|
||||
- rustup self update
|
||||
- rustup --version
|
||||
- rustup toolchain install --profile minimal beta-$TARGET 1.39.0-$TARGET
|
||||
- rustup toolchain install --profile minimal beta-$TARGET 1.43.0-$TARGET
|
||||
- rustup component add --toolchain beta-$TARGET rustfmt clippy
|
||||
- if [ -d cargo/registry/cache ]; then rm -rf $CARGO_HOME/registry/cache; mkdir -p $CARGO_HOME/registry; cp -R cargo/registry/cache $CARGO_HOME/registry/; echo Copied registry/cache; fi
|
||||
- if [ -d $CARGO_HOME/registry/src ]; then rm -r $CARGO_HOME/registry/src; fi
|
||||
|
@ -50,8 +50,8 @@ x86_64-gnulinux:
|
|||
- cargo +beta-$TARGET test --features "fail-on-warnings az f16 serde std"
|
||||
- cargo +beta-$TARGET test --release --features "fail-on-warnings az f16 serde std"
|
||||
- cargo +beta-$TARGET fmt -- --check
|
||||
- cargo +1.39.0-$TARGET test --lib --features "fail-on-warnings az f16 serde std"
|
||||
- cargo +1.39.0-$TARGET test --release --lib --features "fail-on-warnings az f16 serde std"
|
||||
- cargo +1.43.0-$TARGET test --lib --features "fail-on-warnings az f16 serde std"
|
||||
- cargo +1.43.0-$TARGET test --release --lib --features "fail-on-warnings az f16 serde std"
|
||||
|
||||
i686-gnulinux:
|
||||
image: i386/rust:1
|
||||
|
@ -82,8 +82,8 @@ i686-gnulinux:
|
|||
- cargo +beta-$TARGET test --features "fail-on-warnings f16 serde"
|
||||
- cargo +beta-$TARGET test --release --features "fail-on-warnings az f16 serde std"
|
||||
- cargo +beta-$TARGET fmt -- --check
|
||||
- cargo +1.39.0-$TARGET test --lib --features "fail-on-warnings az f16 serde std"
|
||||
- cargo +1.39.0-$TARGET test --release --lib --features "fail-on-warnings az f16 serde std"
|
||||
- cargo +1.43.0-$TARGET test --lib --features "fail-on-warnings az f16 serde std"
|
||||
- cargo +1.43.0-$TARGET test --release --lib --features "fail-on-warnings az f16 serde std"
|
||||
|
||||
x86_64-gnulinux-tarpaulin:
|
||||
image: amd64/rust:1
|
||||
|
|
|
@ -76,6 +76,7 @@ The conversions supported cover the following cases.
|
|||
|
||||
### Version 1.0.0 news (unreleased)
|
||||
|
||||
* The crate now requires rustc version 1.43.0 or later.
|
||||
* All deprecated items were removed.
|
||||
|
||||
### Version 0.5.6 news (2020-05-01)
|
||||
|
@ -173,7 +174,7 @@ it in your crate, add it as a dependency inside [*Cargo.toml*]:
|
|||
fixed = "0.5.6"
|
||||
```
|
||||
|
||||
The *fixed* crate requires rustc version 1.39.0 or later.
|
||||
The *fixed* crate requires rustc version 1.43.0 or later.
|
||||
|
||||
## Optional features
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ as-is, without any warranty. -->
|
|||
Version 1.0.0 (unreleased)
|
||||
==========================
|
||||
|
||||
* The crate now requires rustc version 1.43.0 or later.
|
||||
* All deprecated items were removed.
|
||||
|
||||
Version 0.5.6 (2020-05-01)
|
||||
|
@ -90,7 +91,7 @@ Version 0.5.1 (2019-12-22)
|
|||
Version 0.5.0 (2019-12-06)
|
||||
==========================
|
||||
|
||||
* The *fixed* crate now requires rustc version 1.39.0 or later.
|
||||
* The crate now requires rustc version 1.39.0 or later.
|
||||
* The following methods were added to all fixed-point types and to
|
||||
the `Fixed` trait:
|
||||
* `from_be_bytes`, `from_le_bytes`, `from_ne_bytes`
|
||||
|
@ -139,7 +140,7 @@ Version 0.4.4 (2019-08-24)
|
|||
Version 0.4.3 (2019-08-20)
|
||||
==========================
|
||||
|
||||
* The *fixed* crate now requires rustc version 1.34.0 or later.
|
||||
* The crate now requires rustc version 1.34.0 or later.
|
||||
* The precision argument is no longer ignored when formatting
|
||||
fixed-point numbers; precision should now be handled the same as
|
||||
for primitive floating-point numbers in the standard library.
|
||||
|
@ -187,7 +188,7 @@ Version 0.4.1 (2019-08-12)
|
|||
Version 0.4.0 (2019-08-08)
|
||||
==========================
|
||||
|
||||
* The *fixed* crate now requires rustc version 1.31.0 or later.
|
||||
* The crate now requires rustc version 1.31.0 or later.
|
||||
* The `traits` module was added, with its traits `Fixed`,
|
||||
`FixedSigned`, `FixedUnsigned`, `FromFixed`, `ToFixed`,
|
||||
`LossyFrom` and `LossyInto`.
|
||||
|
|
|
@ -266,11 +266,9 @@ mod tests {
|
|||
assert_eq!(f16::from_fixed(SQRT_2), f16::SQRT_2);
|
||||
assert_eq!(f16::from_fixed(FRAC_1_SQRT_2), f16::FRAC_1_SQRT_2);
|
||||
assert_eq!(f16::from_fixed(E), f16::E);
|
||||
// TODO if half crate adds LOG2_10, use f16::LOG2_10
|
||||
assert_eq!(f16::from_fixed(LOG2_10), f16::from_f32(10f32.log2()));
|
||||
assert_eq!(f16::from_fixed(LOG2_10), f16::from_bits(0x42A5));
|
||||
assert_eq!(f16::from_fixed(LOG2_E), f16::LOG2_E);
|
||||
// TODO if half crate adds LOG10_2, use f16::LOG10_2
|
||||
assert_eq!(f16::from_fixed(LOG10_2), f16::from_f32(2f32.log10()));
|
||||
assert_eq!(f16::from_fixed(LOG10_2), f16::from_bits(0x34D1));
|
||||
assert_eq!(f16::from_fixed(LOG10_E), f16::LOG10_E);
|
||||
assert_eq!(f16::from_fixed(LN_2), f16::LN_2);
|
||||
assert_eq!(f16::from_fixed(LN_10), f16::LN_10);
|
||||
|
@ -308,11 +306,9 @@ mod tests {
|
|||
assert_eq!(bf16::from_fixed(SQRT_2), bf16::SQRT_2);
|
||||
assert_eq!(bf16::from_fixed(FRAC_1_SQRT_2), bf16::FRAC_1_SQRT_2);
|
||||
assert_eq!(bf16::from_fixed(E), bf16::E);
|
||||
// TODO if half crate adds LOG2_10, use bf16::LOG2_10
|
||||
assert_eq!(bf16::from_fixed(LOG2_10), bf16::from_f32(10f32.log2()));
|
||||
assert_eq!(bf16::from_fixed(LOG2_10), bf16::from_bits(0x4055));
|
||||
assert_eq!(bf16::from_fixed(LOG2_E), bf16::LOG2_E);
|
||||
// TODO if half crate adds LOG10_2, use bf16::LOG10_2
|
||||
assert_eq!(bf16::from_fixed(LOG10_2), bf16::from_f32(2f32.log10()));
|
||||
assert_eq!(bf16::from_fixed(LOG10_2), bf16::from_bits(0x3E9A));
|
||||
assert_eq!(bf16::from_fixed(LOG10_E), bf16::LOG10_E);
|
||||
assert_eq!(bf16::from_fixed(LN_2), bf16::LN_2);
|
||||
assert_eq!(bf16::from_fixed(LN_10), bf16::LN_10);
|
||||
|
@ -342,11 +338,9 @@ mod tests {
|
|||
assert_eq!(f32::from_fixed(SQRT_2), f32::consts::SQRT_2);
|
||||
assert_eq!(f32::from_fixed(FRAC_1_SQRT_2), f32::consts::FRAC_1_SQRT_2);
|
||||
assert_eq!(f32::from_fixed(E), f32::consts::E);
|
||||
// TODO when rustc requirement >= 1.43.0, use f32::consts::LOG2_10
|
||||
assert_eq!(f32::from_fixed(LOG2_10), 10f32.log2());
|
||||
assert_eq!(f32::from_fixed(LOG2_10), f32::consts::LOG2_10);
|
||||
assert_eq!(f32::from_fixed(LOG2_E), f32::consts::LOG2_E);
|
||||
// TODO when rustc requirement >= 1.43.0, use f32::consts::LOG10_2
|
||||
assert_eq!(f32::from_fixed(LOG10_2), 2f32.log10());
|
||||
assert_eq!(f32::from_fixed(LOG10_2), f32::consts::LOG10_2);
|
||||
assert_eq!(f32::from_fixed(LOG10_E), f32::consts::LOG10_E);
|
||||
assert_eq!(f32::from_fixed(LN_2), f32::consts::LN_2);
|
||||
assert_eq!(f32::from_fixed(LN_10), f32::consts::LN_10);
|
||||
|
@ -376,11 +370,9 @@ mod tests {
|
|||
assert_eq!(f64::from_fixed(SQRT_2), f64::consts::SQRT_2);
|
||||
assert_eq!(f64::from_fixed(FRAC_1_SQRT_2), f64::consts::FRAC_1_SQRT_2);
|
||||
assert_eq!(f64::from_fixed(E), f64::consts::E);
|
||||
// TODO when rustc requirement >= 1.43.0, use f64::consts::LOG2_10
|
||||
assert_eq!(f64::from_fixed(LOG2_10), 10f64.log2());
|
||||
assert_eq!(f64::from_fixed(LOG2_10), f64::consts::LOG2_10);
|
||||
assert_eq!(f64::from_fixed(LOG2_E), f64::consts::LOG2_E);
|
||||
// TODO when rustc requirement >= 1.43.0, use f64::consts::LOG10_2
|
||||
assert_eq!(f64::from_fixed(LOG10_2), 2f64.log10());
|
||||
assert_eq!(f64::from_fixed(LOG10_2), f64::consts::LOG10_2);
|
||||
assert_eq!(f64::from_fixed(LOG10_E), f64::consts::LOG10_E);
|
||||
assert_eq!(f64::from_fixed(LN_2), f64::consts::LN_2);
|
||||
assert_eq!(f64::from_fixed(LN_10), f64::consts::LN_10);
|
||||
|
|
|
@ -146,7 +146,7 @@ it in your crate, add it as a dependency inside [*Cargo.toml*]:
|
|||
fixed = "0.5.6"
|
||||
```
|
||||
|
||||
The *fixed* crate requires rustc version 1.39.0 or later.
|
||||
The *fixed* crate requires rustc version 1.43.0 or later.
|
||||
|
||||
## Optional features
|
||||
|
||||
|
|
38
src/log10.rs
38
src/log10.rs
|
@ -124,7 +124,7 @@ fn int_part_log10_less_than_16(mut val: u64) -> i32 {
|
|||
// MAX / 100_000_000 < val <= MAX
|
||||
// -8 <= log <= -1
|
||||
fn frac_part_log10_greater_equal_m8_u32(mut val: u32) -> i32 {
|
||||
const MAX: u32 = u32::max_value();
|
||||
const MAX: u32 = u32::MAX;
|
||||
debug_assert!(val > MAX / 100_000_000);
|
||||
let mut log = 0;
|
||||
if val <= MAX / 10_000 {
|
||||
|
@ -146,7 +146,7 @@ fn frac_part_log10_greater_equal_m8_u32(mut val: u32) -> i32 {
|
|||
// MAX / 100_000_000 < val <= MAX
|
||||
// -8 <= log <= 1
|
||||
fn frac_part_log10_greater_equal_m8_u64(mut val: u64) -> i32 {
|
||||
const MAX: u64 = u64::max_value();
|
||||
const MAX: u64 = u64::MAX;
|
||||
debug_assert!(val > MAX / 100_000_000);
|
||||
let mut log = 0;
|
||||
if val <= MAX / 10_000 {
|
||||
|
@ -186,7 +186,7 @@ impl IntFracLog10 for u32 {
|
|||
// 0 < val <= MAX
|
||||
// -10 <= log <= -1
|
||||
fn frac_part_log10(mut self) -> i32 {
|
||||
const MAX: u32 = u32::max_value();
|
||||
const MAX: u32 = u32::MAX;
|
||||
if self <= MAX / 100_000_000 {
|
||||
self *= 100_000_000;
|
||||
// At this point, we have shifted out 8 digits, and we can only shift out 2 more.
|
||||
|
@ -228,7 +228,7 @@ impl IntFracLog10 for u64 {
|
|||
// 0 < val <= MAX
|
||||
// -20 <= log <= -1
|
||||
fn frac_part_log10(mut self) -> i32 {
|
||||
const MAX: u64 = u64::max_value();
|
||||
const MAX: u64 = u64::MAX;
|
||||
let mut log = 0;
|
||||
if self <= MAX / 10_000_000_000_000_000 {
|
||||
// After this, we can only check up to -4 more because -20 <= log <= -16.
|
||||
|
@ -265,21 +265,21 @@ impl IntFracLog10 for u128 {
|
|||
let mut log = 0;
|
||||
if self >= 100_000_000_000_000_000_000_000_000_000_000 {
|
||||
self /= 100_000_000_000_000_000_000_000_000_000_000;
|
||||
debug_assert!(self <= u32::max_value() as u128);
|
||||
debug_assert!(self <= u32::MAX as u128);
|
||||
return 32 + int_part_log10_less_than_8(self as u32);
|
||||
}
|
||||
if self >= 10_000_000_000_000_000 {
|
||||
self /= 10_000_000_000_000_000;
|
||||
log += 16;
|
||||
}
|
||||
debug_assert!(self <= u64::max_value() as u128);
|
||||
debug_assert!(self <= u64::MAX as u128);
|
||||
log + int_part_log10_less_than_16(self as u64)
|
||||
}
|
||||
|
||||
// 0 < val <= MAX
|
||||
// -39 <= log <= -1
|
||||
fn frac_part_log10(mut self) -> i32 {
|
||||
const MAX: u128 = u128::max_value();
|
||||
const MAX: u128 = u128::MAX;
|
||||
let mut log = 0;
|
||||
if self <= MAX / 100_000_000_000_000_000_000_000_000_000_000 {
|
||||
self *= 100_000_000_000_000_000_000_000_000_000_000;
|
||||
|
@ -328,7 +328,7 @@ mod tests {
|
|||
|
||||
macro_rules! check_loop {
|
||||
($T:ty) => {
|
||||
for i in 0..=<$T>::max_value().int_part_log10() {
|
||||
for i in 0..=<$T>::MAX.int_part_log10() {
|
||||
let p = (10 as $T).pow(i as u32);
|
||||
if i > 0 {
|
||||
assert_eq!((p - 1).int_part_log10(), i - 1);
|
||||
|
@ -338,7 +338,7 @@ mod tests {
|
|||
}
|
||||
|
||||
for i in 0..-(1 as $T).frac_part_log10() {
|
||||
let p = <$T>::max_value() / (10 as $T).pow(i as u32);
|
||||
let p = <$T>::MAX / (10 as $T).pow(i as u32);
|
||||
if p > 1 {
|
||||
assert_eq!((p - 1).frac_part_log10(), -1 - i);
|
||||
}
|
||||
|
@ -353,9 +353,9 @@ mod tests {
|
|||
#[test]
|
||||
fn log10_u8() {
|
||||
assert_eq!(1u8.int_part_log10(), 0);
|
||||
assert_eq!(u8::max_value().int_part_log10(), 2);
|
||||
assert_eq!(u8::MAX.int_part_log10(), 2);
|
||||
assert_eq!(1u8.frac_part_log10(), -3);
|
||||
assert_eq!(u8::max_value().frac_part_log10(), -1);
|
||||
assert_eq!(u8::MAX.frac_part_log10(), -1);
|
||||
|
||||
check_loop! { u8 }
|
||||
}
|
||||
|
@ -363,9 +363,9 @@ mod tests {
|
|||
#[test]
|
||||
fn log10_u16() {
|
||||
assert_eq!(1u16.int_part_log10(), 0);
|
||||
assert_eq!(u16::max_value().int_part_log10(), 4);
|
||||
assert_eq!(u16::MAX.int_part_log10(), 4);
|
||||
assert_eq!(1u16.frac_part_log10(), -5);
|
||||
assert_eq!(u16::max_value().frac_part_log10(), -1);
|
||||
assert_eq!(u16::MAX.frac_part_log10(), -1);
|
||||
|
||||
check_loop! { u16 }
|
||||
}
|
||||
|
@ -373,9 +373,9 @@ mod tests {
|
|||
#[test]
|
||||
fn log10_u32() {
|
||||
assert_eq!(1u32.int_part_log10(), 0);
|
||||
assert_eq!(u32::max_value().int_part_log10(), 9);
|
||||
assert_eq!(u32::MAX.int_part_log10(), 9);
|
||||
assert_eq!(1u32.frac_part_log10(), -10);
|
||||
assert_eq!(u32::max_value().frac_part_log10(), -1);
|
||||
assert_eq!(u32::MAX.frac_part_log10(), -1);
|
||||
|
||||
check_loop! { u32 }
|
||||
}
|
||||
|
@ -383,9 +383,9 @@ mod tests {
|
|||
#[test]
|
||||
fn log10_u64() {
|
||||
assert_eq!(1u64.int_part_log10(), 0);
|
||||
assert_eq!(u64::max_value().int_part_log10(), 19);
|
||||
assert_eq!(u64::MAX.int_part_log10(), 19);
|
||||
assert_eq!(1u64.frac_part_log10(), -20);
|
||||
assert_eq!(u64::max_value().frac_part_log10(), -1);
|
||||
assert_eq!(u64::MAX.frac_part_log10(), -1);
|
||||
|
||||
check_loop! { u64 }
|
||||
}
|
||||
|
@ -393,9 +393,9 @@ mod tests {
|
|||
#[test]
|
||||
fn log10_u128() {
|
||||
assert_eq!(1u128.int_part_log10(), 0);
|
||||
assert_eq!(u128::max_value().int_part_log10(), 38);
|
||||
assert_eq!(u128::MAX.int_part_log10(), 38);
|
||||
assert_eq!(1u128.frac_part_log10(), -39);
|
||||
assert_eq!(u128::max_value().frac_part_log10(), -1);
|
||||
assert_eq!(u128::MAX.frac_part_log10(), -1);
|
||||
|
||||
check_loop! { u128 }
|
||||
}
|
||||
|
|
|
@ -144,7 +144,6 @@ assert_eq!(Fix::from_num(0.1875).checked_int_log2(), Some(-3));
|
|||
}
|
||||
}
|
||||
|
||||
// TODO when rustc requirement >= 1.43.0, use MAX instead of max_value()
|
||||
comment! {
|
||||
"Checked integer base-10 logarithm, rounded down.
|
||||
Returns the logarithm or [`None`] if the fixed-point number is
|
||||
|
|
|
@ -208,7 +208,6 @@ use fixed::{
|
|||
types::I16F16,
|
||||
", $s_fixed, ",
|
||||
};
|
||||
# use std::", $s_inner, "; // TODO when rustc requirement >= 1.43.0, remove this
|
||||
type Fix = ", $s_fixed, "<U4>;
|
||||
|
||||
// 1.75 is 1.11 in binary
|
||||
|
@ -357,7 +356,7 @@ assert_eq!(one_point_625.checked_to_num::<f32>(), Some(1.625f32));
|
|||
}
|
||||
|
||||
comment! {
|
||||
"Creates a fixed-point number from another number,
|
||||
"Creates a fixed-point number from another number,
|
||||
saturating if it does not fit.
|
||||
|
||||
The other number can be:
|
||||
|
@ -386,12 +385,7 @@ use fixed::{
|
|||
types::I16F16,
|
||||
", $s_fixed, ",
|
||||
};
|
||||
", if_signed_else_empty_str!(
|
||||
$Signedness,
|
||||
// TODO when rustc requirement >= 1.43.0, remove this
|
||||
"# use std::", $s_inner, ";
|
||||
"),
|
||||
"type Fix = ", $s_fixed, "<U4>;
|
||||
type Fix = ", $s_fixed, "<U4>;
|
||||
|
||||
// 1.75 is 1.11 in binary
|
||||
let src = I16F16::from_bits(0b111 << (16 - 2));
|
||||
|
@ -401,24 +395,24 @@ assert_eq!(Fix::saturating_from_num(too_large), Fix::MAX);
|
|||
|
||||
assert_eq!(Fix::saturating_from_num(3), Fix::from_bits(3 << 4));
|
||||
let too_small = ",
|
||||
if_signed_unsigned! {
|
||||
$Signedness,
|
||||
concat!($s_inner, "::MIN"),
|
||||
"-1",
|
||||
},
|
||||
";
|
||||
if_signed_unsigned! {
|
||||
$Signedness,
|
||||
concat!($s_inner, "::MIN"),
|
||||
"-1",
|
||||
},
|
||||
";
|
||||
assert_eq!(Fix::saturating_from_num(too_small), Fix::MIN);
|
||||
|
||||
// 1.75 is 1.11 in binary
|
||||
let expected = Fix::from_bits(0b111 << (4 - 2));
|
||||
assert_eq!(Fix::saturating_from_num(1.75f32), expected);
|
||||
assert_eq!(Fix::saturating_from_num(",
|
||||
if_signed_unsigned! {
|
||||
$Signedness,
|
||||
"-1.75f64), -",
|
||||
"1.75f64), ",
|
||||
},
|
||||
"expected);
|
||||
if_signed_unsigned! {
|
||||
$Signedness,
|
||||
"-1.75f64), -",
|
||||
"1.75f64), ",
|
||||
},
|
||||
"expected);
|
||||
assert_eq!(Fix::saturating_from_num(2e38), Fix::MAX);
|
||||
assert_eq!(Fix::saturating_from_num(std::f64::NEG_INFINITY), Fix::MIN);
|
||||
```
|
||||
|
@ -444,14 +438,14 @@ assert_eq!(Fix::saturating_from_num(std::f64::NEG_INFINITY), Fix::MIN);
|
|||
[`u8`]: https://doc.rust-lang.org/nightly/std/primitive.u8.html
|
||||
[`usize`]: https://doc.rust-lang.org/nightly/std/primitive.usize.html
|
||||
";
|
||||
#[inline]
|
||||
pub fn saturating_from_num<Src: ToFixed>(src: Src) -> $Fixed<Frac> {
|
||||
src.saturating_to_fixed()
|
||||
}
|
||||
#[inline]
|
||||
pub fn saturating_from_num<Src: ToFixed>(src: Src) -> $Fixed<Frac> {
|
||||
src.saturating_to_fixed()
|
||||
}
|
||||
}
|
||||
|
||||
comment! {
|
||||
"Converts a fixed-point number to another number,
|
||||
"Converts a fixed-point number to another number,
|
||||
saturating the value if it does not fit.
|
||||
|
||||
The other number can be:
|
||||
|
@ -476,12 +470,7 @@ use fixed::{
|
|||
types::I16F16,
|
||||
", $s_fixed, ",
|
||||
};
|
||||
", if_unsigned_else_empty_str!(
|
||||
$Signedness,
|
||||
// TODO when rustc requirement >= 1.43.0, remove this
|
||||
"# use std::i", $s_nbits, ";
|
||||
"),
|
||||
"type Fix = ", $s_fixed, "<U4>;
|
||||
type Fix = ", $s_fixed, "<U4>;
|
||||
|
||||
// 1.75 is 1.11 in binary
|
||||
let src = Fix::from_bits(0b111 << (4 - 2));
|
||||
|
@ -496,15 +485,15 @@ let two_point_5 = Fix::from_bits(0b101 << (4 - 1));
|
|||
assert_eq!(two_point_5.saturating_to_num::<i32>(), 2);
|
||||
type AllInt = ", $s_fixed, "<U0>;
|
||||
assert_eq!(",
|
||||
if_signed_unsigned! {
|
||||
$Signedness,
|
||||
concat!("AllInt::from_bits(-1).saturating_to_num::<u", $s_nbits, ">(), 0"),
|
||||
concat!(
|
||||
"AllInt::MAX.saturating_to_num::<i", $s_nbits, ">(), ",
|
||||
"i", $s_nbits, "::MAX",
|
||||
),
|
||||
},
|
||||
");
|
||||
if_signed_unsigned! {
|
||||
$Signedness,
|
||||
concat!("AllInt::from_bits(-1).saturating_to_num::<u", $s_nbits, ">(), 0"),
|
||||
concat!(
|
||||
"AllInt::MAX.saturating_to_num::<i", $s_nbits, ">(), ",
|
||||
"i", $s_nbits, "::MAX",
|
||||
),
|
||||
},
|
||||
");
|
||||
|
||||
// 1.625 is 1.101 in binary
|
||||
let one_point_625 = Fix::from_bits(0b1101 << (4 - 3));
|
||||
|
@ -531,11 +520,11 @@ assert_eq!(one_point_625.saturating_to_num::<f32>(), 1.625f32);
|
|||
[`u8`]: https://doc.rust-lang.org/nightly/std/primitive.u8.html
|
||||
[`usize`]: https://doc.rust-lang.org/nightly/std/primitive.usize.html
|
||||
";
|
||||
#[inline]
|
||||
pub fn saturating_to_num<Dst: FromFixed>(self) -> Dst {
|
||||
Dst::saturating_from_fixed(self)
|
||||
}
|
||||
#[inline]
|
||||
pub fn saturating_to_num<Dst: FromFixed>(self) -> Dst {
|
||||
Dst::saturating_from_fixed(self)
|
||||
}
|
||||
}
|
||||
|
||||
comment! {
|
||||
"Creates a fixed-point number from another number,
|
||||
|
@ -618,7 +607,7 @@ assert_eq!(Fix::wrapping_from_num(large), wrapped);
|
|||
}
|
||||
|
||||
comment! {
|
||||
"Converts a fixed-point number to another number,
|
||||
"Converts a fixed-point number to another number,
|
||||
wrapping the value on overflow.
|
||||
|
||||
The other number can be:
|
||||
|
@ -643,12 +632,7 @@ use fixed::{
|
|||
types::I16F16,
|
||||
", $s_fixed, ",
|
||||
};
|
||||
", if_signed_else_empty_str!(
|
||||
$Signedness,
|
||||
// TODO when rustc requirement >= 1.43.0, remove this
|
||||
"# use std::u", $s_nbits, ";
|
||||
"),
|
||||
"type Fix = ", $s_fixed, "<U4>;
|
||||
type Fix = ", $s_fixed, "<U4>;
|
||||
|
||||
// 1.75 is 1.11 in binary
|
||||
let src = Fix::from_bits(0b111 << (4 - 2));
|
||||
|
@ -663,15 +647,15 @@ let two_point_5 = Fix::from_bits(0b101 << (4 - 1));
|
|||
assert_eq!(two_point_5.wrapping_to_num::<i32>(), 2);
|
||||
type AllInt = ", $s_fixed, "<U0>;
|
||||
assert_eq!(",
|
||||
if_signed_unsigned! {
|
||||
$Signedness,
|
||||
concat!(
|
||||
"AllInt::from_bits(-1).wrapping_to_num::<u", $s_nbits, ">(), ",
|
||||
"u", $s_nbits, "::MAX",
|
||||
),
|
||||
concat!("AllInt::MAX.wrapping_to_num::<i", $s_nbits, ">(), -1"),
|
||||
},
|
||||
");
|
||||
if_signed_unsigned! {
|
||||
$Signedness,
|
||||
concat!(
|
||||
"AllInt::from_bits(-1).wrapping_to_num::<u", $s_nbits, ">(), ",
|
||||
"u", $s_nbits, "::MAX",
|
||||
),
|
||||
concat!("AllInt::MAX.wrapping_to_num::<i", $s_nbits, ">(), -1"),
|
||||
},
|
||||
");
|
||||
|
||||
// 1.625 is 1.101 in binary
|
||||
let one_point_625 = Fix::from_bits(0b1101 << (4 - 3));
|
||||
|
@ -698,11 +682,11 @@ assert_eq!(one_point_625.wrapping_to_num::<f32>(), 1.625f32);
|
|||
[`u8`]: https://doc.rust-lang.org/nightly/std/primitive.u8.html
|
||||
[`usize`]: https://doc.rust-lang.org/nightly/std/primitive.usize.html
|
||||
";
|
||||
#[inline]
|
||||
pub fn wrapping_to_num<Dst: FromFixed>(self) -> Dst {
|
||||
Dst::wrapping_from_fixed(self)
|
||||
}
|
||||
#[inline]
|
||||
pub fn wrapping_to_num<Dst: FromFixed>(self) -> Dst {
|
||||
Dst::wrapping_from_fixed(self)
|
||||
}
|
||||
}
|
||||
|
||||
comment! {
|
||||
"Creates a fixed-point number from another number.
|
||||
|
|
|
@ -20,7 +20,6 @@ macro_rules! fixed_no_frac {
|
|||
$UInner:ty, $Signedness:tt
|
||||
) => {
|
||||
impl<Frac> $Fixed<Frac> {
|
||||
// TODO when rustc requirement >= 1.43.0, use MIN instead of min_value()
|
||||
comment! {
|
||||
"The smallest value that can be represented.
|
||||
|
||||
|
@ -28,15 +27,13 @@ macro_rules! fixed_no_frac {
|
|||
|
||||
```rust
|
||||
use fixed::{types::extra::U4, ", $s_fixed, "};
|
||||
# use std::", $s_inner, "; // TODO when rustc requirement >= 1.43.0, remove this
|
||||
type Fix = ", $s_fixed, "<U4>;
|
||||
assert_eq!(Fix::MIN, Fix::from_bits(", $s_inner, "::MIN));
|
||||
```
|
||||
";
|
||||
pub const MIN: $Fixed<Frac> = Self::from_bits(<$Inner>::min_value());
|
||||
pub const MIN: $Fixed<Frac> = Self::from_bits(<$Inner>::MIN);
|
||||
}
|
||||
|
||||
// TODO when rustc requirement >= 1.43.0, use MAX instead of max_value()
|
||||
comment! {
|
||||
"The largest value that can be represented.
|
||||
|
||||
|
@ -44,12 +41,11 @@ assert_eq!(Fix::MIN, Fix::from_bits(", $s_inner, "::MIN));
|
|||
|
||||
```rust
|
||||
use fixed::{types::extra::U4, ", $s_fixed, "};
|
||||
# use std::", $s_inner, "; // TODO when rustc requirement >= 1.43.0, remove this
|
||||
type Fix = ", $s_fixed, "<U4>;
|
||||
assert_eq!(Fix::MAX, Fix::from_bits(", $s_inner, "::MAX));
|
||||
```
|
||||
";
|
||||
pub const MAX: $Fixed<Frac> = Self::from_bits(<$Inner>::max_value());
|
||||
pub const MAX: $Fixed<Frac> = Self::from_bits(<$Inner>::MAX);
|
||||
}
|
||||
|
||||
comment! {
|
||||
|
@ -411,7 +407,7 @@ assert!(half.is_power_of_two());
|
|||
";
|
||||
#[inline]
|
||||
pub const fn is_power_of_two(self) -> bool {
|
||||
self.count_ones() == 1
|
||||
self.to_bits().is_power_of_two()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1218,7 +1218,6 @@ where
|
|||
/// ```rust
|
||||
/// use fixed::traits::FromFixed;
|
||||
/// use fixed::types::U8F8;
|
||||
/// # use std::i8; // TODO when rustc requirement >= 1.43.0, remove this
|
||||
/// // 0x87.65
|
||||
/// let f = U8F8::from_bits(0x8765);
|
||||
/// assert_eq!(f32::from_fixed(f), f32::from(0x8765u16) / 256.0);
|
||||
|
|
Loading…
Reference in New Issue