|
1 | | -#![feature(core_intrinsics)] |
| 1 | +#![feature(core_intrinsics, portable_simd)] |
| 2 | +use std::intrinsics::simd::simd_relaxed_fma; |
2 | 3 | use std::intrinsics::{fmuladdf32, fmuladdf64}; |
| 4 | +use std::simd::prelude::*; |
3 | 5 |
|
4 | | -fn main() { |
5 | | - let mut saw_zero = false; |
6 | | - let mut saw_nonzero = false; |
| 6 | +fn ensure_both_happen(f: impl Fn() -> bool) -> bool { |
| 7 | + let mut saw_true = false; |
| 8 | + let mut saw_false = false; |
7 | 9 | for _ in 0..50 { |
8 | | - let a = std::hint::black_box(0.1_f64); |
9 | | - let b = std::hint::black_box(0.2); |
10 | | - let c = std::hint::black_box(-a * b); |
11 | | - // It is unspecified whether the following operation is fused or not. The |
12 | | - // following evaluates to 0.0 if unfused, and nonzero (-1.66e-18) if fused. |
13 | | - let x = unsafe { fmuladdf64(a, b, c) }; |
14 | | - if x == 0.0 { |
15 | | - saw_zero = true; |
| 10 | + let b = f(); |
| 11 | + if b { |
| 12 | + saw_true = true; |
16 | 13 | } else { |
17 | | - saw_nonzero = true; |
| 14 | + saw_false = true; |
| 15 | + } |
| 16 | + if saw_true && saw_false { |
| 17 | + return true; |
18 | 18 | } |
19 | 19 | } |
| 20 | + false |
| 21 | +} |
| 22 | + |
| 23 | +fn main() { |
20 | 24 | assert!( |
21 | | - saw_zero && saw_nonzero, |
| 25 | + ensure_both_happen(|| { |
| 26 | + let a = std::hint::black_box(0.1_f64); |
| 27 | + let b = std::hint::black_box(0.2); |
| 28 | + let c = std::hint::black_box(-a * b); |
| 29 | + // It is unspecified whether the following operation is fused or not. The |
| 30 | + // following evaluates to 0.0 if unfused, and nonzero (-1.66e-18) if fused. |
| 31 | + let x = unsafe { fmuladdf64(a, b, c) }; |
| 32 | + x == 0.0 |
| 33 | + }), |
22 | 34 | "`fmuladdf64` failed to be evaluated as both fused and unfused" |
23 | 35 | ); |
24 | 36 |
|
25 | | - let mut saw_zero = false; |
26 | | - let mut saw_nonzero = false; |
27 | | - for _ in 0..50 { |
28 | | - let a = std::hint::black_box(0.1_f32); |
29 | | - let b = std::hint::black_box(0.2); |
30 | | - let c = std::hint::black_box(-a * b); |
31 | | - // It is unspecified whether the following operation is fused or not. The |
32 | | - // following evaluates to 0.0 if unfused, and nonzero (-8.1956386e-10) if fused. |
33 | | - let x = unsafe { fmuladdf32(a, b, c) }; |
34 | | - if x == 0.0 { |
35 | | - saw_zero = true; |
36 | | - } else { |
37 | | - saw_nonzero = true; |
38 | | - } |
39 | | - } |
40 | 37 | assert!( |
41 | | - saw_zero && saw_nonzero, |
| 38 | + ensure_both_happen(|| { |
| 39 | + let a = std::hint::black_box(0.1_f32); |
| 40 | + let b = std::hint::black_box(0.2); |
| 41 | + let c = std::hint::black_box(-a * b); |
| 42 | + // It is unspecified whether the following operation is fused or not. The |
| 43 | + // following evaluates to 0.0 if unfused, and nonzero (-8.1956386e-10) if fused. |
| 44 | + let x = unsafe { fmuladdf32(a, b, c) }; |
| 45 | + x == 0.0 |
| 46 | + }), |
42 | 47 | "`fmuladdf32` failed to be evaluated as both fused and unfused" |
43 | 48 | ); |
| 49 | + |
| 50 | + assert!( |
| 51 | + ensure_both_happen(|| { |
| 52 | + let a = f32x4::splat(std::hint::black_box(0.1)); |
| 53 | + let b = f32x4::splat(std::hint::black_box(0.2)); |
| 54 | + let c = std::hint::black_box(-a * b); |
| 55 | + let x = unsafe { simd_relaxed_fma(a, b, c) }; |
| 56 | + // Whether we fuse or not is a per-element decision, so sometimes these should be |
| 57 | + // the same and sometimes not. |
| 58 | + x[0] == x[1] |
| 59 | + }), |
| 60 | + "`simd_relaxed_fma` failed to be evaluated as both fused and unfused" |
| 61 | + ); |
| 62 | + |
| 63 | + assert!( |
| 64 | + ensure_both_happen(|| { |
| 65 | + let a = f64x4::splat(std::hint::black_box(0.1)); |
| 66 | + let b = f64x4::splat(std::hint::black_box(0.2)); |
| 67 | + let c = std::hint::black_box(-a * b); |
| 68 | + let x = unsafe { simd_relaxed_fma(a, b, c) }; |
| 69 | + // Whether we fuse or not is a per-element decision, so sometimes these should be |
| 70 | + // the same and sometimes not. |
| 71 | + x[0] == x[1] |
| 72 | + }), |
| 73 | + "`simd_relaxed_fma` failed to be evaluated as both fused and unfused" |
| 74 | + ); |
44 | 75 | } |
0 commit comments