~/bend-docscommunity

math_checked_word.bend checks

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

Exact overflow-checked multiplication for metadata. Zero factors return zero. Other bounded products use 32 Horner steps, each below the native Nat cap.

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

Definitions

def bounded_result source · line 6 · raw

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

def bounded source · line 11 · raw

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

def multiply_step source · line 14 · raw

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

def multiply_bits source · line 19 · raw

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

def encoded source · line 24 · raw

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

def multiply_bounded_when source · line 29 · raw

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

def multiply_bounded source · line 37 · raw

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

def multiply_small source · line 43 · 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 48 · raw

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

def multiply_right source · line 55 · 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 60 · raw

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

def multiply source · line 65 · raw

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