LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] bitops: add 8-bit and 16-bit rotation functions
@ 2008-03-11 16:56 Harvey Harrison
  2008-03-11 20:45 ` Segher Boessenkool
  0 siblings, 1 reply; 5+ messages in thread
From: Harvey Harrison @ 2008-03-11 16:56 UTC (permalink / raw)
  To: Andrew Morton; +Cc: LKML

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.

Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>
---
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(-)

diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index 69c1edb..40d5473 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -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)
-- 
1.5.4.4.592.g32d4c




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] bitops: add 8-bit and 16-bit rotation functions
  2008-03-11 16:56 [PATCH] bitops: add 8-bit and 16-bit rotation functions Harvey Harrison
@ 2008-03-11 20:45 ` Segher Boessenkool
  2008-03-11 20:59   ` Andreas Schwab
  0 siblings, 1 reply; 5+ messages in thread
From: Segher Boessenkool @ 2008-03-11 20:45 UTC (permalink / raw)
  To: Harvey Harrison; +Cc: LKML, Andrew Morton

> +/**
> + * 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] bitops: add 8-bit and 16-bit rotation functions
  2008-03-11 20:45 ` Segher Boessenkool
@ 2008-03-11 20:59   ` Andreas Schwab
  2008-03-11 21:15     ` Harvey Harrison
  2008-03-11 21:18     ` Segher Boessenkool
  0 siblings, 2 replies; 5+ messages in thread
From: Andreas Schwab @ 2008-03-11 20:59 UTC (permalink / raw)
  To: Segher Boessenkool; +Cc: Harvey Harrison, LKML, Andrew Morton

Segher Boessenkool <segher@kernel.crashing.org> writes:

>> +/**
>> + * 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.

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

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
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."

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] bitops: add 8-bit and 16-bit rotation functions
  2008-03-11 20:59   ` Andreas Schwab
@ 2008-03-11 21:15     ` Harvey Harrison
  2008-03-11 21:18     ` Segher Boessenkool
  1 sibling, 0 replies; 5+ messages in thread
From: Harvey Harrison @ 2008-03-11 21:15 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Segher Boessenkool, LKML, Andrew Morton

On Tue, 2008-03-11 at 21:59 +0100, Andreas Schwab wrote:
> Segher Boessenkool <segher@kernel.crashing.org> writes:
> 
> >> +/**
> >> + * 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.
> 
> 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] bitops: add 8-bit and 16-bit rotation functions
  2008-03-11 20:59   ` Andreas Schwab
  2008-03-11 21:15     ` Harvey Harrison
@ 2008-03-11 21:18     ` Segher Boessenkool
  1 sibling, 0 replies; 5+ messages in thread
From: Segher Boessenkool @ 2008-03-11 21:18 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Harvey Harrison, LKML, Andrew Morton

>>> +/**
>>> + * 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.
>
> 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.

> so this is not a problem in this case.

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


Segher


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-03-11 21:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-11 16:56 [PATCH] bitops: add 8-bit and 16-bit rotation functions Harvey Harrison
2008-03-11 20:45 ` Segher Boessenkool
2008-03-11 20:59   ` Andreas Schwab
2008-03-11 21:15     ` Harvey Harrison
2008-03-11 21:18     ` Segher Boessenkool

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).