1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:17:34 +00:00

AK: Cast lhs of left shifts to FixedPoint's underlying integer type

By default, `1` is of the type `int` which is 32-bits wide at max.
Because of that, if `precision` of a `FixedPoint` is greater than 32,
the expression `1 << precision` will get clamped at 32-bits and the
result will always be zero. Casting `1` to the wider underlying type
will make the expression not overflow.
This commit is contained in:
ronak69 2023-08-12 13:39:06 +00:00 committed by Andrew Kaster
parent 4c6ea4a963
commit d40807681d

View file

@ -109,7 +109,7 @@ public:
// add 1 when we see a fract values behind the `.5`s place set, // add 1 when we see a fract values behind the `.5`s place set,
// because that means they are smaller than .5 // because that means they are smaller than .5
// fract(m_value) >= .5? // fract(m_value) >= .5?
if (m_value & (1u << (precision - 1))) { if (m_value & (static_cast<Underlying>(1) << (precision - 1))) {
// fract(m_value) > .5? // fract(m_value) > .5?
if (m_value & (radix_mask >> 1)) { if (m_value & (radix_mask >> 1)) {
// yes: round "up"; // yes: round "up";
@ -128,13 +128,13 @@ public:
constexpr This ceil() const constexpr This ceil() const
{ {
return create_raw((m_value & ~radix_mask) return create_raw((m_value & ~radix_mask)
+ (m_value & radix_mask ? 1 << precision : 0)); + (m_value & radix_mask ? static_cast<Underlying>(1) << precision : 0));
} }
constexpr This trunc() const constexpr This trunc() const
{ {
return create_raw((m_value & ~radix_mask) return create_raw((m_value & ~radix_mask)
+ ((m_value & radix_mask) + ((m_value & radix_mask)
? (m_value > 0 ? 0 : (1 << precision)) ? (m_value > 0 ? 0 : (static_cast<Underlying>(1) << precision))
: 0)); : 0));
} }
@ -157,7 +157,7 @@ public:
constexpr This log2() const constexpr This log2() const
{ {
// 0.5 // 0.5
This b = create_raw(1 << (precision - 1)); This b = create_raw(static_cast<Underlying>(1) << (precision - 1));
This y = 0; This y = 0;
This x = *this; This x = *this;
@ -217,7 +217,7 @@ public:
This ret = create_raw(value >> precision); This ret = create_raw(value >> precision);
// Rounding: // Rounding:
// If last bit cut off is 1: // If last bit cut off is 1:
if (value & (1u << (precision - 1))) { if (value & (static_cast<Underlying>(1) << (precision - 1))) {
// If the bit after is 1 as well // If the bit after is 1 as well
if (value & (radix_mask >> 1)) { if (value & (radix_mask >> 1)) {
// We round away from 0 // We round away from 0
@ -245,12 +245,12 @@ public:
template<Integral I> template<Integral I>
constexpr This operator+(I other) const constexpr This operator+(I other) const
{ {
return create_raw(m_value + (other << precision)); return create_raw(m_value + (static_cast<Underlying>(other) << precision));
} }
template<Integral I> template<Integral I>
constexpr This operator-(I other) const constexpr This operator-(I other) const
{ {
return create_raw(m_value - (other << precision)); return create_raw(m_value - (static_cast<Underlying>(other) << precision));
} }
template<Integral I> template<Integral I>
constexpr This operator*(I other) const constexpr This operator*(I other) const
@ -297,13 +297,13 @@ public:
template<Integral I> template<Integral I>
This& operator+=(I other) This& operator+=(I other)
{ {
m_value += other << precision; m_value += static_cast<Underlying>(other) << precision;
return *this; return *this;
} }
template<Integral I> template<Integral I>
This& operator-=(I other) This& operator-=(I other)
{ {
m_value -= other << precision; m_value -= static_cast<Underlying>(other) << precision;
return *this; return *this;
} }
template<Integral I> template<Integral I>
@ -362,7 +362,7 @@ public:
template<Integral I> template<Integral I>
bool operator<(I other) const bool operator<(I other) const
{ {
return (m_value >> precision) < other || m_value < (other << precision); return (m_value >> precision) < other || m_value < (static_cast<Underlying>(other) << precision);
} }
template<Integral I> template<Integral I>
bool operator<=(I other) const bool operator<=(I other) const