ams: take care of most TODO C++20s

This commit is contained in:
Michael Scire 2020-05-05 19:53:38 -07:00
parent 13bfeed2d5
commit 232203f4c0
30 changed files with 174 additions and 431 deletions

View file

@ -185,10 +185,7 @@ namespace ams::kern {
T *obj;
private:
constexpr ALWAYS_INLINE void Swap(KScopedAutoObject &rhs) {
/* TODO: C++20 constexpr std::swap */
T *tmp = rhs.obj;
rhs.obj = this->obj;
this->obj = tmp;
std::swap(this->obj, rhs.obj);
}
public:
constexpr ALWAYS_INLINE KScopedAutoObject() : obj(nullptr) { /* ... */ }

View file

@ -64,7 +64,7 @@ namespace ams::kern {
static constexpr u32 GetCapabilityId(CapabilityType type) {
const u32 flag = GetCapabilityFlag(type);
if (true /* C++20: std::is_constant_evaluated() */) {
if (std::is_constant_evaluated()) {
return CountTrailingZero(flag);
} else {
return static_cast<u32>(__builtin_ctz(flag));
@ -84,7 +84,7 @@ namespace ams::kern {
template<CapabilityType Type>
static constexpr inline u32 CapabilityId = []() -> u32 {
const u32 flag = static_cast<u32>(Type) + 1;
if (true /* C++20: std::is_constant_evaluated() */) {
if (std::is_constant_evaluated()) {
for (u32 i = 0; i < BITSIZEOF(u32); i++) {
if (flag & (1u << i)) {
return i;

View file

@ -18,39 +18,34 @@
namespace ams::kern {
/*
TODO: C++20
template<typename T>
concept KPriorityQueueAffinityMask = !std::is_reference<T>::value && requires (T &t) {
{ t.GetAffinityMask() } -> std::convertible_to<u64>;
{ t.SetAffinityMask(std::declval<u64>()) };
template<typename T>
concept KPriorityQueueAffinityMask = !std::is_reference<T>::value && requires (T &t) {
{ t.GetAffinityMask() } -> std::convertible_to<u64>;
{ t.SetAffinityMask(std::declval<u64>()) };
{ t.GetAffinity(std::declval<int32_t>()) } -> std::same_as<bool>;
{ t.SetAffinity(std::declval<int32_t>(), std::declval<bool>()) };
{ t.SetAll() };
};
{ t.GetAffinity(std::declval<int32_t>()) } -> std::same_as<bool>;
{ t.SetAffinity(std::declval<int32_t>(), std::declval<bool>()) };
{ t.SetAll() };
};
template<typename T>
concept KPriorityQueueMember = !std::is_reference<T>::value && requires (T &t) {
{ typename T::QueueEntry() };
{ (typename T::QueueEntry()).Initialize() };
{ (typename T::QueueEntry()).SetPrev(std::addressof(t)) };
{ (typename T::QueueEntry()).SetNext(std::addressof(t)) };
{ (typename T::QueueEntry()).GetNext() } -> std::same_as<T*>;
{ (typename T::QueueEntry()).GetPrev() } -> std::same_as<T*>;
{ t.GetPriorityQueueEntry(std::declval<s32>()) } -> std::same_as<typename T::QueueEntry &>;
template<typename T>
concept KPriorityQueueMember = !std::is_reference<T>::value && requires (T &t) {
{ typename T::QueueEntry() };
{ (typename T::QueueEntry()).Initialize() };
{ (typename T::QueueEntry()).SetPrev(std::addressof(t)) };
{ (typename T::QueueEntry()).SetNext(std::addressof(t)) };
{ (typename T::QueueEntry()).GetNext() } -> std::same_as<T*>;
{ (typename T::QueueEntry()).GetPrev() } -> std::same_as<T*>;
{ t.GetPriorityQueueEntry(std::declval<s32>()) } -> std::same_as<typename T::QueueEntry &>;
{ t.GetAffinityMask() };
{ typename std::remove_cvref<decltype(t.GetAffinityMask())>::type() } -> KPriorityQueueAffinityMask;
{ t.GetAffinityMask() };
{ typename std::remove_cvref<decltype(t.GetAffinityMask())>::type() } -> KPriorityQueueAffinityMask;
{ t.GetActiveCore() } -> std::convertible_to<s32>;
{ t.GetPriority() } -> std::convertible_to<s32>;
};
{ t.GetActiveCore() } -> std::convertible_to<s32>;
{ t.GetPriority() } -> std::convertible_to<s32>;
};
*/
template<typename Member, size_t _NumCores, int LowestPriority, int HighestPriority> /* TODO C++20: requires KPriorityQueueMember<Member> */
template<typename Member, size_t _NumCores, int LowestPriority, int HighestPriority> requires KPriorityQueueMember<Member>
class KPriorityQueue {
public:
using AffinityMaskType = typename std::remove_cv<typename std::remove_reference<decltype(std::declval<Member>().GetAffinityMask())>::type>::type;

View file

@ -33,8 +33,6 @@ namespace ams::kern {
NON_COPYABLE(KScheduler);
NON_MOVEABLE(KScheduler);
public:
using LockType = KAbstractSchedulerLock<KScheduler>;
static constexpr s32 HighestCoreMigrationAllowedPriority = 2;
static_assert(ams::svc::LowestThreadPriority >= HighestCoreMigrationAllowedPriority);
static_assert(ams::svc::HighestThreadPriority <= HighestCoreMigrationAllowedPriority);
@ -50,9 +48,6 @@ namespace ams::kern {
private:
friend class KScopedSchedulerLock;
friend class KScopedSchedulerLockAndSleep;
static bool s_scheduler_update_needed;
static LockType s_scheduler_lock;
static KSchedulerPriorityQueue s_priority_queue;
private:
SchedulingState state;
bool is_active;
@ -160,6 +155,12 @@ namespace ams::kern {
}
NOINLINE u64 UpdateHighestPriorityThread(KThread *thread);
public:
using LockType = KAbstractSchedulerLock<KScheduler>;
private:
static bool s_scheduler_update_needed;
static KSchedulerPriorityQueue s_priority_queue;
static LockType s_scheduler_lock;
};
class KScopedSchedulerLock : KScopedLock<KScheduler::LockType> {

View file

@ -23,19 +23,14 @@ namespace ams::kern {
class KThread;
/*
TODO: C++20
template<typename T>
concept KSchedulerLockable = !std::is_reference<T>::value && requires(T) {
{ T::DisableScheduling() } -> std::same_as<void>;
{ T::EnableScheduling(std::declval<u64>()) } -> std::same_as<void>;
{ T::UpdateHighestPriorityThreads() } -> std::convertible_to<u64>;
};
template<typename T>
concept KSchedulerLockable = !std::is_reference<T>::value && requires {
{ T::DisableScheduling() } -> std::same_as<void>;
{ T::EnableScheduling(std::declval<u64>()) } -> std::same_as<void>;
{ T::UpdateHighestPriorityThreads() } -> std::convertible_to<u64>;
};
*/
template<typename SchedulerType> /* TODO C++20: requires KSchedulerLockable<SchedulerType> */
template<typename SchedulerType> requires KSchedulerLockable<SchedulerType>
class KAbstractSchedulerLock {
private:
KAlignedSpinLock spin_lock;

View file

@ -18,18 +18,13 @@
namespace ams::kern {
/*
TODO: C++20
template<typename T>
concept KLockable = !std::is_reference<T>::value && requires (T &t) {
{ t.Lock() } -> std::same_as<void>;
{ t.Unlock() } -> std::same_as<void>;
};
template<typename T>
concept KLockable = !std::is_reference<T>::value && requires (T &t) {
{ t.Lock() } -> std::same_as<void>;
{ t.Unlock() } -> std::same_as<void>;
};
*/
template<typename T> /* TODO C++20: requires KLockable<T> */
template<typename T> requires KLockable<T>
class KScopedLock {
NON_COPYABLE(KScopedLock);
NON_MOVEABLE(KScopedLock);

View file

@ -22,47 +22,35 @@ namespace ams::kern::svc {
namespace impl {
/* TODO: C++20
template<typename T>
concept Pointer = std::is_pointer<T>::value;
template<typename T>
concept NonConstPointer = Pointer<T> && !std::is_const<typename std::remove_pointer<T>::type>::value;
template<typename T>
concept ConstPointer = Pointer<T> && std::is_const<typename std::remove_pointer<T>::type>::value;
template<typename T, size_t N>
concept AlignedNPointer = Pointer<T> && alignof(typename std::remove_pointer<T>::type) >= N && util::IsAligned(sizeof(typename std::remove_pointer<T>::type), N);
template<typename T>
concept Aligned8Pointer = AlignedNPointer<T, sizeof(u8)>;
template<typename T>
concept Aligned16Pointer = AlignedNPointer<T, sizeof(u16)> && Aligned8<T>;
template<typename T>
concept Aligned32Pointer = AlignedNPointer<T, sizeof(u32)> && Aligned16<T>;
template<typename T>
concept Aligned64Pointer = AlignedNPointer<T, sizeof(u64)> && Aligned32<T>;
*/
template<typename T>
concept Pointer = std::is_pointer<T>::value;
template<typename T>
constexpr inline bool IsPointer = std::is_pointer<T>::value;
concept NonConstPointer = Pointer<T> && !std::is_const<typename std::remove_pointer<T>::type>::value;
template<typename T>
constexpr inline bool IsConstPointer = IsPointer<T> && std::is_const<typename std::remove_pointer<T>::type>::value;
template<typename T>
constexpr inline bool IsNonConstPointer = IsPointer<T> && !std::is_const<typename std::remove_pointer<T>::type>::value;
concept ConstPointer = Pointer<T> && std::is_const<typename std::remove_pointer<T>::type>::value;
template<typename T, size_t N>
constexpr inline bool IsAlignedNPointer = IsPointer<T> && alignof(typename std::remove_pointer<T>::type) >= N && util::IsAligned(sizeof(typename std::remove_pointer<T>::type), N);
concept AlignedNPointer = Pointer<T> && alignof(typename std::remove_pointer<T>::type) >= N && util::IsAligned(sizeof(typename std::remove_pointer<T>::type), N);
template<typename _T, typename = void> /* requires Aligned8Pointer<_T> */
class KUserPointerImplTraits {
static_assert(IsAlignedNPointer<_T, sizeof(u8)>);
template<typename T>
concept Aligned8Pointer = AlignedNPointer<T, sizeof(u8)>;
template<typename T>
concept Aligned16Pointer = AlignedNPointer<T, sizeof(u16)> && Aligned8Pointer<T>;
template<typename T>
concept Aligned32Pointer = AlignedNPointer<T, sizeof(u32)> && Aligned16Pointer<T>;
template<typename T>
concept Aligned64Pointer = AlignedNPointer<T, sizeof(u64)> && Aligned32Pointer<T>;
template<typename _T>
class KUserPointerImplTraits;
template<typename _T> requires Aligned8Pointer<_T>
class KUserPointerImplTraits<_T> {
public:
using T = typename std::remove_const<typename std::remove_pointer<_T>::type>::type;
public:
@ -77,9 +65,8 @@ namespace ams::kern::svc {
}
};
template<typename _T> /* requires Aligned32Pointer<_T> */
class KUserPointerImplTraits<_T, typename std::enable_if<IsAlignedNPointer<_T, sizeof(u32)> && !IsAlignedNPointer<_T, sizeof(u64)>>::type> {
static_assert(IsAlignedNPointer<_T, sizeof(u32)>);
template<typename _T> requires Aligned32Pointer<_T>
class KUserPointerImplTraits<_T> {
public:
using T = typename std::remove_const<typename std::remove_pointer<_T>::type>::type;
public:
@ -94,9 +81,8 @@ namespace ams::kern::svc {
}
};
template<typename _T> /* requires Aligned64Pointer<_T> */
class KUserPointerImplTraits<_T, typename std::enable_if<IsAlignedNPointer<_T, sizeof(u64)>>::type> {
static_assert(IsAlignedNPointer<_T, sizeof(u64)>);
template<typename _T> requires Aligned64Pointer<_T>
class KUserPointerImplTraits<_T> {
public:
using T = typename std::remove_const<typename std::remove_pointer<_T>::type>::type;
public:
@ -111,8 +97,11 @@ namespace ams::kern::svc {
}
};
template<typename _T> /* requires Aligned8Pointer<_T> */
class KUserPointerImpl : impl::KUserPointerTag {
template<typename _T>
class KUserPointerImpl;
template<typename _T> requires Aligned8Pointer<_T>
class KUserPointerImpl<_T> : impl::KUserPointerTag {
private:
using Traits = KUserPointerImplTraits<_T>;
protected:
@ -170,11 +159,11 @@ namespace ams::kern::svc {
}
template<typename T, typename = void>
class KUserPointer;
template<typename T>
struct KUserPointer;
template<typename T> /* requires impl::ConstPointer<T> */
struct KUserPointer<T, typename std::enable_if<impl::IsConstPointer<T>>::type> : public impl::KUserPointerImpl<T> {
template<typename T> requires impl::ConstPointer<T>
struct KUserPointer<T> : public impl::KUserPointerImpl<T> {
public:
static constexpr bool IsInput = true;
public:
@ -186,8 +175,8 @@ namespace ams::kern::svc {
using impl::KUserPointerImpl<T>::GetUnsafePointer;
};
template<typename T> /* requires impl::NonConstPointer<T> */
struct KUserPointer<T, typename std::enable_if<impl::IsNonConstPointer<T>>::type> : public impl::KUserPointerImpl<T> {
template<typename T> requires impl::NonConstPointer<T>
struct KUserPointer<T> : public impl::KUserPointerImpl<T> {
public:
static constexpr bool IsInput = false;
public:

View file

@ -19,9 +19,8 @@ namespace ams::kern {
namespace {
/* TODO: C++20 constinit */
KLightLock g_object_list_lock;
KObjectName::List g_object_list;
constinit KLightLock g_object_list_lock;
constinit KObjectName::List g_object_list;
}

View file

@ -77,7 +77,7 @@ namespace ams::sf {
class Out<std::shared_ptr<ServiceImpl>> : public impl::OutObjectTag {
static_assert(std::is_base_of<sf::IServiceObject, ServiceImpl>::value, "Out<std::shared_ptr<ServiceImpl>> requires ServiceObject base.");
template<typename, typename>
template<typename>
friend class Out;
public:
@ -308,11 +308,7 @@ namespace ams::sf::impl {
/* Use insertion sort, which is stable and optimal for small numbers of parameters. */
for (size_t i = 1; i < sizeof...(Ts); i++) {
for (size_t j = i; j > 0 && values[map[j-1]] > values[map[j]]; j--) {
/* std::swap is not constexpr until c++20 :( */
/* TODO: std::swap(map[j], map[j-1]); */
const size_t tmp = map[j];
map[j] = map[j-1];
map[j-1] = tmp;
std::swap(map[j], map[j-1]);
}
}
}

View file

@ -33,9 +33,9 @@ namespace ams::sf {
struct IsOutForceEnabled<::ams::Result> : public std::true_type{};
template<typename T>
using IsOutEnabled = typename std::enable_if<std::is_trivial<T>::value || IsOutForceEnabled<T>::value>::type;
concept OutEnabled = (std::is_trivial<T>::value || IsOutForceEnabled<T>::value) && !std::is_pointer<T>::value;
template<typename T, typename = IsOutEnabled<T>>
template<typename T> requires OutEnabled<T>
class Out : public impl::OutBaseTag {
public:
static constexpr size_t TypeSize = sizeof(T);

View file

@ -18,7 +18,6 @@
namespace ams::os::impl {
/* TODO: C++20 constinit */
TYPED_STORAGE(OsResourceManager) ResourceManagerHolder::s_resource_manager_storage = {};
constinit TYPED_STORAGE(OsResourceManager) ResourceManagerHolder::s_resource_manager_storage = {};
}

View file

@ -23,7 +23,7 @@
namespace ams::crypto {
template<size_t ModulusSize, typename Hash> /* requires HashFunction<Hash> */
template<size_t ModulusSize, typename Hash> requires impl::HashFunction<Hash>
class RsaOaepDecryptor {
NON_COPYABLE(RsaOaepDecryptor);
NON_MOVEABLE(RsaOaepDecryptor);

View file

@ -23,7 +23,7 @@
namespace ams::crypto {
template<size_t ModulusSize, typename Hash> /* requires HashFunction<Hash> */
template<size_t ModulusSize, typename Hash> requires impl::HashFunction<Hash>
class RsaOaepEncryptor {
NON_COPYABLE(RsaOaepEncryptor);
NON_MOVEABLE(RsaOaepEncryptor);

View file

@ -23,7 +23,7 @@
namespace ams::crypto {
template<size_t _ModulusSize, typename Hash> /* requires HashFunction<Hash> */
template<size_t _ModulusSize, typename Hash> requires impl::HashFunction<Hash>
class RsaPssVerifier {
NON_COPYABLE(RsaPssVerifier);
NON_MOVEABLE(RsaPssVerifier);

View file

@ -23,15 +23,13 @@
namespace ams::crypto::impl {
/* TODO: C++20
template<typename T>
concept HashFunction = requires(T &t, const void *cv, void *v, size_t sz) {
{ T::HashSize } -> std::same_as<size_t>;
{ T::BlockSize } -> std::same_as<size_t>;
{ t.Initialize() } -> std::same_as<void>;
{ t.Update(cv, sz) } -> std::same_as<void>;
{ t.GetHash(v, sz) } -> std::same_as<void>;
};
*/
template<typename T>
concept HashFunction = requires(T &t, const void *cv, void *v, size_t sz) {
{ T::HashSize } -> std::convertible_to<size_t>;
{ T::BlockSize } -> std::convertible_to<size_t>;
{ t.Initialize() } -> std::same_as<void>;
{ t.Update(cv, sz) } -> std::same_as<void>;
{ t.GetHash(v, sz) } -> std::same_as<void>;
};
}

View file

@ -22,7 +22,7 @@
namespace ams::crypto::impl {
template<typename Hash> /* requires HashFunction<Hash> */
template<typename Hash> requires HashFunction<Hash>
class RsaOaepImpl {
NON_COPYABLE(RsaOaepImpl);
NON_MOVEABLE(RsaOaepImpl);

View file

@ -22,7 +22,7 @@
namespace ams::crypto::impl {
template<typename Hash> /* requires HashFunction<Hash> */
template<typename Hash> requires HashFunction<Hash>
class RsaPssImpl {
NON_COPYABLE(RsaPssImpl);
NON_MOVEABLE(RsaPssImpl);

View file

@ -66,6 +66,6 @@ namespace ams::crypto::impl {
}
};
/* static_assert(HashFunction<Sha256Impl>); */
static_assert(HashFunction<Sha256Impl>);
}

View file

@ -28,6 +28,7 @@
/* C++ headers. */
#include <type_traits>
#include <concepts>
#include <algorithm>
#include <iterator>
#include <limits>
@ -38,6 +39,8 @@
#include <functional>
#include <tuple>
#include <array>
#include <bit>
#include <span>
/* Stratosphere wants additional libstdc++ headers, others do not. */
#ifdef ATMOSPHERE_IS_STRATOSPHERE

View file

@ -19,207 +19,7 @@
namespace ams {
/* TODO C++20 switch to template<typename T> using Span = std::span<T> */
namespace impl {
template<typename Span>
class SpanConstIterator;
template<typename Span, typename Derived, typename Reference>
class SpanIteratorImpl {
public:
friend class SpanConstIterator<Span>;
using index_type = typename Span::index_type;
using difference_type = typename Span::difference_type;
using value_type = typename std::remove_cv<typename Span::element_type>::type;
using pointer = typename std::add_pointer<Reference>::type;
using reference = Reference;
using iterator_category = std::random_access_iterator_tag;
private:
const Span *span = nullptr;
index_type index = 0;
public:
constexpr ALWAYS_INLINE SpanIteratorImpl() = default;
constexpr ALWAYS_INLINE SpanIteratorImpl(const Span *s, index_type idx) : span(s), index(idx) { /* ... */ }
constexpr ALWAYS_INLINE pointer operator->() const {
return this->span->data() + this->index;
}
constexpr ALWAYS_INLINE reference operator*() const {
return *this->operator->();
}
constexpr ALWAYS_INLINE Derived operator++(int) {
auto prev = static_cast<Derived &>(*this);
++(*this);
return prev;
}
constexpr ALWAYS_INLINE Derived operator--(int) {
auto prev = static_cast<Derived &>(*this);
--(*this);
return prev;
}
constexpr ALWAYS_INLINE Derived &operator++() { ++this->index; return static_cast<Derived &>(*this); }
constexpr ALWAYS_INLINE Derived &operator--() { --this->index; return static_cast<Derived &>(*this); }
constexpr ALWAYS_INLINE Derived &operator+=(difference_type n) { this->index += n; return static_cast<Derived &>(*this); }
constexpr ALWAYS_INLINE Derived &operator-=(difference_type n) { this->index -= n; return static_cast<Derived &>(*this); }
constexpr ALWAYS_INLINE Derived operator+(difference_type n) const { auto r = static_cast<const Derived &>(*this); return r += n; }
constexpr ALWAYS_INLINE Derived operator-(difference_type n) const { auto r = static_cast<const Derived &>(*this); return r -= n; }
constexpr ALWAYS_INLINE friend Derived operator+(difference_type n, Derived it) { return it + n; }
constexpr ALWAYS_INLINE difference_type operator-(Derived rhs) const { AMS_ASSERT(this->span == rhs.span); return this->index - rhs.index; }
constexpr ALWAYS_INLINE reference operator[](difference_type n) const { return *(*this + n); }
constexpr ALWAYS_INLINE friend bool operator==(Derived lhs, Derived rhs) {
return lhs.span == rhs.span && lhs.index == rhs.index;
}
constexpr ALWAYS_INLINE friend bool operator<(Derived lhs, Derived rhs) {
AMS_ASSERT(lhs.span == rhs.span);
return lhs.index < rhs.index;
}
constexpr ALWAYS_INLINE friend bool operator!=(Derived lhs, Derived rhs) { return !(lhs == rhs); }
constexpr ALWAYS_INLINE friend bool operator>(Derived lhs, Derived rhs) { return rhs < lhs; }
constexpr ALWAYS_INLINE friend bool operator<=(Derived lhs, Derived rhs) { return !(lhs > rhs); }
constexpr ALWAYS_INLINE friend bool operator>=(Derived lhs, Derived rhs) { return !(lhs < rhs); }
};
template<typename Span>
class SpanIterator : public SpanIteratorImpl<Span, SpanIterator<Span>, typename Span::element_type&> {
public:
using SpanIteratorImpl<Span, SpanIterator<Span>, typename Span::element_type&>::SpanIteratorImpl;
};
template<typename Span>
class SpanConstIterator : public SpanIteratorImpl<Span, SpanConstIterator<Span>, const typename Span::element_type&> {
public:
using SpanIteratorImpl<Span, SpanConstIterator<Span>, const typename Span::element_type&>::SpanIteratorImpl;
constexpr ALWAYS_INLINE SpanConstIterator() = default;
constexpr ALWAYS_INLINE SpanConstIterator(const SpanIterator<Span> &rhs) : SpanConstIterator(rhs.span, rhs.index) { /* ... */ }
};
}
template<typename T>
class Span {
public:
using element_type = T;
using value_type = typename std::remove_cv<element_type>::type;
using index_type = std::ptrdiff_t;
using difference_type = std::ptrdiff_t;
using pointer = element_type *;
using reference = element_type &;
using iterator = ::ams::impl::SpanIterator<Span>;
using const_iterator = ::ams::impl::SpanConstIterator<Span>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
private:
T *ptr;
index_type num_elements;
public:
constexpr ALWAYS_INLINE Span() : ptr(), num_elements() { /* ... */ }
constexpr ALWAYS_INLINE Span(T *p, index_type size) : ptr(p), num_elements(size) {
AMS_ASSERT(this->num_elements > 0 || this->ptr == nullptr);
}
constexpr ALWAYS_INLINE Span(T *start, T *end) : Span(start, end - start) { /* ... */ }
template<size_t Size>
constexpr ALWAYS_INLINE Span(T (&arr)[Size]) : Span(static_cast<T *>(arr), static_cast<index_type>(Size)) { /* ... */ }
template<size_t Size>
constexpr ALWAYS_INLINE Span(std::array<value_type, Size> &arr) : Span(arr.data(), static_cast<index_type>(Size)) { /* ... */ }
template<size_t Size>
constexpr ALWAYS_INLINE Span(const std::array<value_type, Size> &arr) : Span(arr.data(), static_cast<index_type>(Size)) { /* ... */ }
template<typename U, typename = typename std::enable_if<std::is_convertible<U(*)[], T(*)[]>::value>::type>
constexpr ALWAYS_INLINE Span(const Span<U> &rhs) : Span(rhs.data(), rhs.size()) { /* ... */ }
public:
constexpr ALWAYS_INLINE iterator begin() const { return { this, 0 }; }
constexpr ALWAYS_INLINE iterator end() const { return { this, this->num_elements }; }
constexpr ALWAYS_INLINE const_iterator cbegin() const { return { this, 0 }; }
constexpr ALWAYS_INLINE const_iterator cend() const { return { this, this->num_elements }; }
constexpr ALWAYS_INLINE reverse_iterator rbegin() const { return reverse_iterator(this->end()); }
constexpr ALWAYS_INLINE reverse_iterator rend() const { return reverse_iterator(this->begin()); }
constexpr ALWAYS_INLINE const_reverse_iterator crbegin() const { return reverse_iterator(this->cend()); }
constexpr ALWAYS_INLINE const_reverse_iterator crend() const { return reverse_iterator(this->cbegin()); }
constexpr ALWAYS_INLINE pointer data() const { return this->ptr; }
constexpr ALWAYS_INLINE index_type size() const { return this->num_elements; }
constexpr ALWAYS_INLINE index_type size_bytes() const { return this->size() * sizeof(T); }
constexpr ALWAYS_INLINE bool empty() const { return this->size() == 0; }
constexpr ALWAYS_INLINE T &operator[](index_type idx) const {
AMS_ASSERT(idx < this->size());
return this->ptr[idx];
}
constexpr ALWAYS_INLINE T &operator()(index_type idx) const { return (*this)[idx]; }
constexpr ALWAYS_INLINE Span first(index_type size) const {
AMS_ASSERT(size <= this->size());
return { this->ptr, size };
}
constexpr ALWAYS_INLINE Span last(index_type size) const {
AMS_ASSERT(size <= this->size());
return { this->ptr + (this->size() - size), size };
}
constexpr ALWAYS_INLINE Span subspan(index_type idx, index_type size) const {
AMS_ASSERT(size <= this->size());
AMS_ASSERT(this->size() - size >= idx);
return { this->ptr + idx, size };
}
constexpr ALWAYS_INLINE Span subspan(index_type idx) const {
AMS_ASSERT(idx <= this->size());
return { this->ptr + idx, this->size() - idx };
}
};
template<typename T>
constexpr ALWAYS_INLINE Span<T> MakeSpan(T *start, T *end) {
return { start, end };
}
template<typename T>
constexpr ALWAYS_INLINE Span<T> MakeSpan(T *p, typename Span<T>::index_type size) {
return { p, size };
}
template<typename T, size_t Size>
constexpr ALWAYS_INLINE Span<T> MakeSpan(T (&arr)[Size]) {
return Span<T>(arr);
}
template<typename T, size_t Size>
constexpr ALWAYS_INLINE Span<T> MakeSpan(std::array<T, Size> &arr) {
return Span<T>(arr);
}
template<typename T, size_t Size>
constexpr ALWAYS_INLINE Span<const T> MakeSpan(const std::array<T, Size> &arr) {
return Span<const T>(arr);
}
using Span = std::span<T>;
}

View file

@ -220,11 +220,7 @@ namespace ams::svc::codegen::impl {
}
for (size_t i = 1; i < num_parameters; i++) {
for (size_t j = i; j > 0 && param_layout.GetParameter(map[j-1]).GetLocation(0) > param_layout.GetParameter(map[j]).GetLocation(0); j--) {
/* std::swap is not constexpr until c++20 :( */
/* TODO: std::swap(map[j], map[j-1]); */
const size_t tmp = map[j];
map[j] = map[j-1];
map[j-1] = tmp;
std::swap(map[j], map[j-1]);
}
}

View file

@ -313,11 +313,7 @@ namespace ams::svc::codegen::impl {
}
for (size_t i = 1; i < num_parameters; i++) {
for (size_t j = i; j > 0 && CapturedSvc.GetParameter(map[j-1]).GetLocation(0) > CapturedSvc.GetParameter(map[j]).GetLocation(0); j--) {
/* std::swap is not constexpr until c++20 :( */
/* TODO: std::swap(map[j], map[j-1]); */
const size_t tmp = map[j];
map[j] = map[j-1];
map[j-1] = tmp;
std::swap(map[j], map[j-1]);
}
}
return map;
@ -376,7 +372,6 @@ namespace ams::svc::codegen::impl {
constexpr size_t RegisterSize = SvcAbiType::RegisterSize;
constexpr size_t PassedSize = ProcedureParam.GetTypeSize();
/* TODO: C++20 templated lambdas. For now, use GCC extension syntax. */
constexpr auto SvcIndexSequence = []<auto CapturedSvcParam, size_t... Is>(std::index_sequence<Is...>) {
return std::index_sequence<CapturedSvcParam.GetLocation(Is).GetIndex()...>{};
}.template operator()<SvcParam>(std::make_index_sequence<SvcParam.GetNumLocations()>());

View file

@ -30,11 +30,9 @@ namespace ams::util {
}
template <typename T>
template <typename T> requires std::integral<T>
class BitsOf {
private:
static_assert(std::is_integral<T>::value);
static constexpr ALWAYS_INLINE int GetLsbPos(T v) {
return __builtin_ctzll(static_cast<u64>(v));
}
@ -78,69 +76,68 @@ namespace ams::util {
}
};
template<typename T = u64, typename ...Args>
template<typename T = u64, typename ...Args> requires std::integral<T>
constexpr ALWAYS_INLINE T CombineBits(Args... args) {
return (... | (T(1u) << args));
}
template<typename T>
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T ResetLeastSignificantOneBit(T x) {
return x & (x - 1);
}
template<typename T>
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T SetLeastSignificantZeroBit(T x) {
return x | (x + 1);
}
template<typename T>
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T LeastSignificantOneBit(T x) {
return x & ~(x - 1);
}
template<typename T>
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T LeastSignificantZeroBit(T x) {
return ~x & (x + 1);
}
template<typename T>
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T ResetTrailingOnes(T x) {
return x & (x + 1);
}
template<typename T>
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T SetTrailingZeros(T x) {
return x | (x - 1);
}
template<typename T>
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T MaskTrailingZeros(T x) {
return (~x) & (x - 1);
}
template<typename T>
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T MaskTrailingOnes(T x) {
return ~((~x) | (x + 1));
}
template<typename T>
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T MaskTrailingZerosAndLeastSignificantOneBit(T x) {
return x ^ (x - 1);
}
template<typename T>
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T MaskTrailingOnesAndLeastSignificantZeroBit(T x) {
return x ^ (x + 1);
}
template<typename T>
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE int PopCount(T x) {
/* TODO: C++20 std::bit_cast */
using U = typename std::make_unsigned<T>::type;
U u = static_cast<U>(x);
/* TODO: C++20 std::is_constant_evaluated */
if (false) {
if (std::is_constant_evaluated()) {
/* https://en.wikipedia.org/wiki/Hamming_weight */
constexpr U m1 = U(-1) / 0x03;
constexpr U m2 = U(-1) / 0x05;
@ -168,10 +165,9 @@ namespace ams::util {
}
}
template<typename T>
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE int CountLeadingZeros(T x) {
/* TODO: C++20 std::is_constant_evaluated */
if (false) {
if (std::is_constant_evaluated()) {
for (size_t i = 0; i < impl::Log2<BITSIZEOF(T)>; ++i) {
const size_t shift = (0x1 << i);
x |= x >> shift;
@ -195,18 +191,18 @@ namespace ams::util {
}
}
template<typename T>
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE bool IsPowerOfTwo(T x) {
return x > 0 && ResetLeastSignificantOneBit(x) == 0;
}
template<typename T>
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T CeilingPowerOfTwo(T x) {
AMS_ASSERT(x > 0);
return T(1) << (BITSIZEOF(T) - CountLeadingZeros(T(x - 1)));
}
template<typename T>
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T FloorPowerOfTwo(T x) {
AMS_ASSERT(x > 0);
return T(1) << (BITSIZEOF(T) - CountLeadingZeros(x) - 1);

View file

@ -20,27 +20,17 @@
namespace ams::util {
/* TODO: C++20 std::endian */
constexpr bool IsLittleEndian() {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return true;
#else
return false;
#endif
return std::endian::native == std::endian::little;
}
constexpr bool IsBigEndian() {
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return true;
#else
return false;
#endif
return std::endian::native == std::endian::big;
}
static_assert(IsLittleEndian() ^ IsBigEndian());
template<typename U> /* requires unsigned_integral<U> */
template<typename U> requires std::unsigned_integral<U>
constexpr ALWAYS_INLINE U SwapBytes(const U u) {
static_assert(BITSIZEOF(u8) == 8);
constexpr U ByteMask = 0xFFu;
@ -85,14 +75,14 @@ namespace ams::util {
((u & (ByteMask << 0)) << 40);
}
template<typename T> /* requires integral<T> */
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE void SwapBytes(T *ptr) {
using U = typename std::make_unsigned<T>::type;
*ptr = static_cast<T>(SwapBytes(static_cast<U>(*ptr)));
}
template<typename T> /* requires integral<T> */
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T ConvertToBigEndian(const T val) {
using U = typename std::make_unsigned<T>::type;
@ -104,7 +94,7 @@ namespace ams::util {
}
}
template<typename T> /* requires integral<T> */
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T ConvertToLittleEndian(const T val) {
using U = typename std::make_unsigned<T>::type;
@ -116,7 +106,7 @@ namespace ams::util {
}
}
template<typename T> /* requires integral<T> */
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T ConvertToBigEndian48(const T val) {
using U = typename std::make_unsigned<T>::type;
static_assert(sizeof(T) == sizeof(u64));
@ -130,7 +120,7 @@ namespace ams::util {
}
}
template<typename T> /* requires integral<T> */
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T ConvertToLittleEndian48(const T val) {
using U = typename std::make_unsigned<T>::type;
static_assert(sizeof(T) == sizeof(u64));
@ -144,12 +134,12 @@ namespace ams::util {
}
}
template<typename T> /* requires integral<T> */
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T LoadBigEndian(T *ptr) {
return ConvertToBigEndian(*ptr);
}
template<typename T> /* requires integral<T> */
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T LoadLittleEndian(T *ptr) {
return ConvertToLittleEndian(*ptr);
}

View file

@ -17,16 +17,14 @@
#pragma once
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/util/util_endian.hpp>
namespace ams::util {
template<char A, char B, char C, char D>
struct FourCC {
/* TODO: C++20 std::endian */
static constexpr u32 Code = (static_cast<u32>(A) << 0x00) |
(static_cast<u32>(B) << 0x08) |
(static_cast<u32>(C) << 0x10) |
(static_cast<u32>(D) << 0x18);
static constexpr u32 Code = IsLittleEndian() ? ((static_cast<u32>(A) << 0x00) | (static_cast<u32>(B) << 0x08) | (static_cast<u32>(C) << 0x10) | (static_cast<u32>(D) << 0x18))
: ((static_cast<u32>(A) << 0x18) | (static_cast<u32>(B) << 0x10) | (static_cast<u32>(C) << 0x08) | (static_cast<u32>(D) << 0x00));
static constexpr const char String[] = {A, B, C, D};
@ -36,11 +34,8 @@ namespace ams::util {
template<char A, char B, char C, char D>
struct ReverseFourCC {
/* TODO: C++20 std::endian */
static constexpr u32 Code = (static_cast<u32>(A) << 0x18) |
(static_cast<u32>(B) << 0x10) |
(static_cast<u32>(C) << 0x08) |
(static_cast<u32>(D) << 0x00);
static constexpr u32 Code = IsLittleEndian() ? ((static_cast<u32>(A) << 0x18) | (static_cast<u32>(B) << 0x10) | (static_cast<u32>(C) << 0x08) | (static_cast<u32>(D) << 0x00))
: ((static_cast<u32>(A) << 0x00) | (static_cast<u32>(B) << 0x08) | (static_cast<u32>(C) << 0x10) | (static_cast<u32>(D) << 0x18));
static constexpr const char String[] = {D, C, B, A};

View file

@ -18,22 +18,22 @@
namespace ams::kern {
/* Declare kernel data members in kernel TU. */
Kernel::State Kernel::s_state = Kernel::State::Invalid;
KResourceLimit Kernel::s_system_resource_limit;
KMemoryManager Kernel::s_memory_manager;
KPageTableManager Kernel::s_page_table_manager;
KMemoryBlockSlabManager Kernel::s_app_memory_block_manager;
KMemoryBlockSlabManager Kernel::s_sys_memory_block_manager;
KBlockInfoManager Kernel::s_block_info_manager;
KSupervisorPageTable Kernel::s_supervisor_page_table;
KSynchronization Kernel::s_synchronization;
KUnsafeMemory Kernel::s_unsafe_memory;
KWorkerTaskManager Kernel::s_worker_task_managers[KWorkerTaskManager::WorkerType_Count];
constinit Kernel::State Kernel::s_state = Kernel::State::Invalid;
constinit KResourceLimit Kernel::s_system_resource_limit;
KMemoryManager Kernel::s_memory_manager;
constinit KPageTableManager Kernel::s_page_table_manager;
constinit KMemoryBlockSlabManager Kernel::s_app_memory_block_manager;
constinit KMemoryBlockSlabManager Kernel::s_sys_memory_block_manager;
constinit KBlockInfoManager Kernel::s_block_info_manager;
constinit KSupervisorPageTable Kernel::s_supervisor_page_table;
constinit KSynchronization Kernel::s_synchronization;
constinit KUnsafeMemory Kernel::s_unsafe_memory;
constinit KWorkerTaskManager Kernel::s_worker_task_managers[KWorkerTaskManager::WorkerType_Count];
namespace {
/* TODO: C++20 constinit */ std::array<KThread, cpu::NumCores> g_main_threads;
/* TODO: C++20 constinit */ std::array<KThread, cpu::NumCores> g_idle_threads;
constinit std::array<KThread, cpu::NumCores> g_main_threads;
constinit std::array<KThread, cpu::NumCores> g_idle_threads;
}
KThread &Kernel::GetMainThread(s32 core_id) { return g_main_threads[core_id]; }

View file

@ -19,21 +19,25 @@
namespace ams::mitm {
/* TODO: C++20 Concepts will make this a lot less stupid. */
class ModuleBase {};
template<typename T>
concept IsModule = requires(T, void *arg) {
{ T::ThreadPriority } -> std::convertible_to<s32>;
{ T::StackSize } -> std::convertible_to<size_t>;
{ T::Stack } -> std::convertible_to<void *>;
{ T::ThreadFunction(arg) } -> std::same_as<void>;
};
#define DEFINE_MITM_MODULE_CLASS(ss, prio) class MitmModule : public ::ams::mitm::ModuleBase { \
public: \
static constexpr s32 ThreadPriority = prio; \
static constexpr size_t StackSize = ss; \
alignas(os::ThreadStackAlignment) static inline u8 Stack[StackSize]; \
public: \
static void ThreadFunction(void *); \
#define DEFINE_MITM_MODULE_CLASS(ss, prio) class MitmModule { \
public: \
static constexpr s32 ThreadPriority = prio; \
static constexpr size_t StackSize = ss; \
alignas(os::ThreadStackAlignment) static inline u8 Stack[StackSize]; \
public: \
static void ThreadFunction(void *); \
}
template<class M>
template<class M> requires IsModule<M>
struct ModuleTraits {
static_assert(std::is_base_of<ModuleBase, M>::value, "Mitm Modules must inherit from ams::mitm::Module");
static constexpr void *Stack = &M::Stack[0];
static constexpr size_t StackSize = M::StackSize;

View file

@ -54,7 +54,7 @@ namespace ams::mitm {
.main = Traits::ThreadFunction,
.stack_mem = Traits::Stack,
.priority = Traits::ThreadPriority,
.stack_size = Traits::StackSize,
.stack_size = static_cast<u32>(Traits::StackSize),
};
}

View file

@ -64,7 +64,7 @@ namespace ams::pgl {
constexpr sm::ServiceName ShellServiceName = sm::ServiceName::Encode("pgl");
constexpr size_t ShellMaxSessions = 8; /* Official maximum is 8. */
/* TODO: C++20 constinit */ pgl::srv::ShellInterface g_shell_interface;
constinit pgl::srv::ShellInterface g_shell_interface;
ALWAYS_INLINE std::shared_ptr<pgl::srv::ShellInterface> GetSharedPointerToShellInterface() {
return ams::sf::ServiceObjectTraits<pgl::srv::ShellInterface>::SharedPointerHelper::GetEmptyDeleteSharedPointer(std::addressof(g_shell_interface));