Blame lib/bitrotate.h

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