LMMP
4.3.0
Lamina High-Precision Arithmetic Library
Toggle main menu visibility
载入中...
搜索中...
未找到
xxhash.c
浏览该文件的文档.
1
/**
2
* Copyright (C) 2026 HJimmyK (Jericho Knox)
3
*
4
* This file is part of LMMP.
5
*
6
* LMMP is free software: you can redistribute it and/or modify it
7
* under the terms of the GNU Lesser General Public License as
8
* published by the Free Software Foundation; either version 3 of
9
* the License, or (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library. If not, see
18
* <https://www.gnu.org/licenses/>.
19
*
20
* SPDX-License-Identifier: LGPL-3.0-or-later
21
*/
22
23
#include "
lmmp/secret.h
"
24
25
#define PRIME64_1 0x9E3779B185EBCA87ULL
26
#define PRIME64_2 0xC2B2AE3D27D4EB4FULL
27
#define PRIME64_3 0x165667B19E3779F9ULL
28
#define PRIME64_4 0x85EBCA77C2B2AE63ULL
29
#define PRIME64_5 0x27D4EB2F165667C5ULL
30
31
static
inline
uint64_t
rotl64
(uint64_t x,
int
b) {
32
b &= 63;
33
return
(x << b) | (x >> (64 - b));
34
}
35
36
uint64_t
lmmp_xxhash_
(
mp_srcptr
in,
mp_size_t
inlen,
srckey64_t
key) {
37
uint64_t seed = key == NULL ? 0 : key[0];
38
if
(in == NULL || inlen == 0) {
39
seed +=
PRIME64_5
;
40
seed += 0;
// len = 0
41
seed ^= seed >> 33;
42
seed *=
PRIME64_2
;
43
seed ^= seed >> 29;
44
seed *=
PRIME64_3
;
45
seed ^= seed >> 32;
46
return
seed;
47
}
48
49
const
uint64_t* p = in;
50
const
uint64_t*
const
end = in + inlen;
51
uint64_t h64;
52
53
if
(inlen >= 4) {
54
const
uint64_t*
const
limit = end - 4;
55
uint64_t
v1
= seed +
PRIME64_1
+
PRIME64_2
;
56
uint64_t
v2
= seed +
PRIME64_2
;
57
uint64_t
v3
= seed + 0;
58
uint64_t v4 = seed -
PRIME64_1
;
59
60
do
{
61
v1
+= *p *
PRIME64_2
;
62
v1
=
rotl64
(
v1
, 31);
63
v1
*=
PRIME64_1
;
64
p++;
65
66
v2
+= *p *
PRIME64_2
;
67
v2
=
rotl64
(
v2
, 31);
68
v2
*=
PRIME64_1
;
69
p++;
70
71
v3
+= *p *
PRIME64_2
;
72
v3
=
rotl64
(
v3
, 31);
73
v3
*=
PRIME64_1
;
74
p++;
75
76
v4 += *p *
PRIME64_2
;
77
v4 =
rotl64
(v4, 31);
78
v4 *=
PRIME64_1
;
79
p++;
80
}
while
(p <= limit);
81
82
h64 =
rotl64
(
v1
, 1) +
rotl64
(
v2
, 7) +
rotl64
(
v3
, 12) +
rotl64
(v4, 18);
83
84
v1
*=
PRIME64_2
;
85
v1
=
rotl64
(
v1
, 31);
86
v1
*=
PRIME64_1
;
87
h64 ^=
v1
;
88
h64 = h64 *
PRIME64_1
+
PRIME64_4
;
89
90
v2
*=
PRIME64_2
;
91
v2
=
rotl64
(
v2
, 31);
92
v2
*=
PRIME64_1
;
93
h64 ^=
v2
;
94
h64 = h64 *
PRIME64_1
+
PRIME64_4
;
95
96
v3
*=
PRIME64_2
;
97
v3
=
rotl64
(
v3
, 31);
98
v3
*=
PRIME64_1
;
99
h64 ^=
v3
;
100
h64 = h64 *
PRIME64_1
+
PRIME64_4
;
101
102
v4 *=
PRIME64_2
;
103
v4 =
rotl64
(v4, 31);
104
v4 *=
PRIME64_1
;
105
h64 ^= v4;
106
h64 = h64 *
PRIME64_1
+
PRIME64_4
;
107
}
else
{
108
h64 = seed +
PRIME64_5
;
109
}
110
111
h64 += (uint64_t)(inlen *
sizeof
(
mp_limb_t
));
112
113
while
(p < end) {
114
uint64_t k1 = *p;
115
k1 *=
PRIME64_2
;
116
k1 =
rotl64
(k1, 31);
117
k1 *=
PRIME64_1
;
118
h64 ^= k1;
119
h64 =
rotl64
(h64, 27) *
PRIME64_1
+
PRIME64_4
;
120
p++;
121
}
122
123
h64 ^= h64 >> 33;
124
h64 *=
PRIME64_2
;
125
h64 ^= h64 >> 29;
126
h64 *=
PRIME64_3
;
127
h64 ^= h64 >> 32;
128
129
return
h64;
130
}
mp_size_t
uint64_t mp_size_t
定义
lmmp.h:93
mp_srcptr
const mp_limb_t * mp_srcptr
定义
lmmp.h:97
mp_limb_t
uint64_t mp_limb_t
定义
lmmp.h:92
v1
#define v1
v2
#define v2
v3
#define v3
secret.h
lmmp_xxhash_
uint64_t lmmp_xxhash_(mp_srcptr in, mp_size_t inlen, srckey64_t key)
xxhash 函数(非标准处理任意字节流的 xxhash)
定义
xxhash.c:36
srckey64_t
const uint64_t srckey64_t[1]
Copyright (C) 2026 HJimmyK (Jericho Knox)
定义
secret.h:41
rotl64
static uint64_t rotl64(uint64_t x, int b)
Copyright (C) 2026 HJimmyK (Jericho Knox)
定义
siphash.c:25
PRIME64_4
#define PRIME64_4
定义
xxhash.c:28
PRIME64_1
#define PRIME64_1
Copyright (C) 2026 HJimmyK (Jericho Knox)
定义
xxhash.c:25
PRIME64_5
#define PRIME64_5
定义
xxhash.c:29
PRIME64_3
#define PRIME64_3
定义
xxhash.c:27
PRIME64_2
#define PRIME64_2
定义
xxhash.c:26
src
lmmp
secret
xxhash.c
生成于
, 为 LMMP使用
1.18.0