Blame lib/bitrotate.h

Packit 709fb3
/* bitrotate.h - Rotate bits in integers
Packit 709fb3
   Copyright (C) 2008-2017 Free Software Foundation, Inc.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
/* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
Packit 709fb3
Packit 709fb3
#ifndef _GL_BITROTATE_H
Packit 709fb3
#define _GL_BITROTATE_H
Packit 709fb3
Packit 709fb3
#include <limits.h>
Packit 709fb3
#include <stdint.h>
Packit 709fb3
#include <sys/types.h>
Packit 709fb3
Packit 709fb3
#ifndef _GL_INLINE_HEADER_BEGIN
Packit 709fb3
 #error "Please include config.h first."
Packit 709fb3
#endif
Packit 709fb3
_GL_INLINE_HEADER_BEGIN
Packit 709fb3
#ifndef BITROTATE_INLINE
Packit 709fb3
# define BITROTATE_INLINE _GL_INLINE
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#ifdef UINT64_MAX
Packit 709fb3
/* Given an unsigned 64-bit argument X, return the value corresponding
Packit 709fb3
   to rotating the bits N steps to the left.  N must be between 1 and
Packit 709fb3
   63 inclusive. */
Packit 709fb3
BITROTATE_INLINE uint64_t
Packit 709fb3
rotl64 (uint64_t x, int n)
Packit 709fb3
{
Packit 709fb3
  return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* Given an unsigned 64-bit argument X, return the value corresponding
Packit 709fb3
   to rotating the bits N steps to the right.  N must be between 1 to
Packit 709fb3
   63 inclusive.*/
Packit 709fb3
BITROTATE_INLINE uint64_t
Packit 709fb3
rotr64 (uint64_t x, int n)
Packit 709fb3
{
Packit 709fb3
  return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
Packit 709fb3
}
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
/* Given an unsigned 32-bit argument X, return the value corresponding
Packit 709fb3
   to rotating the bits N steps to the left.  N must be between 1 and
Packit 709fb3
   31 inclusive. */
Packit 709fb3
BITROTATE_INLINE uint32_t
Packit 709fb3
rotl32 (uint32_t x, int n)
Packit 709fb3
{
Packit 709fb3
  return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* Given an unsigned 32-bit argument X, return the value corresponding
Packit 709fb3
   to rotating the bits N steps to the right.  N must be between 1 to
Packit 709fb3
   31 inclusive.*/
Packit 709fb3
BITROTATE_INLINE uint32_t
Packit 709fb3
rotr32 (uint32_t x, int n)
Packit 709fb3
{
Packit 709fb3
  return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* Given a size_t argument X, return the value corresponding
Packit 709fb3
   to rotating the bits N steps to the left.  N must be between 1 and
Packit 709fb3
   (CHAR_BIT * sizeof (size_t) - 1) inclusive.  */
Packit 709fb3
BITROTATE_INLINE size_t
Packit 709fb3
rotl_sz (size_t x, int n)
Packit 709fb3
{
Packit 709fb3
  return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* Given a size_t argument X, return the value corresponding
Packit 709fb3
   to rotating the bits N steps to the right.  N must be between 1 to
Packit 709fb3
   (CHAR_BIT * sizeof (size_t) - 1) inclusive.  */
Packit 709fb3
BITROTATE_INLINE size_t
Packit 709fb3
rotr_sz (size_t x, int n)
Packit 709fb3
{
Packit 709fb3
  return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* Given an unsigned 16-bit argument X, return the value corresponding
Packit 709fb3
   to rotating the bits N steps to the left.  N must be between 1 to
Packit 709fb3
   15 inclusive, but on most relevant targets N can also be 0 and 16
Packit 709fb3
   because 'int' is at least 32 bits and the arguments must widen
Packit 709fb3
   before shifting. */
Packit 709fb3
BITROTATE_INLINE uint16_t
Packit 709fb3
rotl16 (uint16_t x, int n)
Packit 709fb3
{
Packit 709fb3
  return ((x << n) | (x >> (16 - n))) & UINT16_MAX;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* Given an unsigned 16-bit argument X, return the value corresponding
Packit 709fb3
   to rotating the bits N steps to the right.  N must be in 1 to 15
Packit 709fb3
   inclusive, but on most relevant targets N can also be 0 and 16
Packit 709fb3
   because 'int' is at least 32 bits and the arguments must widen
Packit 709fb3
   before shifting. */
Packit 709fb3
BITROTATE_INLINE uint16_t
Packit 709fb3
rotr16 (uint16_t x, int n)
Packit 709fb3
{
Packit 709fb3
  return ((x >> n) | (x << (16 - n))) & UINT16_MAX;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* Given an unsigned 8-bit argument X, return the value corresponding
Packit 709fb3
   to rotating the bits N steps to the left.  N must be between 1 to 7
Packit 709fb3
   inclusive, but on most relevant targets N can also be 0 and 8
Packit 709fb3
   because 'int' is at least 32 bits and the arguments must widen
Packit 709fb3
   before shifting. */
Packit 709fb3
BITROTATE_INLINE uint8_t
Packit 709fb3
rotl8 (uint8_t x, int n)
Packit 709fb3
{
Packit 709fb3
  return ((x << n) | (x >> (8 - n))) & UINT8_MAX;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* Given an unsigned 8-bit argument X, return the value corresponding
Packit 709fb3
   to rotating the bits N steps to the right.  N must be in 1 to 7
Packit 709fb3
   inclusive, but on most relevant targets N can also be 0 and 8
Packit 709fb3
   because 'int' is at least 32 bits and the arguments must widen
Packit 709fb3
   before shifting. */
Packit 709fb3
BITROTATE_INLINE uint8_t
Packit 709fb3
rotr8 (uint8_t x, int n)
Packit 709fb3
{
Packit 709fb3
  return ((x >> n) | (x << (8 - n))) & UINT8_MAX;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
_GL_INLINE_HEADER_END
Packit 709fb3
Packit 709fb3
#endif /* _GL_BITROTATE_H */