195b7b453SJohn Marino /* bitrotate.h - Rotate bits in integers 2*680a9cb8SJohn Marino Copyright (C) 2008-2014 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 1595b7b453SJohn Marino along with this program. If not, see <http://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 26*680a9cb8SJohn Marino #ifndef _GL_INLINE_HEADER_BEGIN 27*680a9cb8SJohn Marino #error "Please include config.h first." 28*680a9cb8SJohn Marino #endif 29*680a9cb8SJohn Marino _GL_INLINE_HEADER_BEGIN 30*680a9cb8SJohn Marino #ifndef BITROTATE_INLINE 31*680a9cb8SJohn Marino # define BITROTATE_INLINE _GL_INLINE 32*680a9cb8SJohn Marino #endif 33*680a9cb8SJohn 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. */ 38*680a9cb8SJohn Marino BITROTATE_INLINE uint64_t 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.*/ 47*680a9cb8SJohn Marino BITROTATE_INLINE uint64_t 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. */ 57*680a9cb8SJohn Marino BITROTATE_INLINE uint32_t 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.*/ 66*680a9cb8SJohn Marino BITROTATE_INLINE uint32_t 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. */ 75*680a9cb8SJohn Marino BITROTATE_INLINE size_t 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. */ 84*680a9cb8SJohn Marino BITROTATE_INLINE size_t 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. */ 95*680a9cb8SJohn Marino BITROTATE_INLINE uint16_t 9695b7b453SJohn Marino rotl16 (uint16_t x, int n) 9795b7b453SJohn Marino { 9895b7b453SJohn Marino return ((x << n) | (x >> (16 - n))) & UINT16_MAX; 9995b7b453SJohn Marino } 10095b7b453SJohn Marino 10195b7b453SJohn Marino /* Given an unsigned 16-bit argument X, return the value corresponding 10295b7b453SJohn Marino to rotating the bits N steps to the right. N must be in 1 to 15 10395b7b453SJohn Marino inclusive, but on most relevant targets N can also be 0 and 16 10495b7b453SJohn Marino because 'int' is at least 32 bits and the arguments must widen 10595b7b453SJohn Marino before shifting. */ 106*680a9cb8SJohn Marino BITROTATE_INLINE uint16_t 10795b7b453SJohn Marino rotr16 (uint16_t x, int n) 10895b7b453SJohn Marino { 10995b7b453SJohn Marino return ((x >> n) | (x << (16 - n))) & UINT16_MAX; 11095b7b453SJohn Marino } 11195b7b453SJohn Marino 11295b7b453SJohn Marino /* Given an unsigned 8-bit argument X, return the value corresponding 11395b7b453SJohn Marino to rotating the bits N steps to the left. N must be between 1 to 7 11495b7b453SJohn Marino inclusive, but on most relevant targets N can also be 0 and 8 11595b7b453SJohn Marino because 'int' is at least 32 bits and the arguments must widen 11695b7b453SJohn Marino before shifting. */ 117*680a9cb8SJohn Marino BITROTATE_INLINE uint8_t 11895b7b453SJohn Marino rotl8 (uint8_t x, int n) 11995b7b453SJohn Marino { 12095b7b453SJohn Marino return ((x << n) | (x >> (8 - n))) & UINT8_MAX; 12195b7b453SJohn Marino } 12295b7b453SJohn Marino 12395b7b453SJohn Marino /* Given an unsigned 8-bit argument X, return the value corresponding 12495b7b453SJohn Marino to rotating the bits N steps to the right. N must be in 1 to 7 12595b7b453SJohn Marino inclusive, but on most relevant targets N can also be 0 and 8 12695b7b453SJohn Marino because 'int' is at least 32 bits and the arguments must widen 12795b7b453SJohn Marino before shifting. */ 128*680a9cb8SJohn Marino BITROTATE_INLINE uint8_t 12995b7b453SJohn Marino rotr8 (uint8_t x, int n) 13095b7b453SJohn Marino { 13195b7b453SJohn Marino return ((x >> n) | (x << (8 - n))) & UINT8_MAX; 13295b7b453SJohn Marino } 13395b7b453SJohn Marino 134*680a9cb8SJohn Marino _GL_INLINE_HEADER_END 135*680a9cb8SJohn Marino 13695b7b453SJohn Marino #endif /* _GL_BITROTATE_H */ 137