Program Rust
left_shift
This Rust is the input prepared for Aeneas. Modeling choices explains the adaptations from the production source.
126 }127 128 /// Computes decimal * 2^shift.129 pub(super) fn left_shift(&mut self, shift: usize) {130 if self.num_digits == 0 {131 return;132 }133 let num_new_digits = number_of_digits_decimal_left_shift(self, shift);134 let mut read_index = self.num_digits;135 let mut write_index = self.num_digits + num_new_digits;136 let mut n = 0_u64;137 138 while read_index != 0 {139 read_index -= 1;140 write_index -= 1;141 n += (self.digits[read_index] as u64) << shift;142 let quotient = n / 10;143 let remainder = n - (10 * quotient);144 if write_index < Self::MAX_DIGITS {145 self.digits[write_index] = remainder as u8;146 } else if remainder > 0 {147 self.truncated = true;148 }149 n = quotient;150 }151 152 while n > 0 {153 write_index -= 1;154 let quotient = n / 10;155 let remainder = n - (10 * quotient);156 if write_index < Self::MAX_DIGITS {157 self.digits[write_index] = remainder as u8;158 } else if remainder > 0 {159 self.truncated = true;160 }161 n = quotient;162 }163 164 self.num_digits += num_new_digits;165 166 if self.num_digits > Self::MAX_DIGITS {167 self.num_digits = Self::MAX_DIGITS;168 }169 170 self.decimal_point += num_new_digits as i32;171 self.trim();172 }173 174 /// Computes decimal * 2^-shift.175 pub(super) fn right_shift(&mut self, shift: usize) {Aeneas declaration
The Lean below was mechanically extracted by Aeneas from Rust.
def num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift
(self : num.imp.dec2flt.decimal_seq.DecimalSeq) (shift : Std.Usize) :
Result num.imp.dec2flt.decimal_seq.DecimalSeq
:= do
if self.num_digits = 0#usize
then ok self
else
let num_new_digits ←
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift self
shift
let write_index ← self.num_digits + num_new_digits
let (b, a, write_index1, n) ←
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0 self.truncated
self.digits shift self.num_digits write_index 0#u64
let (b1, a1) ←
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1 b a write_index1
n
let i ← self.num_digits + num_new_digits
let i1 ←
if i > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
then ok num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
else ok i
let i2 ← lift (UScalar.hcast .I32 num_new_digits)
let i3 ← self.decimal_point + i2
num.imp.dec2flt.decimal_seq.DecimalSeq.trim
{ num_digits := i1, decimal_point := i3, truncated := b1, digits := a1 }Operations used here
number_of_digits_decimal_left_shift · Program Rust
Source details
| Declaration | num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift |
|---|---|
| File | num/imp/../../dec2flt/decimal_seq.rs |
| Lines | 129:4–172:5 |
| Program version | Preparation 2 |