Replaces ceil_log2 by std::bit_width

Also removes unused floor_log2.
This commit is contained in:
Vladislav Belov
2026-09-21 00:47:15 +02:00
parent 0650721b19
commit 2c68f7e426
6 changed files with 9 additions and 121 deletions
-47
View File
@@ -1,47 +0,0 @@
/* Copyright (C) 2025 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* bit-twiddling.
*/
#include "precompiled.h"
#include "bits.h"
#include "lib/types.h"
#include <cstring>
static inline u32 get_float_bits(const float x)
{
u32 ret;
memcpy(&ret, &x, 4);
return ret;
}
int floor_log2(const float x)
{
const u32 i = get_float_bits(x);
const u32 biased_exp = (i >> 23) & 0xFF;
return (int)biased_exp - 127;
}
-52
View File
@@ -143,58 +143,6 @@ inline T ClearLeastSignificantBit(T x)
return x & (x-1);
}
/**
* ceil(log2(x))
*
* @param x (unsigned integer)
* @return ceiling of the base-2 logarithm (i.e. rounded up) or
* zero if the input is zero.
**/
template<typename T>
inline size_t ceil_log2(T x)
{
T bit = 1;
size_t log = 0;
while(bit < x && bit != 0) // must detect overflow
{
log++;
bit *= 2;
}
return log;
}
// compile-time variant of the above
template<size_t N>
struct CeilLog2
{
enum { value = 1 + CeilLog2<(N+1)/2>::value };
};
template<>
struct CeilLog2<1>
{
enum { value = 0 };
};
template<>
struct CeilLog2<0>
{
enum { value = 0 };
};
/**
* floor(log2(f))
* fast, uses the FPU normalization hardware.
*
* @param x (float) input; MUST be > 0, else results are undefined.
* @return floor of the base-2 logarithm (i.e. rounded down).
**/
extern int floor_log2(const float x);
/**
* round number up/down to the next given multiple.
*
-16
View File
@@ -85,22 +85,6 @@ public:
EQUALS(bits<u64>(0xA5A5A5A5A5A5A5A5ull, 32, 63), 0xA5A5A5A5ull);
}
void test_ceil_log2()
{
EQUALS(ceil_log2(3u), 2u);
EQUALS(ceil_log2(0xffffffffu), 32u);
EQUALS(ceil_log2(1u), 0u);
EQUALS(ceil_log2(256u), 8u);
EQUALS(ceil_log2(0x80000000u), 31u);
}
void test_floor_log2()
{
EQUALS(floor_log2(1.f), 0);
EQUALS(floor_log2(3.f), 1);
EQUALS(floor_log2(256.f), 8);
}
void test_round_up()
{
EQUALS(round_up( 0u, 16u), 0u);
+3 -2
View File
@@ -777,8 +777,9 @@ void Tex::UpdateMIPLevels()
if (m_Flags & TEX_MIPMAPS)
{
// We add one because we need to account the smallest 1x1 level.
m_MIPLevels.reserve(ceil_log2(std::max(m_Width, m_Height)) + 1);
// We need to account the smallest 1x1 level.
const size_t maxSide{std::max(m_Width, m_Height)};
m_MIPLevels.reserve(std::bit_width(maxSide) + !std::has_single_bit(maxSide));
}
u8* levelData = m_Data.get();
+4 -2
View File
@@ -43,6 +43,7 @@
#include "lib/types.h"
#include <algorithm>
#include <bit>
#include <cstdlib>
#include <memory>
@@ -570,8 +571,9 @@ static Status decode_sd(const DDS_HEADER* sd, size_t& w, size_t& h, size_t& bpp,
if(mipmap_count)
{
// mipmap chain is incomplete
// note: DDS includes the base level in its count, hence +1.
if(mipmap_count != ceil_log2(std::max(w,h))+1)
// note: we need to account the DDS base level (1x1).
const size_t maxSide{std::max(w, h)};
if(mipmap_count != static_cast<size_t>(std::bit_width(maxSide) + !std::has_single_bit(maxSide)))
return ERR::TEX_FMT_INVALID;
flags |= TEX_MIPMAPS;
}
@@ -20,7 +20,6 @@
#include "LongPathfinder.h"
#include "graphics/SColor.h"
#include "lib/bits.h"
#include "maths/Fixed.h"
#include "maths/FixedVector2D.h"
#include "ps/CLogger.h"
@@ -30,6 +29,7 @@
#include "simulation2/helpers/Pathfinding.h"
#include <algorithm>
#include <bit>
#include <cmath>
#include <cstddef>
#include <mutex>
@@ -249,7 +249,7 @@ class JumpPointCache
if (!data.empty())
{
size_t depth = ceil_log2(data.size() + 1);
size_t depth = std::bit_width(data.size());
tree.resize((1 << depth) - 1);
ConstructTree(tree, 0, data.size() / 2, data.size(), 0);
}