This is an archive of the discontinued LLVM Phabricator instance.

[ADT] Add llvm::rotl and llvm::rotr to bit.h
ClosedPublic

Authored by kazu on Feb 13 2023, 12:18 AM.

Diff Detail

Event Timeline

kazu created this revision.Feb 13 2023, 12:18 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 13 2023, 12:18 AM
kazu requested review of this revision.Feb 13 2023, 12:18 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 13 2023, 12:18 AM
RKSimon accepted this revision.Feb 13 2023, 6:40 AM

LGTM

This revision is now accepted and ready to land.Feb 13 2023, 6:40 AM
This revision was landed with ongoing or failed builds.Feb 13 2023, 9:40 AM
This revision was automatically updated to reflect the committed changes.
andrew.w.kaylor added inline comments.
llvm/include/llvm/ADT/bit.h
366

This is dead code. Because you defined N as unsigned, R will be converted to unsigned for the % operation on line 361 and the result will never be negative. It gets the right answer anyway, but it doesn't seem to be what you intended.

As an example, in your test "llvm::rotl<uint8_t>(0x53, -5)" this will happen

N = 8u; (std::numeric_limits<uint8t>::digits)
R = 0xFFFFFFFB % 8u;
(unsigned)R % N

So R = 3, and rotl(0x53, 3) yields 0x9A, as expected.

The point is, the code on lines 365-366 isn't doing anything. The same applies to lines 378-379 below.

This was reported by Coverity, BTW, which is why I happened to notice.

kazu added inline comments.Feb 26 2023, 11:55 PM
llvm/include/llvm/ADT/bit.h
366

Thank you for catching and reporting this! I'll post a follow-up patch to fix this.