mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2024-11-13 23:46:40 +00:00
util: add BitPack
This commit is contained in:
parent
48772307bf
commit
145ee8fcc8
3 changed files with 92 additions and 2 deletions
|
@ -42,6 +42,8 @@
|
||||||
#define CONCATENATE_IMPL(S1, s2) s1##s2
|
#define CONCATENATE_IMPL(S1, s2) s1##s2
|
||||||
#define CONCATENATE(s1, s2) CONCATENATE_IMPL(s1, s2)
|
#define CONCATENATE(s1, s2) CONCATENATE_IMPL(s1, s2)
|
||||||
|
|
||||||
|
#define BITSIZEOF(x) (sizeof(x) * CHAR_BIT)
|
||||||
|
|
||||||
#ifdef __COUNTER__
|
#ifdef __COUNTER__
|
||||||
#define ANONYMOUS_VARIABLE(pref) CONCATENATE(pref, __COUNTER__)
|
#define ANONYMOUS_VARIABLE(pref) CONCATENATE(pref, __COUNTER__)
|
||||||
#else
|
#else
|
||||||
|
@ -55,8 +57,8 @@
|
||||||
probability; \
|
probability; \
|
||||||
}))
|
}))
|
||||||
|
|
||||||
#define AMS_PREDICT_TRUE(expr, probability) AMS_PREDICT(!!expr, 1, probability)
|
#define AMS_PREDICT_TRUE(expr, probability) AMS_PREDICT(!!(expr), 1, probability)
|
||||||
#define AMS_PREDICT_FALSE(expr, probability) AMS_PREDICT(!!expr, 0, probability)
|
#define AMS_PREDICT_FALSE(expr, probability) AMS_PREDICT(!!(expr), 0, probability)
|
||||||
|
|
||||||
#define AMS_LIKELY(expr) AMS_PREDICT_TRUE(expr, 1.0)
|
#define AMS_LIKELY(expr) AMS_PREDICT_TRUE(expr, 1.0)
|
||||||
#define AMS_UNLIKELY(expr) AMS_PREDICT_FALSE(expr, 1.0)
|
#define AMS_UNLIKELY(expr) AMS_PREDICT_FALSE(expr, 1.0)
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
#include "util/util_alignment.hpp"
|
#include "util/util_alignment.hpp"
|
||||||
#include "util/util_size.hpp"
|
#include "util/util_size.hpp"
|
||||||
#include "util/util_fourcc.hpp"
|
#include "util/util_fourcc.hpp"
|
||||||
|
#include "util/util_bitpack.hpp"
|
||||||
#include "util/util_scope_guard.hpp"
|
#include "util/util_scope_guard.hpp"
|
||||||
#include "util/util_typed_storage.hpp"
|
#include "util/util_typed_storage.hpp"
|
||||||
#include "util/util_intrusive_list.hpp"
|
#include "util/util_intrusive_list.hpp"
|
||||||
|
|
87
libraries/libvapours/include/vapours/util/util_bitpack.hpp
Normal file
87
libraries/libvapours/include/vapours/util/util_bitpack.hpp
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms and conditions of the GNU General Public License,
|
||||||
|
* version 2, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "../defines.hpp"
|
||||||
|
|
||||||
|
namespace ams::util {
|
||||||
|
|
||||||
|
namespace impl {
|
||||||
|
|
||||||
|
template<typename IntegralStorageType>
|
||||||
|
class BitPack {
|
||||||
|
private:
|
||||||
|
static_assert(std::is_integral<IntegralStorageType>::value);
|
||||||
|
static_assert(std::is_unsigned<IntegralStorageType>::value);
|
||||||
|
|
||||||
|
template<size_t Index, size_t Count>
|
||||||
|
static constexpr inline IntegralStorageType Mask = [] {
|
||||||
|
static_assert(Index < BITSIZEOF(IntegralStorageType));
|
||||||
|
static_assert(0 < Count && Count <= BITSIZEOF(IntegralStorageType));
|
||||||
|
static_assert(Index + Count <= BITSIZEOF(IntegralStorageType));
|
||||||
|
|
||||||
|
return ((IntegralStorageType(1) << Count) - 1) << Index;
|
||||||
|
}();
|
||||||
|
public:
|
||||||
|
template<size_t _Index, size_t _Count, typename T>
|
||||||
|
struct Field {
|
||||||
|
using Type = T;
|
||||||
|
static constexpr size_t Index = _Index;
|
||||||
|
static constexpr size_t Count = _Count;
|
||||||
|
static constexpr size_t Next = Index + Count;
|
||||||
|
|
||||||
|
using BitPackType = BitPack<IntegralStorageType>;
|
||||||
|
|
||||||
|
static_assert(Mask<Index, Count> != 0);
|
||||||
|
static_assert(std::is_integral<T>::value);
|
||||||
|
static_assert(!std::is_same<T, bool>::value || Count == 1);
|
||||||
|
};
|
||||||
|
private:
|
||||||
|
IntegralStorageType value;
|
||||||
|
public:
|
||||||
|
constexpr ALWAYS_INLINE void Clear() {
|
||||||
|
constexpr IntegralStorageType Zero = IntegralStorageType(0);
|
||||||
|
this->value = Zero;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename FieldType>
|
||||||
|
constexpr ALWAYS_INLINE typename FieldType::Type Get() const {
|
||||||
|
static_assert(std::is_same<FieldType, Field<FieldType::Index, FieldType::Count, typename FieldType::Type>>::value);
|
||||||
|
return static_cast<typename FieldType::Type>((this->value & Mask<FieldType::Index, FieldType::Count>) >> FieldType::Index);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename FieldType>
|
||||||
|
constexpr ALWAYS_INLINE void Set(typename FieldType::Type field_value) {
|
||||||
|
static_assert(std::is_same<FieldType, Field<FieldType::Index, FieldType::Count, typename FieldType::Type>>::value);
|
||||||
|
constexpr IntegralStorageType FieldMask = Mask<FieldType::Index, FieldType::Count>;
|
||||||
|
this->value &= ~FieldMask;
|
||||||
|
this->value |= (static_cast<IntegralStorageType>(field_value) << FieldType::Index) & FieldMask;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
using BitPack8 = impl::BitPack<u8>;
|
||||||
|
using BitPack16 = impl::BitPack<u16>;
|
||||||
|
using BitPack32 = impl::BitPack<u32>;
|
||||||
|
using BitPack64 = impl::BitPack<u64>;
|
||||||
|
|
||||||
|
static_assert(std::is_pod<BitPack8 >::value);
|
||||||
|
static_assert(std::is_pod<BitPack16>::value);
|
||||||
|
static_assert(std::is_pod<BitPack32>::value);
|
||||||
|
static_assert(std::is_pod<BitPack64>::value);
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue