Skip to content

Commit 9c8df1a

Browse files
authored
Merge pull request #638 from folkertdev/f16-support
support `f16`
2 parents 85c5ca0 + ca9d8e1 commit 9c8df1a

5 files changed

Lines changed: 45 additions & 1 deletion

File tree

proptest/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### New Features
88

99
- Added a memory-efficient strategy for sampling subsets from ranges without replacement. ([\#586](https://github.com/proptest-rs/proptest/pull/586))
10+
- Added support for f16 feature-gated by "f16" or "unstable". ([\#638](https://github.com/proptest-rs/proptest/pull/638))
1011

1112
### Other Notes
1213

proptest/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ default-code-coverage = ["std", "fork", "timeout", "bit-set"]
2626
attr-macro = ["proptest-macro"]
2727

2828
# Enables unstable features of Rust.
29-
unstable = []
29+
unstable = ["f16"]
30+
31+
# Enables f16 support
32+
f16 = []
3033

3134
# Enables the use of standard-library dependent features
3235
std = ["rand/std", "rand/os_rng", "regex-syntax", "num-traits/std"]

proptest/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
feature = "unstable",
2727
feature(allocator_api, try_trait_v2, coroutine_trait, never_type)
2828
)]
29+
#![cfg_attr(feature = "f16", feature(f16))]
2930
#![cfg_attr(all(feature = "std", feature = "unstable"), feature(ip))]
3031
#![cfg_attr(docsrs, feature(doc_cfg))]
3132

proptest/src/num.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,16 @@ where
496496
const MANTISSA_MASK: Self::Bits;
497497
}
498498

499+
#[cfg(feature = "f16")]
500+
impl FloatLayout for f16 {
501+
type Bits = u16;
502+
503+
const SIGN_MASK: u16 = 0x8000;
504+
const EXP_MASK: u16 = 0x7c00;
505+
const EXP_ZERO: u16 = f16::to_bits(1.0);
506+
const MANTISSA_MASK: u16 = !(Self::SIGN_MASK | Self::EXP_MASK);
507+
}
508+
499509
impl FloatLayout for f32 {
500510
type Bits = u32;
501511

@@ -950,6 +960,8 @@ macro_rules! float_bin_search {
950960
};
951961
}
952962

963+
#[cfg(feature = "f16")]
964+
float_bin_search!(f16, F16U);
953965
float_bin_search!(f32, F32U);
954966
float_bin_search!(f64, F64U);
955967

@@ -1180,6 +1192,8 @@ mod test {
11801192
contract_sanity!(i64);
11811193
contract_sanity!(usize);
11821194
contract_sanity!(isize);
1195+
#[cfg(feature = "f16")]
1196+
contract_sanity!(f16);
11831197
contract_sanity!(f32);
11841198
contract_sanity!(f64);
11851199
}
@@ -1436,6 +1450,25 @@ mod test {
14361450
proptest! {
14371451
#![proptest_config(crate::test_runner::Config::with_cases(1024))]
14381452

1453+
#[cfg(feature = "f16")]
1454+
#[test]
1455+
fn f16_any_generates_desired_values(
1456+
strategy in crate::bits::u32::ANY.prop_map(f16::Any::from_bits)
1457+
) {
1458+
float_generation_test_body!(strategy, f16);
1459+
}
1460+
1461+
#[cfg(feature = "f16")]
1462+
#[test]
1463+
fn f16_any_sanity(
1464+
strategy in crate::bits::u32::ANY.prop_map(f16::Any::from_bits)
1465+
) {
1466+
check_strategy_sanity(strategy, Some(CheckStrategySanityOptions {
1467+
strict_complicate_after_simplify: false,
1468+
.. CheckStrategySanityOptions::default()
1469+
}));
1470+
}
1471+
14391472
#[test]
14401473
fn f32_any_generates_desired_values(
14411474
strategy in crate::bits::u32::ANY.prop_map(f32::Any::from_bits)
@@ -1529,6 +1562,8 @@ mod test {
15291562
panic_on_empty!(i64);
15301563
panic_on_empty!(usize);
15311564
panic_on_empty!(isize);
1565+
#[cfg(feature = "f16")]
1566+
panic_on_empty!(f16);
15321567
panic_on_empty!(f32);
15331568
panic_on_empty!(f64);
15341569
}

proptest/src/num/float_samplers.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
//! floating point values at the bounds. At that stage, one is selected at random and
1818
//! returned.
1919
20+
#[cfg(feature = "f16")]
21+
pub(crate) use self::f16::F16U;
2022
pub(crate) use self::f32::F32U;
2123
pub(crate) use self::f64::F64U;
2224

@@ -459,5 +461,7 @@ macro_rules! float_sampler {
459461
};
460462
}
461463

464+
#[cfg(feature = "f16")]
465+
float_sampler!(f16, u16, F16U);
462466
float_sampler!(f32, u32, F32U);
463467
float_sampler!(f64, u64, F64U);

0 commit comments

Comments
 (0)