~/bend-docscommunity

f32array.bend relies on unsafe/foreign

raw source on the hub · import bend-kit-f32array@0.1.0.0/f32array.bend as F32array

Packed F32 arrays: get, set, slice, concat, map, fold, zip, dot, sum, max, and little- or big-endian Bytes and files. Source: https://github.com/paymog/bend-kit/tree/main/f32array

3 imports
import Base
import bend-kit-bytes@0.3.3.0/bytes.bend as Bytes
import bend-kit-files@0.4.0.0/files.bend as Files

Types

type Cell source · line 7 · raw

Data

Eight elements per Array slot, so one access reads or writes eight.

type F32Array source · line 11 · raw

Type

Element i is lane i % 8 of cell i / 8. buf has the fewest 2^d cells that hold len elements, and every lane at or past len is +0.0.

type Read source · line 510 · raw

Type

A file read in progress: more bytes to read, or the final result.

Definitions

def of_bits source · line 14 · raw

@x:U32 -> F32

def to_bits source · line 19 · raw

@x:F32 -> U32

def of_bits.be source · line 24 · raw

@x:U32 -> F32

def to_bits.be source · line 28 · raw

@x:F32 -> U32

def zero source · line 31 · raw

Cell

def cells source · line 34 · raw

@+n:U32 -> U32

def alloc source · line 37 · raw

@+n:U32 -> Array<Cell>

def lane source · line 41 · raw

@c:Cell -> @+k:U32 -> F32

Lane k, for k < 8.

def put source · line 45 · raw

@c:Cell -> @+k:U32 -> @+v:F32 -> Cell

def mask source · line 50 · raw

@c:Cell -> @+k:U32 -> Cell

Lanes at or past k become +0.0.

def peek.of source · line 55 · raw

@+k:U32 -> @r:Pair(Array<Cell>, Cell) -> Pair(Array<Cell>, F32)

Unchecked element read and write. Callers keep i below len.

def peek source · line 59 · raw

@a:Array<Cell> -> @+i:U32 -> Pair(Array<Cell>, F32)

def poke.of source · line 62 · raw

@+i:U32 -> @+v:F32 -> @r:Pair(Array<Cell>, Cell) -> Array<Cell>

def poke source · line 66 · raw

@a:Array<Cell> -> @+i:U32 -> @+v:F32 -> Array<Cell>

def trim.of source · line 69 · raw

@+j:U32 -> @+k:U32 -> @r:Pair(Array<Cell>, Cell) -> Array<Cell>

def trim.if source · line 73 · raw

@part:Bool -> @a:Array<Cell> -> @+len:U32 -> Array<Cell>

def trim source · line 81 · raw

@a:Array<Cell> -> @+len:U32 -> Array<Cell>

The lanes past len in its last cell become +0.0 again.

def new source · line 85 · raw

@+n:U32 -> F32Array

n zeros.

def length source · line 88 · raw

@a:F32Array -> Pair(F32Array, U32)

def get.some source · line 92 · raw

@+len:U32 -> @r:Pair(Array<Cell>, F32) -> Pair(F32Array, Maybe<&2, F32>)

def get.if source · line 96 · raw

@ok:Bool -> @+len:U32 -> @buf:Array<Cell> -> @+i:U32 -> Pair(F32Array, Maybe<&2, F32>)

def get source · line 104 · raw

@a:F32Array -> @+i:U32 -> Pair(F32Array, Maybe<&2, F32>)

Element i, or None when i >= len.

def set.if source · line 108 · raw

@ok:Bool -> @+len:U32 -> @buf:Array<Cell> -> @+i:U32 -> @+v:F32 -> F32Array

def set source · line 116 · raw

@a:F32Array -> @+i:U32 -> @+v:F32 -> F32Array

Element i becomes v. Nothing changes when i >= len.

def count source · line 120 · raw

@xs:List<&2, F32> -> @+n:U32 -> U32

def from_list.go source · line 127 · raw

@xs:List<&2, F32> -> @a:Array<Cell> -> @+i:U32 -> Array<Cell>

def from_list source · line 134 · raw

@+xs:List<&2, F32> -> F32Array

