Skip to content
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
5 changes: 4 additions & 1 deletion crates/core_arch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
aarch64_unstable_target_feature,
bigint_helper_methods,
funnel_shifts,
avx10_target_feature
avx10_target_feature,
const_trait_impl,
const_cmp,
const_eval_select
)]
#![cfg_attr(test, feature(test, abi_vectorcall, stdarch_internal))]
#![deny(clippy::missing_inline_in_public_items)]
Expand Down
5 changes: 5 additions & 0 deletions crates/core_arch/src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#[macro_use]
mod macros;

#[cfg(test)]
mod test;
#[cfg(test)]
use test::assert_eq_const;

#[cfg(any(target_arch = "riscv32", target_arch = "riscv64", doc))]
mod riscv_shared;

Expand Down
24 changes: 15 additions & 9 deletions crates/core_arch/src/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
#![allow(non_camel_case_types)]

#[inline(always)]
pub(crate) unsafe fn simd_imax<T: Copy>(a: T, b: T) -> T {
#[rustc_const_unstable(feature = "stdarch_const_helpers", issue = "none")]
pub(crate) const unsafe fn simd_imax<T: Copy>(a: T, b: T) -> T {
let mask: T = crate::intrinsics::simd::simd_gt(a, b);
crate::intrinsics::simd::simd_select(mask, a, b)
}

#[inline(always)]
pub(crate) unsafe fn simd_imin<T: Copy>(a: T, b: T) -> T {
#[rustc_const_unstable(feature = "stdarch_const_helpers", issue = "none")]
pub(crate) const unsafe fn simd_imin<T: Copy>(a: T, b: T) -> T {
let mask: T = crate::intrinsics::simd::simd_lt(a, b);
crate::intrinsics::simd::simd_select(mask, a, b)
}
Expand All @@ -35,7 +37,8 @@ macro_rules! simd_ty {
}
// FIXME: Workaround rust@60637
#[inline(always)]
pub(crate) fn splat(value: $elem_type) -> Self {
#[rustc_const_unstable(feature = "stdarch_const_helpers", issue = "none")]
pub(crate) const fn splat(value: $elem_type) -> Self {
#[derive(Copy, Clone)]
#[repr(simd)]
struct JustOne([$elem_type; 1]);
Expand All @@ -50,12 +53,12 @@ macro_rules! simd_ty {
/// Use for testing only.
// FIXME: Workaround rust@60637
#[inline(always)]
pub(crate) fn extract(&self, index: usize) -> $elem_type {
pub(crate) const fn extract(&self, index: usize) -> $elem_type {
self.as_array()[index]
}

#[inline]
pub(crate) fn as_array(&self) -> &[$elem_type; $len] {
pub(crate) const fn as_array(&self) -> &[$elem_type; $len] {
let simd_ptr: *const Self = self;
let array_ptr: *const [$elem_type; $len] = simd_ptr.cast();
// SAFETY: We can always read the prefix of a simd type as an array.
Expand All @@ -65,7 +68,8 @@ macro_rules! simd_ty {
}
}

impl core::cmp::PartialEq for $id {
#[rustc_const_unstable(feature = "stdarch_const_helpers", issue = "none")]
const impl core::cmp::PartialEq for $id {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.as_array() == other.as_array()
Expand Down Expand Up @@ -101,7 +105,8 @@ macro_rules! simd_m_ty {

// FIXME: Workaround rust@60637
#[inline(always)]
pub(crate) fn splat(value: bool) -> Self {
#[rustc_const_unstable(feature = "stdarch_const_helpers", issue = "none")]
pub(crate) const fn splat(value: bool) -> Self {
#[derive(Copy, Clone)]
#[repr(simd)]
struct JustOne([$elem_type; 1]);
Expand All @@ -112,7 +117,7 @@ macro_rules! simd_m_ty {
}

#[inline]
pub(crate) fn as_array(&self) -> &[$elem_type; $len] {
pub(crate) const fn as_array(&self) -> &[$elem_type; $len] {
let simd_ptr: *const Self = self;
let array_ptr: *const [$elem_type; $len] = simd_ptr.cast();
// SAFETY: We can always read the prefix of a simd type as an array.
Expand All @@ -122,7 +127,8 @@ macro_rules! simd_m_ty {
}
}

impl core::cmp::PartialEq for $id {
#[rustc_const_unstable(feature = "stdarch_const_helpers", issue = "none")]
const impl core::cmp::PartialEq for $id {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.as_array() == other.as_array()
Expand Down
25 changes: 25 additions & 0 deletions crates/core_arch/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::fmt::Debug;

#[track_caller]
#[allow(unused)]
pub(crate) fn assert_eq_rt<T: PartialEq + Debug>(a: &T, b: &T) {
std::assert_eq!(a, b)
}

#[allow(unused)]
macro_rules! assert_eq_const {
($a:expr, $b:expr $(,)?) => {{
#[inline(always)]
#[rustc_const_unstable(feature = "stdarch_const_helpers", issue = "none")]
const fn assert_eq_ct<T: [const] PartialEq>(a: &T, b: &T) {
assert!(a == b, concat!("`", stringify!($a), "` != `", stringify!($b), "`"));
}

$crate::intrinsics::const_eval_select((&$a, &$b), assert_eq_ct, $crate::core_arch::test::assert_eq_rt);
}};
($a:expr, $b:expr, $($t:tt)+) => {
::std::assert_eq!($a, $b, $($t)+)
};
}

pub(crate) use assert_eq_const;
11 changes: 7 additions & 4 deletions crates/core_arch/src/x86/abm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ use stdarch_test::assert_instr;
#[target_feature(enable = "lzcnt")]
#[cfg_attr(test, assert_instr(lzcnt))]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub fn _lzcnt_u32(x: u32) -> u32 {
#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
pub const fn _lzcnt_u32(x: u32) -> u32 {
x.leading_zeros()
}

Expand All @@ -40,23 +41,25 @@ pub fn _lzcnt_u32(x: u32) -> u32 {
#[target_feature(enable = "popcnt")]
#[cfg_attr(test, assert_instr(popcnt))]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub fn _popcnt32(x: i32) -> i32 {
#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
pub const fn _popcnt32(x: i32) -> i32 {
x.count_ones() as i32
}

#[cfg(test)]
mod tests {
use crate::core_arch::assert_eq_const as assert_eq;
use stdarch_test::simd_test;

use crate::core_arch::x86::*;

#[simd_test(enable = "lzcnt")]
unsafe fn test_lzcnt_u32() {
const unsafe fn test_lzcnt_u32() {
assert_eq!(_lzcnt_u32(0b0101_1010), 25);
}

#[simd_test(enable = "popcnt")]
unsafe fn test_popcnt32() {
const unsafe fn test_popcnt32() {
assert_eq!(_popcnt32(0b0101_1010), 4);
}
}
Loading