Skip to contentSkip to conversation

bitops: add 8-bit and 16-bit rotation functions

PATCH In review Harvey Harrison 3 participants 5 messages Mar 11, 2008 Bitops API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bitops: add 8-bit and 16-bit rotation functions

Flesh out the api a bit, done in the same style as the 32-bit version. Currently there are hand-rolled versions elsewhere in the tree that can be consolidated.

---
Andrew, once this is in I'll feed the patches replacing hand-rolled versions of this through appropriate maintainers.

 include/linux/bitops.h |   40 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 40 insertions(+), 0 deletions(-)
include/linux/bitops.h +40 -0
index 69c1edb..40d5473 100644
@@ -65,6 +65,46 @@ static inline __u32 ror32(__u32 word, unsigned int shift)
 	return (word >> shift) | (word << (32 - shift));
 }
 
+/**
+ * rol16 - rotate a 16-bit value left
+ * @word: value to rotate
+ * @shift: bits to roll
+ */
+static inline __u16 rol16(__u16 word, unsigned int shift)
+{
+	return (word << shift) | (word >> (16 - shift));
+}
+
+/**
+ * ror16 - rotate a 16-bit value right
+ * @word: value to rotate
+ * @shift: bits to roll
+ */
+static inline __u16 ror16(__u16 word, unsigned int shift)
+{
+	return (word >> shift) | (word << (16 - shift));
+}
+
+/**
+ * rol8 - rotate an 8-bit value left
+ * @word: value to rotate
+ * @shift: bits to roll
+ */
+static inline __u8 rol8(__u8 word, unsigned int shift)
+{
+	return (word << shift) | (word >> (8 - shift));
+}
+
+/**
+ * ror8 - rotate an 8-bit value right
+ * @word: value to rotate
+ * @shift: bits to roll
+ */
+static inline __u8 ror8(__u8 word, unsigned int shift)
+{
+	return (word >> shift) | (word << (8 - shift));
+}
+
 static inline unsigned fls_long(unsigned long l)
 {
 	if (sizeof(l) == 4)
Signature · 1 line
1.5.4.4.592.g32d4c
#1 lore raw

include/linux/bitops.h static inline __u32 ror32(__u32 word, unsigned int shift) · line 76

Quoted 9 lines
include/linux/bitops.h +9 -0
+/**
+ * rol16 - rotate a 16-bit value left
+ * @word: value to rotate
+ * @shift: bits to roll
+ */
+static inline __u16 rol16(__u16 word, unsigned int shift)
+{
+	return (word << shift) | (word >> (16 - shift));
+}

This doesn't work for shift values of 0: you get word >> 16, and shifts greater than or equal to the word size aren't valid C. GCC will warn about this, too.

Segher

#2 lore raw
Quoted 5 lines · Segher Boessenkool
Quoted 5 lines

+/**
+ * rol16 - rotate a 16-bit value left
+ * @word: value to rotate
+ * @shift: bits to roll
+ */

Quoted 8 lines

include/linux/bitops.h static inline __u32 ror32(__u32 word, unsigned int shift) · line 76

Quoted 4 lines
include/linux/bitops.h +4 -0
+static inline __u16 rol16(__u16 word, unsigned int shift)
+{
+	return (word << shift) | (word >> (16 - shift));
+}

This doesn't work for shift values of 0: you get word >> 16, and shifts greater than or equal to the word size aren't valid C. GCC will warn about this, too.

On the other hand, a value narrower than int will always be promoted first, so this is not a problem in this case.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#3 lore raw
Quoted 16 lines · Andreas Schwab
Quoted 5 lines · Segher Boessenkool
Quoted 5 lines

+/**
+ * rol16 - rotate a 16-bit value left
+ * @word: value to rotate
+ * @shift: bits to roll
+ */

Quoted 8 lines

include/linux/bitops.h static inline __u32 ror32(__u32 word, unsigned int shift) · line 76

Quoted 4 lines
include/linux/bitops.h +4 -0
+static inline __u16 rol16(__u16 word, unsigned int shift)
+{
+	return (word << shift) | (word >> (16 - shift));
+}

This doesn't work for shift values of 0: you get word >> 16, and shifts greater than or equal to the word size aren't valid C. GCC will warn about this, too.

Quoted 3 lines

On the other hand, a value narrower than int will always be promoted first, so this is not a problem in this case.

It's the same way rol32/ror32 is done directly above this section, I saw this as well, but figured that if checking for shift = 0 was wanted, it would have been there.

So...don't do that ;-)

Harvey

#4 lore raw
In reply to Andreas Schwab
Quoted 14 lines
Quoted 5 lines
Quoted 5 lines

+/**
+ * rol16 - rotate a 16-bit value left
+ * @word: value to rotate
+ * @shift: bits to roll
+ */

Quoted 8 lines

include/linux/bitops.h static inline __u32 ror32(__u32 word, unsigned int shift) · line 76

Quoted 4 lines
include/linux/bitops.h +4 -0
+static inline __u16 rol16(__u16 word, unsigned int shift)
+{
+	return (word << shift) | (word >> (16 - shift));
+}

This doesn't work for shift values of 0: you get word >> 16, and shifts greater than or equal to the word size aren't valid C. GCC will warn about this, too.

Quoted 2 lines

On the other hand, a value narrower than int will always be promoted first,

Erm, yes of course. It is promoted to _signed_ int though, but that works okay here.

Quoted 1 line

so this is not a problem in this case.

It still needs documentation for the valid values of "shift".

Segher