Program Rust
left_shift
This Rust is the input prepared for Aeneas. Modeling choices explains the adaptations from the production source.
127 }128 129 /// Computes decimal * 2^shift.130 pub(super) fn left_shift(&mut self, shift: usize) {131 if self.num_digits == 0 {132 return;133 }134 let num_new_digits = number_of_digits_decimal_left_shift(self, shift);135 let mut read_index = self.num_digits;136 let mut write_index = self.num_digits + num_new_digits;137 let mut n = 0_u64;138 139 while read_index != 0 {140 read_index -= 1;141 write_index -= 1;142 n += (self.digits[read_index] as u64) << shift;143 let quotient = n / 10;144 let remainder = n - (10 * quotient);145 if write_index < Self::MAX_DIGITS {146 self.digits[write_index] = remainder as u8;147 } else if remainder > 0 {148 self.truncated = true;149 }150 n = quotient;151 }152 153 while n > 0 {154 write_index -= 1;155 let quotient = n / 10;156 let remainder = n - (10 * quotient);157 if write_index < Self::MAX_DIGITS {158 self.digits[write_index] = remainder as u8;159 } else if remainder > 0 {160 self.truncated = true;161 }162 n = quotient;163 }164 165 self.num_digits += num_new_digits;166 167 if self.num_digits > Self::MAX_DIGITS {168 self.num_digits = Self::MAX_DIGITS;169 }170 171 self.decimal_point += num_new_digits as i32;172 self.trim();173 }174 175 /// Computes decimal * 2^-shift.176 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 | 130:4–173:5 |
| Program version | Revised program |