M5Utility 0.0.12 git rev:13bfbd6
Loading...
Searching...
No Matches
types.hpp File Reference

Endian-aware integer types for safe register access. More...

#include <cstdint>
#include <cstddef>
#include <utility>
#include <type_traits>
#include "stl/endianness.hpp"
#include "stl/byteswap.hpp"

Go to the source code of this file.

Classes

struct  m5::types::EndianInt< T, DataIsLittle >
 Endian-compliant integer type. More...
 

Namespaces

namespace  m5
 Top level namespace of M5.
 

Typedefs

using m5::types::big_uint16_t = EndianInt<uint16_t, false>
 
using m5::types::little_uint16_t = EndianInt<uint16_t, true>
 
using m5::types::big_int16_t = EndianInt<int16_t, false>
 
using m5::types::little_int16_t = EndianInt<int16_t, true>
 
using m5::types::big_uint32_t = EndianInt<uint32_t, false>
 
using m5::types::little_uint32_t = EndianInt<uint32_t, true>
 
using m5::types::big_int32_t = EndianInt<int32_t, false>
 
using m5::types::little_int32_t = EndianInt<int32_t, true>
 
using m5::types::big_uint64_t = EndianInt<uint64_t, false>
 
using m5::types::little_uint64_t = EndianInt<uint64_t, true>
 
using m5::types::big_int64_t = EndianInt<int64_t, false>
 
using m5::types::little_int64_t = EndianInt<int64_t, true>
 

Detailed Description

Endian-aware integer types for safe register access.

Motivation
Casting raw byte buffers to integer pointers causes undefined behavior:
// UB: alignment violation + wrong endianness
uint16_t val = *(uint16_t*)buf;
Safe usage with endian-aware types
// I2C sensor register (big-endian)
big_uint16_t reg;
memcpy(reg.data(), buf, 2);
uint16_t val = reg.get(); // correct endian conversion