#! @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::sqrt(&this): simd { var made: simd; if const L is real { if !lanes_done::((&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::::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::floor(&this): simd { var made: simd; if const L is real { if !lanes_done::((&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::::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::ceil(&this): simd { var made: simd; if const L is real { if !lanes_done::((&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::::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::trunc(&this): simd { var made: simd; if const L is real { if !lanes_done::((&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::::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::round(&this): simd { var made: simd; if const L is real { if !lanes_done::((&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::::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::round_even(&this): simd { var made: simd; if const L is real { if !lanes_done::((&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::::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::fma(&this, times: &simd, plus: &simd): simd { var made: simd; if !lanes_done::((&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::::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(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; } } 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;