summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2006-10-15 06:17:45 +0000
committerPaul Eggert <eggert@cs.ucla.edu>2006-10-15 06:17:45 +0000
commitdd8398a3c642b1397f64e338d2b0008e33790dec (patch)
treee74087d4d653ebb99f546e0cd894ba85c758d2f0 /lib
parent443d5095786055328de8ede2648b9331405de083 (diff)
downloadcoreutils-dd8398a3c642b1397f64e338d2b0008e33790dec.tar.xz
[m4/ChangeLog]
* sha512.m4 (gl_SHA512): Add u64.h to AC_LIBSOURCES. [lib/ChangeLog] Port sha512sum to hosts where uintmax_t is only 32 bits, e.g., HP/Tandom NonStop OSS circa 2005 has 32-bit uintmax_t, 64-bit intmax_t. * u64.h: New file. * sha512.c (SWAP, sha512_init_ctx, sha384_init_ctx, sha512_read_ctx): (sha384_read_ctx, sha512_conclude_ctx, sha512_process_bytes): (sha512_round_constants, F2, F1, sha512_process_block): (S0, S1, SS0, SS1, M, R): Rewrite to use u64.h instead of assuming uint64_t. * sha512.h: Include u64.h rather than stdint.h. (rol64): Remove; moved to u64.h and renamed to u64rol.
Diffstat (limited to 'lib')
-rw-r--r--lib/ChangeLog13
-rw-r--r--lib/sha512.c213
-rw-r--r--lib/sha512.h13
-rw-r--r--lib/u64.h160
4 files changed, 305 insertions, 94 deletions
diff --git a/lib/ChangeLog b/lib/ChangeLog
index c3774ff9d..17ad7ae53 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,16 @@
+2006-10-14 Paul Eggert <eggert@cs.ucla.edu>
+
+ Port sha512sum to hosts where uintmax_t is only 32 bits, e.g.,
+ HP/Tandom NonStop OSS circa 2005 has 32-bit uintmax_t, 64-bit intmax_t.
+ * u64.h: New file.
+ * sha512.c (SWAP, sha512_init_ctx, sha384_init_ctx, sha512_read_ctx):
+ (sha384_read_ctx, sha512_conclude_ctx, sha512_process_bytes):
+ (sha512_round_constants, F2, F1, sha512_process_block):
+ (S0, S1, SS0, SS1, M, R):
+ Rewrite to use u64.h instead of assuming uint64_t.
+ * sha512.h: Include u64.h rather than stdint.h.
+ (rol64): Remove; moved to u64.h and renamed to u64rol.
+
2006-10-12 Jim Meyering <jim@meyering.net>
* Makefile.am (libcoreutils_a_LIBADD): Append $(LIBOBJS),
diff --git a/lib/sha512.c b/lib/sha512.c
index 554e2dea2..e24c6f5a3 100644
--- a/lib/sha512.c
+++ b/lib/sha512.c
@@ -36,9 +36,14 @@
# define SWAP(n) (n)
#else
# define SWAP(n) \
- (((n) << 56) | (((n) & 0xff00) << 40) | (((n) & 0xff0000UL) << 24) \
- | (((n) & 0xff000000UL) << 8) | (((n) >> 8) & 0xff000000UL) \
- | (((n) >> 24) & 0xff0000UL) | (((n) >> 40) & 0xff00UL) | ((n) >> 56))
+ u64or (u64or (u64or (u64shl (n, 56), \
+ u64shl (u64and (n, u64lo (0x0000ff00)), 40)), \
+ u64or (u64shl (u64and (n, u64lo (0x00ff0000)), 24), \
+ u64shl (u64and (n, u64lo (0xff000000)), 8))), \
+ u64or (u64or (u64and (u64shr (n, 8), u64lo (0xff000000)), \
+ u64and (u64shr (n, 24), u64lo (0x00ff0000))), \
+ u64or (u64and (u64shr (n, 40), u64lo (0x0000ff00)), \
+ u64shr (n, 56))))
#endif
#define BLOCKSIZE 4096
@@ -59,32 +64,32 @@ static const unsigned char fillbuf[128] = { 0x80, 0 /* , 0, 0, ... */ };
void
sha512_init_ctx (struct sha512_ctx *ctx)
{
- ctx->state[0] = 0x6a09e667f3bcc908ULL;
- ctx->state[1] = 0xbb67ae8584caa73bULL;
- ctx->state[2] = 0x3c6ef372fe94f82bULL;
- ctx->state[3] = 0xa54ff53a5f1d36f1ULL;
- ctx->state[4] = 0x510e527fade682d1ULL;
- ctx->state[5] = 0x9b05688c2b3e6c1fULL;
- ctx->state[6] = 0x1f83d9abfb41bd6bULL;
- ctx->state[7] = 0x5be0cd19137e2179ULL;
-
- ctx->total[0] = ctx->total[1] = 0;
+ ctx->state[0] = u64hilo (0x6a09e667, 0xf3bcc908);
+ ctx->state[1] = u64hilo (0xbb67ae85, 0x84caa73b);
+ ctx->state[2] = u64hilo (0x3c6ef372, 0xfe94f82b);
+ ctx->state[3] = u64hilo (0xa54ff53a, 0x5f1d36f1);
+ ctx->state[4] = u64hilo (0x510e527f, 0xade682d1);
+ ctx->state[5] = u64hilo (0x9b05688c, 0x2b3e6c1f);
+ ctx->state[6] = u64hilo (0x1f83d9ab, 0xfb41bd6b);
+ ctx->state[7] = u64hilo (0x5be0cd19, 0x137e2179);
+
+ ctx->total[0] = ctx->total[1] = u64lo (0);
ctx->buflen = 0;
}
void
sha384_init_ctx (struct sha512_ctx *ctx)
{
- ctx->state[0] = 0xcbbb9d5dc1059ed8ULL;
- ctx->state[1] = 0x629a292a367cd507ULL;
- ctx->state[2] = 0x9159015a3070dd17ULL;
- ctx->state[3] = 0x152fecd8f70e5939ULL;
- ctx->state[4] = 0x67332667ffc00b31ULL;
- ctx->state[5] = 0x8eb44a8768581511ULL;
- ctx->state[6] = 0xdb0c2e0d64f98fa7ULL;
- ctx->state[7] = 0x47b5481dbefa4fa4ULL;
-
- ctx->total[0] = ctx->total[1] = 0;
+ ctx->state[0] = u64hilo (0xcbbb9d5d, 0xc1059ed8);
+ ctx->state[1] = u64hilo (0x629a292a, 0x367cd507);
+ ctx->state[2] = u64hilo (0x9159015a, 0x3070dd17);
+ ctx->state[3] = u64hilo (0x152fecd8, 0xf70e5939);
+ ctx->state[4] = u64hilo (0x67332667, 0xffc00b31);
+ ctx->state[5] = u64hilo (0x8eb44a87, 0x68581511);
+ ctx->state[6] = u64hilo (0xdb0c2e0d, 0x64f98fa7);
+ ctx->state[7] = u64hilo (0x47b5481d, 0xbefa4fa4);
+
+ ctx->total[0] = ctx->total[1] = u64lo (0);
ctx->buflen = 0;
}
@@ -99,7 +104,7 @@ sha512_read_ctx (const struct sha512_ctx *ctx, void *resbuf)
int i;
for (i = 0; i < 8; i++)
- ((uint64_t *) resbuf)[i] = SWAP (ctx->state[i]);
+ ((u64 *) resbuf)[i] = SWAP (ctx->state[i]);
return resbuf;
}
@@ -110,7 +115,7 @@ sha384_read_ctx (const struct sha512_ctx *ctx, void *resbuf)
int i;
for (i = 0; i < 6; i++)
- ((uint64_t *) resbuf)[i] = SWAP (ctx->state[i]);
+ ((u64 *) resbuf)[i] = SWAP (ctx->state[i]);
return resbuf;
}
@@ -124,17 +129,18 @@ static void
sha512_conclude_ctx (struct sha512_ctx *ctx)
{
/* Take yet unprocessed bytes into account. */
- uint64_t bytes = ctx->buflen;
+ size_t bytes = ctx->buflen;
size_t size = (bytes < 112) ? 128 / 8 : 128 * 2 / 8;
/* Now count remaining bytes. */
- ctx->total[0] += bytes;
- if (ctx->total[0] < bytes)
- ++ctx->total[1];
+ ctx->total[0] = u64plus (ctx->total[0], u64lo (bytes));
+ if (u64lt (ctx->total[0], u64lo (bytes)))
+ ctx->total[1] = u64plus (ctx->total[1], u64lo (1));
/* Put the 64-bit file length in *bits* at the end of the buffer. */
- ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 61));
- ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
+ ctx->buffer[size - 2] = SWAP (u64or (u64shl (ctx->total[1], 3),
+ u64shr (ctx->total[0], 61)));
+ ctx->buffer[size - 1] = SWAP (u64shl (ctx->total[0], 3));
memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 8 - bytes);
@@ -353,7 +359,7 @@ sha512_process_bytes (const void *buffer, size_t len, struct sha512_ctx *ctx)
{
#if !_STRING_ARCH_unaligned
# define alignof(type) offsetof (struct { char c; type x; }, x)
-# define UNALIGNED_P(p) (((size_t) p) % alignof (uint64_t) != 0)
+# define UNALIGNED_P(p) (((size_t) p) % alignof (u64) != 0)
if (UNALIGNED_P (buffer))
while (len > 128)
{
@@ -391,22 +397,52 @@ sha512_process_bytes (const void *buffer, size_t len, struct sha512_ctx *ctx)
/* SHA512 round constants */
#define K(I) sha512_round_constants[I]
-static const uint64_t sha512_round_constants[80] = {
- 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
- 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
- 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
- 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
- 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
- 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
- 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
- 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
- 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
- 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL,
+static u64 const sha512_round_constants[80] = {
+ u64init (0x428a2f98, 0xd728ae22), u64init (0x71374491, 0x23ef65cd),
+ u64init (0xb5c0fbcf, 0xec4d3b2f), u64init (0xe9b5dba5, 0x8189dbbc),
+ u64init (0x3956c25b, 0xf348b538), u64init (0x59f111f1, 0xb605d019),
+ u64init (0x923f82a4, 0xaf194f9b), u64init (0xab1c5ed5, 0xda6d8118),
+ u64init (0xd807aa98, 0xa3030242), u64init (0x12835b01, 0x45706fbe),
+ u64init (0x243185be, 0x4ee4b28c), u64init (0x550c7dc3, 0xd5ffb4e2),
+ u64init (0x72be5d74, 0xf27b896f), u64init (0x80deb1fe, 0x3b1696b1),
+ u64init (0x9bdc06a7, 0x25c71235), u64init (0xc19bf174, 0xcf692694),
+ u64init (0xe49b69c1, 0x9ef14ad2), u64init (0xefbe4786, 0x384f25e3),
+ u64init (0x0fc19dc6, 0x8b8cd5b5), u64init (0x240ca1cc, 0x77ac9c65),
+ u64init (0x2de92c6f, 0x592b0275), u64init (0x4a7484aa, 0x6ea6e483),
+ u64init (0x5cb0a9dc, 0xbd41fbd4), u64init (0x76f988da, 0x831153b5),
+ u64init (0x983e5152, 0xee66dfab), u64init (0xa831c66d, 0x2db43210),
+ u64init (0xb00327c8, 0x98fb213f), u64init (0xbf597fc7, 0xbeef0ee4),
+ u64init (0xc6e00bf3, 0x3da88fc2), u64init (0xd5a79147, 0x930aa725),
+ u64init (0x06ca6351, 0xe003826f), u64init (0x14292967, 0x0a0e6e70),
+ u64init (0x27b70a85, 0x46d22ffc), u64init (0x2e1b2138, 0x5c26c926),
+ u64init (0x4d2c6dfc, 0x5ac42aed), u64init (0x53380d13, 0x9d95b3df),
+ u64init (0x650a7354, 0x8baf63de), u64init (0x766a0abb, 0x3c77b2a8),
+ u64init (0x81c2c92e, 0x47edaee6), u64init (0x92722c85, 0x1482353b),
+ u64init (0xa2bfe8a1, 0x4cf10364), u64init (0xa81a664b, 0xbc423001),
+ u64init (0xc24b8b70, 0xd0f89791), u64init (0xc76c51a3, 0x0654be30),
+ u64init (0xd192e819, 0xd6ef5218), u64init (0xd6990624, 0x5565a910),
+ u64init (0xf40e3585, 0x5771202a), u64init (0x106aa070, 0x32bbd1b8),
+ u64init (0x19a4c116, 0xb8d2d0c8), u64init (0x1e376c08, 0x5141ab53),
+ u64init (0x2748774c, 0xdf8eeb99), u64init (0x34b0bcb5, 0xe19b48a8),
+ u64init (0x391c0cb3, 0xc5c95a63), u64init (0x4ed8aa4a, 0xe3418acb),
+ u64init (0x5b9cca4f, 0x7763e373), u64init (0x682e6ff3, 0xd6b2b8a3),
+ u64init (0x748f82ee, 0x5defb2fc), u64init (0x78a5636f, 0x43172f60),
+ u64init (0x84c87814, 0xa1f0ab72), u64init (0x8cc70208, 0x1a6439ec),
+ u64init (0x90befffa, 0x23631e28), u64init (0xa4506ceb, 0xde82bde9),
+ u64init (0xbef9a3f7, 0xb2c67915), u64init (0xc67178f2, 0xe372532b),
+ u64init (0xca273ece, 0xea26619c), u64init (0xd186b8c7, 0x21c0c207),
+ u64init (0xeada7dd6, 0xcde0eb1e), u64init (0xf57d4f7f, 0xee6ed178),
+ u64init (0x06f067aa, 0x72176fba), u64init (0x0a637dc5, 0xa2c898a6),
+ u64init (0x113f9804, 0xbef90dae), u64init (0x1b710b35, 0x131c471b),
+ u64init (0x28db77f5, 0x23047d84), u64init (0x32caab7b, 0x40c72493),
+ u64init (0x3c9ebe0a, 0x15c9bebc), u64init (0x431d67c4, 0x9c100d4c),
+ u64init (0x4cc5d4be, 0xcb3e42b6), u64init (0x597f299c, 0xfc657e2a),
+ u64init (0x5fcb6fab, 0x3ad6faec), u64init (0x6c44198c, 0x4a475817),
};
/* Round functions. */
-#define F2(A,B,C) ( ( A & B ) | ( C & ( A | B ) ) )
-#define F1(E,F,G) ( G ^ ( E & ( F ^ G ) ) )
+#define F2(A, B, C) u64or (u64and (A, B), u64and (C, u64or (A, B)))
+#define F1(E, F, G) u64xor (G, u64and (E, u64xor (F, G)))
/* Process LEN bytes of BUFFER, accumulating context into CTX.
It is assumed that LEN % 128 == 0.
@@ -415,47 +451,50 @@ static const uint64_t sha512_round_constants[80] = {
void
sha512_process_block (const void *buffer, size_t len, struct sha512_ctx *ctx)
{
- const uint64_t *words = buffer;
- size_t nwords = len / sizeof (uint64_t);
- const uint64_t *endp = words + nwords;
- uint64_t x[16];
- uint64_t a = ctx->state[0];
- uint64_t b = ctx->state[1];
- uint64_t c = ctx->state[2];
- uint64_t d = ctx->state[3];
- uint64_t e = ctx->state[4];
- uint64_t f = ctx->state[5];
- uint64_t g = ctx->state[6];
- uint64_t h = ctx->state[7];
+ u64 const *words = buffer;
+ u64 const *endp = words + len / sizeof (u64);
+ u64 x[16];
+ u64 a = ctx->state[0];
+ u64 b = ctx->state[1];
+ u64 c = ctx->state[2];
+ u64 d = ctx->state[3];
+ u64 e = ctx->state[4];
+ u64 f = ctx->state[5];
+ u64 g = ctx->state[6];
+ u64 h = ctx->state[7];
/* First increment the byte count. FIPS PUB 180-2 specifies the possible
length of the file up to 2^128 bits. Here we only compute the
number of bytes. Do a double word increment. */
- ctx->total[0] += len;
- if (ctx->total[0] < len)
- ++ctx->total[1];
-
-#define S0(x) (rol64(x,63)^rol64(x,56)^(x>>7))
-#define S1(x) (rol64(x,45)^rol64(x,3)^(x>>6))
-#define SS0(x) (rol64(x,36)^rol64(x,30)^rol64(x,25))
-#define SS1(x) (rol64(x,50)^rol64(x,46)^rol64(x,23))
-
-#define M(I) ( tm = S1(x[(I-2)&0x0f]) + x[(I-7)&0x0f] \
- + S0(x[(I-15)&0x0f]) + x[I&0x0f] \
- , x[I&0x0f] = tm )
-
-#define R(A,B,C,D,E,F,G,H,K,M) do { t0 = SS0(A) + F2(A,B,C); \
- t1 = H + SS1(E) \
- + F1(E,F,G) \
- + K \
- + M; \
- D += t1; H = t0 + t1; \
- } while(0)
+ ctx->total[0] = u64plus (ctx->total[0], u64lo (len));
+ if (u64lt (ctx->total[0], u64lo (len)))
+ ctx->total[1] = u64plus (ctx->total[1], u64lo (1));
+
+#define S0(x) u64xor (u64rol(x, 63), u64xor (u64rol (x, 56), u64shr (x, 7)))
+#define S1(x) u64xor (u64rol (x, 45), u64xor (u64rol (x, 3), u64shr (x, 6)))
+#define SS0(x) u64xor (u64rol (x, 36), u64xor (u64rol (x, 30), u64rol (x, 25)))
+#define SS1(x) u64xor (u64rol(x, 50), u64xor (u64rol (x, 46), u64rol (x, 23)))
+
+#define M(I) (x[(I) & 15] \
+ = u64plus (x[(I) & 15], \
+ u64plus (S1 (x[((I) - 2) & 15]), \
+ u64plus (x[((I) - 7) & 15], \
+ S0 (x[((I) - 15) & 15])))))
+
+#define R(A, B, C, D, E, F, G, H, K, M) \
+ do \
+ { \
+ u64 t0 = u64plus (SS0 (A), F2 (A, B, C)); \
+ u64 t1 = \
+ u64plus (H, u64plus (SS1 (E), \
+ u64plus (F1 (E, F, G), u64plus (K, M)))); \
+ D = u64plus (D, t1); \
+ H = u64plus (t0, t1); \
+ } \
+ while (0)
while (words < endp)
{
- uint64_t tm;
- uint64_t t0, t1;
int t;
/* FIXME: see sha1.c for a better implementation. */
for (t = 0; t < 16; t++)
@@ -545,13 +584,13 @@ sha512_process_block (const void *buffer, size_t len, struct sha512_ctx *ctx)
R( c, d, e, f, g, h, a, b, K(78), M(78) );
R( b, c, d, e, f, g, h, a, K(79), M(79) );
- a = ctx->state[0] += a;
- b = ctx->state[1] += b;
- c = ctx->state[2] += c;
- d = ctx->state[3] += d;
- e = ctx->state[4] += e;
- f = ctx->state[5] += f;
- g = ctx->state[6] += g;
- h = ctx->state[7] += h;
+ a = ctx->state[0] = u64plus (ctx->state[0], a);
+ b = ctx->state[1] = u64plus (ctx->state[1], b);
+ c = ctx->state[2] = u64plus (ctx->state[2], c);
+ d = ctx->state[3] = u64plus (ctx->state[3], d);
+ e = ctx->state[4] = u64plus (ctx->state[4], e);
+ f = ctx->state[5] = u64plus (ctx->state[5], f);
+ g = ctx->state[6] = u64plus (ctx->state[6], g);
+ h = ctx->state[7] = u64plus (ctx->state[7], h);
}
}
diff --git a/lib/sha512.h b/lib/sha512.h
index b90e71ad4..995511e91 100644
--- a/lib/sha512.h
+++ b/lib/sha512.h
@@ -20,16 +20,17 @@
# define SHA512_H 1
# include <stdio.h>
-# include <stdint.h>
+
+# include "u64.h"
/* Structure to save state of computation between the single steps. */
struct sha512_ctx
{
- uint64_t state[8];
+ u64 state[8];
- uint64_t total[2];
- uint64_t buflen;
- uint64_t buffer[32];
+ u64 total[2];
+ size_t buflen;
+ u64 buffer[32];
};
@@ -85,6 +86,4 @@ extern int sha384_stream (FILE *stream, void *resblock);
extern void *sha512_buffer (const char *buffer, size_t len, void *resblock);
extern void *sha384_buffer (const char *buffer, size_t len, void *resblock);
-# define rol64(x,n) ( ((x) << (n)) | ((x) >> (64-(n))) )
-
#endif
diff --git a/lib/u64.h b/lib/u64.h
new file mode 100644
index 000000000..1d581ecc7
--- /dev/null
+++ b/lib/u64.h
@@ -0,0 +1,160 @@
+/* uint64_t-like operations that work even on hosts lacking uint64_t
+
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation; either version 2, or (at your option) any
+ later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
+
+/* Written by Paul Eggert. */
+
+#include <stddef.h>
+#include <stdint.h>
+
+/* Return X rotated left by N bits, where 0 < N < 64. */
+#define u64rol(x, n) u64or (u64shl (x, n), u64shr (x, 64 - n))
+
+#ifdef UINT64_MAX
+
+/* Native implementations are trivial. See below for comments on what
+ these operations do. */
+typedef uint64_t u64;
+# define u64hilo(hi, lo) ((u64) (((u64) (hi) << 32) + (lo)))
+# define u64init(hi, lo) u64hilo (hi, lo)
+# define u64lo(x) ((u64) (x))
+# define u64lt(x, y) ((x) < (y))
+# define u64and(x, y) ((x) & (y))
+# define u64or(x, y) ((x) | (y))
+# define u64xor(x, y) ((x) ^ (y))
+# define u64plus(x, y) ((x) + (y))
+# define u64shl(x, n) ((x) << (n))
+# define u64shr(x, n) ((x) >> (n))
+
+#else
+
+/* u64 is a 64-bit unsigned integer value.
+ u64init (HI, LO), is like u64hilo (HI, LO), but for use in
+ initializer contexts. */
+# ifdef WORDS_BIGENDIAN
+typedef struct { uint32_t hi, lo; } u64;
+# define u64init(hi, lo) { hi, lo }
+# else
+typedef struct { uint32_t lo, hi; } u64;
+# define u64init(hi, lo) { lo, hi }
+# endif
+
+/* Given the high and low-order 32-bit quantities HI and LO, return a u64
+ value representing (HI << 32) + LO. */
+static inline u64
+u64hilo (uint32_t hi, uint32_t lo)
+{
+ u64 r;
+ r.hi = hi;
+ r.lo = lo;
+ return r;
+}
+
+/* Return a u64 value representing LO. */
+static inline u64
+u64lo (uint32_t lo)
+{
+ u64 r;
+ r.hi = 0;
+ r.lo = lo;
+ return r;
+}
+
+/* Return X < Y. */
+static inline int
+u64lt (u64 x, u64 y)
+{
+ return x.hi < y.hi || (x.hi == y.hi && x.lo < y.lo);
+}
+
+/* Return X & Y. */
+static inline u64
+u64and (u64 x, u64 y)
+{
+ u64 r;
+ r.hi = x.hi & y.hi;
+ r.lo = x.lo & y.lo;
+ return r;
+}
+
+/* Return X | Y. */
+static inline u64
+u64or (u64 x, u64 y)
+{
+ u64 r;
+ r.hi = x.hi | y.hi;
+ r.lo = x.lo | y.lo;
+ return r;
+}
+
+/* Return X ^ Y. */
+static inline u64
+u64xor (u64 x, u64 y)
+{
+ u64 r;
+ r.hi = x.hi ^ y.hi;
+ r.lo = x.lo ^ y.lo;
+ return r;
+}
+
+/* Return X + Y. */
+static inline u64
+u64plus (u64 x, u64 y)
+{
+ u64 r;
+ r.lo = x.lo + y.lo;
+ r.hi = x.hi + y.hi + (r.lo < x.lo);
+ return r;
+}
+
+/* Return X << N. */
+static inline u64
+u64shl (u64 x, int n)
+{
+ u64 r;
+ if (n < 32)
+ {
+ r.hi = (x.hi << n) | (x.lo >> (32 - n));
+ r.lo = x.lo << n;
+ }
+ else
+ {
+ r.hi = x.lo << (n - 32);
+ r.lo = 0;
+ }
+ return r;
+}
+
+/* Return X >> N. */
+static inline u64
+u64shr (u64 x, int n)
+{
+ u64 r;
+ if (n < 32)
+ {
+ r.hi = x.hi >> n;
+ r.lo = (x.hi << (32 - n)) | (x.lo >> n);
+ }
+ else
+ {
+ r.hi = 0;
+ r.lo = x.hi >> (n - 32);
+ }
+ return r;
+}
+
+#endif