forked from moonbitlang/moonbit-compiler
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hash.c
executable file
·130 lines (110 loc) · 4.06 KB
/
hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* The MIT License
Copyright (c) 2016--2024 Jane Street Group, LLC <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
* Initially copied from [https://github.com/janestreet/base/blob/master/hash_types/src/internalhash_stubs.c]
*/
#include <stdint.h>
/*
* These headers are typically under /usr/lib/ocaml, if you install ocaml in the default way.
* Don't worry, Dune will sort them right.
*/
#include <caml/mlvalues.h>
#include <caml/hash.h>
CAMLprim value Ppx_fold_int(value st, value i)
{
return Val_long(caml_hash_mix_intnat(Long_val(st), Long_val(i)));
}
/* This code mimics what hashtbl.hash does in OCaml's hash.c */
#define FINAL_MIX(h) \
h ^= h >> 16; \
h *= 0x85ebca6b; \
h ^= h >> 13; \
h *= 0xc2b2ae35; \
h ^= h >> 16;
CAMLprim value Ppx_get_hash_value(value st)
{
uint32_t h = Int_val(st);
FINAL_MIX(h);
return Val_int(h & 0x3FFFFFFFU); /*30 bits*/
}
/* Macros copied from hash.c in ocaml distribution */
#define ROTL32(x,n) ((x) << n | (x) >> (32-n))
#define MIX(h,d) \
d *= 0xcc9e2d51; \
d = ROTL32(d, 15); \
d *= 0x1b873593; \
h ^= d; \
h = ROTL32(h, 13); \
h = h * 5 + 0xe6546b64;
/* Version of [caml_hash_mix_string] from hash.c - adapted for arbitrary char arrays */
CAMLexport uint32_t Ppx_fold_blob(uint32_t h, mlsize_t len, uint8_t *s)
{
mlsize_t i;
uint32_t w;
/* Mix by 32-bit blocks (little-endian) */
for (i = 0; i + 4 <= len; i += 4) {
#ifdef ARCH_BIG_ENDIAN
w = s[i]
| (s[i+1] << 8)
| (s[i+2] << 16)
| (s[i+3] << 24);
#else
w = *((uint32_t *) &(s[i]));
#endif
MIX(h, w);
}
/* Finish with up to 3 bytes */
w = 0;
switch (len & 3) {
case 3: w = s[i+2] << 16; /* fallthrough */
case 2: w |= s[i+1] << 8; /* fallthrough */
case 1: w |= s[i];
MIX(h, w);
default: /*skip*/; /* len & 3 == 0, no extra bytes, do nothing */
}
/* Finally, mix in the length. Ignore the upper 32 bits, generally 0. */
h ^= (uint32_t) len;
return h;
}
CAMLprim value Ppx_fold_string(value st, value v_str)
{
uint32_t h = Long_val(st);
mlsize_t len = caml_string_length(v_str);
uint8_t *s = (uint8_t *) String_val(v_str);
h = Ppx_fold_blob(h, len, s);
return Val_long(h);
}
/* Final mix and return from the hash.c implementation from INRIA */
#define FINAL_MIX_AND_RETURN(h) \
h ^= h >> 16; \
h *= 0x85ebca6b; \
h ^= h >> 13; \
h *= 0xc2b2ae35; \
h ^= h >> 16; \
return Val_int(h & 0x3FFFFFFFU);
CAMLprim value Ppx_hash_string(value string) {
uint32_t h;
h = caml_hash_mix_string(0, string);
FINAL_MIX_AND_RETURN(h)
}
CAMLprim value Ppx_hash_double(value d) {
uint32_t h;
h = caml_hash_mix_double(0, Double_val(d));
FINAL_MIX_AND_RETURN(h);
}