Create rol / ror functions for all integer sizes.

This commit is contained in:
Dag-Erling Smørgrav 2014-07-19 15:36:07 +00:00 committed by des
parent c658232e3e
commit fe2d45ad56
4 changed files with 24 additions and 21 deletions

View file

@ -36,18 +36,21 @@
#ifndef CRYB_BITWISE_H_INCLUDED
#define CRYB_BITWISE_H_INCLUDED
static inline uint32_t
rol(uint32_t i, int n)
{
#define CRYB_ROL_ROR(N) \
static inline uint##N##_t rol##N(uint##N##_t i, int n) \
{ \
return (i << n | i >> (N - n)); \
} \
static inline uint##N##_t ror##N(uint##N##_t i, int n) \
{ \
return (i << (N - n) | i >> n); \
}
return (i << n | i >> (32 - n));
}
CRYB_ROL_ROR(8);
CRYB_ROL_ROR(16);
CRYB_ROL_ROR(32);
CRYB_ROL_ROR(64);
static inline uint32_t
ror(uint32_t i, int n)
{
return (i << (32 - n) | i >> n);
}
#undef CRYB_ROL_ROR
#endif

View file

@ -85,25 +85,25 @@ md5_init(md5_ctx *ctx)
#define md5_f(i, a, b, c, d, x, s) do { \
(a) += (((b) & (c)) | (~(b) & (d))); \
(a) += (x)[(i)] + md5_k[(i)]; \
(a) = rol((a), (s)) + (b); \
(a) = rol32((a), (s)) + (b); \
} while (0)
#define md5_g(i, a, b, c, d, x, s) do { \
(a) += (((b) & (d)) | ((c) & ~(d))); \
(a) += (x)[(5 * (i) + 1) % 16] + md5_k[(i)]; \
(a) = rol((a), (s)) + (b); \
(a) = rol32((a), (s)) + (b); \
} while (0)
#define md5_h(i, a, b, c, d, x, s) do { \
(a) += ((b) ^ (c) ^ (d)); \
(a) += (x)[(3 * (i) + 5) % 16] + md5_k[(i)]; \
(a) = rol((a), (s)) + (b); \
(a) = rol32((a), (s)) + (b); \
} while (0)
#define md5_i(i, a, b, c, d, x, s) do { \
(a) += ((c) ^ ((b) | ~(d))); \
(a) += (x)[(7 * (i)) % 16] + md5_k[(i)]; \
(a) = rol((a), (s)) + (b); \
(a) = rol32((a), (s)) + (b); \
} while (0)
static void

View file

@ -71,10 +71,10 @@ sha1_init(sha1_ctx *ctx)
#define sha1_maj(x, y, z) (((x & y) ^ (x & z) ^ (y & z)))
#define sha1_step(t, a, f, e, w) \
do { \
uint32_t T = rol(a, 5) + f + e + sha1_k[t/20] + w[t]; \
uint32_t T = rol32(a, 5) + f + e + sha1_k[t/20] + w[t]; \
e = d; \
d = c; \
c = rol(b, 30); \
c = rol32(b, 30); \
b = a; \
a = T; \
} while (0)
@ -89,7 +89,7 @@ sha1_compute(sha1_ctx *ctx, const uint8_t *block)
w[i] = be32toh(w[i]);
for (int i = 16; i < 80; ++i) {
w[i] = w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16];
w[i] = rol(w[i], 1);
w[i] = rol32(w[i], 1);
}
a = ctx->h[0];
b = ctx->h[1];

View file

@ -71,10 +71,10 @@ murmur3_32_hash(const void *data, size_t len, uint32_t seed)
bytes += 4;
res -= 4;
k *= 0xcc9e2d51;
k = rol(k, 15);
k = rol32(k, 15);
k *= 0x1b873593;
hash ^= k;
hash = rol(hash, 13);
hash = rol32(hash, 13);
hash *= 5;
hash += 0xe6546b64;
}
@ -91,7 +91,7 @@ murmur3_32_hash(const void *data, size_t len, uint32_t seed)
case 1:
k |= bytes[0];
k *= 0xcc9e2d51;
k = rol(k, 15);
k = rol32(k, 15);
k *= 0x1b873593;
hash ^= k;
break;