• any of em are fine

opinions of varying quality. fishcat with five hammers, not afraid to use them. made out of meat, but no nutritional value.


here's some rust:

#[derive(Copy, Clone)]
#[repr(u8)]
pub enum RotOps {
    A = 0 << 3,
    B = 1 << 3,
    C = 2 << 3,
    D = 3 << 3,
    E = 4 << 3,
}

pub fn execute_op(op: RotOps) {
    match op {
        RotOps::A => { println!("a"); }
        RotOps::B => { println!("b"); }
        RotOps::C => { println!("c"); }
        RotOps::D => { println!("d"); }
        RotOps::E => { println!("e"); }
    }
}

and if you're me, your first question looking at this would be "will llvm figure out that the branch table for that match can be indexed by shifting op right by 3?"

the answer may surprise you:

playground::execute_op:
	sub	rsp, 56                          ; space for println
	rol	dil, 5                           ; the rotate! but left by 5
	movzx	eax, dil                     ; instead of right by 3?
	lea	rcx, [rip + .LJTI0_0]            ; jump table goop
	movsxd	rax, dword ptr [rcx + 4*rax]
	add	rax, rcx
	jmp	rax

so it did exactly the right thing, but with a rol instead of ror? why.

i guess i won't ask too many questions, it did exactly what i wanted. good job llvm!


You must log in to comment.

in reply to @iximeow's post:

nope! afaik they're the same in all regards (except the right/left difference). i think @8051enthusiast is probably right that it's a canonicalization thing. if you're rotating by an immediate (on x86) you can just prefer one style of rotate always instead of complicating it by picking right/left for no benefit.