Skip to content

Commit 7767727

Browse files
committed
[Support] Reformat SipHash.cpp to match libSupport.
While there, give it our usual file header and an acknowledgement.
1 parent cfbed2c commit 7767727

File tree

1 file changed

+129
-135
lines changed

1 file changed

+129
-135
lines changed

llvm/lib/Support/SipHash.cpp

Lines changed: 129 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
1-
/*
2-
SipHash reference C implementation
3-
4-
Copyright (c) 2012-2022 Jean-Philippe Aumasson
5-
6-
Copyright (c) 2012-2014 Daniel J. Bernstein <[email protected]>
7-
8-
To the extent possible under law, the author(s) have dedicated all copyright
9-
and related and neighboring rights to this software to the public domain
10-
worldwide. This software is distributed without any warranty.
11-
12-
You should have received a copy of the CC0 Public Domain Dedication along
13-
with
14-
this software. If not, see
15-
<http://creativecommons.org/publicdomain/zero/1.0/>.
16-
*/
1+
//===--- SipHash.cpp - An ABI-stable string hash --------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
178

189
#include "siphash.h"
1910
#include <assert.h>
2011
#include <stddef.h>
2112
#include <stdint.h>
2213

14+
// Lightly adapted from the SipHash reference C implementation by
15+
// Jean-Philippe Aumasson and Daniel J. Bernstein.
16+
2317
/* default: SipHash-2-4 */
2418
#ifndef cROUNDS
2519
#define cROUNDS 2
@@ -31,49 +25,49 @@
3125
#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
3226

3327
#define U32TO8_LE(p, v) \
34-
(p)[0] = (uint8_t)((v)); \
35-
(p)[1] = (uint8_t)((v) >> 8); \
36-
(p)[2] = (uint8_t)((v) >> 16); \
37-
(p)[3] = (uint8_t)((v) >> 24);
28+
(p)[0] = (uint8_t)((v)); \
29+
(p)[1] = (uint8_t)((v) >> 8); \
30+
(p)[2] = (uint8_t)((v) >> 16); \
31+
(p)[3] = (uint8_t)((v) >> 24);
3832

3933
#define U64TO8_LE(p, v) \
40-
U32TO8_LE((p), (uint32_t)((v))); \
41-
U32TO8_LE((p) + 4, (uint32_t)((v) >> 32));
34+
U32TO8_LE((p), (uint32_t)((v))); \
35+
U32TO8_LE((p) + 4, (uint32_t)((v) >> 32));
4236

