xref: /dflybsd-src/contrib/grep/lib/bitrotate.h (revision 91b9ed38d3db6a8a8ac5b66da1d43e6e331e259a)
195b7b453SJohn Marino /* bitrotate.h - Rotate bits in integers
2*09d4459fSDaniel Fojt    Copyright (C) 2008-2020 Free Software Foundation, Inc.
395b7b453SJohn Marino 
495b7b453SJohn Marino    This program is free software: you can redistribute it and/or modify
595b7b453SJohn Marino    it under the terms of the GNU General Public License as published by
695b7b453SJohn Marino    the Free Software Foundation; either version 3 of the License, or
795b7b453SJohn Marino    (at your option) any later version.
895b7b453SJohn Marino 
995b7b453SJohn Marino    This program is distributed in the hope that it will be useful,
1095b7b453SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
1195b7b453SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1295b7b453SJohn Marino    GNU General Public License for more details.
1395b7b453SJohn Marino 
1495b7b453SJohn Marino    You should have received a copy of the GNU General Public License
15*09d4459fSDaniel Fojt    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
1695b7b453SJohn Marino 
1795b7b453SJohn Marino /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
1895b7b453SJohn Marino 
1995b7b453SJohn Marino #ifndef _GL_BITROTATE_H
2095b7b453SJohn Marino #define _GL_BITROTATE_H
2195b7b453SJohn Marino 
2295b7b453SJohn Marino #include <limits.h>
2395b7b453SJohn Marino #include <stdint.h>
2495b7b453SJohn Marino #include <sys/types.h>
2595b7b453SJohn Marino 
26680a9cb8SJohn Marino #ifndef _GL_INLINE_HEADER_BEGIN
27680a9cb8SJohn Marino  #error "Please include config.h first."
28680a9cb8SJohn Marino #endif
29680a9cb8SJohn Marino _GL_INLINE_HEADER_BEGIN
30680a9cb8SJohn Marino #ifndef BITROTATE_INLINE
31680a9cb8SJohn Marino # define BITROTATE_INLINE _GL_INLINE
32680a9cb8SJohn Marino #endif
33680a9cb8SJohn Marino 
3495b7b453SJohn Marino #ifdef UINT64_MAX
3595b7b453SJohn Marino /* Given an unsigned 64-bit argument X, return the value corresponding
3695b7b453SJohn Marino    to rotating the bits N steps to the left.  N must be between 1 and
3795b7b453SJohn Marino    63 inclusive. */
38680a9cb8SJohn Marino BITROTATE_INLINE uint64_t
rotl64(uint64_t x,int n)3995b7b453SJohn Marino rotl64 (uint64_t x, int n)
4095b7b453SJohn Marino {
4195b7b453SJohn Marino   return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
4295b7b453SJohn Marino }
4395b7b453SJohn Marino 
4495b7b453SJohn Marino /* Given an unsigned 64-bit argument X, return the value corresponding
4595b7b453SJohn Marino    to rotating the bits N steps to the right.  N must be between 1 to
4695b7b453SJohn Marino    63 inclusive.*/
47680a9cb8SJohn Marino BITROTATE_INLINE uint64_t
rotr64(uint64_t x,int n)4895b7b453SJohn Marino rotr64 (uint64_t x, int n)
4995b7b453SJohn Marino {
5095b7b453SJohn Marino   return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
5195b7b453SJohn Marino }
5295b7b453SJohn Marino #endif
5395b7b453SJohn Marino 
5495b7b453SJohn Marino /* Given an unsigned 32-bit argument X, return the value corresponding
5595b7b453SJohn Marino    to rotating the bits N steps to the left.  N must be between 1 and
5695b7b453SJohn Marino    31 inclusive. */
57680a9cb8SJohn Marino BITROTATE_INLINE uint32_t
rotl32(uint32_t x,int n)5895b7b453SJohn Marino rotl32 (uint32_t x, int n)
5995b7b453SJohn Marino {
6095b7b453SJohn Marino   return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
6195b7b453SJohn Marino }
6295b7b453SJohn Marino 
6395b7b453SJohn Marino /* Given an unsigned 32-bit argument X, return the value corresponding
6495b7b453SJohn Marino    to rotating the bits N steps to the right.  N must be between 1 to
6595b7b453SJohn Marino    31 inclusive.*/
66680a9cb8SJohn Marino BITROTATE_INLINE uint32_t
rotr32(uint32_t x,int n)6795b7b453SJohn Marino rotr32 (uint32_t x, int n)
6895b7b453SJohn Marino {
6995b7b453SJohn Marino   return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
7095b7b453SJohn Marino }
7195b7b453SJohn Marino 
7295b7b453SJohn Marino /* Given a size_t argument X, return the value corresponding
7395b7b453SJohn Marino    to rotating the bits N steps to the left.  N must be between 1 and
7495b7b453SJohn Marino    (CHAR_BIT * sizeof (size_t) - 1) inclusive.  */
75680a9cb8SJohn Marino BITROTATE_INLINE size_t
rotl_sz(size_t x,int n)7695b7b453SJohn Marino rotl_sz (size_t x, int n)
7795b7b453SJohn Marino {
7895b7b453SJohn Marino   return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
7995b7b453SJohn Marino }
8095b7b453SJohn Marino 
8195b7b453SJohn Marino /* Given a size_t argument X, return the value corresponding
8295b7b453SJohn Marino    to rotating the bits N steps to the right.  N must be between 1 to
8395b7b453SJohn Marino    (CHAR_BIT * sizeof (size_t) - 1) inclusive.  */
84680a9cb8SJohn Marino BITROTATE_INLINE size_t
rotr_sz(size_t x,int n)8595b7b453SJohn Marino rotr_sz (size_t x, int n)
8695b7b453SJohn Marino {
8795b7b453SJohn Marino   return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
8895b7b453SJohn Marino }
8995b7b453SJohn Marino 
9095b7b453SJohn Marino /* Given an unsigned 16-bit argument X, return the value corresponding
9195b7b453SJohn Marino    to rotating the bits N steps to the left.  N must be between 1 to
9295b7b453SJohn Marino    15 inclusive, but on most relevant targets N can also be 0 and 16
9395b7b453SJohn Marino    because 'int' is at least 32 bits and the arguments must widen
9495b7b453SJohn Marino    before shifting. */
95680a9cb8SJohn Marino BITROTATE_INLINE uint16_t
rotl16(uint16_t x,int n)9695b7b453SJohn Marino rotl16 (uint16_t x, int n)
9795b7b453SJohn Marino {
98*09d4459fSDaniel Fojt   return (((unsigned int) x << n) | ((unsigned int) x >> (16 - n)))
99*09d4459fSDaniel Fojt          & UINT16_MAX;
10095b7b453SJohn Marino }
10195b7b453SJohn Marino 
10295b7b453SJohn Marino /* Given an unsigned 16-bit argument X, return the value corresponding
10395b7b453SJohn Marino    to rotating the bits N steps to the right.  N must be in 1 to 15
10495b7b453SJohn Marino    inclusive, but on most relevant targets N can also be 0 and 16
10595b7b453SJohn Marino    because 'int' is at least 32 bits and the arguments must widen
10695b7b453SJohn Marino    before shifting. */
107680a9cb8SJohn Marino BITROTATE_INLINE uint16_t
rotr16(uint16_t x,int n)10895b7b453SJohn Marino rotr16 (uint16_t x, int n)
10995b7b453SJohn Marino {
110*09d4459fSDaniel Fojt   return (((unsigned int) x >> n) | ((unsigned int) x << (16 - n)))
111*09d4459fSDaniel Fojt          & UINT16_MAX;
11295b7b453SJohn Marino }
11395b7b453SJohn Marino 
11495b7b453SJohn Marino /* Given an unsigned 8-bit argument X, return the value corresponding
11595b7b453SJohn Marino    to rotating the bits N steps to the left.  N must be between 1 to 7
11695b7b453SJohn Marino    inclusive, but on most relevant targets N can also be 0 and 8
11795b7b453SJohn Marino    because 'int' is at least 32 bits and the arguments must widen
11895b7b453SJohn Marino    before shifting. */
119680a9cb8SJohn Marino BITROTATE_INLINE uint8_t
rotl8(uint8_t x,int n)12095b7b453SJohn Marino rotl8 (uint8_t x, int n)
12195b7b453SJohn Marino {
122*09d4459fSDaniel Fojt   return (((unsigned int) x << n) | ((unsigned int) x >> (8 - n))) & UINT8_MAX;
12395b7b453SJohn Marino }
12495b7b453SJohn Marino 
12595b7b453SJohn Marino /* Given an unsigned 8-bit argument X, return the value corresponding
12695b7b453SJohn Marino    to rotating the bits N steps to the right.  N must be in 1 to 7
12795b7b453SJohn Marino    inclusive, but on most relevant targets N can also be 0 and 8
12895b7b453SJohn Marino    because 'int' is at least 32 bits and the arguments must widen
12995b7b453SJohn Marino    before shifting. */
130680a9cb8SJohn Marino BITROTATE_INLINE uint8_t
rotr8(uint8_t x,int n)13195b7b453SJohn Marino rotr8 (uint8_t x, int n)
13295b7b453SJohn Marino {
133*09d4459fSDaniel Fojt   return (((unsigned int) x >> n) | ((unsigned int) x << (8 - n))) & UINT8_MAX;
13495b7b453SJohn Marino }
13595b7b453SJohn Marino 
136680a9cb8SJohn Marino _GL_INLINE_HEADER_END
137680a9cb8SJohn Marino 
13895b7b453SJohn Marino #endif /* _GL_BITROTATE_H */
139