Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better concepts for psyqo's Fragment and fixed points. #1742

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/mips/psyqo/fixed-point.hh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void printInt(uint32_t value, const eastl::function<void(char)>&, unsigned preci
* @tparam T The underlying integer type to use.
*/
template <unsigned precisionBits = 12, std::integral T = int32_t>
requires((precisionBits > 0) && (precisionBits < 32) && ((sizeof(T) == 4) || (sizeof(T) == 2)))
class FixedPoint {
using signedUpType = std::conditional<sizeof(T) == 4, int64_t, int32_t>::type;
using unsignedUpType = std::conditional<sizeof(T) == 4, uint64_t, uint32_t>::type;
Expand Down Expand Up @@ -89,8 +90,7 @@ class FixedPoint {
* fraction.
*/
explicit constexpr FixedPoint(T integer, T fraction) : value(integer * scale + fraction) {
static_assert(sizeof(T) == 4 || sizeof(T) == 2);
static_assert(precisionBits > 0);
static_assert(sizeof(FixedPoint) == sizeof(T));
}

/**
Expand Down
21 changes: 15 additions & 6 deletions src/mips/psyqo/fragment-concept.hh
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@ SOFTWARE.

#include <concepts>
#include <cstddef>
#include <type_traits>

namespace psyqo {

template <typename T, class Enable = void>
struct has_explicit_copy_constructor : std::false_type {};

template <typename T>
struct has_explicit_copy_constructor<
T, typename std::enable_if<std::is_copy_constructible_v<T> && !std::is_convertible_v<const T&, T>>::type>
: std::true_type {};

/**
* @brief The Fragment concept.
* @details This concept can be used as a template type constraint
Expand All @@ -39,12 +48,12 @@ namespace psyqo {

template <typename Frag>
concept Fragment = requires(Frag frag) {
{(alignof(Frag) & 3) == 0};
{(sizeof(Frag) & 3) == 0};
{(sizeof(frag.head)) == 4};
{((offsetof(Frag, head)) & 3) == 0};
{ frag.getActualFragmentSize() }
->std::convertible_to<size_t>;
{ (alignof(Frag) & 3) == 0 };
{ (sizeof(Frag) & 3) == 0 };
{ (sizeof(frag.head)) == 4 };
{ ((offsetof(Frag, head)) & 3) == 0 };
{ has_explicit_copy_constructor<Frag>() } -> std::convertible_to<std::true_type>;
{ frag.getActualFragmentSize() } -> std::convertible_to<size_t>;
};

} // namespace psyqo
3 changes: 3 additions & 0 deletions src/mips/psyqo/fragments.hh
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct SimpleFragment {
SimpleFragment() {
static_assert(sizeof(*this) == (sizeof(uint32_t) + sizeof(Prim)), "Spurious padding in simple fragment");
}
explicit SimpleFragment(const SimpleFragment &) = default;
typedef Prim FragmentBaseType;
constexpr size_t getActualFragmentSize() const { return sizeof(Prim) / sizeof(uint32_t); }
uint32_t head;
Expand All @@ -92,6 +93,7 @@ struct FixedFragment {
static_assert(sizeof(*this) == (sizeof(unsigned) + sizeof(uint32_t) + sizeof(Prim) * N),
"Spurious padding in fixed fragment");
}
explicit FixedFragment(const FixedFragment &) = default;
typedef Prim FragmentBaseType;
size_t getActualFragmentSize() const { return (sizeof(Prim) * count) / sizeof(uint32_t); }
unsigned count = N;
Expand Down Expand Up @@ -120,6 +122,7 @@ struct FixedFragmentWithPrologue {
static_assert(sizeof(*this) == (sizeof(unsigned) + sizeof(uint32_t) + sizeof(P) + sizeof(Prim) * N),
"Spurious padding in fixed fragment");
}
explicit FixedFragmentWithPrologue(const FixedFragmentWithPrologue &) = default;
typedef Prim FragmentBaseType;
size_t getActualFragmentSize() const { return (sizeof(P) + sizeof(Prim) * count) / sizeof(uint32_t); }
unsigned count = N;
Expand Down
Loading