Rename Log2 and add IsPow2 to MathUtils for future use

Also remove unused pow2/pow2f functions.
This commit is contained in:
Fiora 2014-08-24 11:03:07 -07:00
parent aec639470f
commit b51aa4fa89
5 changed files with 16 additions and 17 deletions

View File

@ -166,16 +166,15 @@ struct Rectangle
} // namespace MathUtil } // namespace MathUtil
inline float pow2f(float x) {return x * x;}
inline double pow2(double x) {return x * x;}
float MathFloatVectorSum(const std::vector<float>&); float MathFloatVectorSum(const std::vector<float>&);
#define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1)) #define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define ROUND_DOWN(x, a) ((x) & ~((a) - 1)) #define ROUND_DOWN(x, a) ((x) & ~((a) - 1))
inline bool IsPow2(u32 imm) {return (imm & (imm - 1)) == 0;}
// Rounds down. 0 -> undefined // Rounds down. 0 -> undefined
inline int Log2(u64 val) inline int IntLog2(u64 val)
{ {
#if defined(__GNUC__) #if defined(__GNUC__)
return 63 - __builtin_clzll(val); return 63 - __builtin_clzll(val);

View File

@ -397,7 +397,7 @@ static wxString NiceSizeFormat(u64 _size)
// Find largest power of 2 less than _size. // Find largest power of 2 less than _size.
// div 10 to get largest named unit less than _size // div 10 to get largest named unit less than _size
// 10 == log2(1024) (number of B in a KiB, KiB in a MiB, etc) // 10 == log2(1024) (number of B in a KiB, KiB in a MiB, etc)
const u64 unit = Log2(std::max<u64>(_size, 1)) / 10; const u64 unit = IntLog2(std::max<u64>(_size, 1)) / 10;
const u64 unit_size = (1 << (unit * 10)); const u64 unit_size = (1 << (unit * 10));
// mul 1000 for 3 decimal places, add 5 to round up, div 10 for 2 decimal places // mul 1000 for 3 decimal places, add 5 to round up, div 10 for 2 decimal places

View File

@ -23,7 +23,7 @@ static u32 genBuffer()
} }
StreamBuffer::StreamBuffer(u32 type, u32 size) StreamBuffer::StreamBuffer(u32 type, u32 size)
: m_buffer(genBuffer()), m_buffertype(type), m_size(ROUND_UP_POW2(size)), m_bit_per_slot(Log2(ROUND_UP_POW2(size) / SYNC_POINTS)) : m_buffer(genBuffer()), m_buffertype(type), m_size(ROUND_UP_POW2(size)), m_bit_per_slot(IntLog2(ROUND_UP_POW2(size) / SYNC_POINTS))
{ {
m_iterator = 0; m_iterator = 0;
m_used_iterator = 0; m_used_iterator = 0;

View File

@ -91,8 +91,8 @@ static void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType)
WRITE(p, " int y_block_position = uv1.y & %d;\n", ~(blkH - 1)); WRITE(p, " int y_block_position = uv1.y & %d;\n", ~(blkH - 1));
WRITE(p, " int y_offset_in_block = uv1.y & %d;\n", blkH - 1); WRITE(p, " int y_offset_in_block = uv1.y & %d;\n", blkH - 1);
WRITE(p, " int x_virtual_position = (uv1.x << %d) + y_offset_in_block * position.z;\n", Log2(samples)); WRITE(p, " int x_virtual_position = (uv1.x << %d) + y_offset_in_block * position.z;\n", IntLog2(samples));
WRITE(p, " int x_block_position = (x_virtual_position >> %d) & %d;\n", Log2(blkH), ~(blkW - 1)); WRITE(p, " int x_block_position = (x_virtual_position >> %d) & %d;\n", IntLog2(blkH), ~(blkW - 1));
if (samples == 1) if (samples == 1)
{ {
// 32 bit textures (RGBA8 and Z24) are stored in 2 cache line increments // 32 bit textures (RGBA8 and Z24) are stored in 2 cache line increments
@ -100,7 +100,7 @@ static void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType)
WRITE(p, " x_virtual_position = x_virtual_position << 1;\n"); WRITE(p, " x_virtual_position = x_virtual_position << 1;\n");
} }
WRITE(p, " int x_offset_in_block = x_virtual_position & %d;\n", blkW - 1); WRITE(p, " int x_offset_in_block = x_virtual_position & %d;\n", blkW - 1);
WRITE(p, " int y_offset = (x_virtual_position >> %d) & %d;\n", Log2(blkW), blkH - 1); WRITE(p, " int y_offset = (x_virtual_position >> %d) & %d;\n", IntLog2(blkW), blkH - 1);
WRITE(p, " sampleUv.x = x_offset_in_block + x_block_position;\n"); WRITE(p, " sampleUv.x = x_offset_in_block + x_block_position;\n");
WRITE(p, " sampleUv.y = y_block_position + y_offset;\n"); WRITE(p, " sampleUv.y = y_block_position + y_offset;\n");

View File

@ -44,17 +44,17 @@ TEST(MathUtil, IsSNAN)
EXPECT_TRUE(MathUtil::IsSNAN(std::numeric_limits<double>::signaling_NaN())); EXPECT_TRUE(MathUtil::IsSNAN(std::numeric_limits<double>::signaling_NaN()));
} }
TEST(MathUtil, Log2) TEST(MathUtil, IntLog2)
{ {
EXPECT_EQ(0, Log2(1)); EXPECT_EQ(0, IntLog2(1));
EXPECT_EQ(1, Log2(2)); EXPECT_EQ(1, IntLog2(2));
EXPECT_EQ(2, Log2(4)); EXPECT_EQ(2, IntLog2(4));
EXPECT_EQ(3, Log2(8)); EXPECT_EQ(3, IntLog2(8));
EXPECT_EQ(63, Log2(0x8000000000000000ull)); EXPECT_EQ(63, IntLog2(0x8000000000000000ull));
// Rounding behavior. // Rounding behavior.
EXPECT_EQ(3, Log2(15)); EXPECT_EQ(3, IntLog2(15));
EXPECT_EQ(63, Log2(0xFFFFFFFFFFFFFFFFull)); EXPECT_EQ(63, IntLog2(0xFFFFFFFFFFFFFFFFull));
} }
TEST(MathUtil, FlushToZero) TEST(MathUtil, FlushToZero)