From 295dc574a07bde6e8220a2c9be0f3968ba4f6f24 Mon Sep 17 00:00:00 2001 From: bcook Date: Mon, 18 Aug 2014 19:15:34 +0000 Subject: [PATCH] replace more ROTATE macros with plain-old C code. Let the compiler optimize these. Even older versions of gcc generate equal or better quality code than the inline asm. ok miod@ --- lib/libcrypto/des/des_locl.h | 21 +++++---------- lib/libcrypto/rc5/rc5_locl.h | 38 ++++++++++------------------ lib/libssl/src/crypto/des/des_locl.h | 21 +++++---------- lib/libssl/src/crypto/rc5/rc5_locl.h | 38 ++++++++++------------------ 4 files changed, 38 insertions(+), 80 deletions(-) diff --git a/lib/libcrypto/des/des_locl.h b/lib/libcrypto/des/des_locl.h index 477aeb60d92..9480d374894 100644 --- a/lib/libcrypto/des/des_locl.h +++ b/lib/libcrypto/des/des_locl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: des_locl.h,v 1.16 2014/07/10 22:45:56 jsing Exp $ */ +/* $OpenBSD: des_locl.h,v 1.17 2014/08/18 19:15:34 bcook Exp $ */ /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -60,6 +60,7 @@ #define HEADER_DES_LOCL_H #include +#include #include #include #include @@ -131,20 +132,10 @@ } \ } -#if defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) -# if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) -# define ROTATE(a,n) ({ register unsigned int ret; \ - asm ("rorl %1,%0" \ - : "=r"(ret) \ - : "I"(n),"0"(a) \ - : "cc"); \ - ret; \ - }) -# endif -#endif -#ifndef ROTATE -#define ROTATE(a,n) (((a)>>(n))+((a)<<(32-(n)))) -#endif +static inline uint32_t ROTATE(uint32_t a, uint32_t n) +{ + return (a>>n)+(a<<(32-n)); +} /* Don't worry about the LOAD_DATA() stuff, that is used by * fcrypt() to add it's little bit to the front */ diff --git a/lib/libcrypto/rc5/rc5_locl.h b/lib/libcrypto/rc5/rc5_locl.h index 07671decaab..d4e0d30ecad 100644 --- a/lib/libcrypto/rc5/rc5_locl.h +++ b/lib/libcrypto/rc5/rc5_locl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: rc5_locl.h,v 1.5 2014/07/10 22:45:57 jsing Exp $ */ +/* $OpenBSD: rc5_locl.h,v 1.6 2014/08/18 19:15:34 bcook Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -56,6 +56,7 @@ * [including the GNU Public Licence.] */ +#include #include #include @@ -148,30 +149,17 @@ *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ *((c)++)=(unsigned char)(((l) )&0xff)) -#if defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) -# if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) -# define ROTATE_l32(a,n) ({ register unsigned int ret; \ - asm ("roll %%cl,%0" \ - : "=r"(ret) \ - : "c"(n),"0"((unsigned int)(a)) \ - : "cc"); \ - ret; \ - }) -# define ROTATE_r32(a,n) ({ register unsigned int ret; \ - asm ("rorl %%cl,%0" \ - : "=r"(ret) \ - : "c"(n),"0"((unsigned int)(a)) \ - : "cc"); \ - ret; \ - }) -# endif -#endif -#ifndef ROTATE_l32 -#define ROTATE_l32(a,n) (((a)<<(n&0x1f))|(((a)&0xffffffff)>>(32-(n&0x1f)))) -#endif -#ifndef ROTATE_r32 -#define ROTATE_r32(a,n) (((a)<<(32-(n&0x1f)))|(((a)&0xffffffff)>>(n&0x1f))) -#endif +static inline uint32_t ROTATE_l32(uint32_t a, uint32_t n) +{ + uint32_t amt = n & 0x1f; + return (a << amt) | (a >> (32 - amt)); +} + +static inline uint32_t ROTATE_r32(uint32_t a, uint32_t n) +{ + uint32_t amt = n & 0x1f; + return (a << (32 - amt)) | (a >> amt); +} #define RC5_32_MASK 0xffffffffL diff --git a/lib/libssl/src/crypto/des/des_locl.h b/lib/libssl/src/crypto/des/des_locl.h index 477aeb60d92..9480d374894 100644 --- a/lib/libssl/src/crypto/des/des_locl.h +++ b/lib/libssl/src/crypto/des/des_locl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: des_locl.h,v 1.16 2014/07/10 22:45:56 jsing Exp $ */ +/* $OpenBSD: des_locl.h,v 1.17 2014/08/18 19:15:34 bcook Exp $ */ /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -60,6 +60,7 @@ #define HEADER_DES_LOCL_H #include +#include #include #include #include @@ -131,20 +132,10 @@ } \ } -#if defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) -# if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) -# define ROTATE(a,n) ({ register unsigned int ret; \ - asm ("rorl %1,%0" \ - : "=r"(ret) \ - : "I"(n),"0"(a) \ - : "cc"); \ - ret; \ - }) -# endif -#endif -#ifndef ROTATE -#define ROTATE(a,n) (((a)>>(n))+((a)<<(32-(n)))) -#endif +static inline uint32_t ROTATE(uint32_t a, uint32_t n) +{ + return (a>>n)+(a<<(32-n)); +} /* Don't worry about the LOAD_DATA() stuff, that is used by * fcrypt() to add it's little bit to the front */ diff --git a/lib/libssl/src/crypto/rc5/rc5_locl.h b/lib/libssl/src/crypto/rc5/rc5_locl.h index 07671decaab..d4e0d30ecad 100644 --- a/lib/libssl/src/crypto/rc5/rc5_locl.h +++ b/lib/libssl/src/crypto/rc5/rc5_locl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: rc5_locl.h,v 1.5 2014/07/10 22:45:57 jsing Exp $ */ +/* $OpenBSD: rc5_locl.h,v 1.6 2014/08/18 19:15:34 bcook Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -56,6 +56,7 @@ * [including the GNU Public Licence.] */ +#include #include #include @@ -148,30 +149,17 @@ *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ *((c)++)=(unsigned char)(((l) )&0xff)) -#if defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) -# if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) -# define ROTATE_l32(a,n) ({ register unsigned int ret; \ - asm ("roll %%cl,%0" \ - : "=r"(ret) \ - : "c"(n),"0"((unsigned int)(a)) \ - : "cc"); \ - ret; \ - }) -# define ROTATE_r32(a,n) ({ register unsigned int ret; \ - asm ("rorl %%cl,%0" \ - : "=r"(ret) \ - : "c"(n),"0"((unsigned int)(a)) \ - : "cc"); \ - ret; \ - }) -# endif -#endif -#ifndef ROTATE_l32 -#define ROTATE_l32(a,n) (((a)<<(n&0x1f))|(((a)&0xffffffff)>>(32-(n&0x1f)))) -#endif -#ifndef ROTATE_r32 -#define ROTATE_r32(a,n) (((a)<<(32-(n&0x1f)))|(((a)&0xffffffff)>>(n&0x1f))) -#endif +static inline uint32_t ROTATE_l32(uint32_t a, uint32_t n) +{ + uint32_t amt = n & 0x1f; + return (a << amt) | (a >> (32 - amt)); +} + +static inline uint32_t ROTATE_r32(uint32_t a, uint32_t n) +{ + uint32_t amt = n & 0x1f; + return (a << (32 - amt)) | (a >> amt); +} #define RC5_32_MASK 0xffffffffL -- 2.20.1