rust/crates/core_simd/src/round.rs

79 lines
2.9 KiB
Rust
Raw Normal View History

use crate::simd::intrinsics;
use crate::simd::{LaneCount, Simd, SupportedLaneCount};
2021-08-07 14:28:27 -05:00
2020-10-09 23:16:58 -05:00
macro_rules! implement {
{
2021-08-07 14:28:27 -05:00
$type:ty, $int_type:ty
2020-10-09 23:16:58 -05:00
} => {
#[cfg(feature = "std")]
2021-08-07 14:28:27 -05:00
impl<const LANES: usize> Simd<$type, LANES>
2021-02-12 23:49:51 -06:00
where
2021-08-07 14:28:27 -05:00
LaneCount<LANES>: SupportedLaneCount,
2021-02-12 23:49:51 -06:00
{
2021-04-25 17:03:17 -05:00
/// Returns the smallest integer greater than or equal to each lane.
#[must_use = "method returns a new vector and does not mutate the original value"]
#[inline]
pub fn ceil(self) -> Self {
unsafe { intrinsics::simd_ceil(self) }
2021-04-25 17:03:17 -05:00
}
/// Returns the largest integer value less than or equal to each lane.
2020-12-13 23:07:36 -06:00
#[must_use = "method returns a new vector and does not mutate the original value"]
#[inline]
pub fn floor(self) -> Self {
unsafe { intrinsics::simd_floor(self) }
2020-12-13 23:07:36 -06:00
}
2020-10-09 23:16:58 -05:00
2021-04-25 17:03:17 -05:00
/// Rounds to the nearest integer value. Ties round toward zero.
2020-12-13 23:07:36 -06:00
#[must_use = "method returns a new vector and does not mutate the original value"]
#[inline]
2021-04-25 17:03:17 -05:00
pub fn round(self) -> Self {
unsafe { intrinsics::simd_round(self) }
2021-04-25 17:03:17 -05:00
}
/// Returns the floating point's integer value, with its fractional part removed.
#[must_use = "method returns a new vector and does not mutate the original value"]
#[inline]
pub fn trunc(self) -> Self {
unsafe { intrinsics::simd_trunc(self) }
2021-04-25 17:03:17 -05:00
}
/// Returns the floating point's fractional value, with its integer part removed.
#[must_use = "method returns a new vector and does not mutate the original value"]
#[inline]
pub fn fract(self) -> Self {
self - self.trunc()
2020-12-13 23:07:36 -06:00
}
}
2020-10-11 12:22:26 -05:00
2021-08-07 14:28:27 -05:00
impl<const LANES: usize> Simd<$type, LANES>
where
2021-08-07 14:28:27 -05:00
LaneCount<LANES>: SupportedLaneCount,
{
2020-12-13 23:07:36 -06:00
/// Rounds toward zero and converts to the same-width integer type, assuming that
/// the value is finite and fits in that type.
///
/// # Safety
/// The value must:
///
/// * Not be NaN
/// * Not be infinite
/// * Be representable in the return type, after truncating off its fractional part
#[inline]
2021-08-07 14:28:27 -05:00
pub unsafe fn to_int_unchecked(self) -> Simd<$int_type, LANES> {
unsafe { intrinsics::simd_cast(self) }
2020-12-13 23:07:36 -06:00
}
2020-10-11 13:32:46 -05:00
2020-12-13 23:07:36 -06:00
/// Creates a floating-point vector from an integer vector. Rounds values that are
/// not exactly representable.
#[inline]
2021-08-07 14:28:27 -05:00
pub fn round_from_int(value: Simd<$int_type, LANES>) -> Self {
unsafe { intrinsics::simd_cast(value) }
2020-10-09 23:16:58 -05:00
}
}
}
}
2021-08-07 14:28:27 -05:00
implement! { f32, i32 }
implement! { f64, i64 }