diff --git a/src/xenia/base/byte_order.h b/src/xenia/base/byte_order.h index 2a2dee412..9ca9505ce 100644 --- a/src/xenia/base/byte_order.h +++ b/src/xenia/base/byte_order.h @@ -11,6 +11,14 @@ #define XENIA_BASE_BYTE_ORDER_H_ #include +#if defined __has_include +#if __has_include() +#include +#endif +#endif +#if __cpp_lib_endian +#include +#endif #include "xenia/base/assert.h" #include "xenia/base/platform.h" diff --git a/src/xenia/base/memory.h b/src/xenia/base/memory.h index 96eb9b68a..459f09db3 100644 --- a/src/xenia/base/memory.h +++ b/src/xenia/base/memory.h @@ -15,6 +15,7 @@ #include #include #include +#include #include "xenia/base/assert.h" #include "xenia/base/byte_order.h" @@ -441,6 +442,26 @@ inline void store_and_swap(void* mem, return store_and_swap(mem, value); } +using fourcc_t = uint32_t; + +// Get FourCC in host byte order +// make_fourcc('a', 'b', 'c', 'd') == 0x61626364 +constexpr inline fourcc_t make_fourcc(char a, char b, char c, char d) { + return fourcc_t((static_cast(a) << 24) | + (static_cast(b) << 16) | + (static_cast(c) << 8) | static_cast(d)); +} + +// Get FourCC in host byte order +// This overload requires fourcc.length() == 4 +// make_fourcc("abcd") == 'abcd' == 0x61626364 for most compilers +constexpr inline fourcc_t make_fourcc(const std::string_view fourcc) { + if (fourcc.length() != 4) { + throw std::runtime_error("Invalid fourcc length"); + } + return make_fourcc(fourcc[0], fourcc[1], fourcc[2], fourcc[3]); +} + } // namespace xe #endif // XENIA_BASE_MEMORY_H_