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

Inline always #30

Merged
merged 2 commits into from
Oct 25, 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
15 changes: 15 additions & 0 deletions src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,89 +170,104 @@ impl I256 {
}

/// Cast to a primitive `i8`.
#[inline]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why these ones weren't also marked as #[inline(always)]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. You can change it if you think that's best.

pub const fn as_i8(self) -> i8 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `i16`.
#[inline]
pub const fn as_i16(self) -> i16 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `i32`.
#[inline]
pub const fn as_i32(self) -> i32 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `i64`.
#[inline]
pub const fn as_i64(self) -> i64 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `i128`.
#[inline]
pub const fn as_i128(self) -> i128 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `u8`.
#[inline]
pub const fn as_u8(self) -> u8 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `u16`.
#[inline]
pub const fn as_u16(self) -> u16 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `u32`.
#[inline]
pub const fn as_u32(self) -> u32 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `u64`.
#[inline]
pub const fn as_u64(self) -> u64 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `u128`.
#[inline]
pub const fn as_u128(self) -> u128 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a `U256`.
#[inline]
pub const fn as_u256(self) -> U256 {
let Self([a, b]) = self;
U256([a as _, b as _])
}

/// Cast to a primitive `isize`.
#[inline]
pub const fn as_isize(self) -> isize {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `usize`.
#[inline]
pub const fn as_usize(self) -> usize {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `f32`.
#[inline]
pub fn as_f32(self) -> f32 {
self.as_f64() as _
}

/// Cast to a primitive `f64`.
#[inline]
pub fn as_f64(self) -> f64 {
let sign = self.signum128() as f64;
self.unsigned_abs().as_f64() * sign
Expand Down
40 changes: 20 additions & 20 deletions src/int/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl I256 {
///
/// assert_eq!(n.leading_zeros(), 0);
/// ```
#[inline]
#[inline(always)]
pub fn leading_zeros(self) -> u32 {
intrinsics::signed::ictlz(&self)
}
Expand All @@ -144,7 +144,7 @@ impl I256 {
///
/// assert_eq!(n.trailing_zeros(), 2);
/// ```
#[inline]
#[inline(always)]
pub fn trailing_zeros(self) -> u32 {
intrinsics::signed::icttz(&self)
}
Expand Down Expand Up @@ -206,7 +206,7 @@ impl I256 {
/// ```
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[inline(always)]
pub fn rotate_left(self, n: u32) -> Self {
let mut r = MaybeUninit::uninit();
intrinsics::signed::irol3(&mut r, &self, n);
Expand Down Expand Up @@ -235,7 +235,7 @@ impl I256 {
/// ```
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[inline(always)]
pub fn rotate_right(self, n: u32) -> Self {
let mut r = MaybeUninit::uninit();
intrinsics::signed::iror3(&mut r, &self, n);
Expand Down Expand Up @@ -317,7 +317,7 @@ impl I256 {
/// assert_eq!(I256::from_be(n), n.swap_bytes())
/// }
/// ```
#[inline]
#[inline(always)]
pub const fn from_be(x: Self) -> Self {
#[cfg(target_endian = "big")]
{
Expand Down Expand Up @@ -347,7 +347,7 @@ impl I256 {
/// assert_eq!(I256::from_le(n), n.swap_bytes())
/// }
/// ```
#[inline]
#[inline(always)]
pub const fn from_le(x: Self) -> Self {
#[cfg(target_endian = "little")]
{
Expand Down Expand Up @@ -377,7 +377,7 @@ impl I256 {
/// assert_eq!(n.to_be(), n.swap_bytes())
/// }
/// ```
#[inline]
#[inline(always)]
pub const fn to_be(self) -> Self {
// or not to be?
#[cfg(target_endian = "big")]
Expand Down Expand Up @@ -408,7 +408,7 @@ impl I256 {
/// assert_eq!(n.to_le(), n.swap_bytes())
/// }
/// ```
#[inline]
#[inline(always)]
pub const fn to_le(self) -> Self {
#[cfg(target_endian = "little")]
{
Expand Down Expand Up @@ -792,7 +792,7 @@ impl I256 {
/// assert_eq!(I256::MAX.saturating_neg(), I256::MIN + 1);
/// ```

#[inline]
#[inline(always)]
pub fn saturating_neg(self) -> Self {
I256::ZERO.saturating_sub(self)
}
Expand Down Expand Up @@ -916,7 +916,7 @@ impl I256 {
/// ```
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[inline(always)]
pub fn wrapping_add(self, rhs: Self) -> Self {
let mut result = MaybeUninit::uninit();
intrinsics::signed::iadd3(&mut result, &self, &rhs);
Expand All @@ -937,7 +937,7 @@ impl I256 {
/// ```
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[inline(always)]
pub fn wrapping_sub(self, rhs: Self) -> Self {
let mut result = MaybeUninit::uninit();
intrinsics::signed::isub3(&mut result, &self, &rhs);
Expand All @@ -958,7 +958,7 @@ impl I256 {
/// ```
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[inline(always)]
pub fn wrapping_mul(self, rhs: Self) -> Self {
let mut result = MaybeUninit::uninit();
intrinsics::signed::imul3(&mut result, &self, &rhs);
Expand Down Expand Up @@ -1094,7 +1094,7 @@ impl I256 {
/// assert_eq!(I256::new(100).wrapping_neg(), -100);
/// assert_eq!(I256::MIN.wrapping_neg(), I256::MIN);
/// ```
#[inline]
#[inline(always)]
pub fn wrapping_neg(self) -> Self {
Self::ZERO.wrapping_sub(self)
}
Expand All @@ -1121,7 +1121,7 @@ impl I256 {
/// ```
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[inline(always)]
pub fn wrapping_shl(self, rhs: u32) -> Self {
let mut result = MaybeUninit::uninit();
intrinsics::signed::ishl3(&mut result, &self, rhs & 0xff);
Expand Down Expand Up @@ -1150,7 +1150,7 @@ impl I256 {
/// ```
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[inline(always)]
pub fn wrapping_shr(self, rhs: u32) -> Self {
let mut result = MaybeUninit::uninit();
intrinsics::signed::isar3(&mut result, &self, rhs & 0xff);
Expand Down Expand Up @@ -1212,7 +1212,7 @@ impl I256 {
/// ),
/// );
/// ```
#[inline]
#[inline(always)]
pub fn unsigned_abs(self) -> U256 {
self.wrapping_abs().as_u256()
}
Expand Down Expand Up @@ -1272,7 +1272,7 @@ impl I256 {
/// ```
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[inline(always)]
pub fn overflowing_add(self, rhs: Self) -> (Self, bool) {
let mut result = MaybeUninit::uninit();
let overflow = intrinsics::signed::iaddc(&mut result, &self, &rhs);
Expand All @@ -1296,7 +1296,7 @@ impl I256 {
/// ```
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[inline(always)]
pub fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
let mut result = MaybeUninit::uninit();
let overflow = intrinsics::signed::isubc(&mut result, &self, &rhs);
Expand Down Expand Up @@ -1362,7 +1362,7 @@ impl I256 {
/// ```
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[inline(always)]
pub fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
let mut result = MaybeUninit::uninit();
let overflow = intrinsics::signed::imulc(&mut result, &self, &rhs);
Expand Down Expand Up @@ -1807,7 +1807,7 @@ impl I256 {
/// assert_eq!(I256::new(0).signum(), 0);
/// assert_eq!(I256::new(-10).signum(), -1);
/// ```
#[inline]
#[inline(always)]
pub const fn signum(self) -> Self {
I256::new(self.signum128())
}
Expand Down
15 changes: 15 additions & 0 deletions src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,84 +167,98 @@ impl U256 {
}

/// Cast to a primitive `i8`.
#[inline]
pub const fn as_i8(self) -> i8 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `i16`.
#[inline]
pub const fn as_i16(self) -> i16 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `i32`.
#[inline]
pub const fn as_i32(self) -> i32 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `i64`.
#[inline]
pub const fn as_i64(self) -> i64 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `i128`.
#[inline]
pub const fn as_i128(self) -> i128 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a `I256`.
#[inline]
pub const fn as_i256(self) -> I256 {
let Self([a, b]) = self;
I256([a as _, b as _])
}

/// Cast to a primitive `u8`.
#[inline]
pub const fn as_u8(self) -> u8 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `u16`.
#[inline]
pub const fn as_u16(self) -> u16 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `u32`.
#[inline]
pub const fn as_u32(self) -> u32 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `u64`.
#[inline]
pub const fn as_u64(self) -> u64 {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `u128`.
#[inline]
pub const fn as_u128(self) -> u128 {
let (_, lo) = self.into_words();
lo
}

/// Cast to a primitive `isize`.
#[inline]
pub const fn as_isize(self) -> isize {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `usize`.
#[inline]
pub const fn as_usize(self) -> usize {
let (_, lo) = self.into_words();
lo as _
}

/// Cast to a primitive `f32`.
#[inline]
pub fn as_f32(self) -> f32 {
match self.into_words() {
(0, lo) => lo as _,
Expand All @@ -253,6 +267,7 @@ impl U256 {
}

/// Cast to a primitive `f64`.
#[inline]
pub fn as_f64(self) -> f64 {
// NOTE: Binary representation of 2**128. This is used because `powi` is
// neither `const` nor `no_std`.
Expand Down
Loading