Skip to content

Commit e572952

Browse files
authored
Merge pull request #617 from robertknight/wasm-mask-types
Use separate types for masks and SIMD vectors in wasm32 and generic ISAs
2 parents 8003076 + 94f2928 commit e572952

File tree

2 files changed

+71
-63
lines changed

2 files changed

+71
-63
lines changed

rten-simd/src/safe/arch/generic.rs

+27-24
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ use crate::safe::{Isa, Mask, MaskOps, Simd, SimdFloatOps, SimdIntOps, SimdOps};
66
// Size of SIMD vector in 32-bit lanes.
77
const LEN_X32: usize = 4;
88

9-
#[repr(align(16))]
10-
#[derive(Copy, Clone, Debug)]
11-
pub struct F32x4([f32; LEN_X32]);
12-
13-
#[repr(align(16))]
14-
#[derive(Copy, Clone, Debug)]
15-
pub struct I32x4([i32; LEN_X32]);
9+
macro_rules! simd_type {
10+
($simd:ident, $elem:ty, $len:expr) => {
11+
#[repr(align(16))]
12+
#[derive(Copy, Clone, Debug)]
13+
pub struct $simd([$elem; $len]);
14+
};
15+
}
1616

17-
#[repr(align(16))]
18-
#[derive(Copy, Clone, Debug)]
19-
pub struct I16x8([i16; LEN_X32 * 2]);
17+
// Define SIMD vector types.
18+
simd_type!(F32x4, f32, LEN_X32);
19+
simd_type!(I32x4, i32, LEN_X32);
20+
simd_type!(I16x8, i16, LEN_X32 * 2);
21+
simd_type!(I8x16, i8, LEN_X32 * 4);
2022

21-
#[repr(align(16))]
22-
#[derive(Copy, Clone, Debug)]
23-
pub struct I8x16([i8; LEN_X32 * 4]);
23+
// Define mask vector types. `Mn` is a mask for a vector with n-bit lanes.
24+
simd_type!(M32, i32, LEN_X32);
25+
simd_type!(M16, i16, LEN_X32 * 2);
26+
simd_type!(M8, i8, LEN_X32 * 4);
2427

