Skip to content

Commit 8a1cc5c

Browse files
author
Julian LALU
committed
Improve hashmap
1 parent 588ae14 commit 8a1cc5c

3 files changed

Lines changed: 66 additions & 6 deletions

File tree

interface/core/bits/bits_common.h

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,72 @@ namespace hud::common
6868
/** Returns the number of consecutive 0 bits in the value. */
6969
[[nodiscard]] static constexpr u32 leading_zeros(u32 value) noexcept
7070
{
71-
return value == 0 ? 32 : 31 - hud::math::floor_log2(u32(value));
71+
if (value == 0)
72+
return 32;
73+
u32 count = 0;
74+
if ((value >> 16) == 0)
75+
{
76+
count += 16;
77+
value <<= 16;
78+
}
79+
if ((value >> 24) == 0)
80+
{
81+
count += 8;
82+
value <<= 8;
83+
}
84+
if ((value >> 28) == 0)
85+
{
86+
count += 4;
87+
value <<= 4;
88+
}
89+
if ((value >> 30) == 0)
90+
{
91+
count += 2;
92+
value <<= 2;
93+
}
94+
if ((value >> 31) == 0)
95+
{
96+
count += 1;
97+
}
98+
return count;
7299
}
73100

74101
/** Returns the number of consecutive 0 bits in the value. */
75102
[[nodiscard]] static constexpr u32 leading_zeros(u64 value) noexcept
76103
{
77-
return value == 0 ? 64 : 63 - hud::math::floor_log2(value);
104+
if (value == 0)
105+
return 64;
106+
uint64_t count = 0;
107+
if ((value >> 32) == 0)
108+
{
109+
count += 32;
110+
value <<= 32;
111+
}
112+
if ((value >> 48) == 0)
113+
{
114+
count += 16;
115+
value <<= 16;
116+
}
117+
if ((value >> 56) == 0)
118+
{
119+
count += 8;
120+
value <<= 8;
121+
}
122+
if ((value >> 60) == 0)
123+
{
124+
count += 4;
125+
value <<= 4;
126+
}
127+
if ((value >> 62) == 0)
128+
{
129+
count += 2;
130+
value <<= 2;
131+
}
132+
if ((value >> 63) == 0)
133+
{
134+
count += 1;
135+
}
136+
return count;
78137
}
79138

80139
[[nodiscard]] static constexpr u32 trailing_zeros(u8 value) noexcept

interface/core/containers/hashset.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,7 @@ namespace hud
15581558
[[nodiscard]] constexpr usize free_slot_before_grow() const noexcept
15591559
{
15601560
// Remove the sign bit that represent if the map contains deleted slots
1561-
return free_slot_before_grow_ & ((~size_t {}) >> 1);
1561+
return free_slot_before_grow_ & ((~usize {}) >> 1);
15621562
}
15631563

15641564
/** Retrieves the next capacity after a grow. */
@@ -1571,7 +1571,7 @@ namespace hud
15711571
/** Retrieves the max slot count for the given count. */
15721572
[[nodiscard]] constexpr usize compute_max_count(usize count) const noexcept
15731573
{
1574-
return hud::math::next_power_of_two(count + 1) - 1;
1574+
return count ? ~usize {} >> hud::bits::leading_zeros(count) : 0;
15751575
}
15761576

15771577
/** Compute the size of the allocation needed for the given slot count. */

interface/core/math/math.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "../templates/bit_cast.h"
44
#include "../traits/is_constant_evaluated.h"
55
#include <cmath> // FP_SUBNORMAL, etc...
6+
#include "../bits.h"
67

78
namespace hud
89
{
@@ -36,13 +37,13 @@ namespace hud
3637
/** Compute the next power of two. */
3738
[[nodiscard]] static constexpr u32 next_power_of_two(u32 integral) noexcept
3839
{
39-
return hud::math::next_power_of_two_mask(integral) + 1;
40+
return integral == 0 ? 1 : u32 {1} << (32 - hud::bits::leading_zeros(integral - 1));
4041
}
4142

4243
/** Compute the next power of two. */
4344
[[nodiscard]] static constexpr u64 next_power_of_two(u64 integral) noexcept
4445
{
45-
return hud::math::next_power_of_two_mask(integral) + 1;
46+
return integral == 0 ? 1 : (u64 {1} << (64 - hud::bits::leading_zeros(integral - 1)));
4647
}
4748

4849
/** Compute the natual logarithm of value. */

0 commit comments

Comments
 (0)