def to_list.go source · line 139 · raw

@n:Nat -> @r:Pair(Array<Cell>, F32) -> @+i:U32 -> @acc:List<&2, F32> -> List<&2, F32>

r holds element i; the list is built from the last element back.

def to_list source · line 147 · raw

@a:F32Array -> List<&2, F32>

def copy.go source · line 152 · raw

@n:Nat -> @r:Pair(Array<Cell>, F32) -> @dst:Array<Cell> -> @+s:U32 -> @+d:U32 -> Pair(Array<Cell>, Array<Cell>)

n elements from src[s..] to dst[d..]; r holds src[s].

def copy.cells source · line 162 · raw

@n:Nat -> @r:Pair(Array<Cell>, Cell) -> @dst:Array<Cell> -> @+s:U32 -> @+d:U32 -> Pair(Array<Cell>, Array<Cell>)

n whole cells from src[s..] to dst[d..] (cell indexes); r holds src cell s.

def copy.trim source · line 171 · raw

@+end:U32 -> @r:Pair(Array<Cell>, Array<Cell>) -> Pair(Array<Cell>, Array<Cell>)

def copy.pick source · line 175 · raw

@aligned:Bool -> @+m:U32 -> @src:Array<Cell> -> @dst:Array<Cell> -> @+s:U32 -> @+d:U32 -> Pair(Array<Cell>, Array<Cell>)

def copy source · line 183 · raw

@+m:U32 -> @src:Array<Cell> -> @dst:Array<Cell> -> @+s:U32 -> @+d:U32 -> Pair(Array<Cell>, Array<Cell>)

m elements from src[s..] to dst[d..]. When both are cell-aligned, whole cells move, and dst's lanes past d + m in its last cell become +0.0.

def slice.fin source · line 186 · raw

@+len:U32 -> @+m:U32 -> @r:Pair(Array<Cell>, Array<Cell>) -> Pair(F32Array, F32Array)

def slice source · line 191 · raw

@a:F32Array -> @+start:U32 -> @+n:U32 -> Pair(F32Array, F32Array)

The array back, and a copy of up to n elements from start. Both ends are clamped to len.

def concat.total.con source · line 197 · raw

@x:F32Array -> @r:Pair(List<&1, F32Array>, U32) -> Pair(List<&1, F32Array>, U32)

def concat.total source · line 201 · raw

@xs:List<&1, F32Array> -> @+n:U32 -> Pair(List<&1, F32Array>, U32)

def snd source · line 208 · raw

@r:Pair(Array<Cell>, Array<Cell>) -> Array<Cell>

def concat.go source · line 212 · raw

@xs:List<&1, F32Array> -> @out:Array<Cell> -> @+at:U32 -> Array<Cell>

def concat.of source · line 219 · raw

@r:Pair(List<&1, F32Array>, U32) -> F32Array

def concat source · line 224 · raw

@xs:List<&1, F32Array> -> F32Array

The pieces in order, in one new array.

def fold.fin source · line 270 · raw

@-A:Data -> @+len:U32 -> @r:Pair(Array<Cell>, A) -> Pair(F32Array, A)

def zip.fin source · line 296 · raw

@+la:U32 -> @+lb:U32 -> @r:Pair(Array<Cell>, Array<Cell>) -> Pair(F32Array, Pair(F32Array, Bool))

def add2 source · line 313 · raw

@x:F32 -> @y:F32 -> F32

def mul2 source · line 316 · raw

@x:F32 -> @y:F32 -> F32

def add source · line 320 · raw

@a:F32Array -> @b:F32Array -> Pair(F32Array, Pair(F32Array, Bool))

a[i] + b[i], into a, as zip_with.

def mul source · line 324 · raw

@a:F32Array -> @b:F32Array -> Pair(F32Array, Pair(F32Array, Bool))

a[i] * b[i], into a, as zip_with.

def sum.go source · line 328 · raw

@n:Nat -> @r:Pair(Array<Cell>, Cell) -> @+i:U32 -> @s0:F32 -> @s1:F32 -> @s2:F32 -> @s3:F32 -> Pair(Array<Cell>, F32)