4337
#define U8TO64_LE(p) \
44-
(((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \
45-
((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \
46-
((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \
47-
((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
38+
(((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \
39+
((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \
40+
((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \
41+
((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
4842

4943
#define SIPROUND \
50-
do { \
51-
v0 += v1; \
52-
v1 = ROTL(v1, 13); \
53-
v1 ^= v0; \
54-
v0 = ROTL(v0, 32); \
55-
v2 += v3; \
56-
v3 = ROTL(v3, 16); \
57-
v3 ^= v2; \
58-
v0 += v3; \
59-
v3 = ROTL(v3, 21); \
60-
v3 ^= v0; \
61-
v2 += v1; \
62-
v1 = ROTL(v1, 17); \
63-
v1 ^= v2; \
64-
v2 = ROTL(v2, 32); \
65-
} while (0)
44+
do { \
45+
v0 += v1; \
46+
v1 = ROTL(v1, 13); \
47+
v1 ^= v0; \
48+
v0 = ROTL(v0, 32); \
49+
v2 += v3; \
50+
v3 = ROTL(v3, 16); \
51+
v3 ^= v2; \
52+
v0 += v3; \
53+
v3 = ROTL(v3, 21); \
54+
v3 ^= v0; \
55+
v2 += v1; \
56+
v1 = ROTL(v1, 17); \
57+
v1 ^= v2; \
58+
v2 = ROTL(v2, 32); \
59+
} while (0)
6660

6761
#ifdef DEBUG_SIPHASH
6862
#include <stdio.h>
6963

7064
#define TRACE \
71-
do { \
72-
printf("(%3zu) v0 %016" PRIx64 "\n", inlen, v0); \
73-
printf("(%3zu) v1 %016" PRIx64 "\n", inlen, v1); \
74-
printf("(%3zu) v2 %016" PRIx64 "\n", inlen, v2); \
75-
printf("(%3zu) v3 %016" PRIx64 "\n", inlen, v3); \
76-
} while (0)
65+
do { \
66+
printf("(%3zu) v0 %016" PRIx64 "\n", inlen, v0); \
67+
printf("(%3zu) v1 %016" PRIx64 "\n", inlen, v1); \
68+
printf("(%3zu) v2 %016" PRIx64 "\n", inlen, v2); \
69+
printf("(%3zu) v3 %016" PRIx64 "\n", inlen, v3); \
70+
} while (0)
7771
#else
7872
#define TRACE
7973
#endif
@@ -82,104 +76,104 @@
8276
Computes a SipHash value
8377
*in: pointer to input data (read-only)
8478
inlen: input data length in bytes (any size_t value)
85-
*k: pointer to the key data (read-only), must be 16 bytes
79+
*k: pointer to the key data (read-only), must be 16 bytes
8680
*out: pointer to output data (write-only), outlen bytes must be allocated
8781
outlen: length of the output in bytes, must be 8 or 16
8882
*/
8983
int siphash(const void *in, const size_t inlen, const void *k, uint8_t *out,
9084
const size_t outlen) {
9185

92-
const unsigned char *ni = (const unsigned char *)in;
93-
const unsigned char *kk = (const unsigned char *)k;
94-
95-
assert((outlen == 8) || (outlen == 16));
96-
uint64_t v0 = UINT64_C(0x736f6d6570736575);
97-
uint64_t v1 = UINT64_C(0x646f72616e646f6d);
98-
uint64_t v2 = UINT64_C(0x6c7967656e657261);
99-
uint64_t v3 = UINT64_C(0x7465646279746573);
100-
uint64_t k0 = U8TO64_LE(kk);
101-
uint64_t k1 = U8TO64_LE(kk + 8);
102-
uint64_t m;
103-
int i;
104-
const unsigned char *end = ni + inlen - (inlen % sizeof(uint64_t));
105-
const int left = inlen & 7;
106-
uint64_t b = ((uint64_t)inlen) << 56;
107-
v3 ^= k1;
108-
v2 ^= k0;
109-
v1 ^= k1;
110-
v0 ^= k0;
111-
112-
if (outlen == 16)
113-
v1 ^= 0xee;
114-
115-
for (; ni != end; ni += 8) {
116-
m = U8TO64_LE(ni);
117-
v3 ^= m;
118-
119-
TRACE;
120-
for (i = 0; i < cROUNDS; ++i)
121-
SIPROUND;
122-
123-
v0 ^= m;
124-
}
125-
126-
switch (left) {
127-
case 7:
128-
b |= ((uint64_t)ni[6]) << 48;
129-
/* FALLTHRU */
130-
case 6:
131-
b |= ((uint64_t)ni[5]) << 40;
132-
/* FALLTHRU */
133-
case 5:
134-
b |= ((uint64_t)ni[4]) << 32;
135-
/* FALLTHRU */
136-
case 4:
137-
b |= ((uint64_t)ni[3]) << 24;
138-
/* FALLTHRU */
139-
case 3:
140-
b |= ((uint64_t)ni[2]) << 16;
141-
/* FALLTHRU */
142-
case 2:
143-
b |= ((uint64_t)ni[1]) << 8;
144-
/* FALLTHRU */
145-
case 1:
146-
b |= ((uint64_t)ni[0]);
147-
break;
148-
case 0:
149-
break;
150-
}
151-
152-
v3 ^= b;
86+
const unsigned char *ni = (const unsigned char *)in;
87+
const unsigned char *kk = (const unsigned char *)k;
88+
89+
assert((outlen == 8) || (outlen == 16));
90+
uint64_t v0 = UINT64_C(0x736f6d6570736575);
91+
uint64_t v1 = UINT64_C(0x646f72616e646f6d);
92+
uint64_t v2 = UINT64_C(0x6c7967656e657261);
93+
uint64_t v3 = UINT64_C(0x7465646279746573);
94+
uint64_t k0 = U8TO64_LE(kk);
95+
uint64_t k1 = U8TO64_LE(kk + 8);
96+
uint64_t m;
97+
int i;
98+
const unsigned char *end = ni + inlen - (inlen % sizeof(uint64_t));
99+
const int left = inlen & 7;
100+
uint64_t b = ((uint64_t)inlen) << 56;
101+
v3 ^= k1;
102+
v2 ^= k0;
103+
v1 ^= k1;
104+
v0 ^= k0;
105+
106+
if (outlen == 16)
107+
v1 ^= 0xee;
108+
109+
for (; ni != end; ni += 8) {
110+
m = U8TO64_LE(ni);
111+
v3 ^= m;
153112

154113
TRACE;
155114
for (i = 0; i < cROUNDS; ++i)
156-
SIPROUND;
157-
158-
v0 ^= b;
159-
160-
if (outlen == 16)
161-
v2 ^= 0xee;
162-
else
163-
v2 ^= 0xff;
164-
165-
TRACE;
166-
for (i = 0; i < dROUNDS; ++i)
167-
SIPROUND;
168-
169-
b = v0 ^ v1 ^ v2 ^ v3;
170-
U64TO8_LE(out, b);
171-
172-
if (outlen == 8)
173-
return 0;
115+
SIPROUND;
116+
117+
v0 ^= m;
118+
}
119+
120+
switch (left) {
121+
case 7:
122+
b |= ((uint64_t)ni[6]) << 48;
123+
/* FALLTHRU */
124+
case 6:
125+
b |= ((uint64_t)ni[5]) << 40;
126+
/* FALLTHRU */
127+
case 5:
128+
b |= ((uint64_t)ni[4]) << 32;
129+
/* FALLTHRU */
130+
case 4:
131+
b |= ((uint64_t)ni[3]) << 24;
132+
/* FALLTHRU */
133+
case 3:
134+
b |= ((uint64_t)ni[2]) << 16;
135+
/* FALLTHRU */
136+
case 2:
137+
b |= ((uint64_t)ni[1]) << 8;
138+
/* FALLTHRU */
139+
case 1:
140+
b |= ((uint64_t)ni[0]);
141+
break;
142+
case 0:
143+
break;
144+
}
145+
146+
v3 ^= b;
147+
148+
TRACE;
149+
for (i = 0; i < cROUNDS; ++i)
150+
SIPROUND;
151+
152+
v0 ^= b;
153+
154+
if (outlen == 16)
155+
v2 ^= 0xee;
156+
else
157+
v2 ^= 0xff;
158+
159+
TRACE;
160+
for (i = 0; i < dROUNDS; ++i)
161+
SIPROUND;
162+
163+
b = v0 ^ v1 ^ v2 ^ v3;
164+
U64TO8_LE(out, b);
165+
166+
if (outlen == 8)
167+
return 0;
174168

175-
v1 ^= 0xdd;
169+
v1 ^= 0xdd;
176170

177-
TRACE;
178-
for (i = 0; i < dROUNDS; ++i)
179-
SIPROUND;
171+
TRACE;
172+
for (i = 0; i < dROUNDS; ++i)
173+
SIPROUND;
180174

181-
b = v0 ^ v1 ^ v2 ^ v3;
182-
U64TO8_LE(out + 8, b);
175+
b = v0 ^ v1 ^ v2 ^ v3;
176+
U64TO8_LE(out + 8, b);
183177

184-
return 0;
178+
return 0;
185179
}

0 commit comments

Comments
 (0)