Shfl

sm86.sail

SHFL: PTX's shfl.sync. c's bits 8-12 split the 32 lanes into segments, its bits 0-4 clamp the lane. Mode (58-59) 0 IDX, 1 UP, 2 DOWN, 3 BFLY. The lane is at 53-57 (an immediate) or 32 (a register), c at 40-52 (an immediate) or 64 (a register: SHFL (imm, reg) 0x989 and SHFL (reg, reg) 0x389, as CuAssembler's sm_86 records "SHFL.UP PT, R7, R8, 0x1, RZ" and "SHFL.DOWN P1, R32, R33, R32, R31" show). One game's prefix sums are SHFL.UP P0, R12, R19, 0x1, RZ. UP is in range from the clamp's lane on, as PTX's j >= maxLane

function Shfl(b_imm : bool, c_reg : bool) -> unit = {
    let mode = UInt(Field(58, 2));
    let cc = UInt(Field(40, 13));
    let creg = if c_reg then R_read(UInt(Field(64, 8))) else 0x00000000;
    let b = if b_imm then to_bits(32, UInt(Field(53, 5))) else R_read(UInt(Field(32, 8)));
    let native = ShflNative();
    let id = if native then NativeLane() else 0x00000000;
    let lane = id & 0x0000001f;
    /* c's fields as the immediate's, at run time */
    let seg = if c_reg then (creg >> 8) & 0x0000001f else to_bits(32, MOD(DIV(cc, 256), 32));
    let not_seg_reg = if c_reg then not_vec(seg) & 0x0000001f else 0x00000000;
    let min_lane = lane & seg;
    let clamp_bits = if c_reg then creg & not_seg_reg else to_bits(32, cc) & not_vec(to_bits(32, DIV(cc, 256))) & 0x0000001f;
    let max_lane = min_lane | clamp_bits;
    let j : bits(32) = match mode {
        0 => min_lane | (b & (if c_reg then not_seg_reg else not_vec(to_bits(32, DIV(cc, 256))) & 0x0000001f)),
        1 => lane - b,
        2 => lane + b,
        _ => EOR(lane, b)
    };
    let ok = if mode == 1 then SInt(j) >= SInt(max_lane) else UInt(j) <= UInt(max_lane);
    if native then Capability(65);
    /* the lane in the invocation's own 32, on a subgroup of 32 or 64; a lone lane keeps its own value */
    let source = (id & 0xffffffe0) | (j & 0x0000001f);
    let value = R_read(UInt(Field(24, 8)));
    let got = if native then Shuffle(value, source) else value;
    R_write(Dst(), if ok then got else value);
    PredWrite(UInt(Field(81, 3)), ok, 0)
}

Used by


Made from ScarletNX's Sail at 166ad3910f80. The Sail and the encodings' file are ScarletNX's (GPL-3.0-or-later); its encodings are from Mesa's NAK (sm70_encode.rs, MIT) and CuAssembler's sm_86 records.