CommonFuncs: Generify rotation functions and move them to BitUtils.h

These are bit manipulation functions, so they belong within BitUtils.

This also gets rid of duplicated code and avoids relying on compiler
reserved names existing or not existing to determine whether or not we
define a set of functions.

Optimizers are smart enough in GCC and clang to transform the code to a
ROR or ROL instruction in the respective functions.
This commit is contained in:
Lioncash
2018-03-31 17:09:33 -04:00
parent 76e1a5b892
commit c3483a1823
7 changed files with 81 additions and 65 deletions

View File

@ -30,38 +30,6 @@ constexpr size_t ArraySize(T (&arr)[N])
__builtin_trap(); \
}
// GCC 4.8 defines all the rotate functions now
// Small issue with GCC's lrotl/lrotr intrinsics is they are still 32bit while we require 64bit
#ifndef _rotl
inline u32 _rotl(u32 x, int shift)
{
shift &= 31;
if (!shift)
return x;
return (x << shift) | (x >> (32 - shift));
}
inline u32 _rotr(u32 x, int shift)
{
shift &= 31;
if (!shift)
return x;
return (x >> shift) | (x << (32 - shift));
}
#endif
inline u64 _rotl64(u64 x, unsigned int shift)
{
unsigned int n = shift % 64;
return (x << n) | (x >> (64 - n));
}
inline u64 _rotr64(u64 x, unsigned int shift)
{
unsigned int n = shift % 64;
return (x >> n) | (x << (64 - n));
}
#else // WIN32
// Function Cross-Compatibility
#define strcasecmp _stricmp