diff --git a/source/lib/bits.cpp b/source/lib/bits.cpp deleted file mode 100644 index a4b7659e33..0000000000 --- a/source/lib/bits.cpp +++ /dev/null @@ -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 - -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; -} diff --git a/source/lib/bits.h b/source/lib/bits.h index 883195fd44..8c2ad06c6d 100644 --- a/source/lib/bits.h +++ b/source/lib/bits.h @@ -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 -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 -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. * diff --git a/source/lib/tests/test_bits.h b/source/lib/tests/test_bits.h index 0955d0a696..eb31454680 100644 --- a/source/lib/tests/test_bits.h +++ b/source/lib/tests/test_bits.h @@ -85,22 +85,6 @@ public: EQUALS(bits(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); diff --git a/source/lib/tex/tex.cpp b/source/lib/tex/tex.cpp index e0aad3aff3..91cf0b3750 100644 --- a/source/lib/tex/tex.cpp +++ b/source/lib/tex/tex.cpp @@ -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(); diff --git a/source/lib/tex/tex_dds.cpp b/source/lib/tex/tex_dds.cpp index f2b8be206f..4053150621 100644 --- a/source/lib/tex/tex_dds.cpp +++ b/source/lib/tex/tex_dds.cpp @@ -43,6 +43,7 @@ #include "lib/types.h" #include +#include #include #include @@ -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(std::bit_width(maxSide) + !std::has_single_bit(maxSide))) return ERR::TEX_FMT_INVALID; flags |= TEX_MIPMAPS; } diff --git a/source/simulation2/helpers/LongPathfinder.cpp b/source/simulation2/helpers/LongPathfinder.cpp index 2fcb3532f1..3f573bb1b9 100644 --- a/source/simulation2/helpers/LongPathfinder.cpp +++ b/source/simulation2/helpers/LongPathfinder.cpp @@ -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 +#include #include #include #include @@ -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); }