Address Michael's comments

fix infinite recursion bug

NFC: Remove early ccc check

remember that false is turned on
This commit is contained in:
Alejandro Alonso
2021-09-23 21:34:59 -07:00
parent 98aaa157ec
commit 014e822cb2
11 changed files with 1428 additions and 1038 deletions

View File

@@ -12,6 +12,9 @@
#include "../SwiftShims/UnicodeData.h"
// Every 4 byte chunks of data that we need to hash (in this case only ever
// scalars and levels who are all uint32), we need to calculate K. At the end
// of this scramble sequence to get K, directly apply this to the current hash.
static inline __swift_uint32_t scramble(__swift_uint32_t scalar) {
scalar *= 0xCC9E2D51;
scalar = (scalar << 15) | (scalar >> 17);
@@ -42,6 +45,8 @@ static __swift_uint32_t hash(__swift_uint32_t scalar, __swift_uint32_t level,
return hash % level;
}
// This implementation is based on the minimal perfect hashing strategy found
// here: https://arxiv.org/pdf/1702.03154.pdf
SWIFT_RUNTIME_STDLIB_INTERNAL
__swift_intptr_t _swift_stdlib_getMphIdx(__swift_uint32_t scalar,
__swift_intptr_t levels,
@@ -50,27 +55,46 @@ __swift_intptr_t _swift_stdlib_getMphIdx(__swift_uint32_t scalar,
const __swift_uint16_t * const sizes) {
__swift_intptr_t resultIdx = 0;
// Here, levels represent the numbers of bit arrays used for this hash table.
for (int i = 0; i != levels; i += 1) {
auto bitArray = keys[i];
// Get the specific bit that this scalar hashes to in the bit array.
auto idx = (__swift_uint64_t) hash(scalar, sizes[i], i);
auto word = bitArray[idx / 64];
auto mask = (__swift_uint64_t) 1 << (idx % 64);
// If our scalar's bit is turned on in the bit array, it means we no longer
// need to iterate the bit arrays to find where our scalar is located...
// its in this one.
if (word & mask) {
// Our initial rank corresponds to our current level and there are ranks
// within each bit array every 512 bits. Say our level (bit array)
// contains 16 uint64 integers to represent all of the required bits.
// There would be a total of 1024 bits, so our rankings for this level
// would contain two values for precomputed counted bits for both halfs
// of this bit array (1024 / 512 = 2).
auto rank = ranks[i][idx / 512];
// Because ranks are provided every 512 bits (8 uint64s), we still need to
// count the bits of the uints64s before us in our 8 uint64 sequence. So
// for example, if we are bit 576, we are larger than 512, so there is a
// provided rank for the first 8 uint64s, however we're in the second
// 8 uint64 sequence and within said sequence we are the #2 uint64. This
// loop will count the bits set for the first uint64 and terminate.
for (int j = (idx / 64) & ~7; j != idx / 64; j += 1) {
rank += __builtin_popcountll(bitArray[j]);
}
auto finalWord = bitArray[idx / 64];
// After counting the other bits set in the uint64s before, its time to
// count our word itself and the bits before us.
if (idx % 64 > 0) {
rank += __builtin_popcountll(finalWord << (64 - (idx % 64)));
rank += __builtin_popcountll(word << (64 - (idx % 64)));
}
// Our result is the built up rank value from all of the provided ranks
// and the ones we've manually counted ourselves.
resultIdx = rank;
break;
}