LLVM seems to figure out the rotation idiom just fine here:
julia> rot8(x) = (x << 8) | (x >>> 56)
rot8 (generic function with 1 method)
julia> code_llvm(rot8,(Uint,))
define i64 @julia_rot818217(i64) {
top:
%1 = shl i64 %0, 8, !dbg !5098
%2 = lshr i64 %0, 56, !dbg !5098
%3 = or i64 %1, %2, !dbg !5098, !julia_type !5099
ret i64 %3, !dbg !5098
}
julia> code_native(rot8,(Uint,))
.section __TEXT,__text,regular,pure_instructions
Filename: none
Source line: 1
push RBP
mov RBP, RSP
Source line: 1
rol RDI, 8
mov RAX, RDI
pop RBP
ret
If there are cases where it's not detecting this idiom, we could always
provide Julia intrinsics for bit rotation that emit instructions that we're
sure LLVM will translate to x86 rotation instructions. Seems kind of silly,
but that's one way to do it. The bit reversing thing is much harder – I'm
not sure if there is an idiom that LLVM can detect. We could ask on the
appropriate LLVM list. Keno, Jameson – you guys know where it would be good
to ask about this?
On Sun, Apr 20, 2014 at 8:39 PM, Laszlo Hars <[email protected]> wrote:
> Stefan, do you know about any plans of LLVM to support rotations and bit
> reversals in the near future? Those are the most important ones missing. I
> tried rot8(x) = x<<8 | x>>>56, and in my Windows-7 x64 machine it was slow
> (more than twice slower than a shift alone). Bit reversing Int64 takes
> about 40 machine instructions when coded in C or Julia (without huge lookup
> tables), instead of the appropriate single machine instruction in Intel
> CPUs.
>