Add 64-bit versions of core power of 2 functions

This commit is contained in:
Aaron Franke
2025-05-19 09:53:53 -07:00
parent 64b09905c7
commit f6f1df7d73
25 changed files with 161 additions and 124 deletions

View File

@@ -52,25 +52,6 @@ public:
static constexpr USize MAX_INT = INT64_MAX;
private:
// Function to find the next power of 2 to an integer.
static _FORCE_INLINE_ USize next_po2(USize x) {
if (x == 0) {
return 0;
}
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
if (sizeof(USize) == 8) {
x |= x >> 32;
}
return ++x;
}
// Alignment: ↓ max_align_t ↓ USize ↓ max_align_t
// ┌────────────────────┬──┬─────────────┬──┬───────────...
// │ SafeNumeric<USize> │░░│ USize │░░│ T[]
@@ -107,7 +88,7 @@ private:
}
_FORCE_INLINE_ static USize _get_alloc_size(USize p_elements) {
return next_po2(p_elements * sizeof(T));
return next_power_of_2(p_elements * (USize)sizeof(T));
}
_FORCE_INLINE_ static bool _get_alloc_size_checked(USize p_elements, USize *out) {
@@ -122,7 +103,7 @@ private:
*out = 0;
return false;
}
*out = next_po2(o);
*out = next_power_of_2(o);
if (__builtin_add_overflow(o, static_cast<USize>(32), &p)) {
return false; // No longer allocated here.
}