#! @file lanes.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains which vector instructions each operation on the lanes of a `simd` #! needs: what `core::lanes_have` asks the processor running an executable for, #! as it found it when it started, and what `core::lanes_apply` then works the #! lanes with. SSE2 is every x86-64 processor's, and the rest only some have, so #! an operation needing them is worked as std writes it, a lane at a time, where #! the processor lacks them. module sakura::compiler; #! The instruction sets an executable asks its processor for as it starts, one #! bit each of the word it keeps them in. pub const cpu_sse2: u64 = 0x01; pub const cpu_ssse3: u64 = 0x02; pub const cpu_sse41: u64 = 0x04; pub const cpu_sse42: u64 = 0x08; #! AVX, where the system also keeps the registers' upper halves. pub const cpu_avx: u64 = 0x10; pub const cpu_avx2: u64 = 0x20; pub const cpu_fma: u64 = 0x40; #! Counting a word's ones, `popcnt`, and its leading zeros, `lzcnt`. pub const cpu_popcnt: u64 = 0x80; pub const cpu_lzcnt: u64 = 0x100; #! BMI1, which has `tzcnt`, counting trailing zeros. pub const cpu_bmi1: u64 = 0x200; #! AVX-512's foundation with its instructions of 16-bit lanes (BW), of 64-bit #! integers (DQ), and of 16 and 32 bytes (VL), where the system also keeps the #! mask registers and the registers' upper 384 bits, as x86-64-v4 has them. pub const cpu_avx512: u64 = 0x400; #! What no processor has, for an operation no instructions work. pub const cpu_never: u64 = 0x8000000000000000; #! The operations on lanes, as std numbers them. pub const lanes_add: u32 = 1; pub const lanes_sub: u32 = 2; pub const lanes_mul: u32 = 3; pub const lanes_div: u32 = 4; pub const lanes_and: u32 = 5; pub const lanes_or: u32 = 6; pub const lanes_xor: u32 = 7; pub const lanes_negate: u32 = 8; pub const lanes_not: u32 = 9; pub const lanes_min: u32 = 10; pub const lanes_max: u32 = 11; pub const lanes_abs: u32 = 12; pub const lanes_sqrt: u32 = 13; pub const lanes_floor: u32 = 14; pub const lanes_ceil: u32 = 15; pub const lanes_trunc: u32 = 16; pub const lanes_round_even: u32 = 17; pub const lanes_equal: u32 = 18; pub const lanes_not_equal: u32 = 19; pub const lanes_less: u32 = 20; pub const lanes_less_equal: u32 = 21; pub const lanes_greater: u32 = 22; pub const lanes_greater_equal: u32 = 23; pub const lanes_select: u32 = 24; #! `a` the mask, `b` taken where it is set, `c` where not. pub const lanes_fma: u32 = 25; #! `a * b + c`. pub const lanes_shift_left: u32 = 26; #! `b` the `u32` count. pub const lanes_shift_right: u32 = 27; pub const lanes_bits: u32 = 28; #! The `u32` of the lanes the mask `a` has set. pub const lanes_splat: u32 = 29; #! The lane `a` points at, in every lane. pub const lanes_copy: u32 = 30; #! The lanes `a` points at, as they are, which moves them whole. pub const lanes_pack: u32 = 31; #! (a, b, ...): the vector of these lanes, lane 0 first, as merging stores makes it. pub const lanes_sum: u32 = 32; #! The lanes added together, lane 0 first, as one of the lane's type. pub const lanes_product: u32 = 33; #! The lanes multiplied together, lane 0 first. pub const lanes_smallest: u32 = 34; #! The least lane. pub const lanes_largest: u32 = 35; #! The greatest lane. pub const lanes_round: u32 = 36; #! Each real lane rounded to the nearest integer, halves away from zero. pub const lanes_shuffle: u32 = 37; #! (a, b): `a`'s lanes in the order the bytes of `b` name, read as 16 bytes, one past the last stopping the program. pub const lanes_convert: u32 = 38; #! `a`'s lanes, each as the type of lane above the operation's number converts it, as many. pub const lanes_low: u32 = 39; #! The first half of `a`'s 32 bytes. pub const lanes_high: u32 = 40; #! The second half. pub const lanes_join: u32 = 41; #! (a, b): `a`'s 16 bytes, then `b`'s. pub const lanes_widen: u32 = 42; #! The first half of `a`'s lanes, each as the type above the number, twice as wide. pub const lanes_widen_high: u32 = 43; #! The second half so. pub const lanes_narrow: u32 = 44; #! (a, b): `a`'s lanes, then `b`'s, each as the type above the number, half as wide, saturated. pub const lanes_gather: u32 = 45; #! (a, b): the lanes the address `a` is, at the 32-bit places `b`'s lanes name. #! The bytes the type of lane above an operation's number takes, which the #! operations giving lanes of another type are numbered with. #! @param operation The operation, as `lanes_apply` is given it. #! @returns The type of lane. pub def lanes_target(operation: u32): u32 { return (operation >> 8) & 0xFF; } #! The types of lane, as the instructions take them: signed and unsigned #! integers of each width, then the reals. pub const lane_i8: u32 = 0; pub const lane_u8: u32 = 1; pub const lane_i16: u32 = 2; pub const lane_u16: u32 = 3; pub const lane_i32: u32 = 4; pub const lane_u32: u32 = 5; pub const lane_i64: u32 = 6; pub const lane_u64: u32 = 7; pub const lane_f32: u32 = 8; pub const lane_f64: u32 = 9; #! What no instruction takes. pub const lane_none: u32 = 10; #! The bits of a lane of a type. #! @param lane The type of lane. #! @returns Its bits. pub def lane_bits(lane: u32): u64 { return lane == lane_f32 ? 32 : lane == lane_f64 ? 64 : 8u << (lane / 2) as u64; } #! Whether a type of lane is a signed integer. #! @param lane The type of lane. #! @returns Whether it is. pub def lane_signed(lane: u32): bool { return lane < lane_f32 && lane % 2 == 0; } #! The instruction sets an operation on lanes needs, of 16, 32, or 64 bytes, or #! of one real alone, one bit each as the executable keeps them, or `cpu_never` #! where none work it. #! #! Of 16 bytes, SSE2 has most: arithmetic but integer division, bitwise #! operations of any lanes, comparisons, square roots, splatting a lane, and the #! least and greatest of reals, made IEEE 754's with more instructions; products #! of 32 and 64 bits, from `pmuludq`'s of 32 bits into 64; shifts of bytes, #! through those of 16 bits and a mask; and the absolute values and `>>` of #! 64-bit integers, through their signs. SSSE3 adds integers' absolute values, #! SSE4.1 rounding reals and the least and greatest of more integers, and SSE4.2 #! comparing 64-bit integers, so their least and greatest. FMA fuses multiplying #! and adding. 32 bytes take AVX's forms: of reals, and what only moves bits, AVX's, #! and of integers AVX2's. One real alone, as `std::math` works its rounding and #! `fma` of one number, takes the instruction of one number, which needs the same, #! and splats nothing. 64 bytes take AVX-512's, as `avx512_needs` says. #! @param operation The operation, as std numbers them. #! @param lane The type of lane. #! @param bytes The bytes of the vector. #! @returns The sets. pub def lanes_needs(operation: u32, lane: u32, bytes: u64): u64 { return cpu_never if lane >= lane_none; if (operation & 0xFF) >= lanes_convert { return reshape_needs(operation & 0xFF, lane, lanes_target(operation), bytes); } val real := lane >= lane_f32; val bits := lane_bits(lane); val signed := lane_signed(lane); if bytes == bits / 8 { return cpu_never if !real; return cpu_fma if operation == lanes_fma; return cpu_sse41 if operation >= lanes_floor && operation <= lanes_round_even; return cpu_sse2 if operation == lanes_sqrt; return cpu_never; } return avx512_needs(operation, lane) if bytes == 64; return cpu_never if bytes != 16 && bytes != 32; var need := cpu_never; if operation == lanes_add || operation == lanes_sub || operation == lanes_negate || operation == lanes_select { need = cpu_sse2; } else if operation == lanes_mul { need = cpu_sse2; } else if operation == lanes_div || operation == lanes_sqrt { need = real ? cpu_sse2 : cpu_never; } else if operation == lanes_and || operation == lanes_or || operation == lanes_xor || operation == lanes_not { # Of any lanes, as a mask's are combined. need = cpu_sse2; } else if operation == lanes_min || operation == lanes_max { need = real || lane == lane_i16 || lane == lane_u8 ? cpu_sse2 : bits == 64 ? cpu_sse42 : cpu_sse41; } else if operation == lanes_abs { need = real || !signed || bits == 64 ? cpu_sse2 : cpu_ssse3; } else if operation == lanes_floor || operation == lanes_ceil || operation == lanes_trunc || operation == lanes_round_even { need = real ? cpu_sse41 : cpu_never; } else if operation == lanes_equal || operation == lanes_not_equal { need = real || bits < 64 ? cpu_sse2 : cpu_sse41; } else if operation == lanes_less || operation == lanes_less_equal || operation == lanes_greater || operation == lanes_greater_equal { need = real || bits < 64 ? cpu_sse2 : cpu_sse42; } else if operation == lanes_fma { need = real ? cpu_fma : cpu_never; } else if operation == lanes_shift_left || operation == lanes_shift_right { need = real ? cpu_never : cpu_sse2; } else if operation == lanes_bits { # A mask of 16-bit lanes is packed into bytes first, which 32 bytes would do # within each half of the register, so those are taken a half at a time. need = bits == 16 && bytes == 32 ? cpu_never : cpu_sse2; } else if operation == lanes_splat || operation == lanes_copy { need = cpu_sse2; } else if operation == lanes_sum { # Integers added in any order, and reals a lane at a time, lane 0 first. need = cpu_sse2; } else if operation == lanes_product { need = real ? cpu_sse2 : cpu_never; } else if operation == lanes_smallest || operation == lanes_largest { # Of integers, as their least and greatest; reals' order is kept, which the # registers left do not hold. need = real ? cpu_never : lane == lane_i16 || lane == lane_u8 ? cpu_sse2 : bits == 64 ? cpu_sse42 : cpu_sse41; } else if operation == lanes_round { need = real ? cpu_sse41 : cpu_never; } else if operation == lanes_shuffle { # Bytes chosen by SSSE3's `pshufb`, and 32 bytes of 32- and 64-bit lanes by # AVX2's `vpermd`, which crosses the halves `vpshufb` keeps apart. need = bytes == 16 ? cpu_ssse3 : bits >= 32 ? cpu_sse41 : cpu_never; } return need if need == cpu_never || bytes == 16; val moves_bits := operation == lanes_select || (operation == lanes_abs && !signed) || (operation == lanes_bits && bits != 8) || operation == lanes_splat || operation == lanes_copy; return need | (real || moves_bits ? cpu_avx : cpu_avx2); } #! The instruction sets an operation on 64 bytes of lanes needs: AVX-512's, #! for arithmetic but integer division, bitwise operations, comparisons, choosing #! lanes, splatting one, a mask's bits, and of reals square roots, rounding, and #! `fma`; of integers their least, greatest, and absolute values, and shifts. #! Moving them is SSE2's. The rest, and every operation of bytes, whose mask's #! bits would not fit a `u32`, `simd` works a half or a quarter at a time. #! @param operation The operation, as std numbers them. #! @param lane The type of lane. #! @returns The sets. def avx512_needs(operation: u32, lane: u32): u64 { return cpu_sse2 if operation == lanes_copy; return cpu_never if lane_bits(lane) == 8; val real := lane >= lane_f32; if operation <= lanes_not || (operation >= lanes_equal && operation <= lanes_select) || operation == lanes_bits || operation == lanes_splat { return operation == lanes_div && !real ? cpu_never : cpu_avx512; } if operation == lanes_min || operation == lanes_max || operation == lanes_shift_left || operation == lanes_shift_right { return real ? cpu_never : cpu_avx512; } if operation == lanes_sqrt || (operation >= lanes_floor && operation <= lanes_round_even) || operation == lanes_fma { return real ? cpu_avx512 : cpu_never; } return operation == lanes_abs ? cpu_avx512 : cpu_never; } #! The instruction sets the operations giving lanes of another type or size #! need: `lanes_needs` of those, of `lane` lanes in `bytes` making `target` lanes. #! Halves of 32 bytes are AVX's to take apart and join, as are reals' widening #! and narrowing between `f32` and `f64`, of 32 bytes. Integers widen by #! SSE4.1's `pmovsx` and `pmovzx`, narrow by its `pack`s, and of 32 bytes by #! AVX2's; `i32` becomes `f32` by SSE2's `cvtdq2ps`, and `u32` by two halves of #! 16 bits; and a real becomes a 32-bit integer by `cvttps2dq` or `cvttpd2dq`, #! which `simd`'s `convert` asks only of lanes within `i32`'s range, where it is #! exact. Gathering 32 and 64-bit lanes at 32-bit places is AVX2's. def reshape_needs(operation: u32, lane: u32, target: u32, bytes: u64): u64 { val real := lane >= lane_f32; val bits := lane_bits(lane); if operation == lanes_low || operation == lanes_high { return bytes == 32 ? cpu_sse2 | cpu_avx : cpu_never; } if operation == lanes_join { return bytes == 16 ? cpu_sse2 | cpu_avx : cpu_never; } if operation == lanes_gather { return lane_bits(lane) >= 32 && (bytes / (bits / 8) == 4 || (bits == 32 && bytes == 32)) ? cpu_sse2 | cpu_avx | cpu_avx2 : cpu_never; } return cpu_never if target >= lane_none || (bytes != 16 && bytes != 32); val into_real := target >= lane_f32; val into_bits := lane_bits(target); val wide: u64 = bytes == 32 ? cpu_avx | cpu_avx2 : 0; if operation == lanes_widen || operation == lanes_widen_high { return cpu_never if into_bits != 2 * bits; return cpu_sse2 | (bytes == 32 ? cpu_avx : 0) if real && into_real; return cpu_sse41 | wide if !real && !into_real; return cpu_never; } if operation == lanes_narrow { return cpu_never if 2 * into_bits != bits || real != into_real || bits == 64 && !real; return cpu_sse2 | (bytes == 32 ? cpu_avx : 0) if real; # Signed lanes pack by SSE2's but into `u16`, and unsigned ones are held at # the type's greatest by SSE4.1's least of unsigned lanes first. val plain := lane_signed(lane) && (bits == 16 || lane_signed(target)); return (plain ? cpu_sse2 : cpu_sse41) | wide; } # Converting each lane, as many. val into_bytes := bytes / (bits / 8) * (into_bits / 8); return cpu_never if into_bytes != 16 && into_bytes != 32; if !real && !into_real { return cpu_sse2 | (bytes == 32 ? cpu_avx : 0) if into_bits == bits; return cpu_sse41 | (into_bytes == 32 ? cpu_avx | cpu_avx2 : 0) if into_bits > bits; return cpu_ssse3 | cpu_avx | cpu_avx2 if bytes == 32 && 2 * into_bits == bits; return cpu_never; } if !real && into_real { return cpu_never if bits == 64 || (target == lane_f64 && bits != 32); return cpu_sse2 | cpu_avx if target == lane_f64; return cpu_never if bits != 32 && into_bytes == 16; return cpu_sse41 | cpu_avx | cpu_avx2 if bits < 32; return cpu_sse2 | (bytes == 32 ? cpu_avx | (lane == lane_u32 ? cpu_avx2 : 0) : 0); } if real && !into_real { # Toward zero, of lanes `simd`'s `convert` finds within `i32`'s range. return cpu_never if into_bits != 32; return cpu_sse2 | (bytes == 32 ? cpu_avx : 0) if lane == lane_f32; return cpu_sse2 | cpu_avx; } return cpu_sse2 | (bytes == 32 || into_bytes == 32 ? cpu_avx : 0); } #! How many operands an operation on lanes takes: the vectors, and for a shift #! the count, and for `lanes_splat` the lane. #! @param operation The operation. #! @returns How many. pub def lanes_operands(operation: u32): u32 { return 3 if operation == lanes_select || operation == lanes_fma; val base := operation & 0xFF; return 2 if base == lanes_join || base == lanes_narrow || base == lanes_gather; return 1 if operation == lanes_negate || operation == lanes_not || operation == lanes_bits || operation == lanes_splat || operation == lanes_copy || (operation >= lanes_abs && operation <= lanes_round_even) || (operation >= lanes_sum && operation <= lanes_round) || base >= lanes_convert; return 2; } #! How the result of an operation on lanes is written where `lanes_apply` is #! given it: as the vector, as a `u32` of a mask's bits, or as the lane's type, #! of one lane alone or of the lanes worked together. #! @param operation The operation. #! @param lane The type of lane. #! @param bytes The bytes of the vector. #! @returns The access. pub def lanes_result_access(operation: u32, lane: u32, bytes: u64): access { return access::u32 if operation == lanes_bits; val base := operation & 0xFF; if base == lanes_low || base == lanes_high { return access::v128; } if base == lanes_join { return access::v256; } if base == lanes_convert { val made := bytes / (lane_bits(lane) / 8) * (lane_bits(lanes_target(operation)) / 8); return made == 32 ? access::v256 : access::v128; } if bytes < 16 || (operation >= lanes_sum && operation <= lanes_largest) { return lanes_operand_access(lanes_splat, lane, bytes, 0); } return vector_access(bytes); } #! How a vector of so many bytes is read and written whole. #! @param bytes 16, 32, or 64. #! @returns The access. pub def vector_access(bytes: u64): access { return bytes == 64 ? access::v512 : bytes == 32 ? access::v256 : access::v128; } #! How an operand of an operation on lanes is read from where `lanes_apply` is #! given it: as the vector, or for a shift's count a `u32`, or for #! `lanes_splat`'s lane, and every operand of one lane alone, as the lane's type. #! @param operation The operation. #! @param lane The type of lane. #! @param bytes The bytes of the vector. #! @param k Which operand. #! @returns The access. pub def lanes_operand_access(operation: u32, lane: u32, bytes: u64, k: u32): access { if operation == lanes_splat { return match lane { lane_i8 => access::i8, lane_u8 => access::u8, lane_i16 => access::i16, lane_u16 => access::u16, lane_i32 => access::i32, lane_u32 => access::u32, lane_f32 => access::f32, lane_f64 => access::f64, else => access::i64 }; } return access::u32 if k == 1 && (operation == lanes_shift_left || operation == lanes_shift_right); return access::v128 if k == 1 && operation == lanes_shuffle; if (operation & 0xFF) == lanes_gather { return access::i64 if k == 0; return bytes / (lane_bits(lane) / 8) == 8 ? access::v256 : access::v128; } return (lane == lane_f32 ? access::f32 : access::f64) if bytes == lane_bits(lane) / 8; return vector_access(bytes); }