Skip to content

Commit

Permalink
GDALCopyWords64(): tidy code
Browse files Browse the repository at this point in the history
Technically it is safer to cast a pointer to uintptr_t than our custom
type. At least, it better documents the intent. In practice, I doubt
this changes much.
  • Loading branch information
rouault committed Nov 12, 2024
1 parent e6ae79a commit 98207b4
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions gcore/rasterio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3360,8 +3360,8 @@ void CPL_STDCALL GDALCopyWords64(const void *CPL_RESTRICT pSrcData,
assert(nSrcDataTypeSize != 0);
assert(nDstDataTypeSize != 0);
if (!(eSrcType == eDstType && nSrcPixelStride == nDstPixelStride) &&
((reinterpret_cast<GPtrDiff_t>(pSrcData) % nSrcDataTypeSize) != 0 ||
(reinterpret_cast<GPtrDiff_t>(pDstData) % nDstDataTypeSize) != 0 ||
((reinterpret_cast<uintptr_t>(pSrcData) % nSrcDataTypeSize) != 0 ||
(reinterpret_cast<uintptr_t>(pDstData) % nDstDataTypeSize) != 0 ||
(nSrcPixelStride % nSrcDataTypeSize) != 0 ||
(nDstPixelStride % nDstDataTypeSize) != 0))
{
Expand All @@ -3377,14 +3377,22 @@ void CPL_STDCALL GDALCopyWords64(const void *CPL_RESTRICT pSrcData,
}
else
{
#define ALIGN_PTR(ptr, align) \
((ptr) + ((align) - (reinterpret_cast<size_t>(ptr) % (align))) % (align))
const auto getAlignedPtr = [](GByte *ptr, int align)
{
return ptr +
((align - (reinterpret_cast<uintptr_t>(ptr) % align)) %
align);
};

// The largest we need is for CFloat64 (16 bytes), so 32 bytes to
// be sure to get correctly aligned pointer.
GByte abySrcBuffer[32];
GByte abyDstBuffer[32];
GByte *pabySrcBuffer = ALIGN_PTR(abySrcBuffer, nSrcDataTypeSize);
GByte *pabyDstBuffer = ALIGN_PTR(abyDstBuffer, nDstDataTypeSize);
constexpr size_t SIZEOF_CFLOAT64 = 2 * sizeof(double);
GByte abySrcBuffer[2 * SIZEOF_CFLOAT64];
GByte abyDstBuffer[2 * SIZEOF_CFLOAT64];
GByte *pabySrcBuffer =
getAlignedPtr(abySrcBuffer, nSrcDataTypeSize);
GByte *pabyDstBuffer =
getAlignedPtr(abyDstBuffer, nDstDataTypeSize);
for (decltype(nWordCount) i = 0; i < nWordCount; i++)
{
memcpy(pabySrcBuffer,
Expand Down

0 comments on commit 98207b4

Please sign in to comment.