Skip to content

Commit 30c756d

Browse files
authored
Contracts & Harnesses for [f16, f128] to_int_unchecked (#163)
1 parent c841a12 commit 30c756d

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

library/core/src/num/f128.rs

+8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ use crate::intrinsics;
1717
use crate::mem;
1818
use crate::num::FpCategory;
1919
use crate::panic::const_assert;
20+
use safety::requires;
2021

22+
#[cfg(kani)]
23+
use crate::kani;
24+
25+
#[allow(unused_imports)]
26+
use crate::ub_checks::float_to_int_in_range;
2127
/// Basic mathematical constants.
2228
#[unstable(feature = "f128", issue = "116909")]
2329
pub mod consts {
@@ -869,6 +875,8 @@ impl f128 {
869875
#[inline]
870876
#[unstable(feature = "f128", issue = "116909")]
871877
#[must_use = "this returns the result of the operation, without modifying the original"]
878+
// is_finite() checks if the given float is neither infinite nor NaN.
879+
#[requires(self.is_finite() && float_to_int_in_range::<Self, Int>(self))]
872880
pub unsafe fn to_int_unchecked<Int>(self) -> Int
873881
where
874882
Self: FloatToInt<Int>,

library/core/src/num/f16.rs

+7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ use crate::intrinsics;
1717
use crate::mem;
1818
use crate::num::FpCategory;
1919
use crate::panic::const_assert;
20+
use safety::requires;
2021

22+
#[cfg(kani)]
23+
use crate::kani;
24+
25+
#[allow(unused_imports)]
26+
use crate::ub_checks::float_to_int_in_range;
2127
/// Basic mathematical constants.
2228
#[unstable(feature = "f16", issue = "116909")]
2329
pub mod consts {
@@ -855,6 +861,7 @@ impl f16 {
855861
#[inline]
856862
#[unstable(feature = "f16", issue = "116909")]
857863
#[must_use = "this returns the result of the operation, without modifying the original"]
864+
#[requires(self.is_finite() && float_to_int_in_range::<Self, Int>(self))]
858865
pub unsafe fn to_int_unchecked<Int>(self) -> Int
859866
where
860867
Self: FloatToInt<Int>,

library/core/src/num/mod.rs

+30
Original file line numberDiff line numberDiff line change
@@ -2185,4 +2185,34 @@ mod verify {
21852185
u128, checked_f64_to_int_unchecked_u128,
21862186
usize, checked_f64_to_int_unchecked_usize
21872187
);
2188+
2189+
generate_to_int_unchecked_harness!(f16,
2190+
i8, checked_f16_to_int_unchecked_i8,
2191+
i16, checked_f16_to_int_unchecked_i16,
2192+
i32, checked_f16_to_int_unchecked_i32,
2193+
i64, checked_f16_to_int_unchecked_i64,
2194+
i128, checked_f16_to_int_unchecked_i128,
2195+
isize, checked_f16_to_int_unchecked_isize,
2196+
u8, checked_f16_to_int_unchecked_u8,
2197+
u16, checked_f16_to_int_unchecked_u16,
2198+
u32, checked_f16_to_int_unchecked_u32,
2199+
u64, checked_f16_to_int_unchecked_u64,
2200+
u128, checked_f16_to_int_unchecked_u128,
2201+
usize, checked_f16_to_int_unchecked_usize
2202+
);
2203+
2204+
generate_to_int_unchecked_harness!(f128,
2205+
i8, checked_f128_to_int_unchecked_i8,
2206+
i16, checked_f128_to_int_unchecked_i16,
2207+
i32, checked_f128_to_int_unchecked_i32,
2208+
i64, checked_f128_to_int_unchecked_i64,
2209+
i128, checked_f128_to_int_unchecked_i128,
2210+
isize, checked_f128_to_int_unchecked_isize,
2211+
u8, checked_f128_to_int_unchecked_u8,
2212+
u16, checked_f128_to_int_unchecked_u16,
2213+
u32, checked_f128_to_int_unchecked_u32,
2214+
u64, checked_f128_to_int_unchecked_u64,
2215+
u128, checked_f128_to_int_unchecked_u128,
2216+
usize, checked_f128_to_int_unchecked_usize
2217+
);
21882218
}

0 commit comments

Comments
 (0)