Patchwork [v6,17/24] qemu/bitops.h: add the bit ordering reversal functions stolen from linux

login
register
mail settings
Submitter Kuo-Jung Su
Date March 6, 2013, 7:27 a.m.
Message ID <1362554857-3896-18-git-send-email-dantesu@gmail.com>
Download mbox | patch
Permalink /patch/225405/
State New
Headers show

Comments

Kuo-Jung Su - March 6, 2013, 7:27 a.m.
Signed-off-by: Kuo-Jung Su <dantesu@gmail.com>
---
 include/qemu/bitops.h |   63 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 62 insertions(+), 1 deletion(-)
Paolo Bonzini - March 6, 2013, 8:26 a.m.
Il 06/03/2013 08:27, Kuo-Jung Su ha scritto:
> Signed-off-by: Kuo-Jung Su <dantesu@gmail.com>
> ---
>  include/qemu/bitops.h |   63 ++++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 62 insertions(+), 1 deletion(-)
> 
> diff --git a/include/qemu/bitops.h b/include/qemu/bitops.h
> index affcc96..920d028 100644
> --- a/include/qemu/bitops.h
> +++ b/include/qemu/bitops.h
> @@ -3,7 +3,8 @@
>   *
>   * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
>   *
> - * Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
> + * Mostly inspired by (stolen from) linux/bitmap.h, linux/bitops.h
> + * and linux/bitrev.h
>   *
>   * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
>   * See the COPYING.LIB file in the top-level directory.
> @@ -273,4 +274,64 @@ static inline uint64_t deposit64(uint64_t value, int start, int length,
>      return (value & ~mask) | ((fieldval << start) & mask);
>  }
>  
> +/**
> + * bitrev8:
> + * @value: the value to reverse bit ordering from
> + *
> + * Reverse the 8 bit input @value
> + *
> + * Returns: the input @value with reversed bit ordering
> + */
> +static inline uint8_t bitrev8(uint8_t value)
> +{
> +    int i;
> +    uint8_t ret = 0;
> +    for (i = 0; i < 8; ++i) {
> +        if (value & BIT(i)) {
> +            ret |= BIT(7 - i);
> +        }
> +    }

Please use instead

   value = ((value & 0xAA) >> 1) | ((value & 0x55) << 1);
   value = ((value & 0xCC) >> 2) | ((value & 0x33) << 2);
   value = ((value & 0xF0) >> 4) | ((value & 0x0F) << 4);
   return value;

Paolo

> +    return ret;
> +}
> +
> +/**
> + * bitrev16:
> + * @value: the value to reverse bit ordering from
> + *
> + * Reverse the 16 bit input @value
> + *
> + * Returns: the input @value with reversed bit ordering
> + */
> +static inline uint16_t bitrev16(uint16_t value)
> +{
> +    return (bitrev8(value & 0xff) << 8) | bitrev8(value >> 8);
> +}
> +
> +/**
> + * bitrev32:
> + * @value: the value to reverse bit ordering from
> + *
> + * Reverse the 32 bit input @value
> + *
> + * Returns: the input @value with reversed bit ordering
> + */
> +static inline uint32_t bitrev32(uint32_t value)
> +{
> +    return (bitrev16(value & 0xffff) << 16) | bitrev16(value >> 16);
> +}
> +
> +/**
> + * bitrev64:
> + * @value: the value to reverse bit ordering from
> + *
> + * Reverse the 64 bit input @value
> + *
> + * Returns: the input @value with reversed bit ordering
> + */
> +static inline uint64_t bitrev64(uint64_t value)
> +{
> +    return ((uint64_t)bitrev32(value & 0xffffffffULL) << 32)
> +        | (uint64_t)bitrev32(value >> 32);
> +}
> +
>  #endif
>
Kuo-Jung Su - March 7, 2013, 2:58 a.m.
2013/3/6 Paolo Bonzini <pbonzini@redhat.com>:
> Il 06/03/2013 08:27, Kuo-Jung Su ha scritto:
>> Signed-off-by: Kuo-Jung Su <dantesu@gmail.com>
>> ---
>>  include/qemu/bitops.h |   63 ++++++++++++++++++++++++++++++++++++++++++++++++-
>>  1 file changed, 62 insertions(+), 1 deletion(-)
>>
>> diff --git a/include/qemu/bitops.h b/include/qemu/bitops.h
>> index affcc96..920d028 100644
>> --- a/include/qemu/bitops.h
>> +++ b/include/qemu/bitops.h
>> @@ -3,7 +3,8 @@
>>   *
>>   * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
>>   *
>> - * Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
>> + * Mostly inspired by (stolen from) linux/bitmap.h, linux/bitops.h
>> + * and linux/bitrev.h
>>   *
>>   * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
>>   * See the COPYING.LIB file in the top-level directory.
>> @@ -273,4 +274,64 @@ static inline uint64_t deposit64(uint64_t value, int start, int length,
>>      return (value & ~mask) | ((fieldval << start) & mask);
>>  }
>>
>> +/**
>> + * bitrev8:
>> + * @value: the value to reverse bit ordering from
>> + *
>> + * Reverse the 8 bit input @value
>> + *
>> + * Returns: the input @value with reversed bit ordering
>> + */
>> +static inline uint8_t bitrev8(uint8_t value)
>> +{
>> +    int i;
>> +    uint8_t ret = 0;
>> +    for (i = 0; i < 8; ++i) {
>> +        if (value & BIT(i)) {
>> +            ret |= BIT(7 - i);
>> +        }
>> +    }
>
> Please use instead
>
>    value = ((value & 0xAA) >> 1) | ((value & 0x55) << 1);
>    value = ((value & 0xCC) >> 2) | ((value & 0x33) << 2);
>    value = ((value & 0xF0) >> 4) | ((value & 0x0F) << 4);
>    return value;
>
> Paolo
>

Got it, thanks

>> +    return ret;
>> +}
>> +
>> +/**
>> + * bitrev16:
>> + * @value: the value to reverse bit ordering from
>> + *
>> + * Reverse the 16 bit input @value
>> + *
>> + * Returns: the input @value with reversed bit ordering
>> + */
>> +static inline uint16_t bitrev16(uint16_t value)
>> +{
>> +    return (bitrev8(value & 0xff) << 8) | bitrev8(value >> 8);
>> +}
>> +
>> +/**
>> + * bitrev32:
>> + * @value: the value to reverse bit ordering from
>> + *
>> + * Reverse the 32 bit input @value
>> + *
>> + * Returns: the input @value with reversed bit ordering
>> + */
>> +static inline uint32_t bitrev32(uint32_t value)
>> +{
>> +    return (bitrev16(value & 0xffff) << 16) | bitrev16(value >> 16);
>> +}
>> +
>> +/**
>> + * bitrev64:
>> + * @value: the value to reverse bit ordering from
>> + *
>> + * Reverse the 64 bit input @value
>> + *
>> + * Returns: the input @value with reversed bit ordering
>> + */
>> +static inline uint64_t bitrev64(uint64_t value)
>> +{
>> +    return ((uint64_t)bitrev32(value & 0xffffffffULL) << 32)
>> +        | (uint64_t)bitrev32(value >> 32);
>> +}
>> +
>>  #endif
>>
>

