Skip to content

Commit

Permalink
Write UUID transformation more portably
Browse files Browse the repository at this point in the history
The previous version did not handle endianness correctly. MSVC
refused to compile the code that assumed we knew what we were doing,
which turned out to be a good thing.
  • Loading branch information
mabraham committed Sep 5, 2024
1 parent b48a0e9 commit 30e6b6d
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions src/gromacs/hardware/tests/device_management.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

#include "config.h"

#include <cstdint>

#include <algorithm>
#include <memory>
#include <string>
Expand Down Expand Up @@ -106,36 +108,65 @@ TEST(DevicesManagerTest, Serialization)
}
}

template<std::size_t N>
uint32_t uint32FromBytes(const std::array<std::byte, N>& data, const std::size_t byteOffset)
{
if (byteOffset + sizeof(uint32_t) > N)
{
throw std::invalid_argument("byteOffset would read out of bounds");
}

// Integer to copy the bytes to
uint32_t result;

// Copy the bytes, assuming little-endian layout. The compiler
// generally elides this away.
std::memcpy(&result, &(data[byteOffset]), sizeof(result));
if (GMX_INTEGER_BIG_ENDIAN)
{
// Change the endianness to match the hardware
const auto* ptr = reinterpret_cast<const uint8_t*>(&result);
return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
}
else
{
return result;
}
}

std::string uuidToString(const std::array<std::byte, 16>& uuid)
{
// Write a string in the frequently used 8-4-4-4-12 format,
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, where every x represents 4 bits
std::string result;
result.reserve(37);
const auto* uuidIt = uuid.cbegin();
for (int i = 0; i < 4; ++i, ++uuidIt)

// Compilers tend to think uuid.size() is not a constant expression!
constexpr std::size_t uuidWidthInBytes = 16;
std::size_t byteOffset = 0;
for (int i = 0; i < 4; ++i, byteOffset += sizeof(uint32_t))
{
result += gmx::formatString("%2.2x", static_cast<unsigned int>(*uuidIt));
result += gmx::formatString("%2.2x", uint32FromBytes<uuidWidthInBytes>(uuid, byteOffset));
}
result.append(1, '-');
for (int i = 0; i < 2; ++i, ++uuidIt)
for (int i = 0; i < 2; ++i, byteOffset += sizeof(uint32_t))
{
result += gmx::formatString("%2.2x", static_cast<unsigned int>(*uuidIt));
result += gmx::formatString("%2.2x", uint32FromBytes<uuidWidthInBytes>(uuid, byteOffset));
}
result.append(1, '-');
for (int i = 0; i < 2; ++i, ++uuidIt)
for (int i = 0; i < 2; ++i, byteOffset += sizeof(uint32_t))
{
result += gmx::formatString("%2.2x", static_cast<unsigned int>(*uuidIt));
result += gmx::formatString("%2.2x", uint32FromBytes<uuidWidthInBytes>(uuid, byteOffset));
}
result.append(1, '-');
for (int i = 0; i < 2; ++i, ++uuidIt)
for (int i = 0; i < 2; ++i, byteOffset += sizeof(uint32_t))
{
result += gmx::formatString("%2.2x", static_cast<unsigned int>(*uuidIt));
result += gmx::formatString("%2.2x", uint32FromBytes<uuidWidthInBytes>(uuid, byteOffset));
}
result.append(1, '-');
for (int i = 0; i < 6; ++i, ++uuidIt)
for (int i = 0; i < 6; ++i, byteOffset += sizeof(uint32_t))
{
result += gmx::formatString("%2.2x", static_cast<unsigned int>(*uuidIt));
result += gmx::formatString("%2.2x", uint32FromBytes<uuidWidthInBytes>(uuid, byteOffset));
}
return result;
}
Expand Down

0 comments on commit 30e6b6d

Please sign in to comment.