Efficient bit rotate functions: ror, rol...what is the official way for Julia v1?

Hi,
I found this https://github.com/JuliaLang/julia/issues/11592 but I can not understand what should I do for a UInt64 or any other arbitrary 0b....
Am I missing something in the doc?

Thanks you

Define these:

ror(x::UInt64, k::Int) = (x >>> (0x3f & k)) | (x << (0x3f & -k))
rol(x::UInt64, k::Int) = (x >>> (0x3f & -k)) | (x << (0x3f & k))

Verification:

julia> code_native(ror, (UInt64, Int); syntax = :intel)
        ...
        ror     rax, cl
        ...

julia> code_native(rol, (UInt64, Int); syntax = :intel)
        ...
        rol     rax, cl
        ...
5 Likes

I rather think we should have these in Base for the fixed size integer types. The definitions are non-obvious enough that people shouldn’t have to define them for themselves. If someone wants to make a PR this is a good intro one!

8 Likes

someone:zzz: has been napping.

1 Like

This is included since Julia 1.5, in the form of bitrotate:

help?> bitrotate
search: bitrotate

  bitrotate(x::Base.BitInteger, k::Integer)

  bitrotate(x, k) implements bitwise rotation. It returns the value of x with its bits
  rotated left k times. A negative value of k will rotate to the right instead.

  │ Julia 1.5
  │
  │  This function requires Julia 1.5 or later.

It does compile down to efficient instructions, at least on my machine.

6 Likes

Thanks!