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