Skip to content
velt/half exports

velt/half

API reference

Arrays of the 16-bit storage floats f16 and bf16 in bulk: loading them from the bytes of a model file, widening them to f32[] and narrowing them back, and the dot product of two of them, at the speed of the CPU's conversion instructions. For a walkthrough, read the guide.

import { f16FromBytes, widenF16, dotF16 } from "velt/half";

Exports

function

f16FromBytes

#std/half/bytes.velt:12

Re-exported from velt/half/bytes

function f16FromBytes(
  bytes: u8[],
  at: usize = 0,
  count: usize | null = null,
): f16[]

The count little-endian 16-bit patterns in bytes from byte at on, as a packed f16[]: one copy of 2 * count bytes, every pattern kept (NaN payloads too). count defaults to all the whole patterns after at, so an odd byte left over is not read. Panics with a RangeError when at + 2 * count passes the end of bytes, as u8x16.load does. For big-endian data, swap each pair first (u16x8.reverseBytes, or by hand).

function

bf16FromBytes

#std/half/bytes.velt:24

Re-exported from velt/half/bytes

function bf16FromBytes(
  bytes: u8[],
  at: usize = 0,
  count: usize | null = null,
): bf16[]

As f16FromBytes, for bf16 (the top 16 bits of an f32): the count little-endian patterns from byte at on, one copy.

function

widenF16

#std/half/widen.velt:15

Re-exported from velt/half/widen

function widenF16(ws: f16[]): f32[]

Each element of ws as an f32: exact, NaN payloads kept (their quiet bit set).

function

widenF16Into

#std/half/widen.velt:29

Re-exported from velt/half/widen

function widenF16Into(ws: f16[], xs: f32[], at: usize = 0)

Writes each element of ws as an f32 to xs[at..at + ws.length), allocating nothing. Panics with a RangeError when that range passes the end of xs.

function

widenBf16

#std/half/widen.velt:39

Re-exported from velt/half/widen

function widenBf16(ws: bf16[]): f32[]

Each element of ws as an f32: exact (a bf16 is an f32's top half).

function

widenBf16Into

#std/half/widen.velt:50

Re-exported from velt/half/widen

function widenBf16Into(ws: bf16[], xs: f32[], at: usize = 0)

As widenF16Into, for bf16.

function

narrowF16

#std/half/narrow.velt:15

Re-exported from velt/half/narrow

function narrowF16(xs: f32[]): f16[]

Each element of xs rounded to the nearest f16 (ties to even; overflow to ±Infinity), NaN payloads kept as far as they fit.

function

narrowF16Into

#std/half/narrow.velt:29

Re-exported from velt/half/narrow

function narrowF16Into(xs: f32[], ws: f16[], at: usize = 0)

Writes each element of xs, rounded to f16, to ws[at..at + xs.length), allocating nothing. Panics with a RangeError when that range passes the end of ws.

function

narrowBf16

#std/half/narrow.velt:39

Re-exported from velt/half/narrow

function narrowBf16(xs: f32[]): bf16[]

Each element of xs rounded to the nearest bf16 (ties to even), NaN payloads kept.

function

narrowBf16Into

#std/half/narrow.velt:50

Re-exported from velt/half/narrow

function narrowBf16Into(xs: f32[], ws: bf16[], at: usize = 0)

As narrowF16Into, for bf16.

function

dotF16

#std/half/dot.velt:48

Re-exported from velt/half/dot

function dotF16(a: f16[], b: f16[]): number

The dot product of a and b: a[0] * b[0] + a[1] * b[1] + …, each product exact in f32, accumulated in f32 lanes by f16x8.dotAdd and summed in number at the end, the same number on every CPU. Panics with a RangeError when the lengths differ.

function

dotBf16

#std/half/dot.velt:106

Re-exported from velt/half/dot

function dotBf16(a: bf16[], b: bf16[]): number

The dot product of a and b, as dotF16: accumulated in f32 lanes by bf16x8.dotAdd, the same number on every CPU, with no use of a CPU's bf16 dot product, whose rounding differs (a loop over bf16x8.dotAddFast has it). Panics with a RangeError when the lengths differ.