Skip to content

Commit 36deed6

Browse files
fix: support 32-bit
skarupke/flat_hash_map#18
1 parent 746e505 commit 36deed6

1 file changed

Lines changed: 154 additions & 107 deletions

File tree

ska_flat_hash_map.hpp

Lines changed: 154 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@
2828

2929
#pragma once
3030

31+
// Check windows
32+
#if _WIN32 || _WIN64
33+
#if _WIN64
34+
#define ENV64BIT
35+
#else
36+
#define ENV32BIT
37+
#endif
38+
#endif
39+
40+
// Check GCC
41+
#if __GNUC__
42+
#if __x86_64__ || __ppc64__ || __aarch64__
43+
#define ENV64BIT
44+
#else
45+
#define ENV32BIT
46+
#endif
47+
#endif
48+
3149
#include <cstdint>
3250
#include <cstddef>
3351
#include <functional>
@@ -231,6 +249,7 @@ struct sherwood_v3_entry
231249

232250
inline int8_t log2(size_t value)
233251
{
252+
#ifdef ENV64BIT
234253
static constexpr int8_t table[64] =
235254
{
236255
63, 0, 58, 1, 59, 47, 53, 2,
@@ -249,6 +268,23 @@ inline int8_t log2(size_t value)
249268
value |= value >> 16;
250269
value |= value >> 32;
251270
return table[((value - (value >> 1)) * 0x07EDD5E59A4E28C2) >> 58];
271+
#endif
272+
#ifdef ENV32BIT
273+
static constexpr int8_t table[32] =
274+
{
275+
0, 9, 1, 10, 13, 21, 2, 29,
276+
11, 14, 16, 18, 22, 25, 3, 30,
277+
8, 12, 20, 28, 15, 17, 24, 7,
278+
19, 27, 23, 6, 26, 5, 4, 31
279+
};
280+
281+
value |= value >> 1;
282+
value |= value >> 2;
283+
value |= value >> 4;
284+
value |= value >> 8;
285+
value |= value >> 16;
286+
return table[(value*0x07C4ACDD) >> 27];
287+
#endif
252288
}
253289

254290
template<typename T, bool>
@@ -282,7 +318,9 @@ inline size_t next_power_of_two(size_t i)
282318
i |= i >> 4;
283319
i |= i >> 8;
284320
i |= i >> 16;
321+
#ifdef ENV64BIT
285322
i |= i >> 32;
323+
#endif
286324
++i;
287325
return i;
288326
}
@@ -953,97 +991,99 @@ class sherwood_v3_table : private EntryAlloc, private Hasher, private Equal
953991

