diff options
Diffstat (limited to 'cipher/bithelp.h')
-rw-r--r-- | cipher/bithelp.h | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/cipher/bithelp.h b/cipher/bithelp.h index 785701e3..734dcbb5 100644 --- a/cipher/bithelp.h +++ b/cipher/bithelp.h @@ -20,6 +20,8 @@ #ifndef G10_BITHELP_H #define G10_BITHELP_H +#include "types.h" + /**************** * Rotate the 32 bit unsigned integer X by N bits left/right @@ -52,5 +54,43 @@ ror(u32 x, int n) #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. */ +#ifdef HAVE_BUILTIN_BSWAP32 +# define bswap32 __builtin_bswap32 +#else +static inline u32 bswap32(u32 x) +{ + return ((rol(x, 8) & 0x00ff00ffL) | (ror(x, 8) & 0xff00ff00L)); +} +#endif + +#ifdef HAVE_U64_TYPEDEF +# ifdef HAVE_BUILTIN_BSWAP64 +# define bswap64 __builtin_bswap64 +# else +static inline u64 bswap64(u64 x) +{ + return ((u64)bswap32(x) << 32) | (bswap32(x >> 32)); +} +# endif +#endif + +/* Endian dependent byte swap operations. */ +#ifdef WORDS_BIGENDIAN +# define le_bswap32(x) bswap32(x) +# define be_bswap32(x) ((u32)(x)) +# ifdef HAVE_U64_TYPEDEF +# define le_bswap64(x) bswap64(x) +# define be_bswap64(x) ((u64)(x)) +# endif +#else +# define le_bswap32(x) ((u32)(x)) +# define be_bswap32(x) bswap32(x) +# ifdef HAVE_U64_TYPEDEF +# define le_bswap64(x) ((u64)(x)) +# define be_bswap64(x) bswap64(x) +# endif +#endif #endif /*G10_BITHELP_H*/ |