Skip to main content

vstd_extra/external/
int_specs.rs

1//! Specs for stdlib methods on integer types that aren't covered by vstd.
2use vstd::arithmetic::power2::is_pow2;
3use vstd::bytes::{spec_u16_to_le_bytes, spec_u32_to_le_bytes};
4use vstd::prelude::*;
5
6verus! {
7
8/// Little-endian bytes as implemented by core's uint_macros::to_le_bytes
9/// (`to_le().to_ne_bytes()`), independent of the host endianness. Source:
10/// Rust 1.100.0 (0ef1bc386), library/core/src/num/uint_macros.rs; the delegated
11/// to_ne_bytes uses a transmute to the same-width byte array.
12pub assume_specification[ u16::to_le_bytes ](value: u16) -> (bytes: [u8; 2])
13    ensures
14        bytes@ == spec_u16_to_le_bytes(value),
15    opens_invariants none
16    no_unwind
17;
18
19pub assume_specification[ u32::to_le_bytes ](value: u32) -> (bytes: [u8; 4])
20    ensures
21        bytes@ == spec_u32_to_le_bytes(value),
22    opens_invariants none
23    no_unwind
24;
25
26/// Spec for `<int_type>::is_power_of_two`. Returns true iff `self` is a positive
27/// power of two, i.e., there exists `e: nat` with `pow2(e) == self`.
28pub assume_specification[ u8::is_power_of_two ](self_: u8) -> (r: bool)
29    returns
30        is_pow2(self_ as int),
31    opens_invariants none
32    no_unwind
33;
34
35pub assume_specification[ u16::is_power_of_two ](self_: u16) -> (r: bool)
36    returns
37        is_pow2(self_ as int),
38    opens_invariants none
39    no_unwind
40;
41
42pub assume_specification[ u32::is_power_of_two ](self_: u32) -> (r: bool)
43    returns
44        is_pow2(self_ as int),
45    opens_invariants none
46    no_unwind
47;
48
49/// On a little-endian target, converting a native-endian `u32` to little endian is the identity.
50#[cfg(target_endian = "little")]
51pub assume_specification[ u32::to_le ](value: u32) -> u32
52    returns
53        value,
54    opens_invariants none
55    no_unwind
56;
57
58pub assume_specification[ u64::is_power_of_two ](self_: u64) -> (r: bool)
59    returns
60        is_pow2(self_ as int),
61    opens_invariants none
62    no_unwind
63;
64
65pub assume_specification[ usize::is_power_of_two ](self_: usize) -> (r: bool)
66    returns
67        is_pow2(self_ as int),
68    opens_invariants none
69    no_unwind
70;
71
72/// `usize::div_ceil` panics if `rhs` is zero; the precondition excludes that case.
73pub assume_specification[ usize::div_ceil ](self_: usize, rhs: usize) -> usize
74    requires
75        rhs > 0,
76    returns
77        ((self_ + rhs - 1) / (rhs as int)) as usize,
78    opens_invariants none
79    no_unwind when rhs > 0
80;
81
82} // verus!