Add std::bit_cast fallback for Helpers::bit_cast and run clang-format

This commit is contained in:
wheremyfoodat 2023-06-18 23:33:10 +03:00 committed by GitHub
commit 88e3eb1558
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <cstdarg>
#include <climits> #include <climits>
#include <cstdarg>
#include <cstdint> #include <cstdint>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
@ -8,8 +8,16 @@
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "termcolor.hpp" #include "termcolor.hpp"
// We have to detect and special-case AppleClang at the moment since its C++20 support is finicky and doesn't quite support std::bit_cast
#if defined(__clang__) && defined(__apple_build_version__)
#define HELPERS_APPLE_CLANG
#else
#include <bit>
#endif
using u8 = std::uint8_t; using u8 = std::uint8_t;
using u16 = std::uint16_t; using u16 = std::uint16_t;
using u32 = std::uint32_t; using u32 = std::uint32_t;
@ -45,15 +53,12 @@ namespace Helpers {
static std::vector<u8> loadROM(std::string directory) { static std::vector<u8> loadROM(std::string directory) {
std::ifstream file(directory, std::ios::binary); std::ifstream file(directory, std::ios::binary);
if (file.fail()) if (file.fail()) panic("Couldn't read %s", directory.c_str());
panic("Couldn't read %s", directory.c_str());
std::vector<u8> ROM; std::vector<u8> ROM;
file.unsetf(std::ios::skipws); file.unsetf(std::ios::skipws);
ROM.insert(ROM.begin(), ROM.insert(ROM.begin(), std::istream_iterator<uint8_t>(file), std::istream_iterator<uint8_t>());
std::istream_iterator<uint8_t>(file),
std::istream_iterator<uint8_t>());
file.close(); file.close();
@ -65,7 +70,6 @@ namespace Helpers {
#ifdef NDEBUG #ifdef NDEBUG
return false; return false;
#endif #endif
return true; return true;
} }
@ -117,9 +121,7 @@ namespace Helpers {
} }
/// Check if a bit "bit" of value is set /// Check if a bit "bit" of value is set
static constexpr bool isBitSet (u32 value, int bit) { static constexpr bool isBitSet(u32 value, int bit) { return (value >> bit) & 1; }
return (value >> bit) & 1;
}
/// rotate number right /// rotate number right
template <typename T> template <typename T>
@ -149,29 +151,25 @@ namespace Helpers {
} }
// For values < 0x99 // For values < 0x99
static constexpr inline u8 incBCDByte(u8 value) { static constexpr inline u8 incBCDByte(u8 value) { return ((value & 0xf) == 0x9) ? value + 7 : value + 1; }
return ((value & 0xf) == 0x9) ? value + 7 : value + 1;
} #ifdef HELPERS_APPLE_CLANG
// Use this helper for platforms that lack a working std::bit_cast implementation
// TODO: Replace this with C++20 version if available
template <class To, class From> template <class To, class From>
constexpr To bit_cast(const From& from) noexcept { constexpr To bit_cast(const From& from) noexcept {
return *reinterpret_cast<const To*>(&from); return *reinterpret_cast<const To*>(&from);
} }
}; #else
template <class To, class From>
constexpr To bit_cast(const From& from) noexcept {
return std::bit_cast<To, From>(from);
}
#endif
}; // namespace Helpers
// UDLs for memory size values // UDLs for memory size values
constexpr size_t operator""_KB(unsigned long long int x) { constexpr size_t operator""_KB(unsigned long long int x) { return 1024ULL * x; }
return 1024ULL * x; constexpr size_t operator""_MB(unsigned long long int x) { return 1024_KB * x; }
} constexpr size_t operator""_GB(unsigned long long int x) { return 1024_MB * x; }
constexpr size_t operator""_MB(unsigned long long int x) {
return 1024_KB * x;
}
constexpr size_t operator""_GB(unsigned long long int x) {
return 1024_MB * x;
}
// useful macros // useful macros
// likely/unlikely // likely/unlikely