From 8e08ae3f2b7cc8a154b5b8c07737cfe7f12d3d6c Mon Sep 17 00:00:00 2001 From: chriselrod Date: Mon, 6 Nov 2023 04:20:31 -0500 Subject: [PATCH] huge eltypes shouldn't stack allocate by default --- include/Math/Vector.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/Math/Vector.hpp b/include/Math/Vector.hpp index 5564bfd..f27bde7 100644 --- a/include/Math/Vector.hpp +++ b/include/Math/Vector.hpp @@ -45,7 +45,7 @@ template consteval auto PreAllocStorage() -> ptrdiff_t { constexpr ptrdiff_t remainingBytes = totalBytes - sizeof(T *) - sizeof(S) - sizeof(default_capacity_type_t); constexpr ptrdiff_t N = remainingBytes / sizeof(T); - return std::max(1, N); + return std::max(0, N); } constexpr auto log2Floor(uint64_t x) -> uint64_t { return 63 - std::countl_zero(x); @@ -66,6 +66,7 @@ consteval auto PreAllocSquareStorage() -> ptrdiff_t { // 2* because we want to allow more space for matrices // also removes need for other checks; log2Floor(2)==1 constexpr uint64_t N = 2 * PreAllocStorage(); + if (!N) return 0; // a fairly naive algorirthm for computing the next square `N` // sqrt(x) = x^(1/2) = exp2(log2(x)/2) constexpr uint64_t L = 1 << (log2Floor(N) / 2);