Skip to main content

vstd/
wrapping.rs

1//! Contains trusted specifications for wrapping arithmetic behavior on the
2//! primitive integer types.
3//!
4//! Structured as follows:
5//! To get the spec for `wrapping_add` on `u8`, for example, call `u8_specs::wrapping_add`.
6//! (The module formulation seemed cleaner than defining a `wrapping_add_u8` for every
7//! operation and type.)
8use super::prelude::*;
9
10macro_rules! wrapping_specs {
11    ([$(($uN: ty, $iN: ty, $modname_u:ident, $modname_i:ident, $range:expr, $bits:expr),)*]) => {
12        $(
13            verus! {
14
15            pub mod $modname_u {
16                use super::*;
17
18                pub open spec fn wrapping_add(x: $uN, y: $uN) -> $uN {
19                    if x + y > <$uN>::MAX {
20                        (x + y - $range) as $uN
21                    } else {
22                        (x + y) as $uN
23                    }
24                }
25
26                pub open spec fn wrapping_add_signed(x: $uN, y: $iN) -> $uN {
27                    if x + y > <$uN>::MAX {
28                        (x + y - $range) as $uN
29                    } else if x + y < 0 {
30                        (x + y + $range) as $uN
31                    } else {
32                        (x + y) as $uN
33                    }
34                }
35
36                pub open spec fn wrapping_sub(x: $uN, y: $uN) -> $uN {
37                    if x - y < 0 {
38                        (x - y + $range) as $uN
39                    } else {
40                        (x - y) as $uN
41                    }
42                }
43
44                pub open spec fn wrapping_mul(x: $uN, y: $uN) -> $uN {
45                    ((x as nat * y as nat) % $range as nat) as $uN
46                }
47
48                pub open spec fn wrapping_shl(x: $uN, shift: u32) -> $uN {
49                    x << (shift % $bits)
50                }
51                pub open spec fn wrapping_shr(x: $uN, shift: u32) -> $uN {
52                    x >> (shift % $bits)
53                }
54
55                /// The bits of `x` shifted left by `n % BITS`, with the bits shifted out
56                /// of the top moved into the bottom. A rotation by zero is `x`; it is a
57                /// separate case so that no shift is by the full width.
58                pub open spec fn rotate_left(x: $uN, n: u32) -> $uN {
59                    let r = n % $bits;
60                    if r == 0 {
61                        x
62                    } else {
63                        (x << r) | (x >> (($bits - r) as u32))
64                    }
65                }
66
67                /// The bits of `x` shifted right by `n % BITS`, with the bits shifted out
68                /// of the bottom moved into the top.
69                pub open spec fn rotate_right(x: $uN, n: u32) -> $uN {
70                    let r = n % $bits;
71                    if r == 0 {
72                        x
73                    } else {
74                        (x >> r) | (x << (($bits - r) as u32))
75                    }
76                }
77
78            }
79            pub mod $modname_i {
80                use super::*;
81
82                pub open spec fn wrapping_add(x: $iN, y: $iN) -> $iN {
83                    if x + y > <$iN>::MAX {
84                        (x + y - $range) as $iN
85                    } else if x + y < <$iN>::MIN {
86                        (x + y + $range) as $iN
87                    } else {
88                        (x + y) as $iN
89                    }
90                }
91
92                pub open spec fn wrapping_add_unsigned(x: $iN, y: $uN) -> $iN {
93                    if x + y > <$iN>::MAX {
94                        (x + y - $range) as $iN
95                    } else {
96                        (x + y) as $iN
97                    }
98                }
99
100                pub open spec fn wrapping_sub(x: $iN, y: $iN) -> $iN {
101                    if x - y > <$iN>::MAX {
102                        (x - y - $range) as $iN
103                    } else if x - y < <$iN>::MIN {
104                        (x - y + $range) as $iN
105                    } else {
106                        (x - y) as $iN
107                    }
108                }
109
110                pub open spec fn signed_crop(x: int) -> $iN {
111                    if (x % ($range as int)) > (<$iN>::MAX as int) {
112                        ((x % ($range as int)) - $range) as $iN
113                    } else {
114                        (x % ($range as int)) as $iN
115                    }
116                }
117
118                pub open spec fn wrapping_mul(x: $iN, y: $iN) -> $iN {
119                    signed_crop(x * y)
120                }
121
122                pub open spec fn wrapping_shl(x: $iN, shift: u32) -> $iN {
123                    x << (shift % $bits)
124                }
125                pub open spec fn wrapping_shr(x: $iN, shift: u32) -> $iN {
126                    x >> (shift % $bits)
127                }
128
129                /// Rotates the two's-complement bits of `x`, as the unsigned
130                /// `rotate_left` does. A rotation by a multiple of `BITS` is `x`
131                /// directly, so that case needs no reasoning about the casts.
132                pub open spec fn rotate_left(x: $iN, n: u32) -> $iN {
133                    if n % $bits == 0 {
134                        x
135                    } else {
136                        $modname_u::rotate_left(x as $uN, n) as $iN
137                    }
138                }
139
140                /// Rotates the two's-complement bits of `x`, as the unsigned
141                /// `rotate_right` does.
142                pub open spec fn rotate_right(x: $iN, n: u32) -> $iN {
143                    if n % $bits == 0 {
144                        x
145                    } else {
146                        $modname_u::rotate_right(x as $uN, n) as $iN
147                    }
148                }
149
150            }
151        }
152            )*
153    }
154}
155wrapping_specs!([
156    (u8, i8, u8_specs, i8_specs, 0x100, 8u32),
157    (u16, i16, u16_specs, i16_specs, 0x1_0000, 16u32),
158    (u32, i32, u32_specs, i32_specs, 0x1_0000_0000, 32u32),
159    (u64, i64, u64_specs, i64_specs, 0x1_0000_0000_0000_0000, 64u32),
160    (u128, i128, u128_specs, i128_specs, 0x1_0000_0000_0000_0000_0000_0000_0000_0000, 128u32),
161    (usize, isize, usize_specs, isize_specs, (usize::MAX - usize::MIN + 1), usize::BITS),
162]);