Vectors
<N>T is an array that computes lane-wise — one instruction for N additions, one kernel for every register width, and it still compiles where there is no vector unit at all.
<N>T holds N lanes of T — the same values an [N]T holds, in the same order — with one
difference: its operators work on every lane at once.
val a: <4>f32 = [1, 2, 3, 4]
val b: <4>f32 = [10, 20, 30, 40]
val c = a + b
print(c[0], c[3])
11 44
That + is one machine instruction computing four additions. The two type constructors differ only
in their brackets because a vector is an array whose operators work lane-wise: [4]f32 is
storage, and <4>f32 is a register.
Vector code runs on every target. A vector wider than the hardware becomes several registers, and
on a machine with no vector unit it becomes ordinary scalar operations, so <4>f32 compiles for a
Cortex-M0 exactly as for an Apple M-series; only the speed differs. Where speed matters, the choice
a program makes is how wide its vectors are.
Writing a vector
A literal fills the lanes, and it must fill all of them: the lane count is part of the type.
val v: <4>int = [1, 2, 3, 4]
print(v[0], v[3], v.len)
1 4 4
A scalar broadcasts (a splat, the most common vector operation). A scalar written where a vector is expected — at a binding, an operator, a return — is copied into every lane:
val zero: <4>f32 = 0
val ones: <4>f32 = [1; 4]
half(v: <4>f32) -> <4>f32 = v * 0.5
print(zero[0], ones[3], half(ones)[0])
0 1 0.5
A declaration with no initializer starts every lane at the lane type’s zero.
A module vector over constants is constant data, as an array literal is: a literal, a splat, the
operators, a shuffle, a select, a conversion and a vector worked out from another such val are
computed while compiling and laid into the object file, alone or inside a struct, an array or an enum’s
payload. So a package holding a table of weights builds for a freestanding target, where nothing runs
before main to fill storage in. One that reads a module var, or calls abs or mul_add, is
computed when the program starts, and a freestanding build refuses it, saying which.
A lane is read by a constant index. This is the one subscript in sysl that is not checked at run
time: a vector has no address, and an out-of-range lane read has no defined result at the machine
level. The index must therefore be a literal or a const, and it is checked at compile time:
val v: <4>f32 = [1, 2, 3, 4]
print(v[4])
<4>f32 has lanes 0 to 3, and this is 4
A lane is written by the same constant index, v[1] = 9, and by letter: v.y = 9. For
a computed index, convert the vector to an array, whose subscript is
checked at run time.
Every lane, one at a time
A loop over the lanes is a for const.
It is unrolled while compiling into one copy of its body per value, and in each copy the variable is
a constant, so v[j] is a lane index like v[2]. The range may be bounded by literals, consts or a
value parameter, so one kernel walks every width it is instantiated at:
dot[const N: usize](a: <N>f64, b: <N>f64) -> f64
var s = 0.0
for const j in 0..<N
s += a[j] * b[j]
s
val a: <4>f64 = [1, 2, 3, 4]
var scaled: <4>f64 = 0
for const j in 0..<4
scaled[j] = a[j] * real(j)
print(dot(a, a), scaled[3])
30 12
Each lane read in the copies is one extractelement at a constant lane, exactly what a[0] * b[0] + a[1] * b[1] + … written out would give, and the optimizer is free to keep a lane in its register:
AArch64’s fmla v0.2d, v1.2d, v2.d[1] multiplies by one lane of another register in place. Copies
nest, so acc[i][j] over an array of vectors is two for const loops.
The rules are the for const‘s own: the range is known at compile time, it is unrolled to at most
65 copies, a return in a copy leaves the function, and break and continue are refused, since
no loop is left at run time for them to act on. An ordinary for is a loop at run time, its
variable different on each turn, so it cannot pick a lane:
const NR = 4
total(v: <NR>f64) -> f64
var s = 0.0
for j in 0..<NR
s += v[j]
s
print(total(1))
a lane index is part of the instruction that reads it, so it has to be a constant — a literal, a 'const' naming one, or the variable of a 'for const', which is unrolled
A vector and an array
A vector converts to an array of the same element and length, and an array to such a vector.
<N>T and [N]T hold the same values in the same order, so nothing is rounded or reinterpreted,
and the conversion is made by the position as every lossless
conversion is: a declared val or var, an
assignment, an argument, a returned value, a field, an element.
largest[const N: usize](v: <N>f64) -> f64
val xs: [N]f64 = v
var best = xs[0]
for x in xs
if x > best then best = x
best
val v: <4>f64 = [3, 9, 2, 6]
val doubled: [4]f64 = v * 2
var i = 1
val back: <4>f64 = doubled
print(largest(v), doubled[i], (back + 1)[3])
9 18 13
The array is storage, so it is subscripted by a computed index, walked by an ordinary for, and
checked at run time; the vector is a register, computed with lane-wise. The conversion moves the
lanes one by one (an extractelement and an insertvalue each), which the optimizer merges into
the machine’s own moves; it is never a store of the whole register, because a <4>bool is packed
into bits in memory and a [4]bool is not. For moving a run of a larger array, use
load and store.
The two must agree in element and in length. A position changes only the brackets, so a
<4>f32 is not a [4]f64 there — changing the lane type is a conversion you
write; and a different length is refused naming both:
val v: <4>f32 = [1, 2, 3, 4]
val a: [3]f32 = v
a vector converts to an array, and an array to a vector, only at one length: <4>f32 has 4 lanes and [3]f32 has 3 elements
An operator does not mix the two, in either order: two operands meeting is not a position that names
the type it wants, so v + a with a an array is refused as two different vector types are.
Convert one side first, with a declared val. A bracket list written beside the vector, v + [1, 2], is not an array but the vector’s own literal (see the operators).
Converting lanes
<N>U(v) converts every lane of v to U, exactly as U(x) converts one scalar. An integer
becomes a float, a float an integer, a narrower integer a wider one and a wider one a narrower:
whatever U(x) accepts for one lane, <N>U(v) accepts for N of them, and whatever it refuses is
refused here too.
val counts: <4>i32 = [3, -1, 300, 7]
val scaled = <4>f64(counts) * 0.5
val back = <4>i32(scaled)
val bytes = <4>u8(counts)
print(scaled[0], scaled[1], back[2], bytes[1], bytes[2])
print(<4>f32([1, 2, 3, 4])[3], <2>i64(<2>i8([-5, 100]))[0])
1.5 -0.5 150 255 44
4 -5
Each lane follows the scalar’s rules: a float becomes an integer toward zero, saturating at the
width’s ends with a NaN as zero; a narrower integer keeps the low bits; a wider one extends by the
source’s sign; a mask lane becomes 1 or 0 at any integer width but i1. The one difference is the debug build’s check on a written narrowing, which is the
scalar’s alone — a register traps as a whole or not at all, so a vector narrows by wrapping, as its
arithmetic does.
Where the scalar conversion is one machine instruction, the vector’s is one instruction over the
whole register — <4>f64(counts) is a single sitofp <4 x i32>, and <4>u8(mask) a single
zext — and never a loop over the
lanes. What has no instruction of its own (a fixed-point lane, which rescales and rounds, or an
integer wider than 128 bits becoming a float) is converted one lane at a time, as the scalar is.
An array of the lanes converts too, since it holds the same values: <4>f64(xs) over a [4]i32.
A literal argument is written at the vector, so <4>f32([1, 2, 3, 4]) is four f32 lanes and
<4>f32(0.5) the splat. A scalar of the lane’s own type splats too, whatever it is held in:
<4>f32(x) with x: f32 is x in every lane, and a scalar of any other type is refused, as it is at
a binding. Inside a generic body <N>T(v) converts to whatever T and N are bound
to. The lane count never changes, since a lane has nowhere to go and none to come from otherwise:
val v: <4>f32 = [1, 2, 3, 4]
val w = <2>int(v)
a vector converts lane by lane only at one length: <4>f32 has 4 lanes and <2>int has 2 lanes
Nothing converts without being written: val w: <4>f64 = v is still refused, as val r: real = n
is for one int.
What the operators do
A vector has exactly the operators its lane has, applied to every lane. Both sides must be the same vector, or one side a scalar that broadcasts:
| lanes | operators |
|---|---|
| an integer | + - * & | ^ << >>, and unary - ~ |
| a float | + - * /, and unary - |
| a fixed-point type | + - *, and unary - — saturating and rounding as the scalar does |
bool (a mask) | & | ^ |
The scalar may stand on either side, and so may a bracket list written in place, which is read as the vector beside it rather than as an array:
val dt = 6.0
val v: <2>real = [2, 3]
val m = dt / v + [1, 2] * v
print(m[0], m[1])
5 8
Integer / and % are refused. Scalar integer division traps on a zero divisor and on signed
overflow, but a register traps as a whole or not at all, so a vector divide would either drop the
check or trap for lanes that were valid. No processor sysl targets has an integer vector divide, so
a scalar loop compiles to the same instructions the hardware would use:
val a: <4>int = [10, 20, 30, 40]
f() -> unit
val b = a / a
'/' is not defined on <4>int
Operators do not convert between vector types: two different widths, or two different lane types,
are two different types, and a different lane type is reached by converting the
lanes first, <4>f32(ints) + b:
val a: <4>f32 = [1, 2, 3, 4]
val b: <8>f32 = [1, 2, 3, 4, 5, 6, 7, 8]
f() -> unit
val c = a + b
'+' needs matching types, got <4>f32 and <8>f32
Masks, and the lane-wise if
A comparison between vectors yields a mask — a <N>bool. It is an ordinary value: bind it, combine
it, reduce it.
val a: <4>f32 = [1, 5, 3, 7]
val small = a < 4
print(small.any(), small.all())
true false
select chooses by mask. It is a method rather than an if, because an if evaluates one side
or the other, and a register cannot take one branch in some lanes and the other in the rest. Both
sides are computed, and the mask picks between the results lane by lane:
val a: <4>f32 = [1, 5, 3, 7]
val b: <4>f32 = [4, 2, 6, 0]
val lo = (a < b).select(a, b)
val clamped = (a > 4).select(4, a)
print(lo[0], lo[1], clamped[1], clamped[3])
1 2 4 4
Either side of a select may be a scalar, which broadcasts: (v > hi).select(hi, v) clamps v to
hi with no vector constructed around the bound.
Two masks combine with the bitwise operators, not with &&, because && short-circuits and a
vector cannot short-circuit per lane.
val a: <4>int = [1, 2, 3, 4]
val inside = (a > 1) & (a < 4)
print(inside.any(), inside.all())
true false
For the same reason a comparison chain has no lane-wise form. 1 < a < 4 joins its two
comparisons with &&, so it is refused rather than silently rewritten with &:
val a: <4>int = [1, 2, 3, 4]
f() -> unit
val m = 1 < a < 4
compare two vectors at a time and combine the masks with '&'
A mask as bits
bits() reads a mask as an unsigned integer as wide as it has lanes, lane i at bit i: a
<16>bool is a u16, a <4>bool a u4. It is what a search does once any() has said a lane
matched, since the first matching lane is the integer’s trailing_zeros() and how many matched is
its count_ones():
first_byte(hay: []const u8, needle: u8) -> Option[usize]
var i: usize = 0
while i + 16 <= hay.len
val v: <16>u8 = hay.load(i)
val m = v == needle
if m.any() then return Some(i + usize(m.bits().trailing_zeros()))
i += 16
while i < hay.len
if hay[i] == needle then return Some(i)
i += 1
None
val s = "the quick brown fox jumps over the lazy dog"
val a: <4>f32 = [1, 5, 3, 7]
print(first_byte(s.bytes, u8('z')), first_byte(s.bytes, u8('!')))
print((a > 2).bits(), (a > 2).bits().count_ones())
Some(37) None
14 3
It is one bitcast of the <N x i1> to an iN, which the back end turns into the machine’s
own idiom: pmovmskb on x86, a narrowing and an add across the register on AArch64. A vector that is
neither a mask nor a float vector (whose bits() is its lanes’ encodings) has no bits():
val v: <4>int = [1, 2, 3, 4]
val b = v.bits()
a mask's lanes as an integer are read off a mask, as in '(a == b).bits()'
Rearranging lanes
shuffle picks lanes by number. Given a list, it builds a vector whose lane i is the lane the
list names at i. Given a second vector as well, it picks from both, numbering the second vector’s
lanes after the first’s: lanes 0 to N - 1 are the receiver’s and N to 2N - 1 the second’s.
val a: <4>f32 = [1, 2, 3, 4]
val b: <4>f32 = [10, 20, 30, 40]
val zipped = a.shuffle(b, [0, 4, 1, 5])
val turned = a.shuffle([3, 2, 1, 0])
val rolled = a.shuffle(b, [1, 2, 3, 4])
print(zipped[0], zipped[1], zipped[2], zipped[3])
print(turned[0], turned[3], rolled[0], rolled[3])
1 10 2 20
4 1 2 10
Each shuffle is one shufflevector instruction, with the list as its constant operand, and
the back end turns it into the machine’s own permutes at every optimisation level: on AArch64 the
three above are zip1, rev64 with an ext, and ext; on x86 they are unpcklps, shufps, and
blendps with a shufps. A literal of lane reads, such as [a[0], b[0], a[1], b[1]], usually
reaches the same instructions once LLVM’s optimiser has folded it, but shuffle reaches them
without the optimiser.
The result is as wide as the list is long, so a shuffle also widens, narrows and repeats. A list may name a lane more than once:
val a: <4>f32 = [1, 2, 3, 4]
val b: <4>f32 = [10, 20, 30, 40]
val wide = a.shuffle(b, [0, 1, 2, 3, 4, 5, 6, 7])
val upper = wide.shuffle([4, 5, 6, 7])
val third = a.shuffle([2, 2, 2, 2])
print(wide.len, upper.len, upper[0], third[3])
8 4 10 3
Joining two <4> vectors into an <8>, or taking either half of one, costs nothing at run time: the
eight lanes already sit in the pair of registers the two halves occupy.
The list is a constant
A lane number is part of the instruction, just as a lane index is, so the list must be
known while compiling. It may be an array literal of constants — literals, consts, value
parameters, and arithmetic on them — or the result of a const function. Anything else is refused:
f(a: <4>f32, k: usize) -> <4>f32 = a.shuffle([k, 0, 1, 2])
a shuffle's lanes are part of the instruction, so the list has to be a constant
Picking lanes by numbers computed at run time is a different instruction (tbl on AArch64,
pshufb on x86), with its own rules for an index out of range, and so a different method:
lookup.
A lane outside the vectors is refused while compiling, with both ranges named:
f(a: <4>f32, b: <4>f32) -> <4>f32 = a.shuffle(b, [0, 8, 1, 5])
'shuffle' picks from lanes 0 to 7 — 0 to 3 of the receiver and 4 to 7 of the second vector — and this is 8
The two vectors must be one type, at the same width with the same lanes. A scalar is not
broadcast here: a shuffle with a scalar where the second vector belongs is almost always a
mistake, and a broadcast is spelled [0; 4] or as a typed binding.
f(a: <4>f32, b: <8>f32) -> <4>f32 = a.shuffle(b, [0, 1, 2, 3])
'shuffle' takes two vectors of one type, got <4>f32 and <8>f32
Lists for every width
A const function computes a list once per width, so one kernel shuffles at every width.
sysl.vectors provides the common lists, each taking the width W of the
vectors being shuffled:
| list | lanes at W = 4 | AArch64 |
|---|---|---|
zip_lo[W]() | [0, 4, 1, 5] | zip1 |
zip_hi[W]() | [2, 6, 3, 7] | zip2 |
unzip_even[W]() | [0, 2, 4, 6] | uzp1 |
unzip_odd[W]() | [1, 3, 5, 7] | uzp2 |
reverse[W]() | [3, 2, 1, 0] | rev64 + ext |
rotate[W, K]() | [1, 2, 3, 0] at K = 1 | ext |
import sysl.vectors.{zip_lo, zip_hi}
interleave[const W: usize](a: <W>int, b: <W>int) -> (<W>int, <W>int) =
(a.shuffle(b, zip_lo[W]()), a.shuffle(b, zip_hi[W]()))
val a4: <4>int = [0, 1, 2, 3]
val b4: <4>int = [10, 11, 12, 13]
val a8: <8>int = [0, 1, 2, 3, 4, 5, 6, 7]
val b8: <8>int = [10, 11, 12, 13, 14, 15, 16, 17]
val (lo, hi) = interleave(a4, b4)
val (lo8, hi8) = interleave(a8, b8)
print(lo[1], hi[0], lo8[7], hi8[1])
10 2 13 14
At W = 4 the kernel is zip1 and zip2, and at W = 8 it is two of each. A list of your own is
written the same way — a const function with a value parameter, returning [W]usize. A width that
makes a lane fall outside the vectors is refused for that instantiation only, in the words above.
Shuffling a mask
A mask is a vector, and shuffles like one:
val a: <4>f32 = [1, 5, 3, 7]
val b: <4>f32 = [4, 2, 6, 0]
val m = (a < b).shuffle([3, 2, 1, 0])
print(m[0], m[3])
false true
It is correct, but not free. On AArch64 a comparison’s lanes are narrowed to be shuffled and widened
again afterwards, so (a < b).shuffle(lanes) costs several instructions more than
a.shuffle(lanes) < b.shuffle(lanes). The two are the same mask; prefer shuffling the operands.
Lanes picked at run time
table.lookup(idx) reads lane idx[k] of table into lane k, with positions computed while
the program runs. The positions are a vector of integers, and the answer is as wide as they are and
holds the table’s lanes. A position past the table’s end answers zero, as AArch64’s tbl does,
and so does a negative one: a lookup never traps, so a byte-class table can be read with whatever
bytes the input holds.
val classes: <16>u8 = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0]
val text: <16>u8 = [97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 10]
val by_nibble = classes.lookup(text & 15)
val by_byte = classes.lookup(text)
val t: <4>f32 = [1.5, 2.5, 3.5, 4.5]
val i: <4>i32 = [3, -1, 4, 0]
val r = t.lookup(i)
print(by_nibble[0], by_nibble[9], by_byte[0], by_byte[15])
print(r[0], r[1], r[2], r[3])
1 2 0 2
4.5 0 0 1.5
A table of sixteen bytes read at sixteen or eight byte positions is one tbl on AArch64. Any
other lookup is written as the shape back ends recognise as a variable permute — the positions
clamped once, a lane read at each, and one select that zeroes the lanes out of range — which x86
turns into pshufb on a processor with SSSE3, and a machine with no such instruction into the lane
moves it would have needed anyway. A position type that cannot name a lane past the end, such as a
<4>u2 into a table of four, has no range test at all.
A list of positions known while compiling is a shuffle, which is the cheaper instruction, so a
written list is refused here:
val t: <4>int = [1, 2, 3, 4]
val r = t.lookup([3, 2, 1, 0])
positions known while compiling are a 'shuffle'
v.compress(m) packs the lanes of v where m is true into the low lanes, in order, and
fills the rest with zero. How many it kept is m.bits().count_ones(). It is the step that turns a
filter’s mask into a list of what passed:
val v: <4>i64 = [5, 50, 7, 70]
val m = v > 6
val kept = v.compress(m)
print(kept[0], kept[1], kept[2], kept[3], m.bits().count_ones())
50 7 70 0 3
No processor sysl compiles for by default has a compress instruction (AVX-512’s vpcompress and
SVE’s compact are extensions), so it is a lane move per lane, with no branch. The mask must be the
receiver’s width:
val v: <4>int = [1, 2, 3, 4]
val w: <8>int = [0; 8]
val c = v.compress(w > 0)
'compress' keeps the lanes a mask says to, so it takes the '<4>bool'
Swizzles
A field read on a vector whose name is one to four of the letters x y z w is a swizzle. The
letters are lanes 0 to 3. One letter is the lane itself, a scalar, as v[2] is; two to four are a
vector as wide as the name is long, and a letter may repeat. A swizzle is shuffle under another
spelling, so it is the same single instruction:
val p: <4>f32 = [1, 2, 3, 4]
val back = p.wzyx
val flat = p.xy
val wide = flat.xyxy
val third = p.z
print(back[0], back[3])
print(flat[0], flat[1])
print(wide[2], wide[3])
print(third)
4 1
1 2
1 2
3
The letters reach lanes 0 to 3 of a vector of any width, so a swizzle is at most four lanes; anything
wider is a shuffle. A name with a letter outside xyzw is an ordinary member, which is why len
is still the lane count. Each letter has to be a lane the vector has:
val v: <2>f32 = [1, 2]
print(v.z)
<2>f32 has lanes x and y, and 'z' is lane 2
Writing lanes
A swizzle or a constant index is also a target. v.y = x and v[1] = x replace one lane, which is
one insertelement; v.xy = w replaces the named lanes, which is one shufflevector that merges w
into v. The vector itself has to be something that can be assigned, and a scalar on the right
fills every named lane:
val p: <4>f32 = [1, 2, 3, 4]
var q: <4>f32 = [0, 0, 0, 0]
q.xy = p.zw
q.w = 9
q[1] = 7
print(q[0], q[1], q[2], q[3])
q.zx = [5, 6]
q.yw += 1
print(q[0], q[1], q[2], q[3])
q.yz = 0
print(q[0], q[1], q[2], q[3])
3 7 0 9
6 8 5 10
6 0 0 10
A compound assignment is the longhand, q.yw += 1 being q.yw = q.yw + 1. A lane has no address of
its own, so a write is the vector read, changed and stored back, and four things follow:
- the letters of a swizzle written to must be distinct, since one value cannot go to two lanes;
- the vector must be a place that can be named twice, so a vector reached through a call is refused;
- a write is a statement, and
:=is refused; &v.yis refused, as&v[1]is.
var q: <4>f32 = [0, 0, 0, 0]
q.xx = [1, 2]
a swizzle written to cannot repeat a letter — 'x' names lane 0 twice, and one value cannot go to both
val p: <4>f32 = [1, 2, 3, 4]
p.x = 1
a 'val' is written once, so assignment has nothing to write through
Reductions
A reduction collapses a vector to one scalar. sum, min and max apply to a numeric vector; any
and all to a mask.
dot(a: <4>f32, b: <4>f32) -> f32 = (a * b).sum()
val v: <4>f32 = [3, 1, 4, 1.5]
val w: <4>f32 = [1, 2, 3, 4]
print(v.sum(), v.min(), v.max(), dot(v, w))
9.5 1 4 23
A float sum is computed as a tree rather than left to right, so that it maps to vector instructions; it may therefore differ in the last bit from adding the lanes in order. An integer sum wraps at the lane width, exactly as scalar integer arithmetic does.
A float min() or max() steps over a NaN, as the scalar
min_num and max_num do: a NaN lane loses to any
number, and only a vector of nothing but NaNs reduces to one. Where a NaN must not be lost,
maximum() and minimum() are the reductions that keep it.
The integer dot product
a.dot(b) multiplies two integer vectors lane by lane and adds the products into an integer wider
than the lanes. The accumulator is 32 bits for 8- and 16-bit lanes, 64 bits for 32-bit lanes, and
the lane’s own width for wider ones (nothing is wider to widen into), and it has the lanes’
signedness: <16>i8 answers an i32, <16>u8 a u32. It is (wide(a) * wide(b)).sum() with the
lanes widened first, which is the shape AArch64’s sdot/udot (the dot-product extension) and x86’s
vpdpbusd/vpdpwssd compute, so LLVM selects them where the target has them. Where it does not, the
same expression is a widening multiply and a pairwise add; the answer is the same on every target.
val a: <16>i8 = [127; 16]
val b: <4>i16 = [-32768; 4]
val big: i32 = a.dot(a)
val wrapped: i32 = b.dot(b)
print(big, wrapped)
258064 0
Overflow. A product of two widened lanes always fits the accumulator, so only the sum can
overflow, and it wraps at the accumulator’s width in every build, as an integer sum() does:
four i16 lanes of -32768 give 2^32, which is 0 in an i32. It never traps. A float vector’s
dot product is still (a * b).sum().
Both vectors must be one type. A mixed-signedness product (<16>i8 with <16>u8), two lengths, a
scalar, a float, fixed-point or mask lane, or the wrong number of arguments is refused:
val a: <16>i8 = [1; 16]
val b: <16>u8 = [1; 16]
val r = a.dot(b)
'dot' multiplies vectors of one type, and the receiver is <16>i8 where the argument is <16>byte
Magnitude, and a fused multiply-add
Two methods apply a single machine operation to every lane at once and answer a vector. Each is one instruction over the whole register where the target has one, and never a loop over the lanes.
v.abs() is the magnitude of each lane. A float lane has its sign bit cleared, so -0.0 answers
0.0, a NaN stays a NaN and an infinity stays an infinity, at every width including f16 and
bf16. A signed integer lane answers as the scalar abs does: the most negative value answers
itself, in every build, because its magnitude does not fit the width. An unsigned, bool or
fixed-point vector has no abs and is refused.
a.mul_add(b, c) is a * b + c in every lane with the product never rounded on its own, as the
scalar mul_add is. Either b or c
may be a scalar, which broadcasts. It is defined for f32 and f64 lanes only: a half-width lane
cannot keep the single rounding, so f16 and bf16 are refused, as the scalar methods are absent.
Inside a @strict_fp body it is the constrained multiply-add, as the scalar one is, rounded in the
mode in force. Neither method is evaluated while compiling.
val e = 1.0 / 1073741824.0
val a: <2>f64 = [1.0 + e, 2.0]
val b: <2>f64 = [1.0 - e, 3.0]
val c: <2>f64 = [-1.0, 1.0]
val product = a * b
val twice = product + c
val once = a.mul_add(b, c)
val i: <4>i32 = [-5, 5, -2147483648, 0]
val m = i.abs()
val f: <4>f32 = [-0.0, -1.5, 2.0, -3.0]
val g = f.abs()
print(twice[0] == 0.0, once[0] < 0.0, once[1])
print(m[0], m[1], m[2], m[3])
print(1.0 / g[0], g[1], g[3])
true true 7
5 5 -2147483648 0
inf 1.5 3
val a: <4>f16 = [1.0, 2.0, 3.0, 4.0]
f() -> unit
val r = a.mul_add(a, a)
'mul_add' rounds the product and the sum once, which only a float of 32 or 64 bits can promise
Square roots and roundings
v.sqrt(), v.floor(), v.ceil(), v.round() and v.trunc() are the scalar
Float methods of the same names in every lane, each one
instruction over the register where the target has one. The square root is correctly rounded, so each
lane is bit for bit what x.sqrt() answers for it; the four roundings are exact. round is the
scalar’s round: a half goes away from zero, so 2.5 rounds to 3 and -2.5 to -3, as C’s
round does.
val v: <4>f32 = [2.5, -2.5, 0.5, -0.0]
val r = v.round()
val f = v.floor()
val c = v.ceil()
val t = v.trunc()
print(r[0], r[1], r[2], 1.0 / r[3])
print(f[0], f[1], c[0], c[1], t[1])
print(<4>f32([9, 2, 0.25, -1]).sqrt()[0], <4>f32([9, 2, 0.25, -1]).sqrt()[3])
3 -3 1 -inf
2 -3 3 -2 -2
3 NaN
A NaN lane stays a NaN, an infinity stays an infinity, a negative zero keeps its sign, and the square
root of a negative lane is a NaN, as the scalar’s is. Inside a @strict_fp body each is the
constrained operation, as the scalar method is. They are defined for f32 and f64 lanes; the
scalar methods compute a half-width value at f32, and so does a vector, once it is widened:
val h: <4>f16 = [f16(1.5), f16(2.5), f16(3.5), f16(4.5)]
f() -> unit
val r = h.round()
'round' is defined for f32 and f64 lanes, and <4>f16 is not one — widen the lanes to f32 first
A float’s encoding, lane by lane
v.bits() is every lane’s IEEE 754 encoding as the unsigned integer of its width: a <4>f32
gives a <4>u32, a <2>f64 a <2>u64, and an f16 or bf16 lane a u16, exactly as the scalar
x.bits() does. <4>f32.from_bits(b) goes the other way, taking the
<4>u32 and answering the floats whose encodings it holds. Both are a reinterpretation, not a
conversion — one register read as the other type, so nothing is rounded and a negative zero, an
infinity and a NaN’s payload all come through as their bits. A conversion that keeps the value is
<4>u32(v).
val v: <4>f32 = [1.0, -0.0, 0.5, -2.0]
val b = v.bits()
print(f"${b[0]}%x ${b[1]}%x ${b[2]}%x ${b[3]}%x")
// Clear every sign bit: the magnitude, written on the encoding.
val m = <4>f32.from_bits(b & 0x7fffffff)
print(m[0], 1.0 / m[1], m[2], m[3])
3f800000 80000000 3f000000 c0000000
1 inf 0.5 2
That is the step a vector exp or log is built from: the exponent of a float is a field of its
encoding, and setting it is a shift and an add on the <N>u32. The library’s own
exp is built that way.
from_bits takes exactly the unsigned vector bits() answers, of the same lane count and width; a
signed or a narrower one is refused, as the scalar’s Word is the only integer it takes:
val i: <4>i32 = [0, 1, 2, 3]
f() -> unit
val v = <4>f32.from_bits(i)
'from_bits' of <4>f32 takes the <4>uint 'bits()' answers, and <4>int was given
The exponential and the hyperbolic tangent
v.exp() and v.tanh() are the scalar Float methods of the same
names in every lane of an <N>f32 or an <N>f64, at any width. No machine has an instruction for
either and no C library has an entry point that takes a register, so they are not the compiler’s:
they are sysl.math.VectorFloat, an
impl for every width written in sysl. Each body is straight-line
arithmetic on the whole register — a range reduction, a polynomial, and a power of two set on the
encoding — with no branch and no call per lane, so a loop over them stays a loop of vector
instructions:
val x: <4>f32 = [0.0, 1.0, -1.0, 100.0]
val e = x.exp()
val t = x.tanh()
print(e[0], e[1], e[2], e[3])
print(t[0], t[1], t[2], t[3])
// The logistic function, at whatever width the caller has.
sigmoid[const W: usize](v: <W>f32) -> <W>f32 = 0.5 + 0.5 * (0.5 * v).tanh()
val s = sigmoid(<8>f32([-2.0, 0.0, 2.0, 4.0, 0.0, 0.0, 0.0, 0.0]))
print(s[0], s[1], s[2], s[3])
1 2.7182817 0.36787945 inf
0 0.7615942 -0.7615942 1
0.11920291 0.5 0.8807971 0.9820138
Each lane is within one ulp of the scalar method’s answer — checked over every f32 and over a
dense sweep of f64 — and is exact wherever the answer is: exp of either zero is 1, of inf is
inf and of -inf is 0; an answer past the largest finite value is an infinity and one below the
smallest subnormal is zero, with a subnormal answer rounded once; tanh of either zero is that zero,
of a subnormal is the subnormal, and of an infinity is ±1; a NaN lane answers a NaN. Where a
program needs the scalar’s exact bits, it calls the scalar; two good implementations of a function
that is not correctly rounded agree only to the last bit.
Being sysl rather than libm, they need no C library and are there on a target without one. A
half-width lane has no impl, as it has no lane-wise square root: widen it to f32 first.
val h: <4>f16 = [f16(1.0), f16(2.0), f16(3.0), f16(4.0)]
f() -> unit
val e = h.exp()
type '<4>f16' has no method 'exp' — 'exp' is defined for f32 and f64 lanes, and <4>f16 is not one — widen the lanes to f32 first
NaN-propagating maximum and minimum
a.maximum(b) and a.minimum(b) are IEEE 754-2019’s maximum and minimum in every lane: a NaN
in either lane wins, and -0.0 orders below 0.0, so the maximum of two zeros is the positive one
and the minimum the negative one, whichever way round they are. b may be a scalar, which
broadcasts. a.maximum() and a.minimum(), with no argument, reduce the lanes the same way: one
NaN lane makes the answer a NaN, where max() and min() would step over it. The
names and the meaning are the scalar Float methods’.
Each is one instruction over the register where the target has one. On AArch64 that is fmax and
fmin, which are these operations, and the reduction of two f64 lanes is one fmaxp.
val a: <4>f64 = [1.0, nan, -0.0, 5.0]
val b: <4>f64 = [2.0, 3.0, 0.0, -1.0]
val hi = a.maximum(b)
val lo = a.minimum(b)
print(hi[0], hi[1] != hi[1], 1.0 / hi[2], hi[3])
print(lo[0], lo[1] != lo[1], 1.0 / lo[2], lo[3])
print(a.max(), a.maximum() != a.maximum(), b.maximum())
2 true inf 5
1 true -inf -1
5 true 3
That is what a search that must not step over a NaN is built from. BLAS’s iamax answers the index of
the largest magnitude, and of the first NaN where there is one; a running lane-wise maximum
carries a NaN to the end, so clean data costs nothing beyond the vector pass:
iamax(x: []f64) -> usize
var best: <4>f64 = 0.0
var i = 0
while i + 4 <= x.len
val v: <4>f64 = x.load(i)
best = best.maximum(v.abs())
i += 4
var top = best.maximum()
for k in i..<x.len
top = top.maximum(x[k].abs())
for k in 0..<x.len
if top != top
if x[k] != x[k] then return k
elif x[k].abs() == top then return k
0
end iamax
var xs: [6]f64 = [1.0, -9.0, 3.0, 2.0, 9.0, 0.5]
print(iamax(xs[..]))
xs[3] = nan
print(iamax(xs[..]))
1
3
They are defined for f32 and f64 lanes. An integer vector has no maximum: it has no NaN to
propagate and no signed zero, so the lane-wise maximum of integers is a comparison and a
select. A half-width lane is refused too; widen it first. Inside a
@strict_fp body the lane-wise form is the constrained maximum or minimum, which raises invalid only
for a signalling NaN.
val a: <4>int = [1, 2, 3, 4]
f() -> unit
val m = a.maximum(a)
'maximum' is IEEE 754's NaN-propagating maximum, defined for f32 and f64 lanes, and <4>int is not one
An extern naming llvm.maximum at a vector type is refused, as any vector at that boundary is
(below); the methods are the way to it.
Loading and storing
A vector holds the lanes a kernel computes with; an array or a slice holds the program’s data. Two methods move a run of elements between them. They belong to the array or slice rather than to the vector, because the move needs an address and a length, and a vector has neither.
var xs: [8]f32 = [1, 2, 3, 4, 5, 6, 7, 8]
var out: [8]f32
val v: <4>f32 = xs.load(2)
out.store(0, v * 10)
print(v[0], v[3], out[0], out[3])
3 6 30 60
xs.load(i) reads the run of lanes starting at element i; out.store(i, v) writes one back. The
store needs a slice or an array it may write, so a []const T is refused exactly as xs[i] = v
would be.
These are ordinary names, so a load or store you declare yourself takes precedence: an impl
block for a slice is found before the built-in methods, which apply only to a receiver with no member
of that name. sysl.sync.Atomic, for example, has its own load and store, unaffected by these.
Where the width comes from
A load takes its width from whatever receives the value. A slice’s length does not determine it, and guessing could silently read the wrong run. A binding’s annotation gives the width, as do a parameter and a declared result:
first(v: <4>f32) -> f32 = v[0]
grab[const W: usize](xs: []const f32) -> <W>f32 = xs.load(0)
var xs: [8]f32 = [1, 2, 3, 4, 5, 6, 7, 8]
val annotated: <4>f32 = xs.load(4)
val declared: <2>f32 = grab(xs[..])
print(annotated[0], first(xs.load(1)), declared[1])
5 2 2
An operand of an operator gives the width too, so a load beside a vector takes its lane count from it. Types are resolved in three steps, extending the rule for literals: an operand with a type of its own is resolved first, a load takes the width that operand gives, and a bare literal takes the type the other two determined.
var xs: [4]f32 = [1, 2, 3, 4]
val by: <4>f32 = 10
var out: [4]f32
out.store(0, xs.load(0) * by + 1)
print(out[0], out[3])
11 41
So does the receiver of a vector method. The operands of mul_add, select and shuffle are
the receiver’s vector type (or, for select, the other side’s), so a load in one takes its width as
an operator’s operand would:
var xs: [4]f32 = [1, 2, 3, 4]
var ys: [4]f32 = [1, 1, 2, 2]
var acc: <2>f32 = 0
val a: <2>f32 = xs.load(0)
acc = a.mul_add(ys.load(2), acc)
print(acc[0], acc[1])
2 4
A position that accepts any width gives none. A store accepts any width, so there is nothing to infer from:
f(xs: []const f32, out: []f32)
out.store(0, xs.load(0))
how many lanes it takes is the vector type's to say
Annotate a binding there instead.
What a load or store guarantees
It is bounds-checked as a whole run. xs.load(3) on a five-element array traps even though
element 3 exists, because elements 3 through 6 do not. This is the only vector operation with a
run-time check; every other operation is a register operation that cannot fail, so a vector cannot
read or write past the end of an array.
Inside an @unchecked block that one test is left out, as
a scalar subscript’s is: the load or store is the address and nothing else.
A partial run at the end traps too, and the scalar tail is written by hand. An array whose length is not a multiple of the width ends with fewer elements than lanes; the kernel below shows how to handle it.
The alignment assumed is the element’s. A []f32 guarantees four-byte alignment and nothing
about where a run begins, so the vector’s own alignment cannot be assumed. On every machine sysl
targets, an unaligned vector load of aligned data costs the same as an aligned one.
A lane narrower than its element’s room moves element by element all the same. A []u12 gives
each element two bytes, so a run of u12 is read as two-byte elements and narrowed in the register,
and a store writes each lane back into its own two bytes. Every width does this — u12, i24,
u100, bool — so a run reads exactly what subscripting each element reads.
val xs: [6]u12 = [4095, 1, 2048, 7, 4000, 5]
var ys: [6]u12 = [0; 6]
val v: <4>u12 = xs.load(1)
ys.store(2, v + v)
print(ys[2], ys[3], ys[4], ys[5])
2 0 14 3904
volatile elements are refused rather than widened. A single instruction cannot preserve one
ordered access per element. volatile <4>u32 qualifies the whole register instead.
f(regs: []volatile u32) -> u32
val v: <4>u32 = regs.load(0)
v[0]
the qualifier cannot be kept
A *T is refused for a different reason: it carries no length to check the run against. Take a
slice of the elements first.
Gathering
xs.gather(idx) reads one element per lane, from wherever each lane of idx says, into a
vector as wide as idx. Like load, it belongs to the array or slice. Its width comes from the
positions, so nothing needs annotating:
var col: [10]i64 = [5, 50, 7, 70, 9, 90, 11, 110, 13, 130]
val rows: <4>u32 = [9, 0, 3, 3]
val g = col.gather(rows)
val s = col[2..<6].gather(rows & 3)
print(g[0], g[1], g[2], g[3], s[0], s[3])
130 5 70 70 70 90
Every position is bounds-checked as a subscript is, and a failure names that position, so
xs.gather(idx) traps exactly where the longhand xs[idx[k]] would:
val xs: [4]int = [1, 2, 3, 4]
val idx: <4>usize = [0, 1, 4, 2]
print(xs.gather(idx)[0])
Inside @unchecked the tests are left out, as a subscript’s are. The reads are scalar loads into
one register: no processor sysl compiles for by default has a gather instruction (AVX2’s and SVE’s
are extensions), and those loads are what such a machine needs anyway.
One kernel, every width
A lane count is a value parameter, so it is inferred from the argument as an array’s length is, and one generic kernel serves every width.
scale[const W: usize](v: <W>f32, by: f32) -> <W>f32 = v * by
val four: <4>f32 = [1, 2, 3, 4]
val eight: <8>f32 = [1, 2, 3, 4, 5, 6, 7, 8]
val a = scale(four, 2)
val b = scale(eight, 3)
print(a[3], b[7], a.len, b.len)
8 24 4 8
Neither call states a width. The body is compiled twice, into different instructions — four lanes
wide and eight. A mask inside a generic kernel has the same width, so a comparison there is a
<W>bool and the select works at whatever W is:
clamp_low[const W: usize](v: <W>f32, lo: f32) -> <W>f32 = (v < lo).select(lo, v)
val four: <4>f32 = [1, -2, 3, -4]
val two: <2>f32 = [-1, 5]
print(clamp_low(four, 0)[1], clamp_low(two, 0)[0])
0 0
In C, the same problem is usually solved by writing the kernel once per instruction set behind
#ifdef; Box2D’s contact solver, for example, contains four copies of itself.
A kernel over an array
Combining loads, stores and a width parameter gives a complete kernel in one body: the loads, the
arithmetic and the store all take their width from W, and a scalar loop handles the remaining
elements:
scale[const W: usize](xs: []const f32, out: []f32, by: <W>f32)
var i: usize = 0
while i + W <= xs.len
val v: <W>f32 = xs.load(i)
out.store(i, v * by)
i += W
while i < xs.len
out[i] = xs[i] * by[0]
i += 1
val src: [10]f32 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var four: [10]f32
var eight: [10]f32
val by4: <4>f32 = 3
val by8: <8>f32 = 3
scale(src[..], four[..], by4)
scale(src[..], eight[..], by8)
print(four[0], four[9], eight[0], eight[9])
3 30 3 30
Ten elements is not a multiple of either width, so both instantiations run the tail loop, and the two results agree.
A trait for every width
An impl may be written for a vector, and the lane count is what it is generic over. <N>f32
with N a value parameter is every vector of f32, whatever its width, so one block gives every
width the trait’s members, and a kernel generic over its width calls them:
trait LaneMath
twice(self) -> Self
impl[const N: usize] LaneMath for <N>f32
twice(self) -> Self = self + self
quad[const W: usize](v: <W>f32) -> <W>f32 = v.twice().twice()
again[T: LaneMath](v: T) -> T = v.twice()
val four: <4>f32 = [1, 2, 3, 4]
val eight: <8>f32 = 1
print(four.twice()[3], quad(eight)[7], again(four)[0])
8 4 2
A vector has three subjects an impl may name, and they are found most specific first:
- the vector written out in full,
<4>f32, which is one type like any other and takes no parameters; - every vector of one lane type,
<N>f32; - every vector,
<N>T, where the lane is a parameter of the block too.
trait Tag
tag(self) -> string
impl[const N: usize, T] Tag for <N>T
tag(self) -> string = s"$N lanes"
impl[const N: usize] Tag for <N>f32
tag(self) -> string = "f32"
override impl Tag for <4>f32
tag(self) -> string = "four f32"
val a: <4>f32 = 1
val b: <8>f32 = 1
val c: <2>u8 = 1
print(a.tag(), b.tag(), c.tag())
four f32 f32 2 lanes
The two shapes need no marking between them, as an array’s [2]T and [N]T need none. A vector
written out in full and a shape covering it are an overlap, and the written-out one says override,
exactly as for any other shape.
The lane count is not a shape on its own. A block for <4>T and one for <N>f32 would both
cover a <4>f32 with neither more specific than the other, so a count written out under a block
with parameters is refused:
trait Tag
tag(self) -> string
impl[T] Tag for <4>T
tag(self) -> string = "four"
'<4>T' fixes the lane count, and an 'impl' over vectors is generic over that count
A block may not declare a member named like one of the vector’s own methods — sum, select,
len and the rest — since a call reaches those before it asks any impl:
trait Total
sum(self) -> f32
impl[const N: usize] Total for <N>f32
sum(self) -> f32 = self[0]
'sum' is a member the compiler provides for every vector of f32 — a member of this name would hide it
by is a vector, and that is where W comes from. A kernel whose parameters are all slices
has nothing to infer the width from, so the call writes it, and a scalar by broadcasts across the
lanes where it meets a vector:
scale_by[const W: usize](xs: []const f32, out: []f32, by: f32)
var i: usize = 0
while i + W <= xs.len
val v: <W>f32 = xs.load(i)
out.store(i, v * by)
i += W
while i < xs.len
out[i] = xs[i] * by
i += 1
val src: [10]f32 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var out: [10]f32
scale_by[4](src[..], out[..], 3)
print(out[0], out[4], out[9])
3 15 30
What a vector does not have
A lane with an address. A lane is written, but only by writing its vector back, so &v.y is
refused as &v[1] is: a lane has no address of its own. See Writing lanes.
A masked load or store. A run that would reach past the end traps rather than reading only the lanes that exist. Write a scalar tail, as the kernel above does.
A place in a C signature. A vector may not be passed to or returned from a C function. The
register it would occupy differs by target and by the instruction-set extensions the C side was
compiled with, and a mismatch would produce a call that links but corrupts its arguments. Pass the
lanes through memory instead — a *f32 and a count, as C’s own SIMD functions usually take. (An
extern naming an AES, SHA-3 or dot-product instruction is the exception: the back end hands it
registers directly, so its signature is vectors.)
extern "process_lanes" process(v: <4>f32) -> unit
how a vector reaches a C function differs by target
Lanes
A lane is a scalar: an integer, a fixed-point type, a float, bool or char. A fixed-point lane
computes as its scalar does, a <2>q15 sum being QADD16 on a Cortex-M4; Fixed-point
numbers describes what it supports. An aggregate cannot be a lane — there is
no vector of structs — and neither can a volatile type, since a single load cannot give per-lane
access ordering. volatile <4>u32 qualifies the whole register instead.
struct Point
x: int
y: int
f() -> unit
var v: <4>Point
a vector's lanes are scalars
A constrained lane is allowed, and computes at its base type exactly as a scalar of that subtype does:
type Small = int within 0..100
f() -> unit
val v: <4>Small = [1, 2, 3, 4]
val w = v + v
print(w[0], w[3])
f()
2 8