~/bend-docscommunity

math_checked_word.bend checks

raw source on the hub · import stelliferous@0.0.2.0/math_checked_word.bend as Math_checked_word

Exact overflow-checked multiplication for metadata. Each Horner step starts below word_max and forms at most 3*word_max, safely below the native Nat cap. The loop has exactly 32 bit steps and never touches tensor payloads.

2 imports
import Base
import ./machine_limits.bend as Machine

Definitions

def bounded_result source · line 7 · raw

@valid:Bool -> @value:Nat -> Maybe<&2, Nat>

def bounded source · line 12 · raw

@+value:Nat -> @limit:Nat -> Maybe<&2, Nat>

def multiply_step source · line 15 · raw

@bit:Bool -> @right:Nat -> @previous:Maybe<&2, Nat> -> @limit:Nat -> Maybe<&2, Nat>

def multiply_bits source · line 20 · raw

@width:Nat -> @bits:Word(width) -> @+right:Nat -> @+limit:Nat -> Maybe<&2, Nat>

def encoded source · line 25 · raw

@result:Maybe<&2, Nat> -> Maybe<&2, U32>

def multiply_bounded source · line 30 · raw

@left:U32 -> @right:U32 -> @limit:U32 -> Maybe<&2, U32>

def multiply_right source · line 36 · raw

@unit:Bool -> @left:U32 -> @right:U32 -> Maybe<&2, U32>

Unit products already fit a word. Shapes and contiguous strides frequently use them; returning the operand avoids the 32-step natural Horner traversal.

def multiply_left source · line 41 · raw

@unit:Bool -> @+left:U32 -> @+right:U32 -> Maybe<&2, U32>

def multiply source · line 46 · raw

@+left:U32 -> @+right:U32 -> Maybe<&2, U32>