1 /* bithelp.h - Some bit manipulation helpers
2 * Copyright (C) 1999, 2002 Free Software Foundation, Inc.
4 * This file is part of Libgcrypt.
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
27 * Rotate the 32 bit unsigned integer X by N bits left/right
29 static inline u32 rol(u32 x, int n)
31 return ( (x << (n&(32-1))) | (x >> ((32-n)&(32-1))) );
34 static inline u32 ror(u32 x, int n)
36 return ( (x >> (n&(32-1))) | (x << ((32-n)&(32-1))) );
39 /* Byte swap for 32-bit and 64-bit integers. If available, use compiler
41 #ifdef HAVE_BUILTIN_BSWAP32
42 # define _gcry_bswap32 __builtin_bswap32
47 return ((rol(x, 8) & 0x00ff00ffL) | (ror(x, 8) & 0xff00ff00L));
51 #ifdef HAVE_U64_TYPEDEF
52 # ifdef HAVE_BUILTIN_BSWAP64
53 # define _gcry_bswap64 __builtin_bswap64
58 return ((u64)_gcry_bswap32(x) << 32) | (_gcry_bswap32(x >> 32));
63 /* Endian dependent byte swap operations. */
64 #ifdef WORDS_BIGENDIAN
65 # define le_bswap32(x) _gcry_bswap32(x)
66 # define be_bswap32(x) ((u32)(x))
67 # ifdef HAVE_U64_TYPEDEF
68 # define le_bswap64(x) _gcry_bswap64(x)
69 # define be_bswap64(x) ((u64)(x))
72 # define le_bswap32(x) ((u32)(x))
73 # define be_bswap32(x) _gcry_bswap32(x)
74 # ifdef HAVE_U64_TYPEDEF
75 # define le_bswap64(x) ((u64)(x))
76 # define be_bswap64(x) _gcry_bswap64(x)
80 #endif /*G10_BITHELP_H*/