Files
std/math/lanes.sakura
T
2026-11-10 11:26:34 -06:00

184 lines
5.7 KiB
Plaintext

#! @file lanes.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the functions of reals as methods of `simd`, lane by lane: square
#! roots, rounding to integers, and fused multiplying and adding. Each gives in
#! every lane what the function of the same name gives of one real, worked with
#! the processor's vector instructions where it has them, as `simd`'s own
#! methods are.
import core::intrinsics;
module std::math;
#! Each real lane's square root, correctly rounded, as `sqrt` gives it.
#! @param this The vector.
#! @returns The vector.
pub def simd<L[N]>::sqrt(&this): simd<L[N]> {
var made: simd<L[N]>;
if const L is real {
if !lanes_done::<L, lanes_sqrt, N>((&mut made) as *mut u8, this as *u8, null, null, 16, 16) {
var r: L[N] = this.lanes();
for i in 0..N {
r[i] = sqrt(r[i]);
}
made = simd::<L[N]>::of(r);
}
} else {
compile_error("`sqrt` takes real lanes, and these are integers.");
}
return made;
}
#! Each real lane's greatest integer not above it, as `floor` gives it.
#! @param this The vector.
#! @returns The vector.
pub def simd<L[N]>::floor(&this): simd<L[N]> {
var made: simd<L[N]>;
if const L is real {
if !lanes_done::<L, lanes_floor, N>((&mut made) as *mut u8, this as *u8, null, null, 16, 16) {
var r: L[N] = this.lanes();
for i in 0..N {
r[i] = floor(r[i]);
}
made = simd::<L[N]>::of(r);
}
} else {
compile_error("`floor` takes real lanes, and these are integers.");
}
return made;
}
#! Each real lane's least integer not below it, as `ceil` gives it.
#! @param this The vector.
#! @returns The vector.
pub def simd<L[N]>::ceil(&this): simd<L[N]> {
var made: simd<L[N]>;
if const L is real {
if !lanes_done::<L, lanes_ceil, N>((&mut made) as *mut u8, this as *u8, null, null, 16, 16) {
var r: L[N] = this.lanes();
for i in 0..N {
r[i] = ceil(r[i]);
}
made = simd::<L[N]>::of(r);
}
} else {
compile_error("`ceil` takes real lanes, and these are integers.");
}
return made;
}
#! Each real lane's integer part, toward zero, as `trunc` gives it.
#! @param this The vector.
#! @returns The vector.
pub def simd<L[N]>::trunc(&this): simd<L[N]> {
var made: simd<L[N]>;
if const L is real {
if !lanes_done::<L, lanes_trunc, N>((&mut made) as *mut u8, this as *u8, null, null, 16, 16) {
var r: L[N] = this.lanes();
for i in 0..N {
r[i] = trunc(r[i]);
}
made = simd::<L[N]>::of(r);
}
} else {
compile_error("`trunc` takes real lanes, and these are integers.");
}
return made;
}
#! Each real lane's nearest integer, halves away from zero, as `round` gives
#! it: truncated, then moved one away from zero where a half or more was left,
#! with SSE4.1's instructions.
#! @param this The vector.
#! @returns The vector.
pub def simd<L[N]>::round(&this): simd<L[N]> {
var made: simd<L[N]>;
if const L is real {
if !lanes_done::<L, lanes_round, N>((&mut made) as *mut u8, this as *u8, null, null, 16, 16) {
var r: L[N] = this.lanes();
for i in 0..N {
r[i] = round(r[i]);
}
made = simd::<L[N]>::of(r);
}
} else {
compile_error("`round` takes real lanes, and these are integers.");
}
return made;
}
#! Each real lane's nearest integer, halves to the even one, as `round_even`
#! gives it.
#! @param this The vector.
#! @returns The vector.
pub def simd<L[N]>::round_even(&this): simd<L[N]> {
var made: simd<L[N]>;
if const L is real {
if !lanes_done::<L, lanes_round_even, N>((&mut made) as *mut u8, this as *u8, null, null, 16, 16) {
var r: L[N] = this.lanes();
for i in 0..N {
r[i] = round_even(r[i]);
}
made = simd::<L[N]>::of(r);
}
} else {
compile_error("`round_even` takes real lanes, and these are integers.");
}
return made;
}
#! `a * b + c`, lane by lane: of reals rounded once, as `fma` gives it, and of
#! integers wrapping.
#! @param this The first factors.
#! @param times The second.
#! @param plus What the products are added to.
#! @returns The vector.
pub def simd<L[N]>::fma(&this, times: &simd<L[N]>, plus: &simd<L[N]>): simd<L[N]> {
var made: simd<L[N]>;
if !lanes_done::<L, lanes_fma, N>((&mut made) as *mut u8, this as *u8, times as *u8, plus as *u8, 16, 16) {
val a: L[N] = this.lanes();
val b: L[N] = times.lanes();
var r: L[N] = plus.lanes();
for i in 0..N {
if const L is real {
r[i] = fma(a[i], b[i], r[i]);
} else {
r[i] = a[i] * b[i] + r[i];
}
}
made = simd::<L[N]>::of(r);
}
return made;
}
# Works an operation out with the vector instructions of the processor running
# the program, where it has them, as `simd`'s own methods do: the whole vector
# at once, or 32 bytes a half at a time where it has them only for 16. Where it
# gives false, the caller works the lanes one at a time.
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;
}
}
return false;
}
# The operations on lanes of these functions, as the compiler numbers them.
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_fma: u32 = 25;
const lanes_round: u32 = 36;