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

Add most basic runtime conversion checkers #208

Merged
merged 8 commits into from
Dec 11, 2023
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
112 changes: 107 additions & 5 deletions au/apply_magnitude.hh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,56 @@ constexpr ApplyAs categorize_magnitude(Magnitude<BPs...>) {
template <typename Mag, ApplyAs Category, typename T, bool is_T_integral>
struct ApplyMagnitudeImpl;

template <typename T, bool IsMagnitudeValid>
struct OverflowChecker {
// Default case: `IsMagnitudeValid` is true.
static constexpr bool would_product_overflow(T x, T mag_value) {
return (x > (std::numeric_limits<T>::max() / mag_value)) ||
(x < (std::numeric_limits<T>::lowest() / mag_value));
}
};

template <typename T>
struct OverflowChecker<T, false> {
// Specialization for when `IsMagnitudeValid` is false.
//
// This means that the magnitude itself could not fit inside of the type; therefore, the only
// possible value that would not overflow is zero.
static constexpr bool would_product_overflow(T x, T) { return (x != T{0}); }
};

template <typename T, bool IsTIntegral>
struct TruncationCheckerIfMagnitudeValid {
// Default case: T is integral.
static_assert(std::is_integral<T>::value && IsTIntegral,
"Mismatched instantiation (should never be done manually)");

static constexpr bool would_truncate(T x, T mag_value) { return (x % mag_value != T{0}); }
};

template <typename T>
struct TruncationCheckerIfMagnitudeValid<T, false> {
// Specialization for when T is not integral: by convention, assume no truncation for floats.
static_assert(!std::is_integral<T>::value,
"Mismatched instantiation (should never be done manually)");
static constexpr bool would_truncate(T, T) { return false; }
};

template <typename T, bool IsMagnitudeValid>
// Default case: `IsMagnitudeValid` is true.
struct TruncationChecker : TruncationCheckerIfMagnitudeValid<T, std::is_integral<T>::value> {
static_assert(IsMagnitudeValid, "Mismatched instantiation (should never be done manually)");
};

template <typename T>
struct TruncationChecker<T, false> {
// Specialization for when `IsMagnitudeValid` is false.
//
// This means that the magnitude itself could not fit inside of the type; therefore, the only
// possible value that would not truncate is zero.
static constexpr bool would_truncate(T x, T) { return (x != T{0}); }
};

// Multiplying by an integer, for any type T.
template <typename Mag, typename T, bool is_T_integral>
struct ApplyMagnitudeImpl<Mag, ApplyAs::INTEGER_MULTIPLY, T, is_T_integral> {
Expand All @@ -53,6 +103,14 @@ struct ApplyMagnitudeImpl<Mag, ApplyAs::INTEGER_MULTIPLY, T, is_T_integral> {
"Mismatched instantiation (should never be done manually)");

constexpr T operator()(const T &x) { return x * get_value<T>(Mag{}); }

static constexpr bool would_overflow(const T &x) {
constexpr auto mag_value_result = get_value_result<T>(Mag{});
return OverflowChecker<T, mag_value_result.outcome == MagRepresentationOutcome::OK>::
would_product_overflow(x, mag_value_result.value);
}

static constexpr bool would_truncate(const T &) { return false; }
};

// Dividing by an integer, for any type T.
Expand All @@ -64,6 +122,14 @@ struct ApplyMagnitudeImpl<Mag, ApplyAs::INTEGER_DIVIDE, T, is_T_integral> {
"Mismatched instantiation (should never be done manually)");

constexpr T operator()(const T &x) { return x / get_value<T>(MagInverseT<Mag>{}); }

static constexpr bool would_overflow(const T &) { return false; }

static constexpr bool would_truncate(const T &x) {
constexpr auto mag_value_result = get_value_result<T>(MagInverseT<Mag>{});
return TruncationChecker<T, mag_value_result.outcome == MagRepresentationOutcome::OK>::
would_truncate(x, mag_value_result.value);
}
};

// Applying a (non-integer, non-inverse-integer) rational, for any integral type T.
Expand All @@ -77,6 +143,18 @@ struct ApplyMagnitudeImpl<Mag, ApplyAs::RATIONAL_MULTIPLY, T, true> {
constexpr T operator()(const T &x) {
return x * get_value<T>(numerator(Mag{})) / get_value<T>(denominator(Mag{}));
}

static constexpr bool would_overflow(const T &x) {
constexpr auto mag_value_result = get_value_result<T>(numerator(Mag{}));
return OverflowChecker<T, mag_value_result.outcome == MagRepresentationOutcome::OK>::
would_product_overflow(x, mag_value_result.value);
}

static constexpr bool would_truncate(const T &x) {
constexpr auto mag_value_result = get_value_result<T>(denominator(Mag{}));
return TruncationChecker<T, mag_value_result.outcome == MagRepresentationOutcome::OK>::
would_truncate(x, mag_value_result.value);
}
};

// Applying a (non-integer, non-inverse-integer) rational, for any non-integral type T.
Expand All @@ -88,6 +166,14 @@ struct ApplyMagnitudeImpl<Mag, ApplyAs::RATIONAL_MULTIPLY, T, false> {
"Mismatched instantiation (should never be done manually)");

constexpr T operator()(const T &x) { return x * get_value<T>(Mag{}); }

static constexpr bool would_overflow(const T &x) {
constexpr auto mag_value_result = get_value_result<T>(Mag{});
return OverflowChecker<T, mag_value_result.outcome == MagRepresentationOutcome::OK>::
would_product_overflow(x, mag_value_result.value);
}

static constexpr bool would_truncate(const T &) { return false; }
};

// Applying an irrational for any type T (although only non-integral T makes sense).
Expand All @@ -101,14 +187,30 @@ struct ApplyMagnitudeImpl<Mag, ApplyAs::IRRATIONAL_MULTIPLY, T, is_T_integral> {
"Mismatched instantiation (should never be done manually)");

constexpr T operator()(const T &x) { return x * get_value<T>(Mag{}); }

static constexpr bool would_overflow(const T &x) {
constexpr auto mag_value_result = get_value_result<T>(Mag{});
return OverflowChecker<T, mag_value_result.outcome == MagRepresentationOutcome::OK>::
would_product_overflow(x, mag_value_result.value);
}

static constexpr bool would_truncate(const T &) { return false; }
};

template <typename T, typename MagT>
struct ApplyMagnitudeType;
template <typename T, typename MagT>
using ApplyMagnitudeT = typename ApplyMagnitudeType<T, MagT>::type;
template <typename T, typename... BPs>
struct ApplyMagnitudeType<T, Magnitude<BPs...>>
: stdx::type_identity<ApplyMagnitudeImpl<Magnitude<BPs...>,
categorize_magnitude(Magnitude<BPs...>{}),
T,
std::is_integral<T>::value>> {};

template <typename T, typename... BPs>
constexpr T apply_magnitude(const T &x, Magnitude<BPs...> m) {
return ApplyMagnitudeImpl<Magnitude<BPs...>,
categorize_magnitude(m),
T,
std::is_integral<T>::value>{}(x);
constexpr T apply_magnitude(const T &x, Magnitude<BPs...>) {
return ApplyMagnitudeT<T, Magnitude<BPs...>>{}(x);
}

} // namespace detail
Expand Down
Loading
Loading