1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* bitrotate.h - Rotate bits in integers
4 Copyright (C) 2008-2011 Free Software Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program 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 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
21 #ifndef _GL_BITROTATE_H
22 #define _GL_BITROTATE_H
26 #include <sys/types.h>
29 /* Given an unsigned 64-bit argument X, return the value corresponding
30 to rotating the bits N steps to the left. N must be between 1 and
32 static inline uint64_t
33 rotl64 (uint64_t x, int n)
35 return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
38 /* Given an unsigned 64-bit argument X, return the value corresponding
39 to rotating the bits N steps to the right. N must be between 1 to
41 static inline uint64_t
42 rotr64 (uint64_t x, int n)
44 return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
48 /* Given an unsigned 32-bit argument X, return the value corresponding
49 to rotating the bits N steps to the left. N must be between 1 and
51 static inline uint32_t
52 rotl32 (uint32_t x, int n)
54 return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
57 /* Given an unsigned 32-bit argument X, return the value corresponding
58 to rotating the bits N steps to the right. N must be between 1 to
60 static inline uint32_t
61 rotr32 (uint32_t x, int n)
63 return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
66 /* Given a size_t argument X, return the value corresponding
67 to rotating the bits N steps to the left. N must be between 1 and
68 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */
70 rotl_sz (size_t x, int n)
72 return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
75 /* Given a size_t argument X, return the value corresponding
76 to rotating the bits N steps to the right. N must be between 1 to
77 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */
79 rotr_sz (size_t x, int n)
81 return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
84 /* Given an unsigned 16-bit argument X, return the value corresponding
85 to rotating the bits N steps to the left. N must be between 1 to
86 15 inclusive, but on most relevant targets N can also be 0 and 16
87 because 'int' is at least 32 bits and the arguments must widen
89 static inline uint16_t
90 rotl16 (uint16_t x, int n)
92 return ((x << n) | (x >> (16 - n))) & UINT16_MAX;
95 /* Given an unsigned 16-bit argument X, return the value corresponding
96 to rotating the bits N steps to the right. N must be in 1 to 15
97 inclusive, but on most relevant targets N can also be 0 and 16
98 because 'int' is at least 32 bits and the arguments must widen
100 static inline uint16_t
101 rotr16 (uint16_t x, int n)
103 return ((x >> n) | (x << (16 - n))) & UINT16_MAX;
106 /* Given an unsigned 8-bit argument X, return the value corresponding
107 to rotating the bits N steps to the left. N must be between 1 to 7
108 inclusive, but on most relevant targets N can also be 0 and 8
109 because 'int' is at least 32 bits and the arguments must widen
111 static inline uint8_t
112 rotl8 (uint8_t x, int n)
114 return ((x << n) | (x >> (8 - n))) & UINT8_MAX;
117 /* Given an unsigned 8-bit argument X, return the value corresponding
118 to rotating the bits N steps to the right. N must be in 1 to 7
119 inclusive, but on most relevant targets N can also be 0 and 8
120 because 'int' is at least 32 bits and the arguments must widen
122 static inline uint8_t
123 rotr8 (uint8_t x, int n)
125 return ((x >> n) | (x << (8 - n))) & UINT8_MAX;
128 #endif /* _GL_BITROTATE_H */