Blame lib/bitrotate.h

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