sat.rs - source
Description: Source of the Rust file `library/core/src/../../stdarch/crates/core_arch/src/arm/sat.rs`.

#
core/stdarch/crates/core_arch/src/arm/
sat.rs

    1//! # References:
    2//!
    3//! - Section 8.4 "Saturating intrinsics"
    4
    5#[cfg(test)]
    6use stdarch_test::assert_instr;
    7
    8/// Saturates a 32-bit signed integer to a signed integer with a given
    9/// bit width.
    10#[unstable(feature = "stdarch_arm_sat", issue = "none")]
    11#[inline]
    12#[cfg_attr(test, assert_instr("ssat", WIDTH = 8))]
    13#[rustc_legacy_const_generics(1)]
    14pub unsafe fn __ssat<const WIDTH: u32>(x: i32) -> i32 {
    15    static_assert!(matches!(WIDTH, 1..=32));
    16    arm_ssat(x, WIDTH as i32)
    17}
    18
    19/// Saturates a 32-bit signed integer to an unsigned integer with a given
    20/// bit width.
    21#[unstable(feature = "stdarch_arm_sat", issue = "none")]
    22#[inline]
    23#[cfg_attr(test, assert_instr("usat", WIDTH = 8))]
    24#[rustc_legacy_const_generics(1)]
    25pub unsafe fn __usat<const WIDTH: u32>(x: i32) -> u32 {
    26    static_assert!(matches!(WIDTH, 1..=32));
    27    arm_usat(x, WIDTH as i32)
    28}
    29
    30unsafe extern "unadjusted" {
    31    #[link_name = "llvm.arm.ssat"]
    32    fn arm_ssat(x: i32, y: i32) -> i32;
    33
    34    #[link_name = "llvm.arm.usat"]
    35    fn arm_usat(x: i32, y: i32) -> u32;
    36}
    37
    38#[cfg(test)]
    39mod tests {
    40    use super::*;
    41    use stdarch_test::simd_test;
    42
    43    #[test]
    44    fn test_ssat() {
    45        unsafe {
    46            assert_eq!(__ssat::<8>(1), 1);
    47            assert_eq!(__ssat::<8>(1000), 127);
    48            assert_eq!(__ssat::<8>(-1), -1);
    49            assert_eq!(__ssat::<8>(-1000), -128);
    50        }
    51    }
    52
    53    #[test]
    54    fn test_usat() {
    55        unsafe {
    56            assert_eq!(__usat::<8>(1), 1);
    57            assert_eq!(__usat::<8>(1000), 255);
    58            assert_eq!(__usat::<8>(-1), 0);
    59            assert_eq!(__usat::<8>(-1000), 0);
    60        }
    61    }
    62}