Skip to content

Commit 2f5f5b8

Browse files
authored
Fix casting warnings on 32-bit builds. (#487)
AlignTo is now safe to use with 32-bit multiples and fixed other number type mismatches.
1 parent c7fe7d3 commit 2f5f5b8

File tree

5 files changed

+19
-32
lines changed

5 files changed

+19
-32
lines changed

src/gpgmm/common/SlabMemoryAllocator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ namespace gpgmm {
130130
explicit SlabAllocatorCacheEntry(uint64_t blockSize) : mBlockSize(blockSize) {
131131
}
132132

133-
size_t GetKey() const {
133+
uint64_t GetKey() const {
134134
return mBlockSize;
135135
}
136136

src/gpgmm/d3d12/ResourceAllocationD3D12.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace gpgmm::d3d12 {
4949
5050
Always zero when the resource is placed in a heap or created with it's own heap.
5151
*/
52-
uint64_t OffsetFromResource;
52+
size_t OffsetFromResource;
5353

5454
/** \brief Method to describe how the allocation was created.
5555

src/gpgmm/utils/Math.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ namespace gpgmm {
9999
return number % multiple == 0;
100100
}
101101

102-
uint64_t RoundUp(uint64_t n, uint64_t m) {
103-
ASSERT(m > 0);
104-
ASSERT(n > 0);
105-
ASSERT(m <= std::numeric_limits<uint64_t>::max() - n);
106-
return ((n + m - 1) / m) * m;
102+
uint64_t RoundUp(uint64_t number, uint64_t multiple) {
103+
ASSERT(multiple > 0);
104+
ASSERT(number > 0);
105+
ASSERT(multiple <= std::numeric_limits<uint64_t>::max() - number);
106+
return ((number + multiple - 1) / multiple) * multiple;
107107
}
108108

109109
} // namespace gpgmm

src/gpgmm/utils/Math.h

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,19 @@ namespace gpgmm {
3535
uint64_t NextPowerOfTwo(uint64_t number);
3636
bool IsAligned(uint64_t number, size_t multiple);
3737

38-
template <typename T>
39-
T AlignToPowerOfTwo(T number, size_t alignment) {
40-
ASSERT(number <= std::numeric_limits<T>::max() - (alignment - 1));
41-
ASSERT(IsPowerOfTwo(alignment));
42-
ASSERT(alignment != 0);
43-
const T& alignmentT = static_cast<T>(alignment);
44-
return (number + (alignmentT - 1)) & ~(alignmentT - 1);
45-
}
46-
47-
template <typename T>
48-
T AlignTo(T number, size_t multiple) {
49-
if (IsPowerOfTwo(multiple)) {
50-
return AlignToPowerOfTwo(number, multiple);
51-
}
52-
ASSERT(number <= std::numeric_limits<T>::max() - (multiple - 1));
38+
// Aligns number to the nearest power-of-two multiple.
39+
// Compatible with 32-bit or 64-bit numbers.
40+
template <typename T1, typename T2>
41+
auto AlignTo(T1 number, T2 multiple) {
42+
ASSERT(number <= std::numeric_limits<T1>::max() - (multiple - 1));
43+
ASSERT(IsPowerOfTwo(multiple));
5344
ASSERT(multiple != 0);
54-
const T& multipleT = static_cast<T>(multiple);
55-
return ((number + multipleT - 1) / multipleT) * multipleT;
45+
// Use of bitwise not (~) must be avoided since a 64-bit multiple would be always ~'d to a
46+
// 32-bit range.
47+
return (number + (multiple - 1)) - ((number + (multiple - 1)) & (multiple - 1));
5648
}
5749

58-
uint64_t RoundUp(uint64_t n, uint64_t m);
50+
uint64_t RoundUp(uint64_t number, uint64_t multiple);
5951

6052
// Evaluates a/b, avoiding division by zero.
6153
template <typename T>

src/tests/unittests/MathTests.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,9 @@ TEST(MathTests, AlignTo) {
6868
EXPECT_EQ(AlignTo(10u, 16), 16u);
6969
EXPECT_EQ(AlignTo(16u, 16u), 16u);
7070

71-
// Align NPOT number with NPOT multiple.
72-
EXPECT_EQ(AlignTo(10u, 14), 14u);
73-
EXPECT_EQ(AlignTo(10u, 10u), 10u);
74-
7571
// Align UINT32_MAX to POT multiple.
76-
ASSERT_EQ(AlignTo(static_cast<uint64_t>(0xFFFFFFFF), 4), 0x100000000u);
77-
ASSERT_EQ(AlignTo(static_cast<uint64_t>(0xFFFFFFFF), 7), 0x100000000u + 3u);
72+
ASSERT_EQ(AlignTo(static_cast<uint64_t>(0xFFFFFFFF), 4ull), 0x100000000u);
7873

7974
// Align UINT64_MAX to POT multiple.
80-
ASSERT_EQ(AlignTo(static_cast<uint64_t>(0xFFFFFFFFFFFFFFFF), 1), 0xFFFFFFFFFFFFFFFFull);
75+
ASSERT_EQ(AlignTo(static_cast<uint64_t>(0xFFFFFFFFFFFFFFFF), 1ull), 0xFFFFFFFFFFFFFFFFull);
8176
}

0 commit comments

Comments
 (0)