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

@@ -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};