2016-09-05 12:12:31 +00:00
|
|
|
/**
|
2016-09-11 20:20:15 +00:00
|
|
|
* Author......: See docs/credits.txt
|
2016-09-05 12:12:31 +00:00
|
|
|
* License.....: MIT
|
|
|
|
*/
|
|
|
|
|
2016-09-05 19:47:26 +00:00
|
|
|
#include "common.h"
|
2016-09-16 15:01:18 +00:00
|
|
|
#include "types.h"
|
2016-09-05 12:12:31 +00:00
|
|
|
#include "bitops.h"
|
|
|
|
|
2017-08-27 17:46:17 +00:00
|
|
|
u32 __add3_S (const u32 a, const u32 b, const u32 c)
|
|
|
|
{
|
|
|
|
return a + b + c;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 __add3 (const u32 a, const u32 b, const u32 c)
|
|
|
|
{
|
|
|
|
return a + b + c;
|
|
|
|
}
|
|
|
|
|
2016-09-05 12:12:31 +00:00
|
|
|
u32 rotl32 (const u32 a, const u32 n)
|
|
|
|
{
|
2016-10-01 10:29:05 +00:00
|
|
|
#if defined (_MSC_VER)
|
|
|
|
return _rotl (a, n);
|
|
|
|
#else
|
2016-09-05 12:12:31 +00:00
|
|
|
return ((a << n) | (a >> (32 - n)));
|
2016-10-01 10:29:05 +00:00
|
|
|
#endif
|
2016-09-05 12:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 rotr32 (const u32 a, const u32 n)
|
|
|
|
{
|
2016-10-01 10:29:05 +00:00
|
|
|
#if defined (_MSC_VER)
|
|
|
|
return _rotr (a, n);
|
|
|
|
#else
|
2016-09-05 12:12:31 +00:00
|
|
|
return ((a >> n) | (a << (32 - n)));
|
2016-10-01 10:29:05 +00:00
|
|
|
#endif
|
2016-09-05 12:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 rotl64 (const u64 a, const u64 n)
|
|
|
|
{
|
2016-10-01 10:29:05 +00:00
|
|
|
#if defined (_MSC_VER)
|
|
|
|
return _rotl64 (a, n);
|
|
|
|
#else
|
2016-09-05 12:12:31 +00:00
|
|
|
return ((a << n) | (a >> (64 - n)));
|
2016-10-01 10:29:05 +00:00
|
|
|
#endif
|
2016-09-05 12:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 rotr64 (const u64 a, const u64 n)
|
|
|
|
{
|
2016-10-01 10:29:05 +00:00
|
|
|
#if defined (_MSC_VER)
|
|
|
|
return _rotr64 (a, n);
|
|
|
|
#else
|
2016-09-05 12:12:31 +00:00
|
|
|
return ((a >> n) | (a << (64 - n)));
|
2016-10-01 10:29:05 +00:00
|
|
|
#endif
|
2016-09-05 12:12:31 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 17:03:47 +00:00
|
|
|
u16 byte_swap_16 (const u16 n)
|
|
|
|
{
|
2017-01-21 17:14:44 +00:00
|
|
|
return (n & 0xff00) >> 8
|
|
|
|
| (n & 0x00ff) << 8;
|
2017-01-21 17:03:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-05 12:12:31 +00:00
|
|
|
u32 byte_swap_32 (const u32 n)
|
|
|
|
{
|
2016-10-01 10:29:05 +00:00
|
|
|
#if defined (_MSC_VER)
|
|
|
|
return _byteswap_ulong (n);
|
|
|
|
#elif defined (__clang__) || defined (__GNUC__)
|
|
|
|
return __builtin_bswap32 (n);
|
|
|
|
#else
|
2016-09-05 12:12:31 +00:00
|
|
|
return (n & 0xff000000) >> 24
|
|
|
|
| (n & 0x00ff0000) >> 8
|
|
|
|
| (n & 0x0000ff00) << 8
|
|
|
|
| (n & 0x000000ff) << 24;
|
2016-10-01 10:29:05 +00:00
|
|
|
#endif
|
2016-09-05 12:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 byte_swap_64 (const u64 n)
|
|
|
|
{
|
2016-10-01 10:29:05 +00:00
|
|
|
#if defined (_MSC_VER)
|
|
|
|
return _byteswap_uint64 (n);
|
|
|
|
#elif defined (__clang__) || defined (__GNUC__)
|
|
|
|
return __builtin_bswap64 (n);
|
|
|
|
#else
|
2016-09-05 12:12:31 +00:00
|
|
|
return (n & 0xff00000000000000ULL) >> 56
|
|
|
|
| (n & 0x00ff000000000000ULL) >> 40
|
|
|
|
| (n & 0x0000ff0000000000ULL) >> 24
|
|
|
|
| (n & 0x000000ff00000000ULL) >> 8
|
|
|
|
| (n & 0x00000000ff000000ULL) << 8
|
|
|
|
| (n & 0x0000000000ff0000ULL) << 24
|
|
|
|
| (n & 0x000000000000ff00ULL) << 40
|
|
|
|
| (n & 0x00000000000000ffULL) << 56;
|
2016-10-01 10:29:05 +00:00
|
|
|
#endif
|
2016-09-05 12:12:31 +00:00
|
|
|
}
|