Index Mathematics Conjectures nbv lettuce Widgets

lte_bits

stdlib/bits/lte_bits.nbv

lte_bits(lhs, rhs)

return 1 if lhs is less than or equal to rhs lhs and rhs should both be vectors of bits

Calls
bits/and_bits, bits/xor_bits
Precompiles
assert_eq!, len!, loop!

Source

# return 1 if lhs is less than or equal to rhs
# lhs and rhs should both be vectors of bits
(lhs, rhs)

assert_eq! (len! lhs), (len! rhs)

let mut is_lte = 1
let mut found_diff = 0

loop! (len! lhs), i {
  # iterate in reverse because the inputs are little endian
  static i = (len! lhs) - i - 1
  let is_diff = xor_bits(lhs[i], rhs[i])
  let is_first_diff = and_bits(1 - found_diff, is_diff)
  let bit_gt = and_bits(lhs[i], 1 - rhs[i])

  is_lte = is_lte - is_first_diff * bit_gt
  found_diff = found_diff + is_first_diff
}

is_lte