Four running sums: lanes 0 and 4 of each cell go to s0, 1 and 5 to s1, and so on. The +0.0 lanes past len add nothing.

def sum.fin source · line 337 · raw

@+len:U32 -> @r:Pair(Array<Cell>, F32) -> Pair(F32Array, F32)

def sum source · line 343 · raw

@a:F32Array -> Pair(F32Array, F32)

The sum. Element i goes to running sum i % 4, each from +0.0, and the four are added as (s0 + s1) + (s2 + s3). ponytail: four running sums, so error still grows with len; a pairwise fork-join sum fixes accuracy and uses every core.

def max.go source · line 348 · raw

@n:Nat -> @+k:U32 -> @r:Pair(Array<Cell>, Cell) -> @+i:U32 -> @m0:F32 -> @m1:F32 -> @m2:F32 -> @m3:F32 -> Pair(Array<Cell>, F32)

Four running maxima, as sum.go keeps four sums, over n whole cells; then the first k lanes of the next cell.

def max.fin source · line 357 · raw

@+len:U32 -> @r:Pair(Array<Cell>, F32) -> Pair(F32Array, Maybe<&2, F32>)

def max.if source · line 361 · raw

@some:Bool -> @+len:U32 -> @buf:Array<Cell> -> @+x:F32 -> Pair(F32Array, Maybe<&2, F32>)

def max.first source · line 368 · raw

@+len:U32 -> @r:Pair(Array<Cell>, F32) -> Pair(F32Array, Maybe<&2, F32>)

def max source · line 373 · raw

@a:F32Array -> Pair(F32Array, Maybe<&2, F32>)

The largest element by Base F32.max, or None when empty. Four running maxima all start at a[0], so a NaN is kept only when it comes first.

def dot.go source · line 378 · raw

@n:Nat -> @r:Pair(Array<Cell>, Cell) -> @s:Pair(Array<Cell>, Cell) -> @+i:U32 -> @s0:F32 -> @s1:F32 -> @s2:F32 -> @s3:F32 -> Pair(Array<Cell>, Pair(Array<Cell>, F32))

As sum.go, over the products of the two cells.

def dot.fin source · line 390 · raw

@+la:U32 -> @+lb:U32 -> @r:Pair(Array<Cell>, Pair(Array<Cell>, F32)) -> Pair(F32Array, Pair(F32Array, Maybe<&2, F32>))

def dot.if source · line 394 · raw

@same:Bool -> @+la:U32 -> @xa:Array<Cell> -> @+lb:U32 -> @ya:Array<Cell> -> Pair(F32Array, Pair(F32Array, Maybe<&2, F32>))

def dot source · line 402 · raw

@a:F32Array -> @b:F32Array -> Pair(F32Array, Pair(F32Array, Maybe<&2, F32>))

Both arrays back, and the sum of a[i] * b[i], added in four running sums as sum adds. None when the lengths differ.

def from_bytes.le source · line 461 · raw

@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Maybe<&1, F32Array>

Four bytes per element, least significant first. None when the length is not a multiple of 4.

def from_bytes.be source · line 466 · raw

@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Maybe<&1, F32Array>

Four bytes per element, most significant first. None when the length is not a multiple of 4.

def to.fin source · line 492 · raw

@+n:U32 -> @r:Pair(Array<Cell>, Array<U32>) -> 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes

def to_bytes.le source · line 502 · raw

@a:F32Array -> 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes

Each element as four bytes, least significant first. len must be below 2^30.

def to_bytes.be source · line 506 · raw

@a:F32Array -> 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes

Each element as four bytes, most significant first. len must be below 2^30.

def read.next source · line 514 · raw

@more:Bool -> @file:File -> @acc:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @+left:U32 -> Read

def read.step source · line 522 · raw

@got:Pair(File, Result<&1, &1, Pair(U32, String), 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>) -> @acc:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @+left:U32 -> Read

A read of no bytes ends the loop early, so a file that shrank stops there.

def read.loop source · line 532 · raw

@n:Nat -> @st:Read -> IO(Pair(File, Result<&1, &1, Pair(U32, String), 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>))

