/*
* Copyright (c) 2018-2020 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 .
*/
#pragma once
#include
#include
namespace ams::util {
namespace impl {
template
struct BitPack {
IntegralStorageType value;
private:
static_assert(std::is_integral::value);
static_assert(std::is_unsigned::value);
template
static constexpr inline IntegralStorageType Mask = [] {
static_assert(Index < BITSIZEOF(IntegralStorageType));
static_assert(0 < Count && Count <= BITSIZEOF(IntegralStorageType));
static_assert(Index + Count <= BITSIZEOF(IntegralStorageType));
if constexpr (Count == BITSIZEOF(IntegralStorageType)) {
return ~IntegralStorageType(0);
} else {
return ((IntegralStorageType(1) << Count) - 1) << Index;
}
}();
public:
template
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;
static_assert(util::is_pod::value);
static_assert(Mask != 0);
static_assert(std::is_integral::value || std::is_enum::value);
static_assert(!std::is_same::value || Count == 1);
};
public:
constexpr ALWAYS_INLINE void Clear() {
constexpr IntegralStorageType Zero = IntegralStorageType(0);
this->value = Zero;
}
template
constexpr ALWAYS_INLINE typename FieldType::Type Get() const {
static_assert(std::is_same>::value);
return static_cast((this->value & Mask) >> FieldType::Index);
}
template
constexpr ALWAYS_INLINE void Set(typename FieldType::Type field_value) {
static_assert(std::is_same>::value);
constexpr IntegralStorageType FieldMask = Mask;
this->value &= ~FieldMask;
this->value |= (static_cast(field_value) << FieldType::Index) & FieldMask;
}
constexpr ALWAYS_INLINE bool operator==(const BitPack &rhs) {
return this->value == rhs.value;
}
constexpr ALWAYS_INLINE bool operator!=(const BitPack &rhs) {
return !(*this == rhs);
}
};
}
using BitPack8 = impl::BitPack;
using BitPack16 = impl::BitPack;
using BitPack32 = impl::BitPack;
using BitPack64 = impl::BitPack;
static_assert(util::is_pod::value);
static_assert(util::is_pod::value);
static_assert(util::is_pod::value);
static_assert(util::is_pod::value);
static_assert(std::is_trivially_destructible::value);
static_assert(std::is_trivially_destructible::value);
static_assert(std::is_trivially_destructible::value);
static_assert(std::is_trivially_destructible::value);
}