mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:57:45 +00:00
AK: Add FixedPoint cast operator for up/downcasting to other sizes
This enables casting between different size FixedPoint variables or constructing them from other sized FixedPoint values.
This commit is contained in:
parent
06fc72ca0c
commit
c468a9cc2d
2 changed files with 61 additions and 0 deletions
|
@ -20,6 +20,9 @@ class FixedPoint {
|
|||
using This = FixedPoint<precision, Underlying>;
|
||||
constexpr static Underlying radix_mask = (static_cast<Underlying>(1) << precision) - 1;
|
||||
|
||||
template<size_t P, typename U>
|
||||
friend class FixedPoint;
|
||||
|
||||
public:
|
||||
constexpr FixedPoint() = default;
|
||||
template<Integral I>
|
||||
|
@ -34,6 +37,12 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
template<size_t P, typename U>
|
||||
explicit constexpr FixedPoint(FixedPoint<P, U> const& other)
|
||||
: m_value(other.template cast_to<precision, Underlying>().m_value)
|
||||
{
|
||||
}
|
||||
|
||||
template<FloatingPoint F>
|
||||
explicit ALWAYS_INLINE operator F() const
|
||||
{
|
||||
|
@ -304,7 +313,27 @@ public:
|
|||
template<FloatingPoint F>
|
||||
bool operator<=(F other) const { return *this <= (This)other; }
|
||||
|
||||
template<size_t P, typename U>
|
||||
operator FixedPoint<P, U>() const
|
||||
{
|
||||
return cast_to<P, U>();
|
||||
}
|
||||
|
||||
private:
|
||||
template<size_t P, typename U>
|
||||
constexpr FixedPoint<P, U> cast_to() const
|
||||
{
|
||||
U raw_value = static_cast<U>(m_value >> precision) << P;
|
||||
if constexpr (precision > P)
|
||||
raw_value |= (m_value & radix_mask) >> (precision - P);
|
||||
else if constexpr (precision < P)
|
||||
raw_value |= static_cast<U>(m_value & radix_mask) << (P - precision);
|
||||
else
|
||||
raw_value |= m_value & radix_mask;
|
||||
|
||||
return FixedPoint<P, U>::create_raw(raw_value);
|
||||
}
|
||||
|
||||
static This create_raw(Underlying value)
|
||||
{
|
||||
This t {};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue