[PATCH] Remove i386 inline assembly version of rotation functions

Jussi Kivilinna jussi.kivilinna at iki.fi
Thu Sep 19 13:55:16 CEST 2013


* cipher/bithelp.h (rol, ror): Remove i386 version, change
macros to inline functions.
* src/hmac256.c (ror): Ditto.
--

(Current) compilers can optimize '(x << c) | (x >> (32-c))' to rotation
instruction. So remove i386 specific assembly for manually doing this.
Furthermore, compiler can generate faster code in case where 'c' is
constant and can use rotate with immediate value rather than rotate
with %cl register.

Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
 cipher/bithelp.h |   26 ++++----------------------
 src/hmac256.c    |   13 ++-----------
 2 files changed, 6 insertions(+), 33 deletions(-)

diff --git a/cipher/bithelp.h b/cipher/bithelp.h
index 734dcbb..601ecac 100644
--- a/cipher/bithelp.h
+++ b/cipher/bithelp.h
@@ -26,33 +26,15 @@
 /****************
  * Rotate the 32 bit unsigned integer X by N bits left/right
  */
-#if defined(__GNUC__) && defined(__i386__)
-static inline u32
-rol( u32 x, int n)
+static inline u32 rol( u32 x, int n)
 {
-	__asm__("roll %%cl,%0"
-		:"=r" (x)
-		:"0" (x),"c" (n)
-		:"cc");
-	return x;
+	return ( (x << n) | (x >> (32-n)) );
 }
-#else
-#define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
-#endif
 
-#if defined(__GNUC__) && defined(__i386__)
-static inline u32
-ror(u32 x, int n)
+static inline u32 ror(u32 x, int n)
 {
-	__asm__("rorl %%cl,%0"
-		:"=r" (x)
-		:"0" (x),"c" (n)
-		:"cc");
-	return x;
+	return ( (x >> n) | (x << (32-n)) );
 }
-#else
-#define ror(x,n) ( ((x) >> (n)) | ((x) << (32-(n))) )
-#endif
 
 /* Byte swap for 32-bit and 64-bit integers.  If available, use compiler
    provided helpers.  */
diff --git a/src/hmac256.c b/src/hmac256.c
index 2fda47b..94a26da 100644
--- a/src/hmac256.c
+++ b/src/hmac256.c
@@ -98,19 +98,10 @@ struct hmac256_context
 
 
 /* Rotate a 32 bit word.  */
-#if defined(__GNUC__) && defined(__i386__)
-static inline u32
-ror(u32 x, int n)
+static inline u32 ror(u32 x, int n)
 {
-	__asm__("rorl %%cl,%0"
-		:"=r" (x)
-		:"0" (x),"c" (n)
-		:"cc");
-	return x;
+	return ( ((x) >> (n)) | ((x) << (32-(n))) );
 }
-#else
-#define ror(x,n) ( ((x) >> (n)) | ((x) << (32-(n))) )
-#endif
 
 #define my_wipememory2(_ptr,_set,_len) do { \
               volatile char *_vptr=(volatile char *)(_ptr); \




More information about the Gcrypt-devel mailing list