Skip to main content
added 724 characters in body
Source Link
barak manos
  • 1.2k
  • 1
  • 7
  • 19

I would change this:

static constexpr uint8_t endianValues[4] = {0, 1, 2, 3};

To this:

static const uint32_t value = HL_LITTLE_ENDIAN; // 0x03020100
static const uint8_t* endianValues = (uint8_t*)&value;

Alternatively, you can change the getEndianOrder function to read the endianValues array as a uint32, but you will have to add a preprocessor directive (#pragma) to make sure that it is placed in a memory address aligned to 4 bytes (and that by itself might yield some platform-dependency issues, which is pretty much in contrast with your goal here to begin with):

constexpr hl_endianness getEndianOrder()
{
    switch (*(uint32_t*)endianValues))
    {
        case HL_LITTLE_ENDIAN: return HL_LITTLE_ENDIAN;
        case HL_BIG_ENDIAN:    return HL_BIG_ENDIAN;
        case HL_PDP_ENDIAN:    return HL_PDP_ENDIAN;
    }
    return HL_UNKNOWN_ENDIAN;
}

I would change this:

static constexpr uint8_t endianValues[4] = {0, 1, 2, 3};

To this:

static const uint32_t value = HL_LITTLE_ENDIAN; // 0x03020100
static const uint8_t* endianValues = (uint8_t*)&value;

I would change this:

static constexpr uint8_t endianValues[4] = {0, 1, 2, 3};

To this:

static const uint32_t value = HL_LITTLE_ENDIAN; // 0x03020100
static const uint8_t* endianValues = (uint8_t*)&value;

Alternatively, you can change the getEndianOrder function to read the endianValues array as a uint32, but you will have to add a preprocessor directive (#pragma) to make sure that it is placed in a memory address aligned to 4 bytes (and that by itself might yield some platform-dependency issues, which is pretty much in contrast with your goal here to begin with):

constexpr hl_endianness getEndianOrder()
{
    switch (*(uint32_t*)endianValues))
    {
        case HL_LITTLE_ENDIAN: return HL_LITTLE_ENDIAN;
        case HL_BIG_ENDIAN:    return HL_BIG_ENDIAN;
        case HL_PDP_ENDIAN:    return HL_PDP_ENDIAN;
    }
    return HL_UNKNOWN_ENDIAN;
}
Source Link
barak manos
  • 1.2k
  • 1
  • 7
  • 19

I would change this:

static constexpr uint8_t endianValues[4] = {0, 1, 2, 3};

To this:

static const uint32_t value = HL_LITTLE_ENDIAN; // 0x03020100
static const uint8_t* endianValues = (uint8_t*)&value;