]> git.ipfire.org Git - people/ms/linux.git/commit
bitops.h: correctly handle rol32 with 0 byte shift
authorSasha Levin <sasha.levin@oracle.com>
Fri, 4 Dec 2015 03:04:01 +0000 (22:04 -0500)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 9 Dec 2015 18:35:16 +0000 (10:35 -0800)
commitd7e35dfa2531b53618b9e6edcd8752ce988ac555
treeb8cf70f1d314c519030b6ae387d76313a0698616
parent626d114f46fedb66f56bfbea4743ab68ab88e590
bitops.h: correctly handle rol32 with 0 byte shift

ROL on a 32 bit integer with a shift of 32 or more is undefined and the
result is arch-dependent. Avoid this by handling the trivial case of
roling by 0 correctly.

The trivial solution of checking if shift is 0 breaks gcc's detection
of this code as a ROL instruction, which is unacceptable.

This bug was reported and fixed in GCC
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57157):

The standard rotate idiom,

  (x << n) | (x >> (32 - n))

is recognized by gcc (for concreteness, I discuss only the case that x
is an uint32_t here).

However, this is portable C only for n in the range 0 < n < 32. For n
== 0, we get x >> 32 which gives undefined behaviour according to the
C standard (6.5.7, Bitwise shift operators). To portably support n ==
0, one has to write the rotate as something like

  (x << n) | (x >> ((-n) & 31))

And this is apparently not recognized by gcc.

Note that this is broken on older GCCs and will result in slower ROL.

Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
include/linux/bitops.h