Each pass reads at least one byte or stops, so left + 1 passes are enough.

def read.sized source · line 549 · raw

@sz:Pair(File, Result<&1, &1, Pair(U32, String), U32>) -> IO(Pair(File, Result<&1, &1, Pair(U32, String), 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>))

def read.decode source · line 557 · raw

@m:Maybe<&1, F32Array> -> Result<&1, &1, Pair(U32, String), F32Array>

def read.le source · line 579 · raw

@file:File -> IO(Pair(File, Result<&1, &1, Pair(U32, String), F32Array>))

The rest of an open file, four little-endian bytes per element. Fails EINVAL when its size is not a multiple of 4.

def read.be source · line 583 · raw

@file:File -> IO(Pair(File, Result<&1, &1, Pair(U32, String), F32Array>))

The rest of an open file, four big-endian bytes per element. Fails EINVAL when its size is not a multiple of 4.

Templates

template map.cell source · line 227 · raw

@-f:(@_:F32 -> F32) -> @c:Cell -> Cell

template map.step source · line 231 · raw

@-f:(@_:F32 -> F32) -> @+i:U32 -> @r:Pair(Array<Cell>, Cell) -> Array<Cell>

template map.go source · line 236 · raw

@-f:(@_:F32 -> F32) -> @n:Nat -> @a:Array<Cell> -> @+i:U32 -> Array<Cell>

Each pass reads and writes its own cell; the form that read the next cell ahead ran 4x slower.

template map source · line 244 · raw

@-f:(@_:F32 -> F32) -> @a:F32Array -> F32Array

f on each element, in place.

template fold.cell source · line 248 · raw

@-A:Data -> @-f:(@_:A -> @_:F32 -> A) -> @acc:A -> @c:Cell -> A

template fold.lanes source · line 253 · raw

@-A:Data -> @-f:(@_:A -> @_:F32 -> A) -> @n:Nat -> @+c:Cell -> @+j:U32 -> @acc:A -> A

Lanes j .. j + n - 1 of c.

template fold.go source · line 261 · raw

@-A:Data -> @-f:(@_:A -> @_:F32 -> A) -> @n:Nat -> @+k:U32 -> @r:Pair(Array<Cell>, Cell) -> @+i:U32 -> @acc:A -> Pair(Array<Cell>, A)

n whole cells from cell i, then the first k lanes of the next one; r holds cell i.

template fold source · line 275 · raw

@-A:Data -> @-f:(@_:A -> @_:F32 -> A) -> @a:F32Array -> @init:A -> Pair(F32Array, A)

f(...f(f(init, a[0]), a[1])..., a[len-1]), left to right.

template zip.step source · line 279 · raw

@-f:(@_:F32 -> @_:F32 -> F32) -> @+i:U32 -> @r:Pair(Array<Cell>, Cell) -> @s:Pair(Array<Cell>, Cell) -> Pair(Array<Cell>, Array<Cell>)

template zip.at source · line 284 · raw

@-f:(@_:F32 -> @_:F32 -> F32) -> @+i:U32 -> @ab:Pair(Array<Cell>, Array<Cell>) -> Pair(Array<Cell>, Array<Cell>)

template zip.go source · line 289 · raw

@-f:(@_:F32 -> @_:F32 -> F32) -> @n:Nat -> @ab:Pair(Array<Cell>, Array<Cell>) -> @+i:U32 -> Pair(Array<Cell>, Array<Cell>)

f(a's cell i, b's cell i) goes into a.

template zip.if source · line 300 · raw

@-f:(@_:F32 -> @_:F32 -> F32) -> @same:Bool -> @+la:U32 -> @xa:Array<Cell> -> @+lb:U32 -> @ya:Array<Cell> -> Pair(F32Array, Pair(F32Array, Bool))

template zip_with source · line 308 · raw

@-f:(@_:F32 -> @_:F32 -> F32) -> @a:F32Array -> @b:F32Array -> Pair(F32Array, Pair(F32Array, Bool))

f(a[i], b[i]) for each i, written into a, and b back. When the lengths differ, both come back unchanged beside False.

template from.c7 source · line 408 · raw

