Skip to content

Commit 77e2bcb

Browse files
committed
Auto merge of #49141 - gnzlbg:simd_select, r=alexcrichton
adds simd_select intrinsic The select SIMD intrinsic is used to select elements from two SIMD vectors using a mask: ```rust let mask = b8x4::new(true, false, false, true); let a = f32x4::new(1., 2., 3., 4.); let b = f32x4::new(5., 6., 7., 8.); assert_eq!(simd_select(mask, a, b), f32x4::new(1., 6., 7., 4.)); ``` The number of lanes between the mask and the vectors must match, but the vector width of the mask does not need to match that of the vectors. The mask is required to be a vector of signed integers. Note: this intrinsic will be exposed via `std::simd`'s vector masks - users are not expected to use it directly.
2 parents f5631d9 + 4eff4d9 commit 77e2bcb

File tree

5 files changed

+259
-0
lines changed

5 files changed

+259
-0
lines changed

src/librustc_trans/intrinsic.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,27 @@ fn generic_simd_intrinsic<'a, 'tcx>(
11531153
return Ok(bx.extract_element(args[0].immediate(), args[1].immediate()))
11541154
}
11551155

1156+
if name == "simd_select" {
1157+
let m_elem_ty = in_elem;
1158+
let m_len = in_len;
1159+
let v_len = arg_tys[1].simd_size(tcx);
1160+
require!(m_len == v_len,
1161+
"mismatched lengths: mask length `{}` != other vector length `{}`",
1162+
m_len, v_len
1163+
);
1164+
match m_elem_ty.sty {
1165+
ty::TyInt(_) => {},
1166+
_ => {
1167+
return_error!("mask element type is `{}`, expected `i_`", m_elem_ty);
1168+
}
1169+
}
1170+
// truncate the mask to a vector of i1s
1171+
let i1 = Type::i1(bx.cx);
1172+
let i1xn = Type::vector(&i1, m_len as u64);
1173+
let m_i1s = bx.trunc(args[0].immediate(), i1xn);
1174+
return Ok(bx.select(m_i1s, args[1].immediate(), args[2].immediate()));
1175+
}
1176+
11561177
macro_rules! arith_red {
11571178
($name:tt : $integer_reduce:ident, $float_reduce:ident, $ordered:expr) => {
11581179
if name == $name {

src/librustc_typeck/check/intrinsic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ pub fn check_platform_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
361361
"simd_insert" => (2, vec![param(0), tcx.types.u32, param(1)], param(0)),
362362
"simd_extract" => (2, vec![param(0), tcx.types.u32], param(1)),
363363
"simd_cast" => (2, vec![param(0)], param(1)),
364+
"simd_select" => (2, vec![param(0), param(1), param(1)], param(1)),
364365
"simd_reduce_all" | "simd_reduce_any" => (1, vec![param(0)], tcx.types.bool),
365366
"simd_reduce_add_ordered" | "simd_reduce_mul_ordered"
366367
=> (2, vec![param(0), param(1)], param(1)),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -C no-prepopulate-passes
12+
13+
#![crate_type = "lib"]
14+
15+
#![feature(repr_simd, platform_intrinsics)]
16+
#[allow(non_camel_case_types)]
17+
18+
#[repr(simd)]
19+
#[derive(Copy, Clone, PartialEq, Debug)]
20+
pub struct f32x4(pub f32, pub f32, pub f32, pub f32);
21+
22+
#[repr(simd)]
23+
#[derive(Copy, Clone, PartialEq, Debug)]
24+
pub struct b8x4(pub i8, pub i8, pub i8, pub i8);
25+
26+
extern "platform-intrinsic" {
27+
fn simd_select<T, U>(x: T, a: U, b: U) -> U;
28+
}
29+
30+
// CHECK-LABEL: @select
31+
#[no_mangle]
32+
pub unsafe fn select(m: b8x4, a: f32x4, b: f32x4) -> f32x4 {
33+
// CHECK: select <4 x i1>
34+
simd_select(m, a, b)
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test that the simd_select intrinsic produces ok-ish error
12+
// messages when misused.
13+
14+
#![feature(repr_simd, platform_intrinsics)]
15+
#![allow(non_camel_case_types)]
16+
17+
#[repr(simd)]
18+
#[derive(Copy, Clone)]
19+
pub struct f32x4(pub f32, pub f32, pub f32, pub f32);
20+
21+
#[repr(simd)]
22+
#[derive(Copy, Clone)]
23+
pub struct u32x4(pub u32, pub u32, pub u32, pub u32);
24+
25+
#[repr(simd)]
26+
#[derive(Copy, Clone, PartialEq)]
27+
struct b8x4(pub i8, pub i8, pub i8, pub i8);
28+
29+
#[repr(simd)]
30+
#[derive(Copy, Clone, PartialEq)]
31+
struct b8x8(pub i8, pub i8, pub i8, pub i8,
32+
pub i8, pub i8, pub i8, pub i8);
33+
34+
extern "platform-intrinsic" {
35+
fn simd_select<T, U>(x: T, a: U, b: U) -> U;
36+
}
37+
38+
fn main() {
39+
let m4 = b8x4(0, 0, 0, 0);
40+
let m8 = b8x8(0, 0, 0, 0, 0, 0, 0, 0);
41+
let x = u32x4(0, 0, 0, 0);
42+
let z = f32x4(0.0, 0.0, 0.0, 0.0);
43+
44+
unsafe {
45+
simd_select(m4, x, x);
46+
47+
simd_select(m8, x, x);
48+
//~^ ERROR mismatched lengths: mask length `8` != other vector length `4`
49+
50+
simd_select(x, x, x);
51+
//~^ ERROR mask element type is `u32`, expected `i_`
52+
53+
simd_select(z, z, z);
54+
//~^ ERROR mask element type is `f32`, expected `i_`
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-emscripten
12+
13+
// Test that the simd_select intrinsics produces correct results.
14+
15+
#![feature(repr_simd, platform_intrinsics)]
16+
#[allow(non_camel_case_types)]
17+
18+
#[repr(simd)]
19+
#[derive(Copy, Clone, PartialEq, Debug)]
20+
struct i32x4(pub i32, pub i32, pub i32, pub i32);
21+
22+
#[repr(simd)]
23+
#[derive(Copy, Clone, PartialEq, Debug)]
24+
struct u32x4(pub u32, pub u32, pub u32, pub u32);
25+
26+
#[repr(simd)]
27+
#[derive(Copy, Clone, PartialEq, Debug)]
28+
struct f32x4(pub f32, pub f32, pub f32, pub f32);
29+
30+
#[repr(simd)]
31+
#[derive(Copy, Clone, PartialEq, Debug)]
32+
struct b8x4(pub i8, pub i8, pub i8, pub i8);
33+
34+
extern "platform-intrinsic" {
35+
fn simd_select<T, U>(x: T, a: U, b: U) -> U;
36+
}
37+
38+
fn main() {
39+
let m0 = b8x4(!0, !0, !0, !0);
40+
let m1 = b8x4(0, 0, 0, 0);
41+
let m2 = b8x4(!0, !0, 0, 0);
42+
let m3 = b8x4(0, 0, !0, !0);
43+
let m4 = b8x4(!0, 0, !0, 0);
44+
45+
unsafe {
46+
let a = i32x4(1, -2, 3, 4);
47+
let b = i32x4(5, 6, -7, 8);
48+
49+
let r: i32x4 = simd_select(m0, a, b);
50+
let e = a;
51+
assert_eq!(r, e);
52+
53+
let r: i32x4 = simd_select(m1, a, b);
54+
let e = b;
55+
assert_eq!(r, e);
56+
57+
let r: i32x4 = simd_select(m2, a, b);
58+
let e = i32x4(1, -2, -7, 8);
59+
assert_eq!(r, e);
60+
61+
let r: i32x4 = simd_select(m3, a, b);
62+
let e = i32x4(5, 6, 3, 4);
63+
assert_eq!(r, e);
64+
65+
let r: i32x4 = simd_select(m4, a, b);
66+
let e = i32x4(1, 6, 3, 8);
67+
assert_eq!(r, e);
68+
}
69+
70+
unsafe {
71+
let a = u32x4(1, 2, 3, 4);
72+
let b = u32x4(5, 6, 7, 8);
73+
74+
let r: u32x4 = simd_select(m0, a, b);
75+
let e = a;
76+
assert_eq!(r, e);
77+
78+
let r: u32x4 = simd_select(m1, a, b);
79+
let e = b;
80+
assert_eq!(r, e);
81+
82+
let r: u32x4 = simd_select(m2, a, b);
83+
let e = u32x4(1, 2, 7, 8);
84+
assert_eq!(r, e);
85+
86+
let r: u32x4 = simd_select(m3, a, b);
87+
let e = u32x4(5, 6, 3, 4);
88+
assert_eq!(r, e);
89+
90+
let r: u32x4 = simd_select(m4, a, b);
91+
let e = u32x4(1, 6, 3, 8);
92+
assert_eq!(r, e);
93+
}
94+
95+
unsafe {
96+
let a = f32x4(1., 2., 3., 4.);
97+
let b = f32x4(5., 6., 7., 8.);
98+
99+
let r: f32x4 = simd_select(m0, a, b);
100+
let e = a;
101+
assert_eq!(r, e);
102+
103+
let r: f32x4 = simd_select(m1, a, b);
104+
let e = b;
105+
assert_eq!(r, e);
106+
107+
let r: f32x4 = simd_select(m2, a, b);
108+
let e = f32x4(1., 2., 7., 8.);
109+
assert_eq!(r, e);
110+
111+
let r: f32x4 = simd_select(m3, a, b);
112+
let e = f32x4(5., 6., 3., 4.);
113+
assert_eq!(r, e);
114+
115+
let r: f32x4 = simd_select(m4, a, b);
116+
let e = f32x4(1., 6., 3., 8.);
117+
assert_eq!(r, e);
118+
}
119+
120+
unsafe {
121+
let t = !0 as i8;
122+
let f = 0 as i8;
123+
let a = b8x4(t, f, t, f);
124+
let b = b8x4(f, f, f, t);
125+
126+
let r: b8x4 = simd_select(m0, a, b);
127+
let e = a;
128+
assert_eq!(r, e);
129+
130+
let r: b8x4 = simd_select(m1, a, b);
131+
let e = b;
132+
assert_eq!(r, e);
133+
134+
let r: b8x4 = simd_select(m2, a, b);
135+
let e = b8x4(t, f, f, t);
136+
assert_eq!(r, e);
137+
138+
let r: b8x4 = simd_select(m3, a, b);
139+
let e = b8x4(f, f, t, f);
140+
assert_eq!(r, e);
141+
142+
let r: b8x4 = simd_select(m4, a, b);
143+
let e = b8x4(t, f, t, t);
144+
assert_eq!(r, e);
145+
}
146+
}

0 commit comments

Comments
 (0)