mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-09-21 20:06:40 +00:00
Replaces is_pow2 by std::has_single_bit
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2025 Wildfire Games.
|
||||
/* Copyright (C) 2026 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
@@ -23,7 +23,6 @@
|
||||
#include "graphics/TerrainTextureEntry.h"
|
||||
#include "lib/alignment.h"
|
||||
#include "lib/allocators/shared_ptr.h"
|
||||
#include "lib/bits.h"
|
||||
#include "lib/debug.h"
|
||||
#include "lib/file/vfs/vfs.h"
|
||||
#include "lib/file/vfs/vfs_util.h"
|
||||
@@ -41,6 +40,7 @@
|
||||
#include "renderer/backend/Sampler.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <bit>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -232,7 +232,7 @@ CTerrainTextureManager::LoadAlphaMap(const VfsPath& alphaMapType)
|
||||
//
|
||||
const size_t tileWidth = 2 + base + 2; // 2 pixel border (avoids bilinear filtering artifacts)
|
||||
const size_t totalWidth = round_up_to_pow2(tileWidth * NUM_ALPHA_MAPS);
|
||||
const size_t totalHeight = base; ENSURE(is_pow2(totalHeight));
|
||||
const size_t totalHeight = base; ENSURE(std::has_single_bit(totalHeight));
|
||||
std::shared_ptr<u8> data;
|
||||
AllocateAligned(data, totalWidth * totalHeight, maxSectorSize);
|
||||
// for each tile on row
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
|
||||
#include "lib/alignment.h"
|
||||
#include "lib/allocators/shared_ptr.h"
|
||||
#include "lib/bits.h"
|
||||
#include "lib/debug.h"
|
||||
#include "lib/path.h"
|
||||
#include "lib/regex.h"
|
||||
@@ -40,6 +39,7 @@
|
||||
#include "ps/XMB/XMBStorage.h"
|
||||
#include "ps/XML/Xeromyces.h"
|
||||
|
||||
#include <bit>
|
||||
#include <cstring>
|
||||
#include <utility>
|
||||
|
||||
@@ -343,7 +343,7 @@ bool CTextureConverter::ConvertTexture(const CTexturePtr& texture, const VfsPath
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!is_pow2(tex.m_Width) || !is_pow2(tex.m_Height))
|
||||
if (!std::has_single_bit(tex.m_Width) || !std::has_single_bit(tex.m_Height))
|
||||
{
|
||||
LOGERROR("Texture to convert \"%s\" should have width and height be power of two: %zux%zu",
|
||||
src.string8(), tex.m_Width, tex.m_Height);
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
#include "graphics/Color.h"
|
||||
#include "graphics/SColor.h"
|
||||
#include "graphics/TextureConverter.h"
|
||||
#include "lib/bits.h"
|
||||
#include "lib/debug.h"
|
||||
#include "lib/hash.h"
|
||||
#include "lib/path.h"
|
||||
@@ -43,6 +42,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <boost/iterator/iterator_facade.hpp>
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
@@ -498,7 +498,7 @@ public:
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_pow2(textureData.m_Width) || !is_pow2(textureData.m_Height))
|
||||
if (!std::has_single_bit(textureData.m_Width) || !std::has_single_bit(textureData.m_Height))
|
||||
{
|
||||
LOGERROR("Texture should have width and height be power of two; \"%s\" %zux%zu",
|
||||
texture->m_Properties.m_Path.string8(), textureData.m_Width, textureData.m_Height);
|
||||
|
||||
+6
-17
@@ -31,9 +31,11 @@
|
||||
#include "lib/debug.h"
|
||||
#include "lib/status.h"
|
||||
|
||||
#include <bit>
|
||||
#include <climits>
|
||||
#include <cstddef>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
|
||||
/**
|
||||
* value of bit number \<n\>.
|
||||
@@ -128,21 +130,6 @@ inline T SetBitsTo(T num, size_t lo_idx, size_t hi_idx, size_t value)
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return whether the given number is a power of two.
|
||||
**/
|
||||
template<typename T>
|
||||
inline bool is_pow2(T n)
|
||||
{
|
||||
// 0 would pass the test below but isn't a POT.
|
||||
if(n == 0)
|
||||
return false;
|
||||
return (n & (n-1)) == 0;
|
||||
}
|
||||
|
||||
// as above; intended for use in static_assert
|
||||
#define IS_POW2(n) (((n) != 0) && ((n) & ((n)-1)) == 0)
|
||||
|
||||
template<typename T>
|
||||
inline T LeastSignificantBit(T x)
|
||||
{
|
||||
@@ -235,7 +222,8 @@ inline T round_down_to_pow2(T x)
|
||||
template<typename T>
|
||||
inline T round_up(T n, T multiple)
|
||||
{
|
||||
ASSERT(is_pow2(multiple));
|
||||
ASSERT(multiple > 0);
|
||||
ASSERT(std::has_single_bit(static_cast<std::make_unsigned_t<T>>(multiple)));
|
||||
const T result = (n + multiple-1) & ~(multiple-1);
|
||||
ASSERT(n <= result && result < n+multiple);
|
||||
return result;
|
||||
@@ -244,7 +232,8 @@ inline T round_up(T n, T multiple)
|
||||
template<typename T>
|
||||
inline T round_down(T n, T multiple)
|
||||
{
|
||||
ASSERT(is_pow2(multiple));
|
||||
ASSERT(multiple > 0);
|
||||
ASSERT(std::has_single_bit(static_cast<std::make_unsigned_t<T>>(multiple)));
|
||||
const T result = n & ~(multiple-1);
|
||||
ASSERT(result <= n && n < result+multiple);
|
||||
return result;
|
||||
|
||||
@@ -44,6 +44,7 @@
|
||||
#include "lib/types.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <fcntl.h>
|
||||
@@ -73,7 +74,7 @@ using BufferPtr = std::unique_ptr<u8, FreeAligned>;
|
||||
// never reused (avoids displacing other items).
|
||||
static inline io::BufferPtr Allocate(size_t size, size_t alignment = maxSectorSize)
|
||||
{
|
||||
ENSURE(is_pow2(alignment));
|
||||
ENSURE(std::has_single_bit(alignment));
|
||||
alignment = std::max(alignment, allocationAlignment);
|
||||
|
||||
u8* p = static_cast<u8*>(rtl_AllocateAligned(round_up(size, alignment), alignment));
|
||||
@@ -141,12 +142,12 @@ struct Parameters
|
||||
|
||||
void Validate(const Operation& op) const
|
||||
{
|
||||
ENSURE(is_pow2(alignment));
|
||||
ENSURE(alignment > 0);
|
||||
ENSURE(std::has_single_bit(static_cast<uint64_t>(alignment)));
|
||||
|
||||
if(blockSize != 0)
|
||||
{
|
||||
ENSURE(is_pow2(blockSize));
|
||||
ENSURE(std::has_single_bit(blockSize));
|
||||
ENSURE(g_PageSize <= blockSize); // (don't bother checking an upper bound)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2025 Wildfire Games.
|
||||
/* Copyright (C) 2026 Wildfire Games.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
@@ -44,6 +44,7 @@
|
||||
# include "lib/sysdep/os/win/wutil.h"
|
||||
#endif
|
||||
|
||||
#include <bit>
|
||||
#include <cinttypes>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
@@ -492,7 +493,7 @@ std::string StringFromEnum(Enum /*field*/)
|
||||
#define ENUM(enumerator, VALUE)\
|
||||
if(field.value == VALUE) /* single bit flag or matching enumerator */\
|
||||
return #enumerator;\
|
||||
if(!is_pow2(VALUE)) /* these aren't bit flags */\
|
||||
if(!std::has_single_bit(static_cast<uint64_t>(VALUE))) /* these aren't bit flags */\
|
||||
{\
|
||||
allowFlags = false;\
|
||||
string.clear();\
|
||||
|
||||
@@ -85,15 +85,6 @@ public:
|
||||
EQUALS(bits<u64>(0xA5A5A5A5A5A5A5A5ull, 32, 63), 0xA5A5A5A5ull);
|
||||
}
|
||||
|
||||
void test_is_pow2()
|
||||
{
|
||||
EQUALS(is_pow2(0u), false);
|
||||
EQUALS(is_pow2(~0u), false);
|
||||
EQUALS(is_pow2(0x80000001), false);
|
||||
EQUALS(is_pow2(1), true);
|
||||
EQUALS(is_pow2(1u << 31), true);
|
||||
}
|
||||
|
||||
void test_ceil_log2()
|
||||
{
|
||||
EQUALS(ceil_log2(3u), 2u);
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
#include "lib/tex/tex_codec.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <bit>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
@@ -253,7 +254,7 @@ static Status add_mipmaps(Tex* t, size_t w, size_t h, size_t bpp, void* newData,
|
||||
// this code assumes the image is of POT dimension; we don't
|
||||
// go to the trouble of implementing image scaling because
|
||||
// the only place this is used (backend textures) requires POT anyway.
|
||||
if(!is_pow2(w) || !is_pow2(h))
|
||||
if(!std::has_single_bit(w) || !std::has_single_bit(h))
|
||||
WARN_RETURN(ERR::TEX_INVALID_SIZE);
|
||||
t->m_Flags |= TEX_MIPMAPS; // must come before tex_img_size!
|
||||
const size_t mipmap_size = t->img_size();
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
#include "graphics/ShaderTechnique.h"
|
||||
#include "graphics/ShaderTechniquePtr.h"
|
||||
#include "graphics/TextureManager.h"
|
||||
#include "lib/bits.h"
|
||||
#include "lib/code_generation.h"
|
||||
#include "lib/file/file_system.h"
|
||||
#include "lib/file/vfs/vfs.h"
|
||||
@@ -54,6 +53,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
@@ -130,7 +130,7 @@ void SkyManager::LoadAndUploadSkyTexturesIfNeeded(
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_pow2(textures[i].m_Width) || !is_pow2(textures[i].m_Height))
|
||||
if (!std::has_single_bit(textures[i].m_Width) || !std::has_single_bit(textures[i].m_Height))
|
||||
{
|
||||
LOGERROR("Error creating sky cubemap '%s', cube textures should have power of 2 sizes.", m_SkySet.ToUTF8().c_str());
|
||||
return;
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
#include "precompiled.h"
|
||||
|
||||
#include "lib/alignment.h"
|
||||
#include "lib/bits.h"
|
||||
#include "lib/sysdep/rtl.h"
|
||||
#include "ps/CLogger.h"
|
||||
#include "renderer/Renderer.h"
|
||||
@@ -26,6 +25,8 @@
|
||||
#include "renderer/VertexBuffer.h"
|
||||
#include "renderer/VertexBufferManager.h"
|
||||
|
||||
#include <bit>
|
||||
|
||||
class CVector3D;
|
||||
class CVector4D;
|
||||
struct SColor4ub;
|
||||
@@ -104,7 +105,7 @@ void VertexArray::SetNumberOfVertices(const size_t numberOfVertices)
|
||||
|
||||
void VertexArray::SetMinimumAttributeAlignment(const uint32_t minimumAttributeAlignment)
|
||||
{
|
||||
ENSURE(minimumAttributeAlignment >= 4 || is_pow2(minimumAttributeAlignment));
|
||||
ENSURE(minimumAttributeAlignment >= 4 || std::has_single_bit(minimumAttributeAlignment));
|
||||
if (minimumAttributeAlignment == m_MinimumAttributeAlignment)
|
||||
return;
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
#include "renderer/backend/vulkan/Utilities.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
@@ -275,7 +276,7 @@ uint32_t CDeviceCommandContext::CUploadRing::ScheduleUpload(
|
||||
const uint32_t alignment)
|
||||
{
|
||||
ENSURE(data.size() > 0);
|
||||
ENSURE(is_pow2(alignment));
|
||||
ENSURE(std::has_single_bit(alignment));
|
||||
|
||||
m_BlockOffset = (m_BlockOffset + alignment - 1) & ~(alignment - 1);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user