954992
struct prime_number_hash_policy
955993
{
956-
static size_t mod0(size_t) { return 0llu; }
957-
static size_t mod2(size_t hash) { return hash % 2llu; }
958-
static size_t mod3(size_t hash) { return hash % 3llu; }
959-
static size_t mod5(size_t hash) { return hash % 5llu; }
960-
static size_t mod7(size_t hash) { return hash % 7llu; }
961-
static size_t mod11(size_t hash) { return hash % 11llu; }
962-
static size_t mod13(size_t hash) { return hash % 13llu; }
963-
static size_t mod17(size_t hash) { return hash % 17llu; }
964-
static size_t mod23(size_t hash) { return hash % 23llu; }
965-
static size_t mod29(size_t hash) { return hash % 29llu; }
966-
static size_t mod37(size_t hash) { return hash % 37llu; }
967-
static size_t mod47(size_t hash) { return hash % 47llu; }
968-
static size_t mod59(size_t hash) { return hash % 59llu; }
969-
static size_t mod73(size_t hash) { return hash % 73llu; }
970-
static size_t mod97(size_t hash) { return hash % 97llu; }
971-
static size_t mod127(size_t hash) { return hash % 127llu; }
972-
static size_t mod151(size_t hash) { return hash % 151llu; }
973-
static size_t mod197(size_t hash) { return hash % 197llu; }
974-
static size_t mod251(size_t hash) { return hash % 251llu; }
975-
static size_t mod313(size_t hash) { return hash % 313llu; }
976-
static size_t mod397(size_t hash) { return hash % 397llu; }
977-
static size_t mod499(size_t hash) { return hash % 499llu; }
978-
static size_t mod631(size_t hash) { return hash % 631llu; }
979-
static size_t mod797(size_t hash) { return hash % 797llu; }
980-
static size_t mod1009(size_t hash) { return hash % 1009llu; }
981-
static size_t mod1259(size_t hash) { return hash % 1259llu; }
982-
static size_t mod1597(size_t hash) { return hash % 1597llu; }
983-
static size_t mod2011(size_t hash) { return hash % 2011llu; }
984-
static size_t mod2539(size_t hash) { return hash % 2539llu; }
985-
static size_t mod3203(size_t hash) { return hash % 3203llu; }
986-
static size_t mod4027(size_t hash) { return hash % 4027llu; }
987-
static size_t mod5087(size_t hash) { return hash % 5087llu; }
988-
static size_t mod6421(size_t hash) { return hash % 6421llu; }
989-
static size_t mod8089(size_t hash) { return hash % 8089llu; }
990-
static size_t mod10193(size_t hash) { return hash % 10193llu; }
991-
static size_t mod12853(size_t hash) { return hash % 12853llu; }
992-
static size_t mod16193(size_t hash) { return hash % 16193llu; }
993-
static size_t mod20399(size_t hash) { return hash % 20399llu; }
994-
static size_t mod25717(size_t hash) { return hash % 25717llu; }
995-
static size_t mod32401(size_t hash) { return hash % 32401llu; }
996-
static size_t mod40823(size_t hash) { return hash % 40823llu; }
997-
static size_t mod51437(size_t hash) { return hash % 51437llu; }
998-
static size_t mod64811(size_t hash) { return hash % 64811llu; }
999-
static size_t mod81649(size_t hash) { return hash % 81649llu; }
1000-
static size_t mod102877(size_t hash) { return hash % 102877llu; }
1001-
static size_t mod129607(size_t hash) { return hash % 129607llu; }
1002-
static size_t mod163307(size_t hash) { return hash % 163307llu; }
1003-
static size_t mod205759(size_t hash) { return hash % 205759llu; }
1004-
static size_t mod259229(size_t hash) { return hash % 259229llu; }
1005-
static size_t mod326617(size_t hash) { return hash % 326617llu; }
1006-
static size_t mod411527(size_t hash) { return hash % 411527llu; }
1007-
static size_t mod518509(size_t hash) { return hash % 518509llu; }
1008-
static size_t mod653267(size_t hash) { return hash % 653267llu; }
1009-
static size_t mod823117(size_t hash) { return hash % 823117llu; }
1010-
static size_t mod1037059(size_t hash) { return hash % 1037059llu; }
1011-
static size_t mod1306601(size_t hash) { return hash % 1306601llu; }
1012-
static size_t mod1646237(size_t hash) { return hash % 1646237llu; }
1013-
static size_t mod2074129(size_t hash) { return hash % 2074129llu; }
1014-
static size_t mod2613229(size_t hash) { return hash % 2613229llu; }
1015-
static size_t mod3292489(size_t hash) { return hash % 3292489llu; }
1016-
static size_t mod4148279(size_t hash) { return hash % 4148279llu; }
1017-
static size_t mod5226491(size_t hash) { return hash % 5226491llu; }
1018-
static size_t mod6584983(size_t hash) { return hash % 6584983llu; }
1019-
static size_t mod8296553(size_t hash) { return hash % 8296553llu; }
1020-
static size_t mod10453007(size_t hash) { return hash % 10453007llu; }
1021-
static size_t mod13169977(size_t hash) { return hash % 13169977llu; }
1022-
static size_t mod16593127(size_t hash) { return hash % 16593127llu; }
1023-
static size_t mod20906033(size_t hash) { return hash % 20906033llu; }
1024-
static size_t mod26339969(size_t hash) { return hash % 26339969llu; }
1025-
static size_t mod33186281(size_t hash) { return hash % 33186281llu; }
1026-
static size_t mod41812097(size_t hash) { return hash % 41812097llu; }
1027-
static size_t mod52679969(size_t hash) { return hash % 52679969llu; }
1028-
static size_t mod66372617(size_t hash) { return hash % 66372617llu; }
1029-
static size_t mod83624237(size_t hash) { return hash % 83624237llu; }
1030-
static size_t mod105359939(size_t hash) { return hash % 105359939llu; }
1031-
static size_t mod132745199(size_t hash) { return hash % 132745199llu; }
1032-
static size_t mod167248483(size_t hash) { return hash % 167248483llu; }
1033-
static size_t mod210719881(size_t hash) { return hash % 210719881llu; }
1034-
static size_t mod265490441(size_t hash) { return hash % 265490441llu; }
1035-
static size_t mod334496971(size_t hash) { return hash % 334496971llu; }
1036-
static size_t mod421439783(size_t hash) { return hash % 421439783llu; }
1037-
static size_t mod530980861(size_t hash) { return hash % 530980861llu; }
1038-
static size_t mod668993977(size_t hash) { return hash % 668993977llu; }
1039-
static size_t mod842879579(size_t hash) { return hash % 842879579llu; }
1040-
static size_t mod1061961721(size_t hash) { return hash % 1061961721llu; }
1041-
static size_t mod1337987929(size_t hash) { return hash % 1337987929llu; }
1042-
static size_t mod1685759167(size_t hash) { return hash % 1685759167llu; }
1043-
static size_t mod2123923447(size_t hash) { return hash % 2123923447llu; }
1044-
static size_t mod2675975881(size_t hash) { return hash % 2675975881llu; }
1045-
static size_t mod3371518343(size_t hash) { return hash % 3371518343llu; }
1046-
static size_t mod4247846927(size_t hash) { return hash % 4247846927llu; }
994+
static size_t mod0(size_t) { return 0u; }
995+
static size_t mod2(size_t hash) { return hash % 2u; }
996+
static size_t mod3(size_t hash) { return hash % 3u; }
997+
static size_t mod5(size_t hash) { return hash % 5u; }
998+
static size_t mod7(size_t hash) { return hash % 7u; }
999+
static size_t mod11(size_t hash) { return hash % 11u; }
1000+
static size_t mod13(size_t hash) { return hash % 13u; }
1001+
static size_t mod17(size_t hash) { return hash % 17u; }
1002+
static size_t mod23(size_t hash) { return hash % 23u; }
1003+
static size_t mod29(size_t hash) { return hash % 29u; }
1004+
static size_t mod37(size_t hash) { return hash % 37u; }
1005+
static size_t mod47(size_t hash) { return hash % 47u; }
1006+
static size_t mod59(size_t hash) { return hash % 59u; }
1007+
static size_t mod73(size_t hash) { return hash % 73u; }
1008+
static size_t mod97(size_t hash) { return hash % 97u; }
1009+
static size_t mod127(size_t hash) { return hash % 127u; }
1010+
static size_t mod151(size_t hash) { return hash % 151u; }
1011+
static size_t mod197(size_t hash) { return hash % 197u; }
1012+
static size_t mod251(size_t hash) { return hash % 251u; }
1013+
static size_t mod313(size_t hash) { return hash % 313u; }
1014+
static size_t mod397(size_t hash) { return hash % 397u; }
1015+
static size_t mod499(size_t hash) { return hash % 499u; }
1016+
static size_t mod631(size_t hash) { return hash % 631u; }
1017+
static size_t mod797(size_t hash) { return hash % 797u; }
1018+
static size_t mod1009(size_t hash) { return hash % 1009u; }
1019+
static size_t mod1259(size_t hash) { return hash % 1259u; }
1020+
static size_t mod1597(size_t hash) { return hash % 1597u; }
1021+
static size_t mod2011(size_t hash) { return hash % 2011u; }
1022+
static size_t mod2539(size_t hash) { return hash % 2539u; }
1023+
static size_t mod3203(size_t hash) { return hash % 3203u; }
1024+
static size_t mod4027(size_t hash) { return hash % 4027u; }
1025+
static size_t mod5087(size_t hash) { return hash % 5087u; }
1026+
static size_t mod6421(size_t hash) { return hash % 6421u; }
1027+
static size_t mod8089(size_t hash) { return hash % 8089u; }
1028+
static size_t mod10193(size_t hash) { return hash % 10193u; }
1029+
static size_t mod12853(size_t hash) { return hash % 12853u; }
1030+
static size_t mod16193(size_t hash) { return hash % 16193u; }
1031+
static size_t mod20399(size_t hash) { return hash % 20399u; }
1032+
static size_t mod25717(size_t hash) { return hash % 25717u; }
1033+
static size_t mod32401(size_t hash) { return hash % 32401u; }
1034+
static size_t mod40823(size_t hash) { return hash % 40823u; }
1035+
static size_t mod51437(size_t hash) { return hash % 51437u; }
1036+
static size_t mod64811(size_t hash) { return hash % 64811u; }
1037+
static size_t mod81649(size_t hash) { return hash % 81649u; }
1038+
static size_t mod102877(size_t hash) { return hash % 102877u; }
1039+
static size_t mod129607(size_t hash) { return hash % 129607u; }
1040+
static size_t mod163307(size_t hash) { return hash % 163307u; }
1041+
static size_t mod205759(size_t hash) { return hash % 205759u; }
1042+
static size_t mod259229(size_t hash) { return hash % 259229u; }
1043+
static size_t mod326617(size_t hash) { return hash % 326617u; }
1044+
static size_t mod411527(size_t hash) { return hash % 411527u; }
1045+
static size_t mod518509(size_t hash) { return hash % 518509u; }
1046+
static size_t mod653267(size_t hash) { return hash % 653267u; }
1047+
static size_t mod823117(size_t hash) { return hash % 823117u; }
1048+
static size_t mod1037059(size_t hash) { return hash % 1037059u; }
1049+
static size_t mod1306601(size_t hash) { return hash % 1306601u; }
1050+
static size_t mod1646237(size_t hash) { return hash % 1646237u; }
1051+
static size_t mod2074129(size_t hash) { return hash % 2074129u; }
1052+
static size_t mod2613229(size_t hash) { return hash % 2613229u; }
1053+
static size_t mod3292489(size_t hash) { return hash % 3292489u; }
1054+
static size_t mod4148279(size_t hash) { return hash % 4148279u; }
1055+
static size_t mod5226491(size_t hash) { return hash % 5226491u; }
1056+
static size_t mod6584983(size_t hash) { return hash % 6584983u; }
1057+
static size_t mod8296553(size_t hash) { return hash % 8296553u; }
1058+
static size_t mod10453007(size_t hash) { return hash % 10453007u; }
1059+
static size_t mod13169977(size_t hash) { return hash % 13169977u; }
1060+
static size_t mod16593127(size_t hash) { return hash % 16593127u; }
1061+
static size_t mod20906033(size_t hash) { return hash % 20906033u; }
1062+
static size_t mod26339969(size_t hash) { return hash % 26339969u; }
1063+
static size_t mod33186281(size_t hash) { return hash % 33186281u; }
1064+
static size_t mod41812097(size_t hash) { return hash % 41812097u; }
1065+
static size_t mod52679969(size_t hash) { return hash % 52679969u; }
1066+
static size_t mod66372617(size_t hash) { return hash % 66372617u; }
1067+
static size_t mod83624237(size_t hash) { return hash % 83624237u; }
1068+
static size_t mod105359939(size_t hash) { return hash % 105359939u; }
1069+
static size_t mod132745199(size_t hash) { return hash % 132745199u; }
1070+
static size_t mod167248483(size_t hash) { return hash % 167248483u; }
1071+
static size_t mod210719881(size_t hash) { return hash % 210719881u; }
1072+
static size_t mod265490441(size_t hash) { return hash % 265490441u; }
1073+
static size_t mod334496971(size_t hash) { return hash % 334496971u; }
1074+
static size_t mod421439783(size_t hash) { return hash % 421439783u; }
1075+
static size_t mod530980861(size_t hash) { return hash % 530980861u; }
1076+
static size_t mod668993977(size_t hash) { return hash % 668993977u; }
1077+
static size_t mod842879579(size_t hash) { return hash % 842879579u; }
1078+
static size_t mod1061961721(size_t hash) { return hash % 1061961721u; }
1079+
static size_t mod1337987929(size_t hash) { return hash % 1337987929u; }
1080+
static size_t mod1685759167(size_t hash) { return hash % 1685759167u; }
1081+
static size_t mod2123923447(size_t hash) { return hash % 2123923447u; }
1082+
static size_t mod2675975881(size_t hash) { return hash % 2675975881u; }
1083+
static size_t mod3371518343(size_t hash) { return hash % 3371518343u; }
1084+
static size_t mod4247846927(size_t hash) { return hash % 4247846927u; }
1085+
1086+
#ifdef ENV64BIT
10471087
static size_t mod5351951779(size_t hash) { return hash % 5351951779llu; }
10481088
static size_t mod6743036717(size_t hash) { return hash % 6743036717llu; }
10491089
static size_t mod8495693897(size_t hash) { return hash % 8495693897llu; }
@@ -1140,6 +1180,7 @@ struct prime_number_hash_policy
11401180
static size_t mod11493228998133068689(size_t hash) { return hash % 11493228998133068689llu; }
11411181
static size_t mod14480561146010017169(size_t hash) { return hash % 14480561146010017169llu; }
11421182
static size_t mod18446744073709551557(size_t hash) { return hash % 18446744073709551557llu; }
1183+
#endif
11431184

11441185
using mod_function = size_t (*)(size_t);
11451186

@@ -1154,20 +1195,22 @@ struct prime_number_hash_policy
11541195
// 5. get PrevPrime(2^64) and put it at the end
11551196
static constexpr const size_t prime_list[] =
11561197
{
1157-
2llu, 3llu, 5llu, 7llu, 11llu, 13llu, 17llu, 23llu, 29llu, 37llu, 47llu,
1158-
59llu, 73llu, 97llu, 127llu, 151llu, 197llu, 251llu, 313llu, 397llu,
1159-
499llu, 631llu, 797llu, 1009llu, 1259llu, 1597llu, 2011llu, 2539llu,
1160-
3203llu, 4027llu, 5087llu, 6421llu, 8089llu, 10193llu, 12853llu, 16193llu,
1161-
20399llu, 25717llu, 32401llu, 40823llu, 51437llu, 64811llu, 81649llu,
1162-
102877llu, 129607llu, 163307llu, 205759llu, 259229llu, 326617llu,
1163-
411527llu, 518509llu, 653267llu, 823117llu, 1037059llu, 1306601llu,
1164-
1646237llu, 2074129llu, 2613229llu, 3292489llu, 4148279llu, 5226491llu,
1165-
6584983llu, 8296553llu, 10453007llu, 13169977llu, 16593127llu, 20906033llu,
1166-
26339969llu, 33186281llu, 41812097llu, 52679969llu, 66372617llu,
1167-
83624237llu, 105359939llu, 132745199llu, 167248483llu, 210719881llu,
1168-
265490441llu, 334496971llu, 421439783llu, 530980861llu, 668993977llu,
1169-
842879579llu, 1061961721llu, 1337987929llu, 1685759167llu, 2123923447llu,
1170-
2675975881llu, 3371518343llu, 4247846927llu, 5351951779llu, 6743036717llu,
1198+
2u, 3u, 5u, 7u, 11u, 13u, 17u, 23u, 29u, 37u, 47u,
1199+
59u, 73u, 97u, 127u, 151u, 197u, 251u, 313u, 397u,
1200+
499u, 631u, 797u, 1009u, 1259u, 1597u, 2011u, 2539u,
1201+
3203u, 4027u, 5087u, 6421u, 8089u, 10193u, 12853u, 16193u,
1202+
20399u, 25717u, 32401u, 40823u, 51437u, 64811u, 81649u,
1203+
102877u, 129607u, 163307u, 205759u, 259229u, 326617u,
1204+
411527u, 518509u, 653267u, 823117u, 1037059u, 1306601u,
1205+
1646237u, 2074129u, 2613229u, 3292489u, 4148279u, 5226491u,
1206+
6584983u, 8296553u, 10453007u, 13169977u, 16593127u, 20906033u,
1207+
26339969u, 33186281u, 41812097u, 52679969u, 66372617u,
1208+
83624237u, 105359939u, 132745199u, 167248483u, 210719881u,
1209+
265490441u, 334496971u, 421439783u, 530980861u, 668993977u,
1210+
842879579u, 1061961721u, 1337987929u, 1685759167u, 2123923447u,
1211+
2675975881u, 3371518343u, 4247846927u
1212+
#ifdef ENV64BIT
1213+
, 5351951779llu, 6743036717llu,
11711214
8495693897llu, 10703903591llu, 13486073473llu, 16991387857llu,
11721215
21407807219llu, 26972146961llu, 33982775741llu, 42815614441llu,
11731216
53944293929llu, 67965551447llu, 85631228929llu, 107888587883llu,
@@ -1196,6 +1239,7 @@ struct prime_number_hash_policy
11961239
2873307249533267101llu, 3620140286502504283llu, 4561090950536962147llu,
11971240
5746614499066534157llu, 7240280573005008577llu, 9122181901073924329llu,
11981241
11493228998133068689llu, 14480561146010017169llu, 18446744073709551557llu
1242+
#endif
11991243
};
12001244
static constexpr size_t (* const mod_functions[])(size_t) =
12011245
{
@@ -1211,8 +1255,10 @@ struct prime_number_hash_policy
12111255
&mod41812097, &mod52679969, &mod66372617, &mod83624237, &mod105359939, &mod132745199,
12121256
&mod167248483, &mod210719881, &mod265490441, &mod334496971, &mod421439783,
12131257
&mod530980861, &mod668993977, &mod842879579, &mod1061961721, &mod1337987929,
1214-
&mod1685759167, &mod2123923447, &mod2675975881, &mod3371518343, &mod4247846927,
1215-
&mod5351951779, &mod6743036717, &mod8495693897, &mod10703903591, &mod13486073473,
1258+
&mod1685759167, &mod2123923447, &mod2675975881, &mod3371518343, &mod4247846927
1259+
1260+
#ifdef ENV64BIT
1261+
, &mod5351951779, &mod6743036717, &mod8495693897, &mod10703903591, &mod13486073473,
12161262
&mod16991387857, &mod21407807219, &mod26972146961, &mod33982775741, &mod42815614441,
12171263
&mod53944293929, &mod67965551447, &mod85631228929, &mod107888587883, &mod135931102921,
12181264
&mod171262457903, &mod215777175787, &mod271862205833, &mod342524915839,
@@ -1238,6 +1284,7 @@ struct prime_number_hash_policy
12381284
&mod2873307249533267101, &mod3620140286502504283, &mod4561090950536962147,
12391285
&mod5746614499066534157, &mod7240280573005008577, &mod9122181901073924329,
12401286
&mod11493228998133068689, &mod14480561146010017169, &mod18446744073709551557
1287+
#endif
12411288
};
12421289
const size_t * found = std::lower_bound(std::begin(prime_list), std::end(prime_list) - 1, size);
12431290
size = *found;

0 commit comments

Comments
 (0)