Program Rust
right_shift
This Rust is the input prepared for Aeneas. Modeling choices explains the adaptations from the production source.
173 }174 175 /// Computes decimal * 2^-shift.176 pub(super) fn right_shift(&mut self, shift: usize) {177 let mut read_index = 0;178 let mut write_index = 0;179 let mut n = 0_u64;180 let mut all_zero = false;181 while (n >> shift) == 0 && !all_zero {182 if read_index < self.num_digits {183 n = (10 * n) + self.digits[read_index] as u64;184 read_index += 1;185 } else if n == 0 {186 // The source returns here from inside the loop. Recording the187 // condition and returning immediately after the loop executes188 // no intervening operation and preserves the unchanged value.189 all_zero = true;190 } else {191 while (n >> shift) == 0 {192 n *= 10;193 read_index += 1;194 }195 break;196 }197 }198 if all_zero {199 return;200 }201 self.decimal_point -= read_index as i32 - 1;202 if self.decimal_point < -Self::DECIMAL_POINT_RANGE {203 // `self = Self::Default()`, but without the overhead of clearing `digits`.204 self.num_digits = 0;205 self.decimal_point = 0;206 self.truncated = false;207 return;208 }209 let mask = (1_u64 << shift) - 1;210 while read_index < self.num_digits {211 let new_digit = (n >> shift) as u8;212 n = (10 * (n & mask)) + self.digits[read_index] as u64;213 read_index += 1;214 self.digits[write_index] = new_digit;215 write_index += 1;216 }217 while n > 0 {218 let new_digit = (n >> shift) as u8;219 n = 10 * (n & mask);220 if write_index < Self::MAX_DIGITS {221 self.digits[write_index] = new_digit;222 write_index += 1;223 } else if new_digit > 0 {224 self.truncated = true;225 }226 }227 self.num_digits = write_index;228 self.trim();229 }230 }231 232 // This is exactly the source reverse scan over the already-consumed prefix.Aeneas declaration
The Lean below was mechanically extracted by Aeneas from Rust.
def num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift
(self : num.imp.dec2flt.decimal_seq.DecimalSeq) (shift : Std.Usize) :
Result num.imp.dec2flt.decimal_seq.DecimalSeq
:= do
let (self1, read_index, n, all_zero) ←
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0 self shift 0#usize
0#u64 false
if all_zero
then ok self1
else
let i ← lift (UScalar.hcast .I32 read_index)
let i1 ← i - 1#i32
let i2 ← self1.decimal_point - i1
let i3 ← -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
if i2 < i3
then
ok
{
self1
with
num_digits := 0#usize, decimal_point := 0#i32, truncated := false
}
else
let i4 ← 1#u64 <<< shift
let mask ← i4 - 1#u64
let (a, write_index, n1) ←
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
self1.num_digits self1.digits shift read_index 0#usize n mask
let (b, a1, write_index1) ←
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
self1.truncated a shift write_index n1 mask
num.imp.dec2flt.decimal_seq.DecimalSeq.trim
{
num_digits := write_index1,
decimal_point := i2,
truncated := b,
digits := a1
}Operations used here
DECIMAL_POINT_RANGE · Program Rust
right_shift_loop0 · Program Rust
Source details
| Declaration | num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift |
|---|---|
| File | num/imp/../../dec2flt/decimal_seq.rs |
| Lines | 176:4–229:5 |
| Program version | Revised program |