Patch

diff --git a/include/qemu/bitops.h b/include/qemu/bitops.h
index affcc96..920d028 100644
--- a/include/qemu/bitops.h
+++ b/include/qemu/bitops.h
@@ -3,7 +3,8 @@ 
  *
  * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
  *
- * Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
+ * Mostly inspired by (stolen from) linux/bitmap.h, linux/bitops.h
+ * and linux/bitrev.h
  *
  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  * See the COPYING.LIB file in the top-level directory.
@@ -273,4 +274,64 @@  static inline uint64_t deposit64(uint64_t value, int start, int length,
     return (value & ~mask) | ((fieldval << start) & mask);
 }
 
+/**
+ * bitrev8:
+ * @value: the value to reverse bit ordering from
+ *
+ * Reverse the 8 bit input @value
+ *
+ * Returns: the input @value with reversed bit ordering
+ */
+static inline uint8_t bitrev8(uint8_t value)
+{
+    int i;
+    uint8_t ret = 0;
+    for (i = 0; i < 8; ++i) {
+        if (value & BIT(i)) {
+            ret |= BIT(7 - i);
+        }
+    }
+    return ret;
+}
+
+/**
+ * bitrev16:
+ * @value: the value to reverse bit ordering from
+ *
+ * Reverse the 16 bit input @value
+ *
+ * Returns: the input @value with reversed bit ordering
+ */
+static inline uint16_t bitrev16(uint16_t value)
+{
+    return (bitrev8(value & 0xff) << 8) | bitrev8(value >> 8);
+}
+
+/**
+ * bitrev32:
+ * @value: the value to reverse bit ordering from
+ *
+ * Reverse the 32 bit input @value
+ *
+ * Returns: the input @value with reversed bit ordering
+ */
+static inline uint32_t bitrev32(uint32_t value)
+{
+    return (bitrev16(value & 0xffff) << 16) | bitrev16(value >> 16);
+}
+
+/**
+ * bitrev64:
+ * @value: the value to reverse bit ordering from
+ *
+ * Reverse the 64 bit input @value
+ *
+ * Returns: the input @value with reversed bit ordering
+ */
+static inline uint64_t bitrev64(uint64_t value)
+{
+    return ((uint64_t)bitrev32(value & 0xffffffffULL) << 32)
+        | (uint64_t)bitrev32(value >> 32);
+}
+
 #endif