| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "CustomHashFunctions.h" | ||
| 4 | |||
| 5 | #include <numeric> | ||
| 6 | |||
| 7 | namespace spice::compiler { | ||
| 8 | |||
| 9 | 67480112 | uint64_t hashMix(uint64_t hash) noexcept { | |
| 10 | 67480112 | hash += 0x9e3779b97f4a7c15ull; | |
| 11 | 67480112 | hash = (hash ^ (hash >> 30)) * 0xbf58476d1ce4e5b9ull; | |
| 12 | 67480112 | hash = (hash ^ (hash >> 27)) * 0x94d049bb133111ebull; | |
| 13 | 67480112 | hash ^= (hash >> 31); | |
| 14 | 67480112 | return hash; | |
| 15 | } | ||
| 16 | |||
| 17 | 52163738 | void hashCombine64(uint64_t &hash, uint64_t value) noexcept { | |
| 18 | 52163738 | hash ^= hashMix(value + 0x9e3779b97f4a7c15ull + (hash << 6) + (hash >> 2)); | |
| 19 | 52163738 | } | |
| 20 | |||
| 21 | } // namespace spice::compiler | ||
| 22 | |||
| 23 | namespace std { | ||
| 24 | |||
| 25 | 6969008 | size_t hash<spice::compiler::TypeChainElement>::operator()(const spice::compiler::TypeChainElement &tce) const noexcept { | |
| 26 | using namespace spice::compiler; | ||
| 27 | 6969008 | uint64_t hash = 0; | |
| 28 | |||
| 29 | 6969008 | hashCombine64(hash, tce.superType); | |
| 30 | 6969008 | hashCombine64(hash, std::hash<std::string>{}(tce.subType)); | |
| 31 | 6969008 | hashCombine64(hash, tce.typeId); | |
| 32 | |||
| 33 |
4/4✓ Branch 6 → 7 taken 896 times.
✓ Branch 6 → 9 taken 88093 times.
✓ Branch 6 → 14 taken 695991 times.
✓ Branch 6 → 17 taken 6184028 times.
|
6969008 | switch (tce.superType) { |
| 34 | 896 | case TY_ARRAY: | |
| 35 | 896 | hashCombine64(hash, tce.data.arraySize); | |
| 36 | 896 | break; | |
| 37 | 88093 | case TY_FUNCTION: | |
| 38 | case TY_PROCEDURE: | ||
| 39 |
2/2✓ Branch 9 → 10 taken 247 times.
✓ Branch 9 → 11 taken 87846 times.
|
88093 | hashCombine64(hash, tce.data.hasCaptures ? 0xF00D1234ULL : 0xBAD0C0DEULL); |
| 40 | 88093 | break; | |
| 41 | 695991 | case TY_STRUCT: | |
| 42 | case TY_INTERFACE: | ||
| 43 | case TY_ENUM: | ||
| 44 | // Stable hash based on pointer identity, but randomized for safety | ||
| 45 | 695991 | hashCombine64(hash, hashPointer(tce.data.bodyScope)); | |
| 46 | 695991 | break; | |
| 47 | 6184028 | default: | |
| 48 | 6184028 | break; | |
| 49 | } | ||
| 50 | |||
| 51 | 6969008 | hashCombine64(hash, hashVector(tce.templateTypes)); | |
| 52 | 6969008 | hashCombine64(hash, hashVector(tce.paramTypes)); | |
| 53 | |||
| 54 | 6969008 | return hashMix(hash); | |
| 55 | } | ||
| 56 | |||
| 57 | 6818498 | size_t hash<spice::compiler::Type>::operator()(const spice::compiler::Type &t) const noexcept { | |
| 58 | using namespace spice::compiler; | ||
| 59 | 6818498 | uint64_t hash = 0; | |
| 60 | 6818498 | hashCombine64(hash, hashVector(t.typeChain)); | |
| 61 | 6818498 | return hashMix(hash); | |
| 62 | } | ||
| 63 | |||
| 64 | 764434 | size_t hash<spice::compiler::TypeQualifiers>::operator()(const spice::compiler::TypeQualifiers &qualifiers) const noexcept { | |
| 65 | using namespace spice::compiler; | ||
| 66 | 764434 | const uint8_t bits = (qualifiers.isConst << 0) | (qualifiers.isSigned << 1) | (qualifiers.isUnsigned << 2) | | |
| 67 | 764434 | (qualifiers.isHeap << 3) | (qualifiers.isPublic << 4) | (qualifiers.isInline << 5) | | |
| 68 | 764434 | (qualifiers.isComposition << 6); | |
| 69 | 764434 | return hashMix(bits); | |
| 70 | } | ||
| 71 | |||
| 72 | 764434 | size_t hash<spice::compiler::QualType>::operator()(const spice::compiler::QualType &qualType) const noexcept { | |
| 73 | using namespace spice::compiler; | ||
| 74 | 764434 | uint64_t seed = 0; | |
| 75 | |||
| 76 | // Hash type pointer content if possible | ||
| 77 | 764434 | hashCombine64(seed, std::hash<Type>{}(*qualType.getType())); | |
| 78 | |||
| 79 | 764434 | TypeQualifiers qualifiers = qualType.getQualifiers(); | |
| 80 | 764434 | qualifiers.isPublic = false; // Ignore the public qualifier for hashing | |
| 81 | 764434 | hashCombine64(seed, std::hash<TypeQualifiers>{}(qualifiers)); | |
| 82 | 764434 | return hashMix(seed); | |
| 83 | } | ||
| 84 | |||
| 85 | } // namespace std | ||
| 86 |