bitrotate: Add 64-bit rotates.
[gnulib.git] / lib / bitrotate.h
1 /* bitrotate.h - Rotate bits in integers
2    Copyright (C) 2008 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
18
19 #ifndef _GL_BITROTATE_H
20 #define _GL_BITROTATE_H
21
22 #include <stdint.h>
23
24 #if defined UINT64_MAX && defined UINT64_C
25 /* Given an unsigned 64-bit argument X, return the value corresponding
26    to rotating the bits N steps to the left.  N must be between 1 and
27    31 inclusive. */
28 static inline uint64_t
29 rotl64 (uint64_t x, int n)
30 {
31   return ((x << n) | (x >> (64 - n))) & 0xFFFFFFFFFFFFFFFFULL;
32 }
33
34 /* Given an unsigned 64-bit argument X, return the value corresponding
35    to rotating the bits N steps to the right.  N must be between 1 to
36    31 inclusive.*/
37 static inline uint64_t
38 rotr64 (uint64_t x, int n)
39 {
40   return ((x >> n) | (x << (64 - n))) & 0xFFFFFFFFFFFFFFFFULL;
41 }
42 #endif
43
44 /* Given an unsigned 32-bit argument X, return the value corresponding
45    to rotating the bits N steps to the left.  N must be between 1 and
46    31 inclusive. */
47 static inline uint32_t
48 rotl32 (uint32_t x, int n)
49 {
50   return ((x << n) | (x >> (32 - n))) & 0xFFFFFFFF;
51 }
52
53 /* Given an unsigned 32-bit argument X, return the value corresponding
54    to rotating the bits N steps to the right.  N must be between 1 to
55    31 inclusive.*/
56 static inline uint32_t
57 rotr32 (uint32_t x, int n)
58 {
59   return ((x >> n) | (x << (32 - n))) & 0xFFFFFFFF;
60 }
61
62 /* Given an unsigned 16-bit argument X, return the value corresponding
63    to rotating the bits N steps to the left.  N must be between 1 to
64    15 inclusive. */
65 static inline uint16_t
66 rotl16 (uint16_t x, int n)
67 {
68   return ((x << n) | (x >> (16 - n))) & 0xFFFF;
69 }
70
71 /* Given an unsigned 16-bit argument X, return the value corresponding
72    to rotating the bits N steps to the right.  N must be in 1 to 15
73    inclusive. */
74 static inline uint16_t
75 rotr16 (uint16_t x, int n)
76 {
77   return ((x >> n) | (x << (16 - n))) & 0xFFFF;
78 }
79
80 /* Given an unsigned 8-bit argument X, return the value corresponding
81    to rotating the bits N steps to the left.  N must be between 1 to 7
82    inclusive. */
83 static inline uint8_t
84 rotl8 (uint8_t x, int n)
85 {
86   return ((x << n) | (x >> (8 - n))) & 0xFF;
87 }
88
89 /* Given an unsigned 8-bit argument X, return the value corresponding
90    to rotating the bits N steps to the right.  N must be in 1 to 7
91    inclusive. */
92 static inline uint8_t
93 rotr8 (uint8_t x, int n)
94 {
95   return ((x >> n) | (x << (8 - n))) & 0xFF;
96 }
97
98 #endif /* _GL_BITROTATE_H */