Skip to content

Commit ab669b9

Browse files
committed
GPU: Fix code that was not respecting strict aliasing rules
1 parent e138d7c commit ab669b9

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

GPU/GPUTracking/Base/GPUProcessor.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,29 @@ class GPUProcessor
120120
template <size_t alignment = GPUCA_BUFFER_ALIGNMENT, class S>
121121
static inline S* getPointerWithAlignment(void*& basePtr, size_t nEntries = 1)
122122
{
123-
return getPointerWithAlignment<alignment, S>(reinterpret_cast<size_t&>(basePtr), nEntries);
123+
size_t tmp = (size_t)basePtr;
124+
auto retVal = getPointerWithAlignment<alignment, S>(tmp, nEntries);
125+
basePtr = (void*)tmp;
126+
return retVal;
124127
}
125128

126129
template <size_t alignment = GPUCA_BUFFER_ALIGNMENT, class T, class S>
127130
static inline void computePointerWithAlignment(T*& basePtr, S*& objPtr, size_t nEntries = 1)
128131
{
129-
objPtr = getPointerWithAlignment<alignment, S>(reinterpret_cast<size_t&>(basePtr), nEntries);
132+
size_t tmp = (size_t)basePtr;
133+
objPtr = getPointerWithAlignment<alignment, S>(tmp, nEntries);
134+
basePtr = (T*)tmp;
130135
}
131136

132137
template <class T, class S>
133138
static inline void computePointerWithoutAlignment(T*& basePtr, S*& objPtr, size_t nEntries = 1)
134139
{
135140
if ((size_t)basePtr < GPUCA_BUFFER_ALIGNMENT) {
136-
reinterpret_cast<size_t&>(basePtr) = GPUCA_BUFFER_ALIGNMENT;
141+
basePtr = (T*)GPUCA_BUFFER_ALIGNMENT;
137142
}
138-
objPtr = reinterpret_cast<S*>(getPointerWithAlignment<1, char>(reinterpret_cast<size_t&>(basePtr), nEntries * sizeof(S)));
143+
size_t tmp = (size_t)basePtr;
144+
objPtr = reinterpret_cast<S*>(getPointerWithAlignment<1, char>(tmp, nEntries * sizeof(S)));
145+
basePtr = (T*)tmp;
139146
}
140147
#endif
141148

0 commit comments

Comments
 (0)