Add 'external/ircolib/' from commit 'ce3cd726c8df8388d554abf8bb55d55020eb4450'
git-subtree-dir: external/ircolib git-subtree-mainline:e64eb40b38git-subtree-split:ce3cd726c8
This commit is contained in:
7
external/ircolib/README.md
vendored
Normal file
7
external/ircolib/README.md
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# ircolib
|
||||||
|
|
||||||
|
Collections of useful functions I started copy-pasting in various projects and thus decided to gather all in one place.
|
||||||
|
|
||||||
|
## Flags
|
||||||
|
|
||||||
|
To enable SIMD for `floats.hpp`, add a `#define SIMD_SUPPORT` before `#include "floats.hpp"`.
|
||||||
43
external/ircolib/file.hpp
vendored
Normal file
43
external/ircolib/file.hpp
vendored
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <types.hpp>
|
||||||
|
#include <fstream>
|
||||||
|
#include <vector>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
namespace ircolib {
|
||||||
|
static inline std::vector<u8> ReadFileBinary(const std::string &path) {
|
||||||
|
std::ifstream file(path, std::ios::binary);
|
||||||
|
return {std::istreambuf_iterator{file}, {}};
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void WriteFileBinary(const std::vector<u8> &data, const std::string &path) {
|
||||||
|
std::ofstream file(path, std::ios::binary);
|
||||||
|
std::copy(data.begin(), data.end(), std::ostreambuf_iterator{file});
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void WriteFileBinary(const u8 *data, const size_t size, const std::string &path) {
|
||||||
|
FILE *out = fopen(path.c_str(), "wb");
|
||||||
|
fwrite(data, size, 1, out);
|
||||||
|
fclose(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <size_t Size>
|
||||||
|
static inline void WriteFileBinary(const std::array<u8, Size> &data, const std::string &path) {
|
||||||
|
std::ofstream file(path, std::ios::binary);
|
||||||
|
std::copy(data.begin(), data.end(), std::ostreambuf_iterator{file});
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline size_t NextPow2(size_t num) {
|
||||||
|
// Taken from "Bit Twiddling Hacks" by Sean Anderson:
|
||||||
|
// https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
|
||||||
|
--num;
|
||||||
|
num |= num >> 1;
|
||||||
|
num |= num >> 2;
|
||||||
|
num |= num >> 4;
|
||||||
|
num |= num >> 8;
|
||||||
|
num |= num >> 16;
|
||||||
|
return num + 1;
|
||||||
|
}
|
||||||
|
} // namespace Util
|
||||||
106
external/ircolib/floats.hpp
vendored
Normal file
106
external/ircolib/floats.hpp
vendored
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <cmath>
|
||||||
|
#include <types.hpp>
|
||||||
|
|
||||||
|
namespace ircolib {
|
||||||
|
static inline auto roundCeil(float f) {
|
||||||
|
#ifdef SIMD_SUPPORT
|
||||||
|
__m128 t = _mm_set_ss(f);
|
||||||
|
t = _mm_round_ss(t, t, _MM_FROUND_TO_POS_INF);
|
||||||
|
return _mm_cvtss_f32(t);
|
||||||
|
#else
|
||||||
|
return ceilf(f);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline auto roundCeil(double f) {
|
||||||
|
#ifdef SIMD_SUPPORT
|
||||||
|
__m128d t = _mm_set_sd(f);
|
||||||
|
t = _mm_round_sd(t, t, _MM_FROUND_TO_POS_INF);
|
||||||
|
return _mm_cvtsd_f64(t);
|
||||||
|
#else
|
||||||
|
return ceil(f);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline auto roundNearest(float f) {
|
||||||
|
#ifdef SIMD_SUPPORT
|
||||||
|
__m128 t = _mm_set_ss(f);
|
||||||
|
t = _mm_round_ss(t, t, _MM_FROUND_TO_NEAREST_INT);
|
||||||
|
return _mm_cvtss_f32(t);
|
||||||
|
#else
|
||||||
|
return roundf(f);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline auto roundNearest(double f) {
|
||||||
|
#ifdef SIMD_SUPPORT
|
||||||
|
__m128d t = _mm_set_sd(f);
|
||||||
|
t = _mm_round_sd(t, t, _MM_FROUND_TO_NEAREST_INT);
|
||||||
|
return _mm_cvtsd_f64(t);
|
||||||
|
#else
|
||||||
|
return round(f);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline auto roundCurrent(float f) {
|
||||||
|
#ifdef SIMD_SUPPORT
|
||||||
|
auto t = _mm_set_ss(f);
|
||||||
|
t = _mm_round_ss(t, t, _MM_FROUND_CUR_DIRECTION);
|
||||||
|
return _mm_cvtss_f32(t);
|
||||||
|
#else
|
||||||
|
return rint(f);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline auto roundCurrent(double f) {
|
||||||
|
#ifdef SIMD_SUPPORT
|
||||||
|
auto t = _mm_set_sd(f);
|
||||||
|
t = _mm_round_sd(t, t, _MM_FROUND_CUR_DIRECTION);
|
||||||
|
return _mm_cvtsd_f64(t);
|
||||||
|
#else
|
||||||
|
return rint(f);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static inline auto roundFloor(float f) {
|
||||||
|
#ifdef SIMD_SUPPORT
|
||||||
|
__m128 t = _mm_set_ss(f);
|
||||||
|
t = _mm_round_ss(t, t, _MM_FROUND_TO_NEG_INF);
|
||||||
|
return _mm_cvtss_f32(t);
|
||||||
|
#else
|
||||||
|
return floor(f);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline auto roundFloor(double f) {
|
||||||
|
#ifdef SIMD_SUPPORT
|
||||||
|
__m128d t = _mm_set_sd(f);
|
||||||
|
t = _mm_round_sd(t, t, _MM_FROUND_TO_NEG_INF);
|
||||||
|
return _mm_cvtsd_f64(t);
|
||||||
|
#else
|
||||||
|
return floor(f);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline auto roundTrunc(float f) {
|
||||||
|
#ifdef SIMD_SUPPORT
|
||||||
|
__m128 t = _mm_set_ss(f);
|
||||||
|
t = _mm_round_ss(t, t, _MM_FROUND_TO_ZERO);
|
||||||
|
return _mm_cvtss_f32(t);
|
||||||
|
#else
|
||||||
|
return trunc(f);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline auto roundTrunc(double f) {
|
||||||
|
#ifdef SIMD_SUPPORT
|
||||||
|
__m128d t = _mm_set_sd(f);
|
||||||
|
t = _mm_round_sd(t, t, _MM_FROUND_TO_ZERO);
|
||||||
|
return _mm_cvtsd_f64(t);
|
||||||
|
#else
|
||||||
|
return trunc(f);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
} // namespace Util
|
||||||
146
external/ircolib/mem_access.hpp
vendored
Normal file
146
external/ircolib/mem_access.hpp
vendored
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <types.hpp>
|
||||||
|
#include <cstring>
|
||||||
|
#include <functional>
|
||||||
|
#include <bit>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <vector>
|
||||||
|
#include <concepts>
|
||||||
|
|
||||||
|
namespace ircolib {
|
||||||
|
static inline std::vector<u8> IntegralToBuffer(const std::integral auto &val) {
|
||||||
|
std::vector<u8> ret{};
|
||||||
|
ret.resize(sizeof(val));
|
||||||
|
|
||||||
|
memcpy(ret.data(), &val, sizeof(val));
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline constexpr bool IsInsideRange(const std::integral auto& addr,
|
||||||
|
const std::integral auto& start,
|
||||||
|
const std::integral auto& end) {
|
||||||
|
return addr >= start && addr <= end;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static constexpr inline T ReadAccess(const u8 *data, const u32 index);
|
||||||
|
template <typename T>
|
||||||
|
static constexpr inline T ReadAccess(const std::vector<u8> &data, const u32 index);
|
||||||
|
template <typename T, size_t Size>
|
||||||
|
static constexpr inline T ReadAccess(const std::array<u8, Size> &data, const u32 index);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static constexpr inline void WriteAccess(u8 *data, const u32 index, const T val);
|
||||||
|
template <typename T>
|
||||||
|
static constexpr inline void WriteAccess(std::vector<u8> &data, const u32 index, const T val);
|
||||||
|
template <typename T, size_t Size>
|
||||||
|
static constexpr inline void WriteAccess(std::array<u8, Size> &data, const u32 index, const T val);
|
||||||
|
|
||||||
|
template <>
|
||||||
|
constexpr inline u64 ReadAccess(const u8 *data, const u32 index) {
|
||||||
|
u32 hi = *reinterpret_cast<const u32 *>(&data[index + 0]);
|
||||||
|
u32 lo = *reinterpret_cast<const u32 *>(&data[index + 4]);
|
||||||
|
const auto& result = static_cast<u64>(hi) << 32 | static_cast<u64>(lo);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static constexpr inline T ReadAccess(const u8 *data, const u32 index) {
|
||||||
|
return *reinterpret_cast<const T *>(&data[index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
constexpr inline u64 ReadAccess(const std::vector<u8> &data, const u32 index) {
|
||||||
|
u32 hi = *reinterpret_cast<const u32 *>(&data[index + 0]);
|
||||||
|
u32 lo = *reinterpret_cast<const u32 *>(&data[index + 4]);
|
||||||
|
return (static_cast<u64>(hi) << 32) | static_cast<u64>(lo);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static constexpr inline T ReadAccess(const std::vector<u8> &data, const u32 index) {
|
||||||
|
return *reinterpret_cast<const T *>(&data[index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <size_t Size>
|
||||||
|
constexpr inline u64 ReadAccess(const std::array<u8, Size> &data, const u32 index) {
|
||||||
|
u32 hi = *reinterpret_cast<const u32 *>(&data[index + 0]);
|
||||||
|
u32 lo = *reinterpret_cast<const u32 *>(&data[index + 4]);
|
||||||
|
return static_cast<u64>(hi) << 32 | static_cast<u64>(lo);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, size_t Size>
|
||||||
|
static constexpr inline T ReadAccess(const std::array<u8, Size> &data, const u32 index) {
|
||||||
|
return *reinterpret_cast<const T *>(&data[index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <size_t Size>
|
||||||
|
constexpr inline void WriteAccess(std::array<u8, Size> &data, const u32 index, const u64 val) {
|
||||||
|
const u32 hi = val >> 32;
|
||||||
|
const u32 lo = val;
|
||||||
|
|
||||||
|
*reinterpret_cast<u32 *>(&data[index + 0]) = hi;
|
||||||
|
*reinterpret_cast<u32 *>(&data[index + 4]) = lo;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, size_t Size>
|
||||||
|
static constexpr inline void WriteAccess(std::array<u8, Size> &data, const u32 index, const T val) {
|
||||||
|
*reinterpret_cast<T *>(&data[index]) = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
constexpr inline void WriteAccess(std::vector<u8> &data, const u32 index, const u64 val) {
|
||||||
|
const u32 hi = val >> 32;
|
||||||
|
const u32 lo = val;
|
||||||
|
|
||||||
|
*reinterpret_cast<u32 *>(&data[index + 0]) = hi;
|
||||||
|
*reinterpret_cast<u32 *>(&data[index + 4]) = lo;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static constexpr inline void WriteAccess(std::vector<u8> &data, const u32 index, const T val) {
|
||||||
|
*reinterpret_cast<T *>(&data[index]) = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
constexpr inline void WriteAccess(u8 *data, const u32 index, const u64 val) {
|
||||||
|
const u32 hi = val >> 32;
|
||||||
|
const u32 lo = val;
|
||||||
|
|
||||||
|
*reinterpret_cast<u32 *>(&data[index + 0]) = hi;
|
||||||
|
*reinterpret_cast<u32 *>(&data[index + 4]) = lo;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static constexpr inline void WriteAccess(u8 *data, const u32 index, const T val) {
|
||||||
|
*reinterpret_cast<T *>(&data[index]) = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static constexpr inline void SwapBuffer(std::vector<u8> &data) {
|
||||||
|
for (size_t i = 0; i < data.size(); i += sizeof(T)) {
|
||||||
|
const T original = *reinterpret_cast<T *>(&data[i]);
|
||||||
|
*reinterpret_cast<T *>(&data[i]) = std::byteswap(original);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, size_t Size>
|
||||||
|
static constexpr inline void SwapBuffer(std::array<u8, Size> &data) {
|
||||||
|
for (size_t i = 0; i < data.size(); i += sizeof(T)) {
|
||||||
|
const T original = *reinterpret_cast<T *>(&data[i]);
|
||||||
|
*reinterpret_cast<T *>(&data[i]) = std::byteswap(original);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
inline void *aligned_alloc(const size_t alignment, const size_t size) { return _aligned_malloc(size, alignment); }
|
||||||
|
|
||||||
|
inline void aligned_free(void *ptr) { _aligned_free(ptr); }
|
||||||
|
#else
|
||||||
|
inline void *aligned_alloc(const size_t alignment, const size_t size) {
|
||||||
|
return std::aligned_alloc(alignment, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void aligned_free(void *ptr) { std::free(ptr); }
|
||||||
|
#endif
|
||||||
|
} // namespace Util
|
||||||
13
external/ircolib/types.hpp
vendored
Normal file
13
external/ircolib/types.hpp
vendored
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace ircolib {
|
||||||
|
using u8 = uint8_t;
|
||||||
|
using u16 = uint16_t;
|
||||||
|
using u32 = uint32_t;
|
||||||
|
using u64 = uint64_t;
|
||||||
|
using s8 = int8_t;
|
||||||
|
using s16 = int16_t;
|
||||||
|
using s32 = int32_t;
|
||||||
|
using s64 = int64_t;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user