Source context

Program Rust

right_shift

This Rust is the input prepared for Aeneas. Modeling choices explains the adaptations from the production source.

172     }173 174     /// Computes decimal * 2^-shift.175     pub(super) fn right_shift(&mut self, shift: usize) {176         let mut read_index = 0;177         let mut write_index = 0;178         let mut n = 0_u64;179         let mut all_zero = false;180         while (n >> shift) == 0 && !all_zero {181             if read_index < self.num_digits {182                 n = (10 * n) + self.digits[read_index] as u64;183                 read_index += 1;184             } else if n == 0 {185                 // The source returns here from inside the loop.  Recording the186                 // condition and returning immediately after the loop executes187                 // no intervening operation and preserves the unchanged value.188                 all_zero = true;189             } else {190                 while (n >> shift) == 0 {191                     n *= 10;192                     read_index += 1;193                 }194                 break;195             }196         }197         if all_zero {198             return;199         }200         self.decimal_point -= read_index as i32 - 1;201         if self.decimal_point < -Self::DECIMAL_POINT_RANGE {202             // `self = Self::Default()`, but without the overhead of clearing `digits`.203             self.num_digits = 0;204             self.decimal_point = 0;205             self.truncated = false;206             return;207         }208         let mask = (1_u64 << shift) - 1;209         while read_index < self.num_digits {210             let new_digit = (n >> shift) as u8;211             n = (10 * (n & mask)) + self.digits[read_index] as u64;212             read_index += 1;213             self.digits[write_index] = new_digit;214             write_index += 1;215         }216         while n > 0 {217             let new_digit = (n >> shift) as u8;218             n = 10 * (n & mask);219             if write_index < Self::MAX_DIGITS {220                 self.digits[write_index] = new_digit;221                 write_index += 1;222             } else if new_digit > 0 {223                 self.truncated = true;224             }225         }226         self.num_digits = write_index;227         self.trim();228     }229 }230 231 // 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

DecimalSeq · Program Rust

DECIMAL_POINT_RANGE · Program Rust

trim · Program Rust

right_shift_loop0 · Program Rust

right_shift_loop1 · Program Rust

right_shift_loop2 · Program Rust

Source details
Declarationnum.imp.dec2flt.decimal_seq.DecimalSeq.right_shift
Filenum/imp/../../dec2flt/decimal_seq.rs
Lines175:4–228:5
Program versionOriginal program