2528
#[derive(Copy, Clone)]
2629
pub struct GenericIsa {
@@ -195,7 +198,7 @@ macro_rules! simd_ops_common {
195198
}
196199

197200
unsafe impl SimdOps<F32x4> for GenericIsa {
198-
simd_ops_common!(F32x4, f32, 4, I32x4);
201+
simd_ops_common!(F32x4, f32, 4, M32);
199202
}
200203

201204
impl SimdFloatOps<F32x4> for GenericIsa {
@@ -254,9 +257,9 @@ macro_rules! impl_simd_int_ops {
254257
};
255258
}
256259

257-
impl_simd_int_ops!(I32x4, i32, 4, I32x4);
258-
impl_simd_int_ops!(I16x8, i16, 8, I16x8);
259-
impl_simd_int_ops!(I8x16, i8, 16, I8x16);
260+
impl_simd_int_ops!(I32x4, i32, 4, M32);
261+
impl_simd_int_ops!(I16x8, i16, 8, M16);
262+
impl_simd_int_ops!(I8x16, i8, 16, M8);
260263

261264
macro_rules! impl_mask {
262265
($mask:ident, $len:expr) => {
@@ -280,9 +283,9 @@ macro_rules! impl_mask {
280283
};
281284
}
282285

283-
impl_mask!(I32x4, LEN_X32);
284-
impl_mask!(I16x8, LEN_X32 * 2);
285-
impl_mask!(I8x16, LEN_X32 * 4);
286+
impl_mask!(M32, LEN_X32);
287+
impl_mask!(M16, LEN_X32 * 2);
288+
impl_mask!(M8, LEN_X32 * 4);
286289

287290
macro_rules! impl_simd {
288291
($simd:ty, $elem:ty, $mask:ty, $len:expr) => {
@@ -312,7 +315,7 @@ macro_rules! impl_simd {
312315
};
313316
}
314317

315-
impl_simd!(F32x4, f32, I32x4, 4);
316-
impl_simd!(I32x4, i32, I32x4, 4);
317-
impl_simd!(I16x8, i16, I16x8, 8);
318-
impl_simd!(I8x16, i8, I8x16, 16);
318+
impl_simd!(F32x4, f32, M32, 4);
319+
impl_simd!(I32x4, i32, M32, 4);
320+
impl_simd!(I16x8, i16, M16, 8);
321+
impl_simd!(I8x16, i8, M8, 16);

rten-simd/src/safe/arch/wasm32.rs

+44-39
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use std::mem::transmute;
1212
use super::{lanes, simd_type};
1313
use crate::safe::{Isa, Mask, MaskOps, Simd, SimdFloatOps, SimdIntOps, SimdOps};
1414

15-
simd_type!(F32x4, v128, f32, I32x4, Wasm32Isa);
16-
simd_type!(I32x4, v128, i32, I32x4, Wasm32Isa);
17-
simd_type!(I16x8, v128, i16, I16x8, Wasm32Isa);
18-
simd_type!(I8x16, v128, i8, I8x16, Wasm32Isa);
15+
simd_type!(F32x4, v128, f32, M32, Wasm32Isa);
16+
simd_type!(I32x4, v128, i32, M32, Wasm32Isa);
17+
simd_type!(I16x8, v128, i16, M16, Wasm32Isa);
18+
simd_type!(I8x16, v128, i8, M8, Wasm32Isa);
1919

2020
#[derive(Copy, Clone)]
2121
pub struct Wasm32Isa {
@@ -119,7 +119,7 @@ macro_rules! simd_ops_common {
119119
}
120120

121121
unsafe impl SimdOps<F32x4> for Wasm32Isa {
122-
simd_ops_common!(F32x4, I32x4, i32);
122+
simd_ops_common!(F32x4, M32, i32);
123123

124124
#[inline]
125125
fn add(self, x: F32x4, y: F32x4) -> F32x4 {
@@ -149,28 +149,28 @@ unsafe impl SimdOps<F32x4> for Wasm32Isa {
149149
}
150150

151151
#[inline]
152-
fn lt(self, x: F32x4, y: F32x4) -> I32x4 {
153-
I32x4(f32x4_lt(x.0, y.0))
152+
fn lt(self, x: F32x4, y: F32x4) -> M32 {
153+
M32(f32x4_lt(x.0, y.0))
154154
}
155155

156156
#[inline]
157-
fn le(self, x: F32x4, y: F32x4) -> I32x4 {
158-
I32x4(f32x4_le(x.0, y.0))
157+
fn le(self, x: F32x4, y: F32x4) -> M32 {
158+
M32(f32x4_le(x.0, y.0))
159159
}
160160

161161
#[inline]
162-
fn eq(self, x: F32x4, y: F32x4) -> I32x4 {
163-
I32x4(f32x4_eq(x.0, y.0))
162+
fn eq(self, x: F32x4, y: F32x4) -> M32 {
163+
M32(f32x4_eq(x.0, y.0))
164164
}
165165

166166
#[inline]
167-
fn ge(self, x: F32x4, y: F32x4) -> I32x4 {
168-
I32x4(f32x4_ge(x.0, y.0))
167+
fn ge(self, x: F32x4, y: F32x4) -> M32 {
168+
M32(f32x4_ge(x.0, y.0))
169169
}
170170

171171
#[inline]
172-
fn gt(self, x: F32x4, y: F32x4) -> I32x4 {
173-
I32x4(f32x4_gt(x.0, y.0))
172+
fn gt(self, x: F32x4, y: F32x4) -> M32 {
173+
M32(f32x4_gt(x.0, y.0))
174174
}
175175

176176
#[inline]
@@ -231,7 +231,7 @@ impl SimdFloatOps<F32x4> for Wasm32Isa {
231231
}
232232

233233
unsafe impl SimdOps<I32x4> for Wasm32Isa {
234-
simd_ops_common!(I32x4, I32x4, i32);
234+
simd_ops_common!(I32x4, M32, i32);
235235

236236
#[inline]
237237
fn add(self, x: I32x4, y: I32x4) -> I32x4 {
@@ -254,18 +254,18 @@ unsafe impl SimdOps<I32x4> for Wasm32Isa {
254254
}
255255

256256
#[inline]
257-
fn eq(self, x: I32x4, y: I32x4) -> I32x4 {
258-
I32x4(i32x4_eq(x.0, y.0))
257+
fn eq(self, x: I32x4, y: I32x4) -> M32 {
258+
M32(i32x4_eq(x.0, y.0))
259259
}
260260

261261
#[inline]
262-
fn ge(self, x: I32x4, y: I32x4) -> I32x4 {
263-
I32x4(i32x4_ge(x.0, y.0))
262+
fn ge(self, x: I32x4, y: I32x4) -> M32 {
263+
M32(i32x4_ge(x.0, y.0))
264264
}
265265

266266
#[inline]
267-
fn gt(self, x: I32x4, y: I32x4) -> I32x4 {
268-
I32x4(i32x4_gt(x.0, y.0))
267+
fn gt(self, x: I32x4, y: I32x4) -> M32 {
268+
M32(i32x4_gt(x.0, y.0))
269269
}
270270
}
271271

@@ -282,7 +282,7 @@ impl SimdIntOps<I32x4> for Wasm32Isa {
282282
}
283283

284284
unsafe impl SimdOps<I16x8> for Wasm32Isa {
285-
simd_ops_common!(I16x8, I16x8, i16);
285+
simd_ops_common!(I16x8, M16, i16);
286286

287287
#[inline]
288288
fn add(self, x: I16x8, y: I16x8) -> I16x8 {
@@ -305,18 +305,18 @@ unsafe impl SimdOps<I16x8> for Wasm32Isa {
305305
}
306306

307307
#[inline]
308-
fn eq(self, x: I16x8, y: I16x8) -> I16x8 {
309-
I16x8(i16x8_eq(x.0, y.0))
308+
fn eq(self, x: I16x8, y: I16x8) -> M16 {
309+
M16(i16x8_eq(x.0, y.0))
310310
}
311311

312312
#[inline]
313-
fn ge(self, x: I16x8, y: I16x8) -> I16x8 {
314-
I16x8(i16x8_ge(x.0, y.0))
313+
fn ge(self, x: I16x8, y: I16x8) -> M16 {
314+
M16(i16x8_ge(x.0, y.0))
315315
}
316316

317317
#[inline]
318-
fn gt(self, x: I16x8, y: I16x8) -> I16x8 {
319-
I16x8(i16x8_gt(x.0, y.0))
318+
fn gt(self, x: I16x8, y: I16x8) -> M16 {
319+
M16(i16x8_gt(x.0, y.0))
320320
}
321321
}
322322

@@ -333,7 +333,7 @@ impl SimdIntOps<I16x8> for Wasm32Isa {
333333
}
334334

335335
unsafe impl SimdOps<I8x16> for Wasm32Isa {
336-
simd_ops_common!(I8x16, I8x16, i8);
336+
simd_ops_common!(I8x16, M8, i8);
337337

338338
#[inline]
339339
fn add(self, x: I8x16, y: I8x16) -> I8x16 {
@@ -365,18 +365,18 @@ unsafe impl SimdOps<I8x16> for Wasm32Isa {
365365
}
366366

367367
#[inline]
368-
fn eq(self, x: I8x16, y: I8x16) -> I8x16 {
369-
I8x16(i8x16_eq(x.0, y.0))
368+
fn eq(self, x: I8x16, y: I8x16) -> M8 {
369+
M8(i8x16_eq(x.0, y.0))
370370
}
371371

372372
#[inline]
373-
fn ge(self, x: I8x16, y: I8x16) -> I8x16 {
374-
I8x16(i8x16_ge(x.0, y.0))
373+
fn ge(self, x: I8x16, y: I8x16) -> M8 {
374+
M8(i8x16_ge(x.0, y.0))
375375
}
376376

377377
#[inline]
378-
fn gt(self, x: I8x16, y: I8x16) -> I8x16 {
379-
I8x16(i8x16_gt(x.0, y.0))
378+
fn gt(self, x: I8x16, y: I8x16) -> M8 {
379+
M8(i8x16_gt(x.0, y.0))
380380
}
381381
}
382382

@@ -394,6 +394,10 @@ impl SimdIntOps<I8x16> for Wasm32Isa {
394394

395395
macro_rules! mask_type {
396396
($mask:ident, $elem:ty, $len: expr) => {
397+
#[derive(Copy, Clone, Debug)]
398+
#[repr(transparent)]
399+
pub struct $mask(v128);
400+
397401
impl Mask for $mask {
398402
type Array = [bool; $len];
399403

@@ -413,6 +417,7 @@ macro_rules! mask_type {
413417
};
414418
}
415419

416-
mask_type!(I32x4, i32, 4);
417-
mask_type!(I16x8, i16, 8);
418-
mask_type!(I8x16, i8, 16);
420+
// Define mask vector types. `Mn` is a mask for a vector with n-bit lanes.
421+
mask_type!(M32, i32, 4);
422+
mask_type!(M16, i16, 8);
423+
mask_type!(M8, i8, 16);

0 commit comments

Comments
 (0)