Fix BSD license name
[platform/upstream/libgcrypt.git] / cipher / bithelp.h
1 /* bithelp.h  -  Some bit manipulation helpers
2  *      Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3  *
4  * This file is part of Libgcrypt.
5  *
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.
10  *
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.
15  *
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
19  */
20 #ifndef G10_BITHELP_H
21 #define G10_BITHELP_H
22
23 #include "types.h"
24
25
26 /****************
27  * Rotate the 32 bit unsigned integer X by N bits left/right
28  */
29 static inline u32 rol(u32 x, int n)
30 {
31         return ( (x << (n&(32-1))) | (x >> ((32-n)&(32-1))) );
32 }
33
34 static inline u32 ror(u32 x, int n)
35 {
36         return ( (x >> (n&(32-1))) | (x << ((32-n)&(32-1))) );
37 }
38
39 /* Byte swap for 32-bit and 64-bit integers.  If available, use compiler
40    provided helpers.  */
41 #ifdef HAVE_BUILTIN_BSWAP32
42 # define _gcry_bswap32 __builtin_bswap32
43 #else
44 static inline u32
45 _gcry_bswap32(u32 x)
46 {
47         return ((rol(x, 8) & 0x00ff00ffL) | (ror(x, 8) & 0xff00ff00L));
48 }
49 #endif
50
51 #ifdef HAVE_U64_TYPEDEF
52 # ifdef HAVE_BUILTIN_BSWAP64
53 #  define _gcry_bswap64 __builtin_bswap64
54 # else
55 static inline u64
56 _gcry_bswap64(u64 x)
57 {
58         return ((u64)_gcry_bswap32(x) << 32) | (_gcry_bswap32(x >> 32));
59 }
60 # endif
61 #endif
62
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))
70 # endif
71 #else
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)
77 # endif
78 #endif
79
80 #endif /*G10_BITHELP_H*/