From 2c340f490e67efd1ba37312e40b30289c025488b Mon Sep 17 00:00:00 2001 From: Vas Crabb Date: Sun, 1 Apr 2018 19:10:26 +1000 Subject: move some not-directly-emulation-related helpers to lib/util, further extricate emu.h from tools (nw) --- src/lib/util/coretmpl.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/lib/util/disasmintf.h | 10 ++++++++++ 2 files changed, 55 insertions(+) (limited to 'src/lib') diff --git a/src/lib/util/coretmpl.h b/src/lib/util/coretmpl.h index 011fb28264b..8d317db7596 100644 --- a/src/lib/util/coretmpl.h +++ b/src/lib/util/coretmpl.h @@ -937,6 +937,51 @@ private: bool m_empty; }; + +template +using enable_enum_t = typename std::enable_if_t::value, typename std::underlying_type_t >; + +// template function which takes a strongly typed enumerator and returns its value as a compile-time constant +template +constexpr enable_enum_t underlying_value(E e) noexcept +{ + return static_cast >(e); +} + +// template function which takes an integral value and returns its representation as enumerator (even strongly typed) +template +constexpr typename std::enable_if_t::value && std::is_integral::value, E> enum_value(T value) noexcept +{ + return static_cast(value); +} + + +// useful functions to deal with bit shuffling +template constexpr T BIT(T x, U n) noexcept { return (x >> n) & T(1); } + +template constexpr T bitswap(T val, U b) noexcept { return BIT(val, b) << 0U; } + +template constexpr T bitswap(T val, U b, V... c) noexcept +{ + return (BIT(val, b) << sizeof...(c)) | bitswap(val, c...); +} + +// explicit version that checks number of bit position arguments +template T bitswap(T val, U... b) noexcept +{ + static_assert(sizeof...(b) == B, "wrong number of bits"); + static_assert((sizeof(std::remove_reference_t) * 8) >= B, "return type too small for result"); + return bitswap(val, b...); +} + + +// constexpr absolute value of an integer +template +constexpr std::enable_if_t::value, T> iabs(T v) noexcept +{ + return (v < T(0)) ? -v : v; +} + }; // namespace util #endif // MAME_UTIL_CORETMPL_H diff --git a/src/lib/util/disasmintf.h b/src/lib/util/disasmintf.h index 908bd762bdb..bf4022c5a01 100644 --- a/src/lib/util/disasmintf.h +++ b/src/lib/util/disasmintf.h @@ -16,10 +16,20 @@ #include "coretmpl.h" namespace util { + // class implementing a disassembler class disasm_interface { public: + // independence from emu.h + using u8 = osd::u8; + using u16 = osd::u16; + using u32 = osd::u32; + using u64 = osd::u64; + using s8 = osd::s8; + using s16 = osd::s16; + using s32 = osd::s32; + using s64 = osd::s64; using offs_t = u32; // Disassembler constants for the return value -- cgit v1.2.3