1251 lines
43 KiB
Plaintext
1251 lines
43 KiB
Plaintext
#! @file simd.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains `simd`, a vector of numbers one instruction works on lane by lane,
|
||
|
|
#! and `mask`, which lane comparisons give: `simd<f32[4]>` is four `f32`s, 16
|
||
|
|
#! bytes, and `simd<u8[32]>` thirty-two bytes. A vector holds 16 or 32 bytes of
|
||
|
|
#! numbers, or 64 of 16 bits or more, and the aliases name each, as `f32x4`,
|
||
|
|
#! `u8x32`, and `f32x16`.
|
||
|
|
#!
|
||
|
|
#! Each operation is written here lane by lane, as the number's own operator
|
||
|
|
#! gives it, which is what the virtual machine and constants worked out while
|
||
|
|
#! compiling run. An executable asks the processor running it, once
|
||
|
|
#! as it starts, which vector instructions it has, and where it has those of an
|
||
|
|
#! operation, as SSE2 for most of 16 bytes, SSE4.1 for some, and AVX2 for 32,
|
||
|
|
#! works the lanes with them instead, and AVX-512's for 64. Where it has them
|
||
|
|
#! only for fewer bytes, a vector is worked a half or a quarter at a time, and
|
||
|
|
#! where it has none, as here. The bits are the same either way.
|
||
|
|
#!
|
||
|
|
#! The functions of real lanes, as `sqrt`, `floor`, and `fma`, are methods
|
||
|
|
#! `std::math` declares, as the functions of reals are.
|
||
|
|
|
||
|
|
module core;
|
||
|
|
|
||
|
|
|
||
|
|
#! A vector of numbers, worked on lane by lane: `L` is the type of each lane,
|
||
|
|
#! and `N` how many lanes there are, as `simd<f32[4]>` writes them, 16 or 32
|
||
|
|
#! bytes in all, or 64 of at most 32 lanes. It is zero where nothing is written.
|
||
|
|
#! @tparam L The type of each lane, a number.
|
||
|
|
#! @tparam N How many lanes it has.
|
||
|
|
pub struct simd<L[N]> where L: numeric, ordered, zeroable {
|
||
|
|
var _lanes: L[N];
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What comparing two vectors lane by lane gives: for each lane, every bit set
|
||
|
|
#! where it holds, and every bit clear where it does not, as the processor's
|
||
|
|
#! comparisons give it.
|
||
|
|
#! @tparam L The type of each lane of the vectors compared.
|
||
|
|
#! @tparam N How many lanes they have.
|
||
|
|
pub struct mask<L[N]> where L: numeric, ordered, zeroable {
|
||
|
|
var _lanes: L[N];
|
||
|
|
}
|
||
|
|
|
||
|
|
pub type f32x4 = simd<f32[4]>;
|
||
|
|
pub type f32x8 = simd<f32[8]>;
|
||
|
|
pub type f64x2 = simd<f64[2]>;
|
||
|
|
pub type f64x4 = simd<f64[4]>;
|
||
|
|
pub type i8x16 = simd<i8[16]>;
|
||
|
|
pub type i8x32 = simd<i8[32]>;
|
||
|
|
pub type u8x16 = simd<u8[16]>;
|
||
|
|
pub type u8x32 = simd<u8[32]>;
|
||
|
|
pub type i16x8 = simd<i16[8]>;
|
||
|
|
pub type i16x16 = simd<i16[16]>;
|
||
|
|
pub type u16x8 = simd<u16[8]>;
|
||
|
|
pub type u16x16 = simd<u16[16]>;
|
||
|
|
pub type i32x4 = simd<i32[4]>;
|
||
|
|
pub type i32x8 = simd<i32[8]>;
|
||
|
|
pub type u32x4 = simd<u32[4]>;
|
||
|
|
pub type u32x8 = simd<u32[8]>;
|
||
|
|
pub type i64x2 = simd<i64[2]>;
|
||
|
|
pub type i64x4 = simd<i64[4]>;
|
||
|
|
pub type u64x2 = simd<u64[2]>;
|
||
|
|
pub type u64x4 = simd<u64[4]>;
|
||
|
|
pub type f32x16 = simd<f32[16]>;
|
||
|
|
pub type f64x8 = simd<f64[8]>;
|
||
|
|
pub type i16x32 = simd<i16[32]>;
|
||
|
|
pub type u16x32 = simd<u16[32]>;
|
||
|
|
pub type i32x16 = simd<i32[16]>;
|
||
|
|
pub type u32x16 = simd<u32[16]>;
|
||
|
|
pub type i64x8 = simd<i64[8]>;
|
||
|
|
pub type u64x8 = simd<u64[8]>;
|
||
|
|
|
||
|
|
|
||
|
|
# Making and taking apart.
|
||
|
|
|
||
|
|
#! The vector of an array's numbers, the first in lane 0.
|
||
|
|
#! @param lanes The numbers.
|
||
|
|
#! @returns The vector.
|
||
|
|
pub def simd<L[N]>::of(lanes: L[N]): simd<L[N]> {
|
||
|
|
return simd::<L[N]> { _lanes = lanes };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The vector holding a number in every lane.
|
||
|
|
#! @param value The number.
|
||
|
|
#! @returns The vector.
|
||
|
|
pub def simd<L[N]>::splat(value: L): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
val lane: L = value;
|
||
|
|
if !lanes_done::<L, lanes_splat, N>((&mut made._lanes) as *mut u8, (&lane) as *u8, null, null, 0, 0) {
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = lane;
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The vector of the numbers a view holds from a place, as many as it has lanes.
|
||
|
|
#! @param from The view.
|
||
|
|
#! @param at Where its first lane is read from.
|
||
|
|
#! @returns The vector.
|
||
|
|
#! @remarks Reading past the view's end stops the program, as indexing does.
|
||
|
|
pub def simd<L[N]>::load(from: span<L>, at: u64): simd<L[N]> {
|
||
|
|
_ = from[at];
|
||
|
|
_ = from[at + N - 1];
|
||
|
|
return ((from._dat + at) as *simd<L[N]>)[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Writes the vector's lanes into a view from a place.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @param into The view.
|
||
|
|
#! @param at Where its first lane is written.
|
||
|
|
#! @remarks Writing past the view's end stops the program, as indexing does.
|
||
|
|
pub def simd<L[N]>::store(&this, into: slice<L>, at: u64): void {
|
||
|
|
_ = into[at];
|
||
|
|
_ = into[at + N - 1];
|
||
|
|
val copied: simd<L[N]> = this;
|
||
|
|
((into._dat + at) as *mut simd<L[N]>)[0] = copied;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The vector's numbers as an array, lane 0 first.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @returns The array.
|
||
|
|
pub def simd<L[N]>::lanes(&this): L[N] {
|
||
|
|
return lanes_read(this);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Reaches a lane, to read it, which `v[i]` does.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @param lane The lane, below how many there are.
|
||
|
|
#! @returns The lane.
|
||
|
|
#! @remarks A lane past the last stops the program.
|
||
|
|
pub def simd<L[N]>::at(&this, lane: u64): &L {
|
||
|
|
return &this._lanes[lane];
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Reaches a lane, to write it, which `v[i] = x` does.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @param lane The lane, below how many there are.
|
||
|
|
#! @returns The lane.
|
||
|
|
#! @remarks A lane past the last stops the program.
|
||
|
|
pub def simd<L[N]>::at(&mut this, lane: u64): &mut L {
|
||
|
|
return &mut this._lanes[lane];
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The lanes in another order: lane `i` of what it gives is lane `order[i]` of
|
||
|
|
#! the vector.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @param order For each lane, the lane it is taken from.
|
||
|
|
#! @returns The vector.
|
||
|
|
#! @remarks A lane past the last stops the program.
|
||
|
|
pub def simd<L[N]>::shuffle(&this, order: u8[N]): simd<L[N]> {
|
||
|
|
if lanes_have::<L, lanes_shuffle, N>() {
|
||
|
|
# The order, then zeros, as the instructions read 16 bytes of it. In a
|
||
|
|
# checked program, each lane it names one of the vector's, or the lanes are
|
||
|
|
# read one at a time, each checked as an index is.
|
||
|
|
var held: u8[32];
|
||
|
|
((&mut held) as *mut u8[N])[0] = order;
|
||
|
|
val named := ((&held) as *simd<u8[16]>)[0];
|
||
|
|
if !checking() || named.less(&simd::<u8[16]>::splat(N as u8)).all() {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
lanes_apply::<L, lanes_shuffle, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&held) as *u8, null);
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
val a: L[N] = lanes_read(this);
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = a[order[i] as u64];
|
||
|
|
}
|
||
|
|
return simd::<L[N]> { _lanes = r };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Each lane converted to another type of number, as `as` converts it: a real
|
||
|
|
#! made an integer rounds toward zero, as a 64-bit integer, which holds at its
|
||
|
|
#! limits where the real is past them and is 0 for NaN, and an integer narrower
|
||
|
|
#! than that keeps its low bits.
|
||
|
|
#! @tparam M The type of each lane of what it gives.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @returns The vector, of as many lanes.
|
||
|
|
pub def simd<L[N]>::convert<M>(&this): simd<M[N]> where M: numeric, ordered, zeroable {
|
||
|
|
var worked: simd<M[N]>;
|
||
|
|
if within_integers::<L, M, N>(this) {
|
||
|
|
return worked if lanes_to::<L, M, lanes_convert, N>((&mut worked._lanes) as *mut u8, (&this._lanes) as *u8, null);
|
||
|
|
}
|
||
|
|
val a: L[N] = lanes_read(this);
|
||
|
|
var r: M[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = a[i] as M;
|
||
|
|
}
|
||
|
|
return simd::<M[N]> { _lanes = r };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The vector's bytes as a vector of other lanes, of the same size in all.
|
||
|
|
#! @tparam M The type of each lane of what it gives.
|
||
|
|
#! @tparam K How many lanes it has.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @returns The vector.
|
||
|
|
#! @remarks A vector of another size is refused while compiling.
|
||
|
|
pub def simd<L[N]>::bits<M, const K: u64>(&this): simd<M[K]> where M: numeric, ordered, zeroable {
|
||
|
|
if const K * size_of::<M>() != N * size_of::<L>() {
|
||
|
|
compile_error("`bits` is asked for a vector of another size than the one it is called on.");
|
||
|
|
}
|
||
|
|
return ((&this._lanes) as *simd<M[K]>)[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
#! The first half of the vector's lanes, as a vector of half as many: of 32
|
||
|
|
#! bytes, the first 16.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @returns The half.
|
||
|
|
pub def simd<L[N]>::low(&this): simd<L[N / 2]> {
|
||
|
|
var made: simd<L[N / 2]>;
|
||
|
|
return made if lanes_also::<L, lanes_low, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, null);
|
||
|
|
return ((&this._lanes) as *simd<L[N / 2]>)[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The second half of the vector's lanes, as a vector of half as many.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @returns The half.
|
||
|
|
pub def simd<L[N]>::high(&this): simd<L[N / 2]> {
|
||
|
|
var made: simd<L[N / 2]>;
|
||
|
|
return made if lanes_also::<L, lanes_high, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, null);
|
||
|
|
return ((&this._lanes) as *simd<L[N / 2]>)[1];
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The vector of two vectors' lanes, the first's first: of two of 16 bytes, one
|
||
|
|
#! of 32.
|
||
|
|
#! @param low The first half.
|
||
|
|
#! @param high The second.
|
||
|
|
#! @returns The vector.
|
||
|
|
pub def simd<L[N]>::join(low: simd<L[N]>, high: simd<L[N]>): simd<L[N * 2]> {
|
||
|
|
var made: simd<L[N * 2]>;
|
||
|
|
return made if lanes_also::<L, lanes_join, N>((&mut made._lanes) as *mut u8, (&low._lanes) as *u8, (&high._lanes) as *u8);
|
||
|
|
val halves := (&mut made._lanes) as *mut simd<L[N]>;
|
||
|
|
halves[0] = low;
|
||
|
|
halves[1] = high;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The first half of the vector's lanes, each converted to a type twice as
|
||
|
|
#! wide, as `as` converts it: an integer to one of twice the bits, keeping its
|
||
|
|
#! value, or an `f32` to an `f64`.
|
||
|
|
#! @tparam M The type of each lane of what it gives.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @returns The vector, of half as many lanes, as many bytes.
|
||
|
|
pub def simd<L[N]>::widened<M>(&this): simd<M[N / 2]> where M: numeric, ordered, zeroable {
|
||
|
|
if const size_of::<M>() != 2 * size_of::<L>() {
|
||
|
|
compile_error("`widened` gives lanes twice as wide as the vector's.");
|
||
|
|
}
|
||
|
|
var made: simd<M[N / 2]>;
|
||
|
|
return made if lanes_to::<L, M, lanes_widen, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, null);
|
||
|
|
val a: L[N] = lanes_read(this);
|
||
|
|
var r: M[N / 2];
|
||
|
|
for i in 0..N / 2 {
|
||
|
|
r[i] = a[i] as M;
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The second half of the vector's lanes, each converted to a type twice as
|
||
|
|
#! wide, as `widened` converts the first.
|
||
|
|
#! @tparam M The type of each lane of what it gives.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @returns The vector, of half as many lanes, as many bytes.
|
||
|
|
pub def simd<L[N]>::widened_high<M>(&this): simd<M[N / 2]> where M: numeric, ordered, zeroable {
|
||
|
|
if const size_of::<M>() != 2 * size_of::<L>() {
|
||
|
|
compile_error("`widened_high` gives lanes twice as wide as the vector's.");
|
||
|
|
}
|
||
|
|
var made: simd<M[N / 2]>;
|
||
|
|
return made if lanes_to::<L, M, lanes_widen_high, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, null);
|
||
|
|
val a: L[N] = lanes_read(this);
|
||
|
|
var r: M[N / 2];
|
||
|
|
for i in 0..N / 2 {
|
||
|
|
r[i] = a[N / 2 + i] as M;
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The lanes of the vector and another, this one's first, each converted to a
|
||
|
|
#! type half as wide: an integer to one of half the bits, held at its least or
|
||
|
|
#! greatest where it is past them, as packing saturates it, or an `f64` to the
|
||
|
|
#! `f32` `as` rounds it to.
|
||
|
|
#! @tparam M The type of each lane of what it gives.
|
||
|
|
#! @param this The first vector.
|
||
|
|
#! @param other The second.
|
||
|
|
#! @returns The vector, of twice as many lanes, as many bytes as each.
|
||
|
|
pub def simd<L[N]>::narrowed<M>(&this, other: &simd<L[N]>): simd<M[N * 2]> where M: numeric, ordered, zeroable {
|
||
|
|
if const 2 * size_of::<M>() != size_of::<L>() {
|
||
|
|
compile_error("`narrowed` gives lanes half as wide as the vector's.");
|
||
|
|
}
|
||
|
|
var made: simd<M[N * 2]>;
|
||
|
|
return made if lanes_to::<L, M, lanes_narrow, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8);
|
||
|
|
val a: L[N] = lanes_read(this);
|
||
|
|
val b: L[N] = lanes_read(other);
|
||
|
|
var r: M[N * 2];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = saturated::<L, M>(a[i]);
|
||
|
|
r[N + i] = saturated::<L, M>(b[i]);
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The lanes a view holds at the places another vector names: lane `i` is
|
||
|
|
#! `from[at[i]]`, which AVX2 reads at once.
|
||
|
|
#! @param from The view.
|
||
|
|
#! @param at Where each lane is read from.
|
||
|
|
#! @returns The vector.
|
||
|
|
#! @remarks A place below zero or past the view's end stops a checked program,
|
||
|
|
#! as indexing does.
|
||
|
|
pub def simd<L[N]>::gather(from: span<L>, at: simd<i32[N]>): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if lanes_have::<L, lanes_gather, N>() {
|
||
|
|
# In a checked program, each place below the length, as an unsigned number,
|
||
|
|
# so one below zero is past it too, or the lanes are read one at a time,
|
||
|
|
# each place checked as an index is.
|
||
|
|
val limit: u32 = from.length < 0xFFFFFFFF ? from.length as u32 : 0xFFFFFFFF;
|
||
|
|
if !checking() || at.bits::<u32, N>().less(&simd::<u32[N]>::splat(limit)).all() {
|
||
|
|
val base := from._dat;
|
||
|
|
lanes_apply::<L, lanes_gather, N>((&mut made._lanes) as *mut u8, (&base) as *u8, (&at._lanes) as *u8, null);
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
val places: i32[N] = at.lanes();
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = from[places[i] as u64];
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Writes the vector's lanes into a view at the places another vector names:
|
||
|
|
#! lane `i` to `into[at[i]]`, a later lane's lasting where two places are one.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @param into The view.
|
||
|
|
#! @param at Where each lane is written.
|
||
|
|
#! @remarks A place below zero or past the view's end stops the program, as
|
||
|
|
#! indexing does, before any lane is written.
|
||
|
|
pub def simd<L[N]>::scatter(&this, into: slice<L>, at: simd<i32[N]>): void {
|
||
|
|
val places: i32[N] = at.lanes();
|
||
|
|
for i in 0..N {
|
||
|
|
_ = into[places[i] as u64];
|
||
|
|
}
|
||
|
|
val a: L[N] = lanes_read(this);
|
||
|
|
for i in 0..N {
|
||
|
|
into[places[i] as u64] = a[i];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# Arithmetic, which the operators call.
|
||
|
|
|
||
|
|
#! `a + b`, lane by lane. Integers wrap, as their own `+` does.
|
||
|
|
#! @param this The left vector.
|
||
|
|
#! @param other The right one.
|
||
|
|
#! @returns The sums.
|
||
|
|
pub def simd<L[N]>::add(&this, other: &simd<L[N]>): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if !lanes_done::<L, lanes_add, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
val b: L[N] = other._lanes;
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = a[i] + b[i];
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a - b`, lane by lane. Integers wrap, as their own `-` does.
|
||
|
|
#! @param this The left vector.
|
||
|
|
#! @param other The right one.
|
||
|
|
#! @returns The differences.
|
||
|
|
pub def simd<L[N]>::sub(&this, other: &simd<L[N]>): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if !lanes_done::<L, lanes_sub, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
val b: L[N] = other._lanes;
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = a[i] - b[i];
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a * b`, lane by lane. Integers wrap, as their own `*` does.
|
||
|
|
#! @param this The left vector.
|
||
|
|
#! @param other The right one.
|
||
|
|
#! @returns The products.
|
||
|
|
pub def simd<L[N]>::mul(&this, other: &simd<L[N]>): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if !lanes_done::<L, lanes_mul, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
val b: L[N] = other._lanes;
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = a[i] * b[i];
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a / b`, lane by lane. An integer lane divided by zero stops the program, as
|
||
|
|
#! dividing the integer does, and has no instruction, so integers are divided a
|
||
|
|
#! lane at a time.
|
||
|
|
#! @param this The left vector.
|
||
|
|
#! @param other The right one.
|
||
|
|
#! @returns The quotients.
|
||
|
|
pub def simd<L[N]>::div(&this, other: &simd<L[N]>): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if !lanes_done::<L, lanes_div, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
val b: L[N] = other._lanes;
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = a[i] / b[i];
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a % b`, lane by lane, as the number's own `%` gives it, a lane at a time.
|
||
|
|
#! @param this The left vector.
|
||
|
|
#! @param other The right one.
|
||
|
|
#! @returns The remainders.
|
||
|
|
pub def simd<L[N]>::rem(&this, other: &simd<L[N]>): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
val a: L[N] = lanes_read(this);
|
||
|
|
val b: L[N] = lanes_read(other);
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = a[i] % b[i];
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `-a`, lane by lane: a real's sign flipped, and an integer wrapping.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @returns The negations.
|
||
|
|
pub def simd<L[N]>::negate(&this): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if !lanes_done::<L, lanes_negate, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, null, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = -a[i];
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a & b`, the bits both integer lanes have.
|
||
|
|
#! @param this The left vector.
|
||
|
|
#! @param other The right one.
|
||
|
|
#! @returns The vector.
|
||
|
|
pub def simd<L[N]>::bit_and(&this, other: &simd<L[N]>): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if const L is integer {
|
||
|
|
if !lanes_done::<L, lanes_and, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
val b: L[N] = other._lanes;
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = a[i] & b[i];
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
compile_error("`&` combines the bits of integer lanes, and these are reals.");
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a | b`, the bits either integer lane has.
|
||
|
|
#! @param this The left vector.
|
||
|
|
#! @param other The right one.
|
||
|
|
#! @returns The vector.
|
||
|
|
pub def simd<L[N]>::bit_or(&this, other: &simd<L[N]>): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if const L is integer {
|
||
|
|
if !lanes_done::<L, lanes_or, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
val b: L[N] = other._lanes;
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = a[i] | b[i];
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
compile_error("`|` combines the bits of integer lanes, and these are reals.");
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a ^ b`, the bits one integer lane has and the other does not.
|
||
|
|
#! @param this The left vector.
|
||
|
|
#! @param other The right one.
|
||
|
|
#! @returns The vector.
|
||
|
|
pub def simd<L[N]>::bit_xor(&this, other: &simd<L[N]>): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if const L is integer {
|
||
|
|
if !lanes_done::<L, lanes_xor, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
val b: L[N] = other._lanes;
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = a[i] ^ b[i];
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
compile_error("`^` combines the bits of integer lanes, and these are reals.");
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `~a`, each integer lane's bits flipped.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @returns The vector.
|
||
|
|
pub def simd<L[N]>::bit_not(&this): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if const L is integer {
|
||
|
|
if !lanes_done::<L, lanes_not, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, null, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = ~a[i];
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
compile_error("`~` flips the bits of integer lanes, and these are reals.");
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a << n`, each integer lane shifted toward its top by as many bits, as the
|
||
|
|
#! integer's own `<<` shifts it: by its width or more gives 0.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @param by How many bits.
|
||
|
|
#! @returns The vector.
|
||
|
|
pub def simd<L[N]>::shift_left(&this, by: &u32): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if const L is integer {
|
||
|
|
val count: u32 = by;
|
||
|
|
if !lanes_done::<L, lanes_shift_left, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&count) as *u8, null, 16, 0) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = a[i] << count;
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
compile_error("`<<` shifts the bits of integer lanes, and these are reals.");
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `a >> n`, each integer lane shifted toward its bottom by as many bits, as the
|
||
|
|
#! integer's own `>>` shifts it: a signed one keeps its sign.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @param by How many bits.
|
||
|
|
#! @returns The vector.
|
||
|
|
pub def simd<L[N]>::shift_right(&this, by: &u32): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if const L is integer {
|
||
|
|
val count: u32 = by;
|
||
|
|
if !lanes_done::<L, lanes_shift_right, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&count) as *u8, null, 16, 0) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = a[i] >> count;
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
compile_error("`>>` shifts the bits of integer lanes, and these are reals.");
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether every lane of one vector equals that of another, as `==` asks.
|
||
|
|
#! @param this The one vector.
|
||
|
|
#! @param other The other.
|
||
|
|
#! @returns Whether they do.
|
||
|
|
pub def simd<L[N]>::equals(&this, other: &simd<L[N]>): bool {
|
||
|
|
return this.equal(other).all();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# Comparing lanes.
|
||
|
|
|
||
|
|
#! Which lanes equal those of another vector. A NaN equals nothing.
|
||
|
|
#! @param this The left vector.
|
||
|
|
#! @param other The right one.
|
||
|
|
#! @returns The mask.
|
||
|
|
pub def simd<L[N]>::equal(&this, other: &simd<L[N]>): mask<L[N]> {
|
||
|
|
var made: mask<L[N]>;
|
||
|
|
if !lanes_done::<L, lanes_equal, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
val b: L[N] = other._lanes;
|
||
|
|
var bits: u32 = 0;
|
||
|
|
for i in 0..N {
|
||
|
|
if a[i] == b[i] {
|
||
|
|
bits = bits | ((1 as u32) << i);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
made = mask_of::<L, N>(bits);
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Which lanes do not equal those of another vector. A NaN equals nothing.
|
||
|
|
#! @param this The left vector.
|
||
|
|
#! @param other The right one.
|
||
|
|
#! @returns The mask.
|
||
|
|
pub def simd<L[N]>::not_equal(&this, other: &simd<L[N]>): mask<L[N]> {
|
||
|
|
var made: mask<L[N]>;
|
||
|
|
if !lanes_done::<L, lanes_not_equal, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
val b: L[N] = other._lanes;
|
||
|
|
var bits: u32 = 0;
|
||
|
|
for i in 0..N {
|
||
|
|
if a[i] != b[i] {
|
||
|
|
bits = bits | ((1 as u32) << i);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
made = mask_of::<L, N>(bits);
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Which lanes are below those of another vector.
|
||
|
|
#! @param this The left vector.
|
||
|
|
#! @param other The right one.
|
||
|
|
#! @returns The mask.
|
||
|
|
pub def simd<L[N]>::less(&this, other: &simd<L[N]>): mask<L[N]> {
|
||
|
|
var made: mask<L[N]>;
|
||
|
|
if !lanes_done::<L, lanes_less, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
val b: L[N] = other._lanes;
|
||
|
|
var bits: u32 = 0;
|
||
|
|
for i in 0..N {
|
||
|
|
if a[i] < b[i] {
|
||
|
|
bits = bits | ((1 as u32) << i);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
made = mask_of::<L, N>(bits);
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Which lanes are not above those of another vector.
|
||
|
|
#! @param this The left vector.
|
||
|
|
#! @param other The right one.
|
||
|
|
#! @returns The mask.
|
||
|
|
pub def simd<L[N]>::less_equal(&this, other: &simd<L[N]>): mask<L[N]> {
|
||
|
|
var made: mask<L[N]>;
|
||
|
|
if !lanes_done::<L, lanes_less_equal, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
val b: L[N] = other._lanes;
|
||
|
|
var bits: u32 = 0;
|
||
|
|
for i in 0..N {
|
||
|
|
if a[i] <= b[i] {
|
||
|
|
bits = bits | ((1 as u32) << i);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
made = mask_of::<L, N>(bits);
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Which lanes are above those of another vector.
|
||
|
|
#! @param this The left vector.
|
||
|
|
#! @param other The right one.
|
||
|
|
#! @returns The mask.
|
||
|
|
pub def simd<L[N]>::greater(&this, other: &simd<L[N]>): mask<L[N]> {
|
||
|
|
var made: mask<L[N]>;
|
||
|
|
if !lanes_done::<L, lanes_greater, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
val b: L[N] = other._lanes;
|
||
|
|
var bits: u32 = 0;
|
||
|
|
for i in 0..N {
|
||
|
|
if a[i] > b[i] {
|
||
|
|
bits = bits | ((1 as u32) << i);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
made = mask_of::<L, N>(bits);
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Which lanes are not below those of another vector.
|
||
|
|
#! @param this The left vector.
|
||
|
|
#! @param other The right one.
|
||
|
|
#! @returns The mask.
|
||
|
|
pub def simd<L[N]>::greater_equal(&this, other: &simd<L[N]>): mask<L[N]> {
|
||
|
|
var made: mask<L[N]>;
|
||
|
|
if !lanes_done::<L, lanes_greater_equal, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
val b: L[N] = other._lanes;
|
||
|
|
var bits: u32 = 0;
|
||
|
|
for i in 0..N {
|
||
|
|
if a[i] >= b[i] {
|
||
|
|
bits = bits | ((1 as u32) << i);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
made = mask_of::<L, N>(bits);
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# Functions of lanes.
|
||
|
|
|
||
|
|
#! The lesser of each pair of lanes: of reals, IEEE 754's `minimum`, NaN where
|
||
|
|
#! either is NaN, and `-0` below `0`, as `math::min` gives it.
|
||
|
|
#! @param this The one vector.
|
||
|
|
#! @param other The other.
|
||
|
|
#! @returns The vector.
|
||
|
|
pub def simd<L[N]>::min(&this, other: &simd<L[N]>): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if !lanes_done::<L, lanes_min, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
val b: L[N] = other._lanes;
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = lane_min(a[i], b[i]);
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The greater of each pair of lanes: of reals, IEEE 754's `maximum`, NaN where
|
||
|
|
#! either is NaN, and `0` above `-0`, as `math::max` gives it.
|
||
|
|
#! @param this The one vector.
|
||
|
|
#! @param other The other.
|
||
|
|
#! @returns The vector.
|
||
|
|
pub def simd<L[N]>::max(&this, other: &simd<L[N]>): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if !lanes_done::<L, lanes_max, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
val b: L[N] = other._lanes;
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = lane_max(a[i], b[i]);
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Each lane's distance from zero: a real's sign bit cleared, a signed integer
|
||
|
|
#! negated where it is below zero, the most negative staying itself, and an
|
||
|
|
#! unsigned integer as it is.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @returns The vector.
|
||
|
|
pub def simd<L[N]>::abs(&this): simd<L[N]> {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if !lanes_done::<L, lanes_abs, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, null, null, 16, 16) {
|
||
|
|
val a: L[N] = this._lanes;
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = lane_abs(a[i]);
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Across the lanes.
|
||
|
|
|
||
|
|
#! The lanes added together, lane 0 first: `((l0 + l1) + l2) + ...`.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @returns The sum.
|
||
|
|
pub def simd<L[N]>::sum(&this): L {
|
||
|
|
var worked: L;
|
||
|
|
return worked if lanes_whole::<L, lanes_sum, N>((&mut worked) as *mut u8, (&this._lanes) as *u8);
|
||
|
|
val a: L[N] = lanes_read(this);
|
||
|
|
var total: L = a[0];
|
||
|
|
for i in 1..N {
|
||
|
|
total = total + a[i];
|
||
|
|
}
|
||
|
|
return total;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The lanes multiplied together, lane 0 first.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @returns The product.
|
||
|
|
pub def simd<L[N]>::product(&this): L {
|
||
|
|
var worked: L;
|
||
|
|
return worked if lanes_whole::<L, lanes_product, N>((&mut worked) as *mut u8, (&this._lanes) as *u8);
|
||
|
|
val a: L[N] = lanes_read(this);
|
||
|
|
var total: L = a[0];
|
||
|
|
for i in 1..N {
|
||
|
|
total = total * a[i];
|
||
|
|
}
|
||
|
|
return total;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The least lane, as `min` gives it of each pair, lane 0 first.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @returns The lane.
|
||
|
|
pub def simd<L[N]>::smallest(&this): L {
|
||
|
|
var worked: L;
|
||
|
|
return worked if lanes_whole::<L, lanes_smallest, N>((&mut worked) as *mut u8, (&this._lanes) as *u8);
|
||
|
|
val a: L[N] = lanes_read(this);
|
||
|
|
var least: L = a[0];
|
||
|
|
for i in 1..N {
|
||
|
|
val lane: L = a[i];
|
||
|
|
least = lane_min(least, lane);
|
||
|
|
}
|
||
|
|
return least;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The greatest lane, as `max` gives it of each pair, lane 0 first.
|
||
|
|
#! @param this The vector.
|
||
|
|
#! @returns The lane.
|
||
|
|
pub def simd<L[N]>::largest(&this): L {
|
||
|
|
var worked: L;
|
||
|
|
return worked if lanes_whole::<L, lanes_largest, N>((&mut worked) as *mut u8, (&this._lanes) as *u8);
|
||
|
|
val a: L[N] = lanes_read(this);
|
||
|
|
var most: L = a[0];
|
||
|
|
for i in 1..N {
|
||
|
|
val lane: L = a[i];
|
||
|
|
most = lane_max(most, lane);
|
||
|
|
}
|
||
|
|
return most;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# Masks.
|
||
|
|
|
||
|
|
#! Whether a lane of the mask is set.
|
||
|
|
#! @param this The mask.
|
||
|
|
#! @param lane The lane, below how many there are.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
#! @remarks A lane past the last stops the program.
|
||
|
|
pub def mask<L[N]>::has(&this, lane: u64): bool {
|
||
|
|
if lane >= N {
|
||
|
|
val held: L[N] = this._lanes;
|
||
|
|
_ = held[lane];
|
||
|
|
}
|
||
|
|
return ((this.bits() >> lane) & 1) != 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! One bit for each lane of the mask, set where the lane is: lane 0 the lowest.
|
||
|
|
#! @param this The mask.
|
||
|
|
#! @returns The bits.
|
||
|
|
pub def mask<L[N]>::bits(&this): u32 {
|
||
|
|
var made: u32 = 0;
|
||
|
|
if lanes_have::<L, lanes_bits, N>() {
|
||
|
|
lanes_apply::<L, lanes_bits, N>((&mut made) as *mut u8, (&this._lanes) as *u8, null, null);
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
if const N * size_of::<L>() >= 32 {
|
||
|
|
# A half, or a quarter, of the lanes at a time.
|
||
|
|
val half := N * size_of::<L>() / 2;
|
||
|
|
if lanes_have::<L, lanes_bits, N / 2>() {
|
||
|
|
var high: u32 = 0;
|
||
|
|
lanes_apply::<L, lanes_bits, N / 2>((&mut made) as *mut u8, (&this._lanes) as *u8, null, null);
|
||
|
|
lanes_apply::<L, lanes_bits, N / 2>((&mut high) as *mut u8, ((&this._lanes) as *u8) + half, null, null);
|
||
|
|
return made | (high << (N / 2));
|
||
|
|
}
|
||
|
|
if const N * size_of::<L>() == 64 {
|
||
|
|
if lanes_have::<L, lanes_bits, N / 4>() {
|
||
|
|
for k in 0..4u {
|
||
|
|
var part: u32 = 0;
|
||
|
|
lanes_apply::<L, lanes_bits, N / 4>((&mut part) as *mut u8, ((&this._lanes) as *u8) + 16 * k, null, null);
|
||
|
|
made = made | (part << (N / 4 * k) as u32);
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
val held: L[N] = this._lanes;
|
||
|
|
val bytes := (&held) as *u8;
|
||
|
|
for i in 0..N {
|
||
|
|
if bytes[i * size_of::<L>()] != 0 {
|
||
|
|
made = made | ((1 as u32) << i);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether any lane of the mask is set.
|
||
|
|
#! @param this The mask.
|
||
|
|
#! @returns Whether one is.
|
||
|
|
pub def mask<L[N]>::any(&this): bool {
|
||
|
|
return this.bits() != 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether every lane of the mask is set.
|
||
|
|
#! @param this The mask.
|
||
|
|
#! @returns Whether each is.
|
||
|
|
pub def mask<L[N]>::all(&this): bool {
|
||
|
|
return this.bits() as u64 == ((1 as u64) << N) - 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether no lane of the mask is set.
|
||
|
|
#! @param this The mask.
|
||
|
|
#! @returns Whether none is.
|
||
|
|
pub def mask<L[N]>::none(&this): bool {
|
||
|
|
return this.bits() == 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many lanes of the mask are set.
|
||
|
|
#! @param this The mask.
|
||
|
|
#! @returns How many.
|
||
|
|
pub def mask<L[N]>::count(&this): u32 {
|
||
|
|
return count_ones(this.bits());
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The lowest lane of the mask that is set.
|
||
|
|
#! @param this The mask.
|
||
|
|
#! @returns The lane, or nothing where none is.
|
||
|
|
pub def mask<L[N]>::first(&this): maybe<u32> {
|
||
|
|
val bits := this.bits();
|
||
|
|
return nothing {} if bits == 0;
|
||
|
|
return trailing_zeros(bits);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The lanes both masks have set, as `a & b` asks.
|
||
|
|
#! @param this The one mask.
|
||
|
|
#! @param other The other.
|
||
|
|
#! @returns The mask.
|
||
|
|
pub def mask<L[N]>::bit_and(&this, other: &mask<L[N]>): mask<L[N]> {
|
||
|
|
var made: mask<L[N]>;
|
||
|
|
return made if lanes_done::<L, lanes_and, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16);
|
||
|
|
return mask_of::<L, N>(this.bits() & other.bits());
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The lanes either mask has set, as `a | b` asks.
|
||
|
|
#! @param this The one mask.
|
||
|
|
#! @param other The other.
|
||
|
|
#! @returns The mask.
|
||
|
|
pub def mask<L[N]>::bit_or(&this, other: &mask<L[N]>): mask<L[N]> {
|
||
|
|
var made: mask<L[N]>;
|
||
|
|
return made if lanes_done::<L, lanes_or, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16);
|
||
|
|
return mask_of::<L, N>(this.bits() | other.bits());
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The lanes one mask has set and the other does not, as `a ^ b` asks.
|
||
|
|
#! @param this The one mask.
|
||
|
|
#! @param other The other.
|
||
|
|
#! @returns The mask.
|
||
|
|
pub def mask<L[N]>::bit_xor(&this, other: &mask<L[N]>): mask<L[N]> {
|
||
|
|
var made: mask<L[N]>;
|
||
|
|
return made if lanes_done::<L, lanes_xor, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16);
|
||
|
|
return mask_of::<L, N>(this.bits() ^ other.bits());
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The lanes the mask does not have set, as `~a` asks.
|
||
|
|
#! @param this The mask.
|
||
|
|
#! @returns The mask.
|
||
|
|
pub def mask<L[N]>::bit_not(&this): mask<L[N]> {
|
||
|
|
var made: mask<L[N]>;
|
||
|
|
return made if lanes_done::<L, lanes_not, N>((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, null, null, 16, 16);
|
||
|
|
return mask_of::<L, N>(~this.bits());
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether two masks have the same lanes set, as `==` asks.
|
||
|
|
#! @param this The one mask.
|
||
|
|
#! @param other The other.
|
||
|
|
#! @returns Whether they do.
|
||
|
|
pub def mask<L[N]>::equals(&this, other: &mask<L[N]>): bool {
|
||
|
|
return this.bits() == other.bits();
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Each lane taken from one vector where the mask has it set, and from the other
|
||
|
|
#! where it does not.
|
||
|
|
#! @tparam L The type of each lane.
|
||
|
|
#! @tparam N How many lanes there are.
|
||
|
|
#! @param which The mask.
|
||
|
|
#! @param then The lanes taken where it is set.
|
||
|
|
#! @param otherwise The lanes taken where it is not.
|
||
|
|
#! @returns The vector.
|
||
|
|
pub def select<L, const N: u64>(which: mask<L[N]>, then: simd<L[N]>, otherwise: simd<L[N]>): simd<L[N]>
|
||
|
|
where L: numeric, ordered, zeroable {
|
||
|
|
var made: simd<L[N]>;
|
||
|
|
if !lanes_done::<L, lanes_select, N>((&mut made._lanes) as *mut u8, (&which._lanes) as *u8, (&then._lanes) as *u8, (&otherwise._lanes) as *u8, 16, 16) {
|
||
|
|
val bits := which.bits();
|
||
|
|
val b: L[N] = then._lanes;
|
||
|
|
val c: L[N] = otherwise._lanes;
|
||
|
|
var r: L[N];
|
||
|
|
for i in 0..N {
|
||
|
|
r[i] = ((bits >> i) & 1) != 0 ? b[i] : c[i];
|
||
|
|
}
|
||
|
|
made._lanes = r;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# How the lanes are worked.
|
||
|
|
|
||
|
|
# The mask whose lanes are set where the bits are, lane 0 the lowest: every bit
|
||
|
|
# of a lane set, or none.
|
||
|
|
def mask_of<L, const N: u64>(bits: u32): mask<L[N]> where L: numeric, ordered, zeroable {
|
||
|
|
var lanes: L[N];
|
||
|
|
val at := (&mut lanes) as *mut u8;
|
||
|
|
for i in 0..N {
|
||
|
|
if ((bits >> i) & 1) != 0 {
|
||
|
|
for k in 0..size_of::<L>() {
|
||
|
|
at[i * size_of::<L>() + k] = 0xFF;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return mask::<L[N]> { _lanes = lanes };
|
||
|
|
}
|
||
|
|
|
||
|
|
# Whether converting a vector's lanes to `M` is one the instructions work: any
|
||
|
|
# but real lanes to integers, which they convert only where each is within
|
||
|
|
# `i32`'s range, and not NaN, where `as` and they agree.
|
||
|
|
def within_integers<L, M, const N: u64>(v: &simd<L[N]>): bool where L: numeric, ordered, zeroable where M: numeric, ordered, zeroable {
|
||
|
|
if const L is real {
|
||
|
|
if const M is integer {
|
||
|
|
return v.abs().less(&simd::<L[N]>::splat(2147483648 as L)).all();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A lane converted to a type half as wide, held at the type's least or greatest
|
||
|
|
# where it is past them, or of a real, rounded as `as` rounds it.
|
||
|
|
def saturated<L, M>(x: L): M where L: numeric, ordered, zeroable where M: numeric, ordered, zeroable {
|
||
|
|
if const L is integer {
|
||
|
|
if const M is integer {
|
||
|
|
val top := (size_of::<M>() * 8 - 1) as u32;
|
||
|
|
val unsigned := ~(0 as M) > (0 as M);
|
||
|
|
val most: M = unsigned ? ~(0 as M) : ((1 as M) << top) - (1 as M);
|
||
|
|
return most if x > most as L;
|
||
|
|
if !(~(0 as L) > (0 as L)) {
|
||
|
|
val least: M = unsigned ? 0 as M : (1 as M) << top;
|
||
|
|
return least if x < least as L;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return x as M;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The lanes of a vector as an array, which what works them one at a time reads:
|
||
|
|
# moved whole where the processor's instructions move them, so a vector held in
|
||
|
|
# a register is read from it whole, 32 bytes as one.
|
||
|
|
def lanes_read<L, const N: u64>(v: &simd<L[N]>): L[N] where L: numeric, ordered, zeroable {
|
||
|
|
var made: L[N];
|
||
|
|
if !lanes_done::<L, lanes_copy, N>((&mut made) as *mut u8, (&v._lanes) as *u8, null, null, 16, 16) {
|
||
|
|
made = v._lanes;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A lane's distance from zero: an unsigned integer is its own, and a real's sign
|
||
|
|
# bit is cleared, so `-0` gives `0`, and NaN a NaN.
|
||
|
|
def lane_abs<L>(x: L): L where L: numeric, ordered, zeroable {
|
||
|
|
if const L is integer {
|
||
|
|
return x if ~(0 as L) > (0 as L);
|
||
|
|
return x < -x ? -x : x;
|
||
|
|
}
|
||
|
|
var held := x;
|
||
|
|
if const size_of::<L>() == 4 {
|
||
|
|
((&mut held) as *mut u32)[0] = ((&mut held) as *mut u32)[0] & 0x7FFFFFFF;
|
||
|
|
} else {
|
||
|
|
((&mut held) as *mut u64)[0] = ((&mut held) as *mut u64)[0] & 0x7FFFFFFFFFFFFFFF;
|
||
|
|
}
|
||
|
|
return held;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The lesser of two lanes: of reals, NaN where either is NaN, and `-0` below `0`.
|
||
|
|
def lane_min<L>(a: L, b: L): L where L: numeric, ordered, zeroable {
|
||
|
|
if const L is real {
|
||
|
|
return a + b if a != a || b != b;
|
||
|
|
if a == b {
|
||
|
|
return lane_negative(a) ? a : b;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return b < a ? b : a;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The greater of two lanes: of reals, NaN where either is NaN, and `0` above `-0`.
|
||
|
|
def lane_max<L>(a: L, b: L): L where L: numeric, ordered, zeroable {
|
||
|
|
if const L is real {
|
||
|
|
return a + b if a != a || b != b;
|
||
|
|
if a == b {
|
||
|
|
return lane_negative(a) ? b : a;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return a < b ? b : a;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Whether a real lane's sign bit is set, as `-0`'s is.
|
||
|
|
def lane_negative<L>(x: L): bool where L: numeric, ordered, zeroable {
|
||
|
|
var held := x;
|
||
|
|
if const size_of::<L>() == 4 {
|
||
|
|
return (((&mut held) as *mut u32)[0] >> 31) != 0;
|
||
|
|
}
|
||
|
|
return (((&mut held) as *mut u64)[0] >> 63) != 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Works an operation out with the vector instructions of the processor running
|
||
|
|
# the program, where it has them: the whole vector at once, or a part at a time
|
||
|
|
# where it has them only for fewer bytes, 32 as two halves of 16, and 64 as two
|
||
|
|
# of 32 or four of 16. The first and second operands' parts are `a_half` and
|
||
|
|
# `b_half` bytes on for each 16 bytes of the vector, which a lane splatted or a
|
||
|
|
# shift's count, the same for every part, is not.
|
||
|
|
#
|
||
|
|
# Where it gives false, the caller works the lanes one at a time, reading and
|
||
|
|
# writing each vector whole, so a vector read and written only so is a value
|
||
|
|
# held in a register wherever its cell is promoted.
|
||
|
|
def lanes_done<L, const OP: u32, const N: u64>(into: *mut u8, a: *u8, b: *u8, c: *u8, a_half: u64, b_half: u64): bool {
|
||
|
|
if lanes_have::<L, OP, N>() {
|
||
|
|
lanes_apply::<L, OP, N>(into, a, b, c);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
if const N * size_of::<L>() == 32 {
|
||
|
|
if lanes_have::<L, OP, N / 2>() {
|
||
|
|
lanes_apply::<L, OP, N / 2>(into, a, b, c);
|
||
|
|
lanes_apply::<L, OP, N / 2>(into + 16, a + a_half, b == null ? b : b + b_half, c == null ? c : c + 16);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if const N * size_of::<L>() == 64 {
|
||
|
|
if lanes_have::<L, OP, N / 2>() {
|
||
|
|
lanes_apply::<L, OP, N / 2>(into, a, b, c);
|
||
|
|
lanes_apply::<L, OP, N / 2>(into + 32, a + 2 * a_half, b == null ? b : b + 2 * b_half, c == null ? c : c + 32);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
if lanes_have::<L, OP, N / 4>() {
|
||
|
|
lanes_apply::<L, OP, N / 4>(into, a, b, c);
|
||
|
|
lanes_apply::<L, OP, N / 4>(into + 16, a + a_half, b == null ? b : b + b_half, c == null ? c : c + 16);
|
||
|
|
lanes_apply::<L, OP, N / 4>(into + 32, a + 2 * a_half, b == null ? b : b + 2 * b_half, c == null ? c : c + 32);
|
||
|
|
lanes_apply::<L, OP, N / 4>(into + 48, a + 3 * a_half, b == null ? b : b + 3 * b_half, c == null ? c : c + 48);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Works an operation out with the processor's instructions, where it has them,
|
||
|
|
# the operation's result of another size, or of two vectors' lanes: never a
|
||
|
|
# half at a time.
|
||
|
|
def lanes_also<L, const OP: u32, const N: u64>(into: *mut u8, a: *u8, b: *u8): bool {
|
||
|
|
return false if !lanes_have::<L, OP, N>();
|
||
|
|
lanes_apply::<L, OP, N>(into, a, b, null);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Works an operation giving lanes of another type, `M`, as `lanes_also` works
|
||
|
|
# one: its number with `M`'s type of lane above it, as the compiler numbers
|
||
|
|
# them, chosen while compiling.
|
||
|
|
def lanes_to<L, M, const OP: u32, const N: u64>(into: *mut u8, a: *u8, b: *u8): bool where M: numeric, ordered, zeroable {
|
||
|
|
if const M is i8 { return lanes_also::<L, OP, N>(into, a, b); }
|
||
|
|
if const M is u8 { return lanes_also::<L, OP | 0x100, N>(into, a, b); }
|
||
|
|
if const M is i16 { return lanes_also::<L, OP | 0x200, N>(into, a, b); }
|
||
|
|
if const M is u16 { return lanes_also::<L, OP | 0x300, N>(into, a, b); }
|
||
|
|
if const M is i32 { return lanes_also::<L, OP | 0x400, N>(into, a, b); }
|
||
|
|
if const M is u32 { return lanes_also::<L, OP | 0x500, N>(into, a, b); }
|
||
|
|
if const M is i64 { return lanes_also::<L, OP | 0x600, N>(into, a, b); }
|
||
|
|
if const M is u64 { return lanes_also::<L, OP | 0x700, N>(into, a, b); }
|
||
|
|
if const M is f32 { return lanes_also::<L, OP | 0x800, N>(into, a, b); }
|
||
|
|
if const M is f64 { return lanes_also::<L, OP | 0x900, N>(into, a, b); }
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Works out an operation of the whole vector at once, as working the lanes
|
||
|
|
# together does, where the processor has the instructions for it: never a half
|
||
|
|
# at a time, as the halves would have to be worked together after.
|
||
|
|
def lanes_whole<L, const OP: u32, const N: u64>(into: *mut u8, a: *u8): bool {
|
||
|
|
return false if !lanes_have::<L, OP, N>();
|
||
|
|
lanes_apply::<L, OP, N>(into, a, null, null);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The operations on lanes the compiler gives instructions for.
|
||
|
|
const lanes_add: u32 = 1;
|
||
|
|
const lanes_sub: u32 = 2;
|
||
|
|
const lanes_mul: u32 = 3;
|
||
|
|
const lanes_div: u32 = 4;
|
||
|
|
const lanes_and: u32 = 5;
|
||
|
|
const lanes_or: u32 = 6;
|
||
|
|
const lanes_xor: u32 = 7;
|
||
|
|
const lanes_negate: u32 = 8;
|
||
|
|
const lanes_not: u32 = 9;
|
||
|
|
const lanes_min: u32 = 10;
|
||
|
|
const lanes_max: u32 = 11;
|
||
|
|
const lanes_abs: u32 = 12;
|
||
|
|
const lanes_sqrt: u32 = 13;
|
||
|
|
const lanes_floor: u32 = 14;
|
||
|
|
const lanes_ceil: u32 = 15;
|
||
|
|
const lanes_trunc: u32 = 16;
|
||
|
|
const lanes_round_even: u32 = 17;
|
||
|
|
const lanes_equal: u32 = 18;
|
||
|
|
const lanes_not_equal: u32 = 19;
|
||
|
|
const lanes_less: u32 = 20;
|
||
|
|
const lanes_less_equal: u32 = 21;
|
||
|
|
const lanes_greater: u32 = 22;
|
||
|
|
const lanes_greater_equal: u32 = 23;
|
||
|
|
const lanes_select: u32 = 24; # `a` the mask, `b` taken where it is set, `c` where not.
|
||
|
|
const lanes_fma: u32 = 25; # `a * b + c`.
|
||
|
|
const lanes_shift_left: u32 = 26; # `b` the `u32` count.
|
||
|
|
const lanes_shift_right: u32 = 27;
|
||
|
|
const lanes_bits: u32 = 28; # `into` the `u32` of the lanes `a` has set.
|
||
|
|
const lanes_splat: u32 = 29; # The lane `a` points at, in every lane.
|
||
|
|
const lanes_copy: u32 = 30; # The lanes `a` points at, moved whole.
|
||
|
|
const lanes_sum: u32 = 32; # `into` the lanes added together, lane 0 first.
|
||
|
|
const lanes_product: u32 = 33; # `into` the lanes multiplied together, lane 0 first.
|
||
|
|
const lanes_smallest: u32 = 34; # `into` the least lane.
|
||
|
|
const lanes_largest: u32 = 35; # `into` the greatest lane.
|
||
|
|
const lanes_shuffle: u32 = 37; # `a`'s lanes in the order the bytes `b` points at name.
|
||
|
|
const lanes_convert: u32 = 38; # `a`'s lanes as the type above the number, as many.
|
||
|
|
const lanes_low: u32 = 39; # `a`'s first half.
|
||
|
|
const lanes_high: u32 = 40; # `a`'s second half.
|
||
|
|
const lanes_join: u32 = 41; # `a`'s lanes, then `b`'s.
|
||
|
|
const lanes_widen: u32 = 42; # `a`'s first half as the type above the number, twice as wide.
|
||
|
|
const lanes_widen_high: u32 = 43; # `a`'s second half so.
|
||
|
|
const lanes_narrow: u32 = 44; # `a`'s lanes, then `b`'s, as the type above the number, half as wide, saturated.
|
||
|
|
const lanes_gather: u32 = 45; # The lanes at the address `a` points at, at the 32-bit places `b` names.
|
||
|
|
|
||
|
|
#! Whether the program holds vectors in registers between their operations, as
|
||
|
|
#! an executable optimized with `-O2` does, so code working many numbers at once
|
||
|
|
#! with them outruns code working one at a time. The virtual machine and
|
||
|
|
#! constants work their lanes one at a time, and the C compiler's executables
|
||
|
|
#! move each through memory, where code for one number is faster. It is known
|
||
|
|
#! while compiling, so only the code it chooses is kept.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
@intrinsic pub def lanes_held(): bool;
|
||
|
|
|
||
|
|
# Whether the processor running the program has the instructions working `OP` on
|
||
|
|
# `N` lanes of `L`, which the program asked it once as it started. The virtual
|
||
|
|
# machine and constants have none.
|
||
|
|
@intrinsic def lanes_have<L, const OP: u32, const N: u64>(): bool;
|
||
|
|
|
||
|
|
# Works `OP` on `N` lanes of `L`, from the bytes `a`, `b`, and `c` into those of
|
||
|
|
# `into`, with the instructions `lanes_have` says the processor has.
|
||
|
|
@intrinsic def lanes_apply<L, const OP: u32, const N: u64>(into: *mut u8, a: *u8, b: *u8, c: *u8): void;
|