@@ -1831,6 +1831,21 @@ mod verify {
1831
1831
}
1832
1832
}
1833
1833
1834
+ // Part 3: Float to Integer Conversion function Harness Generation Macro
1835
+ macro_rules! generate_to_int_unchecked_harness {
1836
+ ( $floatType: ty, $( $intType: ty, $harness_name: ident) ,+) => {
1837
+ $(
1838
+ #[ kani:: proof_for_contract( $floatType:: to_int_unchecked) ]
1839
+ pub fn $harness_name( ) {
1840
+ let num1: $floatType = kani:: any:: <$floatType>( ) ;
1841
+ let result = unsafe { num1. to_int_unchecked:: <$intType>( ) } ;
1842
+
1843
+ assert_eq!( result, num1 as $intType) ;
1844
+ }
1845
+ ) +
1846
+ }
1847
+ }
1848
+
1834
1849
// `unchecked_add` proofs
1835
1850
//
1836
1851
// Target types:
@@ -2128,4 +2143,46 @@ mod verify {
2128
2143
generate_wrapping_shift_harness ! ( u128 , wrapping_shr, checked_wrapping_shr_u128) ;
2129
2144
generate_wrapping_shift_harness ! ( usize , wrapping_shr, checked_wrapping_shr_usize) ;
2130
2145
2146
+ // `f{16,32,64,128}::to_int_unchecked` proofs
2147
+ //
2148
+ // Target integer types:
2149
+ // i{8,16,32,64,128,size} and u{8,16,32,64,128,size} -- 12 types in total
2150
+ //
2151
+ // Target contracts:
2152
+ // 1. Float is not `NaN` and infinite
2153
+ // 2. Float is representable in the return type `Int`, after truncating
2154
+ // off its fractional part
2155
+ // [requires(self.is_finite() && kani::float::float_to_int_in_range::<Self, Int>(self))]
2156
+ //
2157
+ // Target function:
2158
+ // pub unsafe fn to_int_unchecked<Int>(self) -> Int where Self: FloatToInt<Int>
2159
+ generate_to_int_unchecked_harness ! ( f32 ,
2160
+ i8 , checked_f32_to_int_unchecked_i8,
2161
+ i16 , checked_f32_to_int_unchecked_i16,
2162
+ i32 , checked_f32_to_int_unchecked_i32,
2163
+ i64 , checked_f32_to_int_unchecked_i64,
2164
+ i128 , checked_f32_to_int_unchecked_i128,
2165
+ isize , checked_f32_to_int_unchecked_isize,
2166
+ u8 , checked_f32_to_int_unchecked_u8,
2167
+ u16 , checked_f32_to_int_unchecked_u16,
2168
+ u32 , checked_f32_to_int_unchecked_u32,
2169
+ u64 , checked_f32_to_int_unchecked_u64,
2170
+ u128 , checked_f32_to_int_unchecked_u128,
2171
+ usize , checked_f32_to_int_unchecked_usize
2172
+ ) ;
2173
+
2174
+ generate_to_int_unchecked_harness ! ( f64 ,
2175
+ i8 , checked_f64_to_int_unchecked_i8,
2176
+ i16 , checked_f64_to_int_unchecked_i16,
2177
+ i32 , checked_f64_to_int_unchecked_i32,
2178
+ i64 , checked_f64_to_int_unchecked_i64,
2179
+ i128 , checked_f64_to_int_unchecked_i128,
2180
+ isize , checked_f64_to_int_unchecked_isize,
2181
+ u8 , checked_f64_to_int_unchecked_u8,
2182
+ u16 , checked_f64_to_int_unchecked_u16,
2183
+ u32 , checked_f64_to_int_unchecked_u32,
2184
+ u64 , checked_f64_to_int_unchecked_u64,
2185
+ u128 , checked_f64_to_int_unchecked_u128,
2186
+ usize , checked_f64_to_int_unchecked_usize
2187
+ ) ;
2131
2188
}
0 commit comments