Fix for power-of-two selection in purchaseParams

This commit is contained in:
ThatBen 2025-07-10 10:47:35 +02:00
parent be2f266278
commit 09e8e5f343
No known key found for this signature in database
GPG Key ID: 62C543548433D43E

View File

@ -33,7 +33,7 @@ namespace CodexReleaseTests.Utils
var numSlotBlocks = 1 + ((numBlocks - 1) / Nodes); // round-up div.
// Next power of two:
var numSlotBlocksPow2 = NextPowerOf2(numSlotBlocks);
var numSlotBlocksPow2 = IsOrNextPowerOf2(numSlotBlocks);
return new ByteSize(blockSize.SizeInBytes * numSlotBlocksPow2);
}
@ -51,8 +51,9 @@ namespace CodexReleaseTests.Utils
return new ByteSize(blockSize.SizeInBytes * totalBlocks);
}
private int NextPowerOf2(int n)
private int IsOrNextPowerOf2(int n)
{
if (IsPowerOfTwo(n)) return n;
n = n - 1;
var lg = Convert.ToInt32(Math.Round(Math.Log2(Convert.ToDouble(n))));
return 1 << (lg + 1);
@ -63,5 +64,10 @@ namespace CodexReleaseTests.Utils
var x = size.SizeInBytes;
return (x != 0) && ((x & (x - 1)) == 0);
}
private static bool IsPowerOfTwo(int x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
}
}