From 90258fdadace50db3ef72a46aa84e9004c7cb61a Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 16 Apr 2013 00:06:18 +0200 Subject: fix binary logarithm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- c/sha3.c | 14 +++++++++++--- java-c-jni/SHA3.c | 14 +++++++++++--- pure-java/SHA3.java | 10 ++++++---- python3/sha3sum.py | 7 ++++++- 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/c/sha3.c b/c/sha3.c index d8981ed..72f3e83 100644 --- a/c/sha3.c +++ b/c/sha3.c @@ -241,10 +241,18 @@ inline void revarraycopy(byte* src, long soff, byte* dest, long doff, long lengt /** * Binary logarithm * - * @param X:long The value of which to calculate the binary logarithm - * @return :long The binary logarithm + * @param x The value of which to calculate the binary logarithm + * @return The binary logarithm */ -#define lb(X) ((((X & 0xFF00) == 0 ? 0 : 8) + ((X & 0xF0F0) == 0 ? 0 : 4)) + (((X & 0xCCCC) == 0 ? 0 : 2) + ((X & 0xAAAA) == 0 ? 0 : 1))) +static long lb(long x) +{ + long rc = 0; + if ((x & 0xFF00) != 0) { rc += 8; x >>= 8; } + if ((x & 0x00F0) != 0) { rc += 4; x >>= 4; } + if ((x & 0x000C) != 0) { rc += 2; x >>= 2; } + if ((x & 0x0002) != 0) rc += 1; + return rc; +} /** diff --git a/java-c-jni/SHA3.c b/java-c-jni/SHA3.c index 9e93f01..ad01ebc 100644 --- a/java-c-jni/SHA3.c +++ b/java-c-jni/SHA3.c @@ -230,10 +230,18 @@ inline void revarraycopy(byte* src, long soff, byte* dest, long doff, long lengt /** * Binary logarithm * - * @param X:long The value of which to calculate the binary logarithm - * @return :long The binary logarithm + * @param x The value of which to calculate the binary logarithm + * @return The binary logarithm */ -#define lb(X) ((((X & 0xFF00) == 0 ? 0 : 8) + ((X & 0xF0F0) == 0 ? 0 : 4)) + (((X & 0xCCCC) == 0 ? 0 : 2) + ((X & 0xAAAA) == 0 ? 0 : 1))) +static long lb(long x) +{ + long rc = 0; + if ((x & 0xFF00) != 0) { rc += 8; x >>= 8; } + if ((x & 0x00F0) != 0) { rc += 4; x >>= 4; } + if ((x & 0x000C) != 0) { rc += 2; x >>= 2; } + if ((x & 0x0002) != 0) rc += 1; + return rc; +} /** diff --git a/pure-java/SHA3.java b/pure-java/SHA3.java index 0568b1f..6253094 100644 --- a/pure-java/SHA3.java +++ b/pure-java/SHA3.java @@ -150,10 +150,12 @@ public class SHA3 */ private static int lb(int x) { - return (((x & 0xFF00) == 0 ? 0 : 8) + - ((x & 0xF0F0) == 0 ? 0 : 4)) + - (((x & 0xCCCC) == 0 ? 0 : 2) + - ((x & 0xAAAA) == 0 ? 0 : 1)); + int rc = 0; + if ((x & 0xFF00) != 0) { rc += 8; x >>= 8; } + if ((x & 0x00F0) != 0) { rc += 4; x >>= 4; } + if ((x & 0x000C) != 0) { rc += 2; x >>= 2; } + if ((x & 0x0002) != 0) rc += 1; + return rc; } /** diff --git a/python3/sha3sum.py b/python3/sha3sum.py index 27b9fd1..e331df8 100755 --- a/python3/sha3sum.py +++ b/python3/sha3sum.py @@ -109,7 +109,12 @@ class SHA3: @param x:int The value of which to calculate the binary logarithm @return :int The binary logarithm ''' - return ((0 if (x & 0xFF00) == 0 else 8) + (0 if (x & 0xF0F0) == 0 else 4)) + ((0 if (x & 0xCCCC) == 0 else 2) + (0 if (x & 0xAAAA) == 0 else 1)) + rc = 0 + if (x & 0xFF00) != 0: rc += 8 ; x >>= 8 + if (x & 0x00F0) != 0: rc += 4 ; x >>= 4 + if (x & 0x000C) != 0: rc += 2 ; x >>= 2 + if (x & 0x0002) != 0: rc += 1 + return rc @staticmethod -- cgit v1.2.3-70-g09d2