Skip to content

Commit 8ad4f14

Browse files
Merge pull request #77 from rust-lang/reorg-vectors
Reorg vector types (nfc)
2 parents e3b729c + a2302da commit 8ad4f14

21 files changed

+460
-460
lines changed

crates/core_simd/src/macros.rs renamed to crates/core_simd/src/first.rs

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,3 @@
1-
/// Provides implementations of `From<$a> for $b` and `From<$b> for $a` that transmutes the value.
2-
macro_rules! from_transmute {
3-
{ unsafe $a:ty => $b:ty } => {
4-
from_transmute!{ @impl $a => $b }
5-
from_transmute!{ @impl $b => $a }
6-
};
7-
{ @impl $from:ty => $to:ty } => {
8-
impl core::convert::From<$from> for $to {
9-
#[inline]
10-
fn from(value: $from) -> $to {
11-
unsafe { core::mem::transmute(value) }
12-
}
13-
}
14-
};
15-
}
16-
17-
/// Provides implementations of `From<$generic> for core::arch::{x86, x86_64}::$intel` and
18-
/// vice-versa that transmutes the value.
19-
macro_rules! from_transmute_x86 {
20-
{ unsafe $generic:ty => $intel:ident } => {
21-
#[cfg(target_arch = "x86")]
22-
from_transmute! { unsafe $generic => core::arch::x86::$intel }
23-
24-
#[cfg(target_arch = "x86_64")]
25-
from_transmute! { unsafe $generic => core::arch::x86_64::$intel }
26-
}
27-
}
28-
291
/// Implements common traits on the specified vector `$name`, holding multiple `$lanes` of `$type`.
302
macro_rules! impl_vector {
313
{ $name:ident, $type:ty } => {
@@ -150,69 +122,3 @@ macro_rules! impl_vector {
150122
impl_shuffle_2pow_lanes!{ $name }
151123
}
152124
}
153-
154-
/// Implements additional integer traits (Eq, Ord, Hash) on the specified vector `$name`, holding multiple `$lanes` of `$type`.
155-
macro_rules! impl_integer_vector {
156-
{ $name:ident, $type:ty } => {
157-
impl_vector! { $name, $type }
158-
159-
impl<const LANES: usize> Eq for $name<LANES> where Self: crate::LanesAtMost64 {}
160-
161-
impl<const LANES: usize> Ord for $name<LANES> where Self: crate::LanesAtMost64 {
162-
#[inline]
163-
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
164-
// TODO use SIMD cmp
165-
self.to_array().cmp(other.as_ref())
166-
}
167-
}
168-
169-
impl<const LANES: usize> core::hash::Hash for $name<LANES> where Self: crate::LanesAtMost64 {
170-
#[inline]
171-
fn hash<H>(&self, state: &mut H)
172-
where
173-
H: core::hash::Hasher
174-
{
175-
self.as_slice().hash(state)
176-
}
177-
}
178-
}
179-
}
180-
181-
/// Implements inherent methods for a float vector `$name` containing multiple
182-
/// `$lanes` of float `$type`, which uses `$bits_ty` as its binary
183-
/// representation. Called from `define_float_vector!`.
184-
macro_rules! impl_float_vector {
185-
{ $name:ident, $type:ty, $bits_ty:ident } => {
186-
impl_vector! { $name, $type }
187-
188-
impl<const LANES: usize> $name<LANES>
189-
where
190-
Self: crate::LanesAtMost64,
191-
crate::$bits_ty<LANES>: crate::LanesAtMost64,
192-
{
193-
/// Raw transmutation to an unsigned integer vector type with the
194-
/// same size and number of lanes.
195-
#[inline]
196-
pub fn to_bits(self) -> crate::$bits_ty<LANES> {
197-
assert_eq!(core::mem::size_of::<Self>(), core::mem::size_of::<crate::$bits_ty<LANES>>());
198-
unsafe { core::mem::transmute_copy(&self) }
199-
}
200-
201-
/// Raw transmutation from an unsigned integer vector type with the
202-
/// same size and number of lanes.
203-
#[inline]
204-
pub fn from_bits(bits: crate::$bits_ty<LANES>) -> Self {
205-
assert_eq!(core::mem::size_of::<Self>(), core::mem::size_of::<crate::$bits_ty<LANES>>());
206-
unsafe { core::mem::transmute_copy(&bits) }
207-
}
208-
209-
/// Produces a vector where every lane has the absolute value of the
210-
/// equivalently-indexed lane in `self`.
211-
#[inline]
212-
pub fn abs(self) -> Self {
213-
let no_sign = crate::$bits_ty::splat(!0 >> 1);
214-
Self::from_bits(self.to_bits() & no_sign)
215-
}
216-
}
217-
};
218-
}

crates/core_simd/src/lib.rs

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
//! Portable SIMD module.
66
77
#[macro_use]
8-
mod macros;
8+
mod first;
99
#[macro_use]
1010
mod permute;
11+
#[macro_use]
12+
mod transmute;
1113

1214
mod fmt;
1315
mod intrinsics;
@@ -20,33 +22,5 @@ pub use lanes_at_most_64::LanesAtMost64;
2022
mod masks;
2123
pub use masks::*;
2224

