summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/alignment.h29
-rw-r--r--src/common/lz4_compression.cpp6
-rw-r--r--src/common/lz4_compression.h2
3 files changed, 35 insertions, 2 deletions
diff --git a/src/common/alignment.h b/src/common/alignment.h
index fa715d497..0057052af 100644
--- a/src/common/alignment.h
+++ b/src/common/alignment.h
@@ -3,6 +3,7 @@
3 3
4#pragma once 4#pragma once
5 5
6#include <bit>
6#include <cstddef> 7#include <cstddef>
7#include <new> 8#include <new>
8#include <type_traits> 9#include <type_traits>
@@ -10,7 +11,7 @@
10namespace Common { 11namespace Common {
11 12
12template <typename T> 13template <typename T>
13 requires std::is_unsigned_v<T> 14 requires std::is_integral_v<T>
14[[nodiscard]] constexpr T AlignUp(T value, size_t size) { 15[[nodiscard]] constexpr T AlignUp(T value, size_t size) {
15 auto mod{static_cast<T>(value % size)}; 16 auto mod{static_cast<T>(value % size)};
16 value -= mod; 17 value -= mod;
@@ -24,7 +25,7 @@ template <typename T>
24} 25}
25 26
26template <typename T> 27template <typename T>
27 requires std::is_unsigned_v<T> 28 requires std::is_integral_v<T>
28[[nodiscard]] constexpr T AlignDown(T value, size_t size) { 29[[nodiscard]] constexpr T AlignDown(T value, size_t size) {
29 return static_cast<T>(value - value % size); 30 return static_cast<T>(value - value % size);
30} 31}
@@ -55,6 +56,30 @@ template <typename T, typename U>
55 return (x + (y - 1)) / y; 56 return (x + (y - 1)) / y;
56} 57}
57 58
59template <typename T>
60 requires std::is_integral_v<T>
61[[nodiscard]] constexpr T LeastSignificantOneBit(T x) {
62 return x & ~(x - 1);
63}
64
65template <typename T>
66 requires std::is_integral_v<T>
67[[nodiscard]] constexpr T ResetLeastSignificantOneBit(T x) {
68 return x & (x - 1);
69}
70
71template <typename T>
72 requires std::is_integral_v<T>
73[[nodiscard]] constexpr bool IsPowerOfTwo(T x) {
74 return x > 0 && ResetLeastSignificantOneBit(x) == 0;
75}
76
77template <typename T>
78 requires std::is_integral_v<T>
79[[nodiscard]] constexpr T FloorPowerOfTwo(T x) {
80 return T{1} << (sizeof(T) * 8 - std::countl_zero(x) - 1);
81}
82
58template <typename T, size_t Align = 16> 83template <typename T, size_t Align = 16>
59class AlignmentAllocator { 84class AlignmentAllocator {
60public: 85public:
diff --git a/src/common/lz4_compression.cpp b/src/common/lz4_compression.cpp
index ffb32fecf..6867c03c4 100644
--- a/src/common/lz4_compression.cpp
+++ b/src/common/lz4_compression.cpp
@@ -71,4 +71,10 @@ std::vector<u8> DecompressDataLZ4(std::span<const u8> compressed, std::size_t un
71 return uncompressed; 71 return uncompressed;
72} 72}
73 73
74int DecompressLZ4(void* dst, size_t dst_size, const void* src, size_t src_size) {
75 // This is just a thin wrapper around LZ4.
76 return LZ4_decompress_safe(reinterpret_cast<const char*>(src), reinterpret_cast<char*>(dst),
77 static_cast<int>(src_size), static_cast<int>(dst_size));
78}
79
74} // namespace Common::Compression 80} // namespace Common::Compression
diff --git a/src/common/lz4_compression.h b/src/common/lz4_compression.h
index 7fd53a960..7200e0f22 100644
--- a/src/common/lz4_compression.h
+++ b/src/common/lz4_compression.h
@@ -56,4 +56,6 @@ namespace Common::Compression {
56[[nodiscard]] std::vector<u8> DecompressDataLZ4(std::span<const u8> compressed, 56[[nodiscard]] std::vector<u8> DecompressDataLZ4(std::span<const u8> compressed,
57 std::size_t uncompressed_size); 57 std::size_t uncompressed_size);
58 58
59int DecompressLZ4(void* dst, size_t dst_size, const void* src, size_t src_size);
60
59} // namespace Common::Compression 61} // namespace Common::Compression