Skip to content

Commit

Permalink
Avoid using wrapping_shl/shr from unsigned_integer.h
Browse files Browse the repository at this point in the history
The implementation is in unsigned_integer_impl.h and library code
can't always include both due to cycles.
  • Loading branch information
danakj committed Nov 29, 2023
1 parent b4f9b5f commit 0b91993
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
16 changes: 16 additions & 0 deletions sus/num/__private/intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,10 @@ sus_pure_const inline constexpr OverflowOut<T> pow_with_overflow(
.overflow = overflow || r.overflow, .value = r.value};
}

/// Shift left operation, wrapping the rhs if it's as large or larger than
/// num_bits<T>.
///
/// Returns the wrapped result along with whether the rhs overflowed.
template <class T>
requires(std::is_integral_v<T> && !std::is_signed_v<T> &&
(::sus::mem::size_of<T>() == 1 || ::sus::mem::size_of<T>() == 2 ||
Expand All @@ -1075,6 +1079,10 @@ sus_pure_const inline constexpr OverflowOut<T> shl_with_overflow(
.value = unchecked_shl(x, shift)};
}

/// Shift left operation, wrapping the rhs if it's as large or larger than
/// num_bits<T>.
///
/// Returns the wrapped result along with whether the rhs overflowed.
template <class T>
requires(std::is_integral_v<T> && std::is_signed_v<T> &&
(::sus::mem::size_of<T>() == 1 || ::sus::mem::size_of<T>() == 2 ||
Expand All @@ -1091,6 +1099,10 @@ sus_pure_const inline constexpr OverflowOut<T> shl_with_overflow(
.value = unchecked_shl(x, shift)};
}

/// Shift right operation, wrapping the rhs if it's as large or larger than
/// num_bits<T>.
///
/// Returns the wrapped result along with whether the rhs overflowed.
template <class T>
requires(std::is_integral_v<T> && !std::is_signed_v<T> &&
(::sus::mem::size_of<T>() == 1 || ::sus::mem::size_of<T>() == 2 ||
Expand All @@ -1107,6 +1119,10 @@ sus_pure_const inline constexpr OverflowOut<T> shr_with_overflow(
.value = unchecked_shr(x, shift)};
}

/// Shift right operation, wrapping the rhs if it's as large or larger than
/// num_bits<T>.
///
/// Returns the wrapped result along with whether the rhs overflowed.
template <class T>
requires(std::is_integral_v<T> && std::is_signed_v<T> &&
(::sus::mem::size_of<T>() == 1 || ::sus::mem::size_of<T>() == 2 ||
Expand Down
48 changes: 36 additions & 12 deletions sus/num/unsigned_integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ constexpr inline P operator>>(P l, U r) noexcept = delete;
return u8(
__private::unchecked_shl(l.primitive_value, u64(r).primitive_value));
#else
return l.wrapping_shl(u64(r));
return u8(
__private::shl_with_overflow(l.primitive_value, u64(r).primitive_value)
.value);
#endif
}
/// #[doc.overloads=unsignedint.<<]
Expand All @@ -295,7 +297,9 @@ constexpr inline u8 operator<<(u8 l, U r) noexcept = delete;
return u8(
__private::unchecked_shr(l.primitive_value, u64(r).primitive_value));
#else
return l.wrapping_shr(u64(r));
return u8(
__private::shr_with_overflow(l.primitive_value, u64(r).primitive_value)
.value);
#endif
}
/// #[doc.overloads=unsignedint.>>]
Expand All @@ -310,7 +314,9 @@ constexpr inline u8 operator>>(u8 l, U r) noexcept = delete;
return u16(
__private::unchecked_shl(l.primitive_value, u64(r).primitive_value));
#else
return l.wrapping_shl(u64(r));
return u16(
__private::shl_with_overflow(l.primitive_value, u64(r).primitive_value)
.value);
#endif
}
/// #[doc.overloads=unsignedint.<<]
Expand All @@ -325,7 +331,9 @@ constexpr inline u16 operator<<(u16 l, U r) noexcept = delete;
return u16(
__private::unchecked_shr(l.primitive_value, u64(r).primitive_value));
#else
return l.wrapping_shr(u64(r));
return u16(
__private::shr_with_overflow(l.primitive_value, u64(r).primitive_value)
.value);
#endif
}
/// #[doc.overloads=unsignedint.>>]
Expand All @@ -340,7 +348,9 @@ constexpr inline u16 operator>>(u16 l, U r) noexcept = delete;
return u32(
__private::unchecked_shl(l.primitive_value, u64(r).primitive_value));
#else
return l.wrapping_shl(u64(r));
return u32(
__private::shl_with_overflow(l.primitive_value, u64(r).primitive_value)
.value);
#endif
}
/// #[doc.overloads=unsignedint.<<]
Expand All @@ -355,7 +365,9 @@ constexpr inline u32 operator<<(u32 l, U r) noexcept = delete;
return u32(
__private::unchecked_shr(l.primitive_value, u64(r).primitive_value));
#else
return l.wrapping_shr(u64(r));
return u32(
__private::shr_with_overflow(l.primitive_value, u64(r).primitive_value)
.value);
#endif
}
/// #[doc.overloads=unsignedint.>>]
Expand All @@ -370,7 +382,9 @@ constexpr inline u32 operator>>(u32 l, U r) noexcept = delete;
return u64(
__private::unchecked_shl(l.primitive_value, u64(r).primitive_value));
#else
return l.wrapping_shl(u64(r));
return u64(
__private::shl_with_overflow(l.primitive_value, u64(r).primitive_value)
.value);
#endif
}
/// #[doc.overloads=unsignedint.<<]
Expand All @@ -385,7 +399,9 @@ constexpr inline u64 operator<<(u64 l, U r) noexcept = delete;
return u64(
__private::unchecked_shr(l.primitive_value, u64(r).primitive_value));
#else
return l.wrapping_shr(u64(r));
return u64(
__private::shr_with_overflow(l.primitive_value, u64(r).primitive_value)
.value);
#endif
}
/// #[doc.overloads=unsignedint.>>]
Expand All @@ -400,7 +416,9 @@ constexpr inline u64 operator>>(u64 l, U r) noexcept = delete;
return usize(
__private::unchecked_shl(l.primitive_value, u64(r).primitive_value));
#else
return l.wrapping_shl(u64(r));
return usize(
__private::shl_with_overflow(l.primitive_value, u64(r).primitive_value)
.value);
#endif
}
/// #[doc.overloads=unsignedint.<<]
Expand All @@ -415,7 +433,9 @@ constexpr inline usize operator<<(usize l, U r) noexcept = delete;
return usize(
__private::unchecked_shr(l.primitive_value, u64(r).primitive_value));
#else
return l.wrapping_shr(u64(r));
return usize(
__private::shr_with_overflow(l.primitive_value, u64(r).primitive_value)
.value);
#endif
}
/// #[doc.overloads=unsignedint.>>]
Expand All @@ -430,7 +450,9 @@ constexpr inline usize operator>>(usize l, U r) noexcept = delete;
return uptr(
__private::unchecked_shl(l.primitive_value, u64(r).primitive_value));
#else
return l.wrapping_shl(u64(r));
return usize(
__private::shl_with_overflow(l.primitive_value, u64(r).primitive_value)
.value);
#endif
}
/// #[doc.overloads=unsignedint.<<]
Expand All @@ -445,7 +467,9 @@ constexpr inline uptr operator<<(uptr l, U r) noexcept = delete;
return uptr(
__private::unchecked_shr(l.primitive_value, u64(r).primitive_value));
#else
return l.wrapping_shr(u64(r));
return uptr(
__private::shr_with_overflow(l.primitive_value, u64(r).primitive_value)
.value);
#endif
}
/// #[doc.overloads=unsignedint.>>]
Expand Down

0 comments on commit 0b91993

Please sign in to comment.