From e059318d6cc92b3b11b974dfe252a8c4267e6188 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 21 Jan 2014 06:59:01 +0100 Subject: bit reversing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/algorithms/bits/Bits.java | 123 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 117 insertions(+), 6 deletions(-) diff --git a/src/algorithms/bits/Bits.java b/src/algorithms/bits/Bits.java index ace5fed..4d5c944 100644 --- a/src/algorithms/bits/Bits.java +++ b/src/algorithms/bits/Bits.java @@ -62,12 +62,35 @@ public class Bits * Lookup table for the parity of the bits in a byte */ private static byte[] PARITY_TABLE_256 = { £(parity-table 4 0) }; + + +£} + + /** + * Lookup table for the revered bit order in a byte + */ + private static byte[] REVERSE_TABLE_256 = { £(reverse-table 4 0) }; /** * Compute the parity of all bits in an integer, 64-bit multiply–modulus version * - * @param value The interger + * @param value The integer * @return The parity */ public static int parity_64bit(byte value) @@ -75,6 +98,39 @@ public class Bits return (int)(((value * 0x0101010101010101L) & 0x8040201008040201L) % 0x1FF) & 1; } + /** + * Reverse the order of all bits in an integer, 64-bit instructions version + * + * @param value The integer + * @return The integer reversed + */ + public static byte reverse_64bit(byte value) + { + return (byte)(((value * 0x0202020202L) & 0x010884422010L) % 1023); + } + + /** + * Reverse the order of all bits in an integer, division-free 64-bit instructions version + * + * @param value The integer + * @return The integer reversed + */ + public static byte reverse_64bit_noMod(byte value) + { + return (byte)(((value * 0x80200802L) & 0x0884422110L) * 0x0101010101L >> 32); + } + + /** + * Reverse the order of all bits in an integer, 32-bit instructions version + * + * @param value The integer + * @return The integer reversed + */ + public static byte reverse_32bit(byte value) + { + return (byte)(((value * 0x0802 & 0x22110) | (value * 0x8020 & 0x88440)) * 0x10101 >> 16); + } + £S=${T_S#*_} @@ -274,7 +330,7 @@ public class Bits /** * Compute the parity of all bits in an integer, naïve version * - * @param value The interger + * @param value The integer * @return The parity */ public static £{T} parity_naïve(£{T} value) @@ -291,7 +347,7 @@ public class Bits /** * Compute the parity of all bits in an integer, parallel version * - * @param value The interger + * @param value The integer * @return The parity */ public static £{T} parity_parallel(£{T} value) @@ -312,7 +368,7 @@ public class Bits /** * Compute the parity of all bits in an integer, partial lookup table version * - * @param value The interger + * @param value The integer * @return The parity */ public static byte parity_table(£{T} value) @@ -329,7 +385,7 @@ public class Bits /** * Compute the parity of all bits in an integer, optimised parallel version * - * @param value The interger + * @param value The integer * @return The parity */ public static int parity_optimised_parallel(£{T} value) @@ -347,7 +403,7 @@ public class Bits /** * Compute the parity of all bits in an integer, multiplication version * - * @param value The interger + * @param value The integer * @return The parity */ public static £{T} parity_multiplication(£{T} value) @@ -357,6 +413,61 @@ public class Bits value = (£{T})((value & (£{T})0x1111111111111111L) * (£{T})0x1111111111111111L); return (£{T})((value >> (£{S} - 4)) & 1); } + + /** + * Reverse the order of all bits in an integer, naïve version + * + * @param value The integer + * @return The integer reversed + */ + public static £{T} reverse_naïve(£{T} value) + { + int s = £{S} * 8 - 1; + £{T} rc = value; + for (value >>>= 1; value != 0; value >>>= 1) + { + rc <<= 1; + rc |= value & 1; + s--; + } + return (£{T})(rc << s); + } + + /** + * Reverse the order of all bits in an integer, parallel version + * + * @param value The integer + * @return The integer reversed + */ + public static £{T} reverse_parallel(£{T} value) + { + value = (£{T})(((value & (£{T})0x5555555555555555L) << 1) | ((value >> 1) & (£{T})0x5555555555555555L)); + value = (£{T})(((value & (£{T})0x3333333333333333L) << 2) | ((value >> 2) & (£{T})0x3333333333333333L)); + value = (£{T})(((value & (£{T})0x0F0F0F0F0F0F0F0FL) << 4) | ((value >> 4) & (£{T})0x0F0F0F0F0F0F0F0FL)); +£>(( $S > 1 )) && + value = (£{T})(((value & (£{T})0x00FF00FF00FF00FFL) << 8) | ((value >> 8) & (£{T})0x00FF00FF00FF00FFL)); +£>(( $S > 2 )) && + value = (£{T})(((value & (£{T})0x0000FFFF0000FFFFL) << 16) | ((value >> 16) & (£{T})0x0000FFFF0000FFFFL)); +£>(( $S > 4 )) && + value = (£{T})(((value & (£{T})0x00000000FFFFFFFFL) << 32) | ((value >> 32) & (£{T})0x00000000FFFFFFFFL)); + return value; + } + + /** + * Reverse the order of all bits in an integer, partial lookup table version + * + * @param value The integer + * @return The integer reversed + */ + public static £{T} reverse_table(£{T} value) + { +£>function _ { echo "(${T})(REVERSE_TABLE_256[(int)((value >> $1) & 255)] << $2)" ; } + £{T} rc = 0; +£>for s in `seq 0 8 $(( $S - 8 ))`; do + rc |= £(_ $s "($S - 8 - $s)"); +£>done + return rc; + } £>done } -- cgit v1.2.3-70-g09d2