@-f:(@_:U32 -> F32) -> @+j:U32 -> @r:Pair(Array<U32>, U32) -> @+x0:U32 -> @+x1:U32 -> @+x2:U32 -> @+x3:U32 -> @+x4:U32 -> @+x5:U32 -> @+x6:U32 -> Pair(Array<U32>, Cell)

Words j .. j + 7 of a Bytes buffer as one cell; r holds word j.

template from.c6 source · line 412 · raw

@-f:(@_:U32 -> F32) -> @+j:U32 -> @r:Pair(Array<U32>, U32) -> @+x0:U32 -> @+x1:U32 -> @+x2:U32 -> @+x3:U32 -> @+x4:U32 -> @+x5:U32 -> Pair(Array<U32>, Cell)

template from.c5 source · line 416 · raw

@-f:(@_:U32 -> F32) -> @+j:U32 -> @r:Pair(Array<U32>, U32) -> @+x0:U32 -> @+x1:U32 -> @+x2:U32 -> @+x3:U32 -> @+x4:U32 -> Pair(Array<U32>, Cell)

template from.c4 source · line 420 · raw

@-f:(@_:U32 -> F32) -> @+j:U32 -> @r:Pair(Array<U32>, U32) -> @+x0:U32 -> @+x1:U32 -> @+x2:U32 -> @+x3:U32 -> Pair(Array<U32>, Cell)

template from.c3 source · line 424 · raw

@-f:(@_:U32 -> F32) -> @+j:U32 -> @r:Pair(Array<U32>, U32) -> @+x0:U32 -> @+x1:U32 -> @+x2:U32 -> Pair(Array<U32>, Cell)

template from.c2 source · line 428 · raw

@-f:(@_:U32 -> F32) -> @+j:U32 -> @r:Pair(Array<U32>, U32) -> @+x0:U32 -> @+x1:U32 -> Pair(Array<U32>, Cell)

template from.c1 source · line 432 · raw

@-f:(@_:U32 -> F32) -> @+j:U32 -> @r:Pair(Array<U32>, U32) -> @+x0:U32 -> Pair(Array<U32>, Cell)

template from.c0 source · line 436 · raw

@-f:(@_:U32 -> F32) -> @+j:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, Cell)

template from.go source · line 441 · raw

@-f:(@_:U32 -> F32) -> @n:Nat -> @r:Pair(Array<U32>, Cell) -> @out:Array<Cell> -> @+i:U32 -> Array<Cell>

r holds cell i; each pass writes cell i and reads cell i + 1.

template from_bytes.if source · line 452 · raw

@-f:(@_:U32 -> F32) -> @aligned:Bool -> @+len:U32 -> @buf:Array<U32> -> Maybe<&1, F32Array>

Reads past the words wrap around, so the last cell is trimmed.

template to.cell source · line 470 · raw

@-g:(@_:F32 -> U32) -> @c:Cell -> @w:Array<U32> -> @+j:U32 -> Array<U32>

template to.tail source · line 475 · raw

@-g:(@_:F32 -> U32) -> @n:Nat -> @+c:Cell -> @w:Array<U32> -> @+j:U32 -> @+l:U32 -> Array<U32>

The first n lanes of c, from lane l, into words j ..

template to.go source · line 483 · raw

@-g:(@_:F32 -> U32) -> @n:Nat -> @+k:U32 -> @r:Pair(Array<Cell>, Cell) -> @w:Array<U32> -> @+i:U32 -> Pair(Array<Cell>, Array<U32>)

n whole cells from cell i, then the first k lanes of the next one, so no write runs past the words.

template to_bytes.with source · line 496 · raw

@-g:(@_:F32 -> U32) -> @a:F32Array -> 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes

template read.fin source · line 564 · raw

@-f:(@_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Maybe<&1, F32Array>) -> @got:Pair(File, Result<&1, &1, Pair(U32, String), 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>) -> Pair(File, Result<&1, &1, Pair(U32, String), F32Array>)

template read.with source · line 572 · raw

@-f:(@_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Maybe<&1, F32Array>) -> @file:File -> IO(Pair(File, Result<&1, &1, Pair(U32, String), F32Array>))