Skip to content

Commit 5206d9c

Browse files
authored
wasm: Change *_bitmask return values (#1173)
First change them all to unsigned since they're just returning bits, and then also change them to the smallest-size integer which fits the return value (`u16` for `i8x16_bitmask` and `u8` for everything else). This suffers from an LLVM codegen bug for now, but it will hopefully get fixed in the not too distant future.
1 parent 907cfb2 commit 5206d9c

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

crates/core_arch/src/wasm32/simd128.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -2026,8 +2026,12 @@ pub unsafe fn i8x16_all_true(a: v128) -> bool {
20262026
#[cfg_attr(test, assert_instr(i8x16.bitmask))]
20272027
#[target_feature(enable = "simd128")]
20282028
#[doc(alias("i8x16.bitmask"))]
2029-
pub unsafe fn i8x16_bitmask(a: v128) -> i32 {
2030-
llvm_bitmask_i8x16(transmute(a))
2029+
pub unsafe fn i8x16_bitmask(a: v128) -> u16 {
2030+
// FIXME(https://bugs.llvm.org/show_bug.cgi?id=50507) - this produces an
2031+
// extraneous `i32.and` instruction against a mask of 65535 when converting
2032+
// from the native intrinsic's i32 return value to our desired u16. This
2033+
// shouldn't be necessary, though, but requires upstream LLVM changes.
2034+
llvm_bitmask_i8x16(transmute(a)) as u16
20312035
}
20322036

20332037
/// Converts two input vectors into a smaller lane vector by narrowing each
@@ -2277,8 +2281,8 @@ pub unsafe fn i16x8_all_true(a: v128) -> bool {
22772281
#[cfg_attr(test, assert_instr(i16x8.bitmask))]
22782282
#[target_feature(enable = "simd128")]
22792283
#[doc(alias("i16x8.bitmask"))]
2280-
pub unsafe fn i16x8_bitmask(a: v128) -> i32 {
2281-
llvm_bitmask_i16x8(transmute(a))
2284+
pub unsafe fn i16x8_bitmask(a: v128) -> u8 {
2285+
llvm_bitmask_i16x8(transmute(a)) as u8
22822286
}
22832287

22842288
/// Converts two input vectors into a smaller lane vector by narrowing each
@@ -2633,8 +2637,8 @@ pub unsafe fn i32x4_all_true(a: v128) -> bool {
26332637
#[cfg_attr(test, assert_instr(i32x4.bitmask))]
26342638
#[target_feature(enable = "simd128")]
26352639
#[doc(alias("i32x4.bitmask"))]
2636-
pub unsafe fn i32x4_bitmask(a: v128) -> i32 {
2637-
llvm_bitmask_i32x4(transmute(a))
2640+
pub unsafe fn i32x4_bitmask(a: v128) -> u8 {
2641+
llvm_bitmask_i32x4(transmute(a)) as u8
26382642
}
26392643

26402644
/// Converts low half of the smaller lane vector to a larger lane
@@ -2904,8 +2908,8 @@ pub unsafe fn i64x2_all_true(a: v128) -> bool {
29042908
#[cfg_attr(test, assert_instr(i64x2.bitmask))]
29052909
#[target_feature(enable = "simd128")]
29062910
#[doc(alias("i64x2.bitmask"))]
2907-
pub unsafe fn i64x2_bitmask(a: v128) -> i32 {
2908-
llvm_bitmask_i64x2(transmute(a))
2911+
pub unsafe fn i64x2_bitmask(a: v128) -> u8 {
2912+
llvm_bitmask_i64x2(transmute(a)) as u8
29092913
}
29102914

29112915
/// Converts low half of the smaller lane vector to a larger lane
@@ -3805,27 +3809,27 @@ pub mod tests {
38053809
let ones = i8x16_splat(!0);
38063810

38073811
assert_eq!(i8x16_bitmask(zero), 0);
3808-
assert_eq!(i8x16_bitmask(ones), (1 << 16) - 1);
3812+
assert_eq!(i8x16_bitmask(ones), 0xffff);
38093813
assert_eq!(i8x16_bitmask(i8x16_splat(i8::MAX)), 0);
3810-
assert_eq!(i8x16_bitmask(i8x16_splat(i8::MIN)), (1 << 16) - 1);
3814+
assert_eq!(i8x16_bitmask(i8x16_splat(i8::MIN)), 0xffff);
38113815
assert_eq!(i8x16_bitmask(i8x16_replace_lane::<1>(zero, -1)), 0b10);
38123816

38133817
assert_eq!(i16x8_bitmask(zero), 0);
3814-
assert_eq!(i16x8_bitmask(ones), (1 << 8) - 1);
3818+
assert_eq!(i16x8_bitmask(ones), 0xff);
38153819
assert_eq!(i16x8_bitmask(i16x8_splat(i16::MAX)), 0);
3816-
assert_eq!(i16x8_bitmask(i16x8_splat(i16::MIN)), (1 << 8) - 1);
3820+
assert_eq!(i16x8_bitmask(i16x8_splat(i16::MIN)), 0xff);
38173821
assert_eq!(i16x8_bitmask(i16x8_replace_lane::<1>(zero, -1)), 0b10);
38183822

38193823
assert_eq!(i32x4_bitmask(zero), 0);
3820-
assert_eq!(i32x4_bitmask(ones), (1 << 4) - 1);
3824+
assert_eq!(i32x4_bitmask(ones), 0b1111);
38213825
assert_eq!(i32x4_bitmask(i32x4_splat(i32::MAX)), 0);
3822-
assert_eq!(i32x4_bitmask(i32x4_splat(i32::MIN)), (1 << 4) - 1);
3826+
assert_eq!(i32x4_bitmask(i32x4_splat(i32::MIN)), 0b1111);
38233827
assert_eq!(i32x4_bitmask(i32x4_replace_lane::<1>(zero, -1)), 0b10);
38243828

38253829
assert_eq!(i64x2_bitmask(zero), 0);
3826-
assert_eq!(i64x2_bitmask(ones), (1 << 2) - 1);
3830+
assert_eq!(i64x2_bitmask(ones), 0b11);
38273831
assert_eq!(i64x2_bitmask(i64x2_splat(i64::MAX)), 0);
3828-
assert_eq!(i64x2_bitmask(i64x2_splat(i64::MIN)), (1 << 2) - 1);
3832+
assert_eq!(i64x2_bitmask(i64x2_splat(i64::MIN)), 0b11);
38293833
assert_eq!(i64x2_bitmask(i64x2_replace_lane::<1>(zero, -1)), 0b10);
38303834
}
38313835
}

0 commit comments

Comments
 (0)