GCC Code Coverage Report


src/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
34 / 34
100.0%
Branches:
14 / 16
87.5%
Functions:
8 / 8
100.0%

util/custom-hash-functions.spice
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 // Std imports
4 import "std/math/hash";
5
6 // Own imports
7 import "@/symboltablebuilder/qual-type";
8 import "@/symboltablebuilder/type-qualifiers";
9
10 // Generic type defs
11 type T dyn;
12
13 /**
14 * Mix the bits of the given hash value (splitmix64 finalizer)
15 *
16 * @param hash Input hash value
17 * @return Mixed hash value
18 */
19 7828482 public f<Hash> hashMix(Hash hash) {
20 7828482 hash += 0x9e3779b97f4a7c15ul;
21 7828482 hash = (hash ^ (hash >> 30ul)) * 0xbf58476d1ce4e5b9ul;
22 7828482 hash = (hash ^ (hash >> 27ul)) * 0x94d049bb133111ebul;
23 7828482 hash ^= (hash >> 31ul);
24 7828482 return hash;
25 }
26
27 /**
28 * Combine the given value into the given hash
29 *
30 * @param hash Hash to combine the value into
31 * @param value Value to combine
32 */
33 5778526 public p hashCombine64(Hash& hash, Hash value) {
34 5778526 hash ^= hashMix(value + 0x9e3779b97f4a7c15ul + (hash << 6ul) + (hash >> 2ul));
35 5778526 }
36
37 /**
38 * Hash a pointer by its address
39 *
40 * @param value Pointer to hash
41 * @return Hash value
42 */
43 1766686 public f<Hash> hashPointer<T>(const T* value) {
44 unsafe {
45 1766686 return cast<Hash>(value) >> 3ul;
46 }
47 }
48
49 /**
50 * Hash type qualifiers
51 *
52 * @param qualifiers Type qualifiers
53 * @return Hash value
54 */
55 1024978 public f<Hash> hashTypeQualifiers(const TypeQualifiers& qualifiers) {
56 1024978 Hash bits = 0ul;
57
2/2
✓ Branch 0 taken 902550 times.
✓ Branch 1 taken 122428 times.
1024978 if qualifiers.isConst { bits |= 1ul << 0ul; }
58
2/2
✓ Branch 0 taken 861264 times.
✓ Branch 1 taken 163714 times.
1024978 if qualifiers.isSigned { bits |= 1ul << 1ul; }
59
2/2
✓ Branch 0 taken 349565 times.
✓ Branch 1 taken 675413 times.
1024978 if qualifiers.isUnsigned { bits |= 1ul << 2ul; }
60
2/2
✓ Branch 0 taken 947909 times.
✓ Branch 1 taken 77069 times.
1024978 if qualifiers.isHeap { bits |= 1ul << 3ul; }
61
1/2
✓ Branch 0 taken 1024978 times.
✗ Branch 1 not taken.
1024978 if qualifiers.isPublic { bits |= 1ul << 4ul; }
62
1/2
✓ Branch 0 taken 1024978 times.
✗ Branch 1 not taken.
1024978 if qualifiers.isInline { bits |= 1ul << 5ul; }
63
2/2
✓ Branch 0 taken 1024844 times.
✓ Branch 1 taken 134 times.
1024978 if qualifiers.isComposition { bits |= 1ul << 6ul; }
64 1024978 return hashMix(bits);
65 }
66
67 /**
68 * Hash a qualified type. Types are interned in the type registry, so the address of the type identifies its content.
69 *
70 * @param qualType Qualified type
71 * @return Hash value
72 */
73 1024978 public f<Hash> hashQualType(const QualType& qualType) {
74 1024978 Hash seed = 0ul;
75
76 // Hash type pointer
77 1024978 hashCombine64(seed, hashPointer(qualType.rawType));
78
79 1024978 TypeQualifiers qualifiers = qualType.qualifiers;
80 1024978 qualifiers.isPublic = false; // Ignore the public qualifier for hashing
81 1024978 hashCombine64(seed, hashTypeQualifiers(qualifiers));
82 1024978 return hashMix(seed);
83 }
84
85 /**
86 * Hash a list of qualified types
87 *
88 * @param qualTypes List of qualified types
89 * @return Hash value
90 */
91 646064 public f<Hash> hashQualTypeList(const QualTypeList& qualTypes) {
92 646064 Hash hash = 0ul;
93
2/2
✓ Branch 0 taken 171294 times.
✓ Branch 1 taken 646064 times.
817358 foreach const QualType& qualType : qualTypes {
94 171294 hashCombine64(hash, hashQualType(qualType));
95 171294 }
96 646064 return hash;
97 }
98