• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* bitrotate.h - Rotate bits in integers
2    Copyright (C) 2008-2012 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 <limits.h>
23 #include <stdint.h>
24 #include <sys/types.h>
25 
26 _GL_INLINE_HEADER_BEGIN
27 #ifndef BITROTATE_INLINE
28 # define BITROTATE_INLINE _GL_INLINE
29 #endif
30 
31 #ifdef UINT64_MAX
32 /* Given an unsigned 64-bit argument X, return the value corresponding
33    to rotating the bits N steps to the left.  N must be between 1 and
34    63 inclusive. */
35 BITROTATE_INLINE uint64_t
rotl64(uint64_t x,int n)36 rotl64 (uint64_t x, int n)
37 {
38   return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
39 }
40 
41 /* Given an unsigned 64-bit argument X, return the value corresponding
42    to rotating the bits N steps to the right.  N must be between 1 to
43    63 inclusive.*/
44 BITROTATE_INLINE uint64_t
rotr64(uint64_t x,int n)45 rotr64 (uint64_t x, int n)
46 {
47   return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
48 }
49 #endif
50 
51 /* Given an unsigned 32-bit argument X, return the value corresponding
52    to rotating the bits N steps to the left.  N must be between 1 and
53    31 inclusive. */
54 BITROTATE_INLINE uint32_t
rotl32(uint32_t x,int n)55 rotl32 (uint32_t x, int n)
56 {
57   return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
58 }
59 
60 /* Given an unsigned 32-bit argument X, return the value corresponding
61    to rotating the bits N steps to the right.  N must be between 1 to
62    31 inclusive.*/
63 BITROTATE_INLINE uint32_t
rotr32(uint32_t x,int n)64 rotr32 (uint32_t x, int n)
65 {
66   return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
67 }
68 
69 /* Given a size_t argument X, return the value corresponding
70    to rotating the bits N steps to the left.  N must be between 1 and
71    (CHAR_BIT * sizeof (size_t) - 1) inclusive.  */
72 BITROTATE_INLINE size_t
rotl_sz(size_t x,int n)73 rotl_sz (size_t x, int n)
74 {
75   return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
76 }
77 
78 /* Given a size_t argument X, return the value corresponding
79    to rotating the bits N steps to the right.  N must be between 1 to
80    (CHAR_BIT * sizeof (size_t) - 1) inclusive.  */
81 BITROTATE_INLINE size_t
rotr_sz(size_t x,int n)82 rotr_sz (size_t x, int n)
83 {
84   return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
85 }
86 
87 /* Given an unsigned 16-bit argument X, return the value corresponding
88    to rotating the bits N steps to the left.  N must be between 1 to
89    15 inclusive, but on most relevant targets N can also be 0 and 16
90    because 'int' is at least 32 bits and the arguments must widen
91    before shifting. */
92 BITROTATE_INLINE uint16_t
rotl16(uint16_t x,int n)93 rotl16 (uint16_t x, int n)
94 {
95   return ((x << n) | (x >> (16 - n))) & UINT16_MAX;
96 }
97 
98 /* Given an unsigned 16-bit argument X, return the value corresponding
99    to rotating the bits N steps to the right.  N must be in 1 to 15
100    inclusive, but on most relevant targets N can also be 0 and 16
101    because 'int' is at least 32 bits and the arguments must widen
102    before shifting. */
103 BITROTATE_INLINE uint16_t
rotr16(uint16_t x,int n)104 rotr16 (uint16_t x, int n)
105 {
106   return ((x >> n) | (x << (16 - n))) & UINT16_MAX;
107 }
108 
109 /* Given an unsigned 8-bit argument X, return the value corresponding
110    to rotating the bits N steps to the left.  N must be between 1 to 7
111    inclusive, but on most relevant targets N can also be 0 and 8
112    because 'int' is at least 32 bits and the arguments must widen
113    before shifting. */
114 BITROTATE_INLINE uint8_t
rotl8(uint8_t x,int n)115 rotl8 (uint8_t x, int n)
116 {
117   return ((x << n) | (x >> (8 - n))) & UINT8_MAX;
118 }
119 
120 /* Given an unsigned 8-bit argument X, return the value corresponding
121    to rotating the bits N steps to the right.  N must be in 1 to 7
122    inclusive, but on most relevant targets N can also be 0 and 8
123    because 'int' is at least 32 bits and the arguments must widen
124    before shifting. */
125 BITROTATE_INLINE uint8_t
rotr8(uint8_t x,int n)126 rotr8 (uint8_t x, int n)
127 {
128   return ((x >> n) | (x << (8 - n))) & UINT8_MAX;
129 }
130 
131 _GL_INLINE_HEADER_END
132 
133 #endif /* _GL_BITROTATE_H */
134