23-
mod vectors_u8;
24-
pub use vectors_u8::*;
25-
mod vectors_u16;
26-
pub use vectors_u16::*;
27-
mod vectors_u32;
28-
pub use vectors_u32::*;
29-
mod vectors_u64;
30-
pub use vectors_u64::*;
31-
mod vectors_u128;
32-
pub use vectors_u128::*;
33-
mod vectors_usize;
34-
pub use vectors_usize::*;
35-
36-
mod vectors_i8;
37-
pub use vectors_i8::*;
38-
mod vectors_i16;
39-
pub use vectors_i16::*;
40-
mod vectors_i32;
41-
pub use vectors_i32::*;
42-
mod vectors_i64;
43-
pub use vectors_i64::*;
44-
mod vectors_i128;
45-
pub use vectors_i128::*;
46-
mod vectors_isize;
47-
pub use vectors_isize::*;
48-
49-
mod vectors_f32;
50-
pub use vectors_f32::*;
51-
mod vectors_f64;
52-
pub use vectors_f64::*;
25+
mod vector;
26+
pub use vector::*;

crates/core_simd/src/transmute.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// Provides implementations of `From<$a> for $b` and `From<$b> for $a` that transmutes the value.
2+
macro_rules! from_transmute {
3+
{ unsafe $a:ty => $b:ty } => {
4+
from_transmute!{ @impl $a => $b }
5+
from_transmute!{ @impl $b => $a }
6+
};
7+
{ @impl $from:ty => $to:ty } => {
8+
impl core::convert::From<$from> for $to {
9+
#[inline]
10+
fn from(value: $from) -> $to {
11+
unsafe { core::mem::transmute(value) }
12+
}
13+
}
14+
};
15+
}
16+
17+
/// Provides implementations of `From<$generic> for core::arch::{x86, x86_64}::$intel` and
18+
/// vice-versa that transmutes the value.
19+
macro_rules! from_transmute_x86 {
20+
{ unsafe $generic:ty => $intel:ident } => {
21+
#[cfg(target_arch = "x86")]
22+
from_transmute! { unsafe $generic => core::arch::x86::$intel }
23+
24+
#[cfg(target_arch = "x86_64")]
25+
from_transmute! { unsafe $generic => core::arch::x86_64::$intel }
26+
}
27+
}

crates/core_simd/src/vector.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mod float;
2+
mod int;
3+
mod uint;
4+
5+
pub use float::*;
6+
pub use int::*;
7+
pub use uint::*;

crates/core_simd/src/vector/float.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#![allow(non_camel_case_types)]
2+
3+
/// Implements inherent methods for a float vector `$name` containing multiple
4+
/// `$lanes` of float `$type`, which uses `$bits_ty` as its binary
5+
/// representation. Called from `define_float_vector!`.
6+
macro_rules! impl_float_vector {
7+
{ $name:ident, $type:ty, $bits_ty:ident } => {
8+
impl_vector! { $name, $type }
9+
10+
impl<const LANES: usize> $name<LANES>
11+
where
12+
Self: crate::LanesAtMost64,
13+
crate::$bits_ty<LANES>: crate::LanesAtMost64,
14+
{
15+
/// Raw transmutation to an unsigned integer vector type with the
16+
/// same size and number of lanes.
17+
#[inline]
18+
pub fn to_bits(self) -> crate::$bits_ty<LANES> {
19+
assert_eq!(core::mem::size_of::<Self>(), core::mem::size_of::<crate::$bits_ty<LANES>>());
20+
unsafe { core::mem::transmute_copy(&self) }
21+
}
22+
23+
/// Raw transmutation from an unsigned integer vector type with the
24+
/// same size and number of lanes.
25+
#[inline]
26+
pub fn from_bits(bits: crate::$bits_ty<LANES>) -> Self {
27+
assert_eq!(core::mem::size_of::<Self>(), core::mem::size_of::<crate::$bits_ty<LANES>>());
28+
unsafe { core::mem::transmute_copy(&bits) }
29+
}
30+
31+
/// Produces a vector where every lane has the absolute value of the
32+
/// equivalently-indexed lane in `self`.
33+
#[inline]
34+
pub fn abs(self) -> Self {
35+
let no_sign = crate::$bits_ty::splat(!0 >> 1);
36+
Self::from_bits(self.to_bits() & no_sign)
37+
}
38+
}
39+
};
40+
}
41+
42+
43+
/// A SIMD vector of containing `LANES` `f32` values.
44+
#[repr(simd)]
45+
pub struct SimdF32<const LANES: usize>([f32; LANES])
46+
where
47+
Self: crate::LanesAtMost64;
48+
49+
impl_float_vector! { SimdF32, f32, SimdU32 }
50+
51+
from_transmute_x86! { unsafe f32x4 => __m128 }
52+
from_transmute_x86! { unsafe f32x8 => __m256 }
53+
//from_transmute_x86! { unsafe f32x16 => __m512 }
54+
55+
/// A SIMD vector of containing `LANES` `f64` values.
56+
#[repr(simd)]
57+
pub struct SimdF64<const LANES: usize>([f64; LANES])
58+
where
59+
Self: crate::LanesAtMost64;
60+
61+
impl_float_vector! { SimdF64, f64, SimdU64 }
62+
63+
from_transmute_x86! { unsafe f64x2 => __m128d }
64+
from_transmute_x86! { unsafe f64x4 => __m256d }
65+
//from_transmute_x86! { unsafe f64x8 => __m512d }
66+
67+
/// Vector of two `f32` values
68+
pub type f32x2 = SimdF32<2>;
69+
70+
/// Vector of four `f32` values
71+
pub type f32x4 = SimdF32<4>;
72+
73+
/// Vector of eight `f32` values
74+
pub type f32x8 = SimdF32<8>;
75+
76+
/// Vector of 16 `f32` values
77+
pub type f32x16 = SimdF32<16>;
78+
79+
/// Vector of two `f64` values
80+
pub type f64x2 = SimdF64<2>;
81+
82+
/// Vector of four `f64` values
83+
pub type f64x4 = SimdF64<4>;
84+
85+
/// Vector of eight `f64` values
86+
pub type f64x8 = SimdF64<8>;

0 commit comments

Comments
 (0)