~/bend-docscommunity

math_checked_word.bend checks

raw source on the hub · import stelliferous@0.0.3.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_small source · line 37 · raw

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

A left operand at most 2^31 divided by a right operand of at most 2^30 gives a product of at most 2^31, which the native product holds. Other products take the exact bit traversal.

def multiply_divided source · line 42 · raw

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

def multiply_right source · line 49 · raw

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

Unit products already fit a word; shapes and contiguous strides frequently use them.

def multiply_left source · line 54 · raw

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

def multiply source · line 59 · raw

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