forked from bluealloy/revm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi256.rs
More file actions
177 lines (156 loc) · 5.01 KB
/
i256.rs
File metadata and controls
177 lines (156 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::primitives::U256;
use core::cmp::Ordering;
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(
any(test, feature = "arbitrary"),
derive(arbitrary::Arbitrary, proptest_derive::Arbitrary)
)]
pub(super) enum Sign {
Plus,
Minus,
Zero,
}
pub(super) const _SIGN_BIT_MASK: U256 = U256::from_limbs([
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF,
0x7FFFFFFFFFFFFFFF,
]);
pub(super) const MIN_NEGATIVE_VALUE: U256 = U256::from_limbs([
0x0000000000000000,
0x0000000000000000,
0x0000000000000000,
0x8000000000000000,
]);
const FLIPH_BITMASK_U64: u64 = 0x7FFFFFFFFFFFFFFF;
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(
any(test, feature = "arbitrary"),
derive(arbitrary::Arbitrary, proptest_derive::Arbitrary)
)]
pub(super) struct I256(pub(super) Sign, pub(super) U256);
#[inline(always)]
pub(super) fn i256_sign<const DO_TWO_COMPL: bool>(val: &mut U256) -> Sign {
if !val.bit(U256::BITS - 1) {
if *val == U256::ZERO {
Sign::Zero
} else {
Sign::Plus
}
} else {
if DO_TWO_COMPL {
two_compl_mut(val);
}
Sign::Minus
}
}
#[inline(always)]
fn u256_remove_sign(val: &mut U256) {
unsafe {
val.as_limbs_mut()[3] &= FLIPH_BITMASK_U64;
}
}
#[inline(always)]
pub(super) fn two_compl_mut(op: &mut U256) {
*op = two_compl(*op);
}
pub(super) fn two_compl(op: U256) -> U256 {
op.wrapping_neg()
}
#[inline(always)]
pub(super) fn i256_cmp(mut first: U256, mut second: U256) -> Ordering {
let first_sign = i256_sign::<false>(&mut first);
let second_sign = i256_sign::<false>(&mut second);
match (first_sign, second_sign) {
(Sign::Zero, Sign::Zero) => Ordering::Equal,
(Sign::Zero, Sign::Plus) => Ordering::Less,
(Sign::Zero, Sign::Minus) => Ordering::Greater,
(Sign::Minus, Sign::Zero) => Ordering::Less,
(Sign::Minus, Sign::Plus) => Ordering::Less,
(Sign::Minus, Sign::Minus) => first.cmp(&second),
(Sign::Plus, Sign::Minus) => Ordering::Greater,
(Sign::Plus, Sign::Zero) => Ordering::Greater,
(Sign::Plus, Sign::Plus) => first.cmp(&second),
}
}
#[inline(always)]
pub(super) fn i256_div(mut first: U256, mut second: U256) -> U256 {
let second_sign = i256_sign::<true>(&mut second);
if second_sign == Sign::Zero {
return U256::ZERO;
}
let first_sign = i256_sign::<true>(&mut first);
if first_sign == Sign::Minus && first == MIN_NEGATIVE_VALUE && second == U256::from(1) {
return two_compl(MIN_NEGATIVE_VALUE);
}
//let mut d = first / second;
let mut d = first.div_rem(second).0;
u256_remove_sign(&mut d);
//set sign bit to zero
if d == U256::ZERO {
return U256::ZERO;
}
match (first_sign, second_sign) {
(Sign::Zero, Sign::Plus)
| (Sign::Plus, Sign::Zero)
| (Sign::Zero, Sign::Zero)
| (Sign::Plus, Sign::Plus)
| (Sign::Minus, Sign::Minus) => d,
(Sign::Zero, Sign::Minus)
| (Sign::Plus, Sign::Minus)
| (Sign::Minus, Sign::Zero)
| (Sign::Minus, Sign::Plus) => two_compl(d),
}
}
#[inline(always)]
pub(super) fn i256_mod(mut first: U256, mut second: U256) -> U256 {
let first_sign = i256_sign::<true>(&mut first);
if first_sign == Sign::Zero {
return U256::ZERO;
}
let _ = i256_sign::<true>(&mut second);
let mut r = first % second;
u256_remove_sign(&mut r);
if r == U256::ZERO {
return U256::ZERO;
}
if first_sign == Sign::Minus {
two_compl(r)
} else {
r
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::primitives::U256;
use core::num::Wrapping;
#[test]
fn div_i256() {
// Sanity checks based on i8. Notice that we need to use `Wrapping` here because
// Rust will prevent the overflow by default whereas the EVM does not.
assert_eq!(Wrapping(i8::MIN) / Wrapping(-1), Wrapping(i8::MIN));
assert_eq!(i8::MAX / -1, -i8::MAX);
// Now the same calculations based on i256
let one = U256::from(1);
let one_hundred = U256::from(100);
let fifty = U256::from(50);
let _fifty_sign = Sign::Plus;
let two = U256::from(2);
let neg_one_hundred = U256::from(100);
let _neg_one_hundred_sign = Sign::Minus;
let minus_one = U256::from(1);
let max_value = U256::from(2).pow(U256::from(255)) - U256::from(1);
let neg_max_value = U256::from(2).pow(U256::from(255)) - U256::from(1);
assert_eq!(i256_div(MIN_NEGATIVE_VALUE, minus_one), MIN_NEGATIVE_VALUE);
assert_eq!(i256_div(MIN_NEGATIVE_VALUE, one), MIN_NEGATIVE_VALUE);
assert_eq!(i256_div(max_value, one), max_value);
assert_eq!(i256_div(max_value, minus_one), neg_max_value);
assert_eq!(i256_div(one_hundred, minus_one), neg_one_hundred);
assert_eq!(i256_div(one_hundred, two), fifty);
}
#[test]
fn arbitrary() {
proptest::proptest!(|(_value: I256)| { })
}
}