#! @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` is four `f32`s, 16 #! bytes, and `simd` 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` 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 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 where L: numeric, ordered, zeroable { var _lanes: L[N]; } pub type f32x4 = simd; pub type f32x8 = simd; pub type f64x2 = simd; pub type f64x4 = simd; pub type i8x16 = simd; pub type i8x32 = simd; pub type u8x16 = simd; pub type u8x32 = simd; pub type i16x8 = simd; pub type i16x16 = simd; pub type u16x8 = simd; pub type u16x16 = simd; pub type i32x4 = simd; pub type i32x8 = simd; pub type u32x4 = simd; pub type u32x8 = simd; pub type i64x2 = simd; pub type i64x4 = simd; pub type u64x2 = simd; pub type u64x4 = simd; pub type f32x16 = simd; pub type f64x8 = simd; pub type i16x32 = simd; pub type u16x32 = simd; pub type i32x16 = simd; pub type u32x16 = simd; pub type i64x8 = simd; pub type u64x8 = simd; # 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::of(lanes: L[N]): simd { return simd:: { _lanes = lanes }; } #! The vector holding a number in every lane. #! @param value The number. #! @returns The vector. pub def simd::splat(value: L): simd { var made: simd; val lane: L = value; if !lanes_done::((&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::load(from: span, at: u64): simd { _ = from[at]; _ = from[at + N - 1]; return ((from._dat + at) as *simd)[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::store(&this, into: slice, at: u64): void { _ = into[at]; _ = into[at + N - 1]; val copied: simd = this; ((into._dat + at) as *mut simd)[0] = copied; } #! The vector's numbers as an array, lane 0 first. #! @param this The vector. #! @returns The array. pub def simd::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::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::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::shuffle(&this, order: u8[N]): simd { if lanes_have::() { # 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)[0]; if !checking() || named.less(&simd::::splat(N as u8)).all() { var made: simd; lanes_apply::((&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:: { _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::convert(&this): simd where M: numeric, ordered, zeroable { var worked: simd; if within_integers::(this) { return worked if lanes_to::((&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:: { _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::bits(&this): simd where M: numeric, ordered, zeroable { if const K * size_of::() != N * size_of::() { compile_error("`bits` is asked for a vector of another size than the one it is called on."); } return ((&this._lanes) as *simd)[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::low(&this): simd { var made: simd; return made if lanes_also::((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, null); return ((&this._lanes) as *simd)[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::high(&this): simd { var made: simd; return made if lanes_also::((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, null); return ((&this._lanes) as *simd)[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::join(low: simd, high: simd): simd { var made: simd; return made if lanes_also::((&mut made._lanes) as *mut u8, (&low._lanes) as *u8, (&high._lanes) as *u8); val halves := (&mut made._lanes) as *mut simd; 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::widened(&this): simd where M: numeric, ordered, zeroable { if const size_of::() != 2 * size_of::() { compile_error("`widened` gives lanes twice as wide as the vector's."); } var made: simd; return made if lanes_to::((&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::widened_high(&this): simd where M: numeric, ordered, zeroable { if const size_of::() != 2 * size_of::() { compile_error("`widened_high` gives lanes twice as wide as the vector's."); } var made: simd; return made if lanes_to::((&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::narrowed(&this, other: &simd): simd where M: numeric, ordered, zeroable { if const 2 * size_of::() != size_of::() { compile_error("`narrowed` gives lanes half as wide as the vector's."); } var made: simd; return made if lanes_to::((&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::(a[i]); r[N + i] = saturated::(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::gather(from: span, at: simd): simd { var made: simd; if lanes_have::() { # 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::().less(&simd::::splat(limit)).all() { val base := from._dat; lanes_apply::((&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::scatter(&this, into: slice, at: simd): 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::add(&this, other: &simd): simd { var made: simd; if !lanes_done::((&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::sub(&this, other: &simd): simd { var made: simd; if !lanes_done::((&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::mul(&this, other: &simd): simd { var made: simd; if !lanes_done::((&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::div(&this, other: &simd): simd { var made: simd; if !lanes_done::((&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::rem(&this, other: &simd): simd { var made: simd; 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::negate(&this): simd { var made: simd; if !lanes_done::((&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::bit_and(&this, other: &simd): simd { var made: simd; if const L is integer { if !lanes_done::((&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::bit_or(&this, other: &simd): simd { var made: simd; if const L is integer { if !lanes_done::((&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::bit_xor(&this, other: &simd): simd { var made: simd; if const L is integer { if !lanes_done::((&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::bit_not(&this): simd { var made: simd; if const L is integer { if !lanes_done::((&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::shift_left(&this, by: &u32): simd { var made: simd; if const L is integer { val count: u32 = by; if !lanes_done::((&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::shift_right(&this, by: &u32): simd { var made: simd; if const L is integer { val count: u32 = by; if !lanes_done::((&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::equals(&this, other: &simd): 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::equal(&this, other: &simd): mask { var made: mask; if !lanes_done::((&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::(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::not_equal(&this, other: &simd): mask { var made: mask; if !lanes_done::((&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::(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::less(&this, other: &simd): mask { var made: mask; if !lanes_done::((&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::(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::less_equal(&this, other: &simd): mask { var made: mask; if !lanes_done::((&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::(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::greater(&this, other: &simd): mask { var made: mask; if !lanes_done::((&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::(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::greater_equal(&this, other: &simd): mask { var made: mask; if !lanes_done::((&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::(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::min(&this, other: &simd): simd { var made: simd; if !lanes_done::((&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::max(&this, other: &simd): simd { var made: simd; if !lanes_done::((&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::abs(&this): simd { var made: simd; if !lanes_done::((&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::sum(&this): L { var worked: L; return worked if lanes_whole::((&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::product(&this): L { var worked: L; return worked if lanes_whole::((&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::smallest(&this): L { var worked: L; return worked if lanes_whole::((&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::largest(&this): L { var worked: L; return worked if lanes_whole::((&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::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::bits(&this): u32 { var made: u32 = 0; if lanes_have::() { lanes_apply::((&mut made) as *mut u8, (&this._lanes) as *u8, null, null); return made; } if const N * size_of::() >= 32 { # A half, or a quarter, of the lanes at a time. val half := N * size_of::() / 2; if lanes_have::() { var high: u32 = 0; lanes_apply::((&mut made) as *mut u8, (&this._lanes) as *u8, null, null); lanes_apply::((&mut high) as *mut u8, ((&this._lanes) as *u8) + half, null, null); return made | (high << (N / 2)); } if const N * size_of::() == 64 { if lanes_have::() { for k in 0..4u { var part: u32 = 0; lanes_apply::((&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::()] != 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::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::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::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::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::first(&this): maybe { 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::bit_and(&this, other: &mask): mask { var made: mask; return made if lanes_done::((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16); return mask_of::(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::bit_or(&this, other: &mask): mask { var made: mask; return made if lanes_done::((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16); return mask_of::(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::bit_xor(&this, other: &mask): mask { var made: mask; return made if lanes_done::((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, (&other._lanes) as *u8, null, 16, 16); return mask_of::(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::bit_not(&this): mask { var made: mask; return made if lanes_done::((&mut made._lanes) as *mut u8, (&this._lanes) as *u8, null, null, 16, 16); return mask_of::(~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::equals(&this, other: &mask): 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(which: mask, then: simd, otherwise: simd): simd where L: numeric, ordered, zeroable { var made: simd; if !lanes_done::((&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(bits: u32): mask 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::() { at[i * size_of::() + k] = 0xFF; } } } return mask:: { _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(v: &simd): 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::::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(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::() * 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(v: &simd): L[N] where L: numeric, ordered, zeroable { var made: L[N]; if !lanes_done::((&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(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::() == 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(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(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(x: L): bool where L: numeric, ordered, zeroable { var held := x; if const size_of::() == 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(into: *mut u8, a: *u8, b: *u8, c: *u8, a_half: u64, b_half: u64): bool { if lanes_have::() { lanes_apply::(into, a, b, c); return true; } if const N * size_of::() == 32 { if lanes_have::() { lanes_apply::(into, a, b, c); lanes_apply::(into + 16, a + a_half, b == null ? b : b + b_half, c == null ? c : c + 16); return true; } } if const N * size_of::() == 64 { if lanes_have::() { lanes_apply::(into, a, b, c); lanes_apply::(into + 32, a + 2 * a_half, b == null ? b : b + 2 * b_half, c == null ? c : c + 32); return true; } if lanes_have::() { lanes_apply::(into, a, b, c); lanes_apply::(into + 16, a + a_half, b == null ? b : b + b_half, c == null ? c : c + 16); lanes_apply::(into + 32, a + 2 * a_half, b == null ? b : b + 2 * b_half, c == null ? c : c + 32); lanes_apply::(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(into: *mut u8, a: *u8, b: *u8): bool { return false if !lanes_have::(); lanes_apply::(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(into: *mut u8, a: *u8, b: *u8): bool where M: numeric, ordered, zeroable { if const M is i8 { return lanes_also::(into, a, b); } if const M is u8 { return lanes_also::(into, a, b); } if const M is i16 { return lanes_also::(into, a, b); } if const M is u16 { return lanes_also::(into, a, b); } if const M is i32 { return lanes_also::(into, a, b); } if const M is u32 { return lanes_also::(into, a, b); } if const M is i64 { return lanes_also::(into, a, b); } if const M is u64 { return lanes_also::(into, a, b); } if const M is f32 { return lanes_also::(into, a, b); } if const M is f64 { return lanes_also::(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(into: *mut u8, a: *u8): bool { return false if !lanes_have::(); lanes_apply::(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(): 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(into: *mut u8, a: *u8, b: *u8, c: *u8): void;