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-2014 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>
28 #ifndef _GL_INLINE_HEADER_BEGIN
29 #error "Please include config.h first."
31 _GL_INLINE_HEADER_BEGIN
32 #ifndef BITROTATE_INLINE
33 # define BITROTATE_INLINE _GL_INLINE
37 /* Given an unsigned 64-bit argument X, return the value corresponding
38 to rotating the bits N steps to the left. N must be between 1 and
40 BITROTATE_INLINE uint64_t
41 rotl64 (uint64_t x, int n)
43 return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
46 /* Given an unsigned 64-bit argument X, return the value corresponding
47 to rotating the bits N steps to the right. N must be between 1 to
49 BITROTATE_INLINE uint64_t
50 rotr64 (uint64_t x, int n)
52 return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
56 /* Given an unsigned 32-bit argument X, return the value corresponding
57 to rotating the bits N steps to the left. N must be between 1 and
59 BITROTATE_INLINE uint32_t
60 rotl32 (uint32_t x, int n)
62 return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
65 /* Given an unsigned 32-bit argument X, return the value corresponding
66 to rotating the bits N steps to the right. N must be between 1 to
68 BITROTATE_INLINE uint32_t
69 rotr32 (uint32_t x, int n)
71 return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
74 /* Given a size_t argument X, return the value corresponding
75 to rotating the bits N steps to the left. N must be between 1 and
76 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */
77 BITROTATE_INLINE size_t
78 rotl_sz (size_t x, int n)
80 return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
83 /* Given a size_t argument X, return the value corresponding
84 to rotating the bits N steps to the right. N must be between 1 to
85 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */
86 BITROTATE_INLINE size_t
87 rotr_sz (size_t x, int n)
89 return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
92 /* Given an unsigned 16-bit argument X, return the value corresponding
93 to rotating the bits N steps to the left. N must be between 1 to
94 15 inclusive, but on most relevant targets N can also be 0 and 16
95 because 'int' is at least 32 bits and the arguments must widen
97 BITROTATE_INLINE uint16_t
98 rotl16 (uint16_t x, int n)
100 return ((x << n) | (x >> (16 - n))) & UINT16_MAX;
103 /* Given an unsigned 16-bit argument X, return the value corresponding
104 to rotating the bits N steps to the right. N must be in 1 to 15
105 inclusive, but on most relevant targets N can also be 0 and 16
106 because 'int' is at least 32 bits and the arguments must widen
108 BITROTATE_INLINE uint16_t
109 rotr16 (uint16_t x, int n)
111 return ((x >> n) | (x << (16 - n))) & UINT16_MAX;
114 /* Given an unsigned 8-bit argument X, return the value corresponding
115 to rotating the bits N steps to the left. N must be between 1 to 7
116 inclusive, but on most relevant targets N can also be 0 and 8
117 because 'int' is at least 32 bits and the arguments must widen
119 BITROTATE_INLINE uint8_t
120 rotl8 (uint8_t x, int n)
122 return ((x << n) | (x >> (8 - n))) & UINT8_MAX;
125 /* Given an unsigned 8-bit argument X, return the value corresponding
126 to rotating the bits N steps to the right. N must be in 1 to 7
127 inclusive, but on most relevant targets N can also be 0 and 8
128 because 'int' is at least 32 bits and the arguments must widen
130 BITROTATE_INLINE uint8_t
131 rotr8 (uint8_t x, int n)
133 return ((x >> n) | (x << (8 - n))) & UINT8_MAX;
136 _GL_INLINE_HEADER_END
138 #endif /* _GL_BITROTATE_H */