-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
97 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,75 @@ | ||
#![feature(core_intrinsics)] | ||
#![feature(core_intrinsics, portable_simd)] | ||
use std::intrinsics::simd::simd_relaxed_fma; | ||
use std::intrinsics::{fmuladdf32, fmuladdf64}; | ||
use std::simd::prelude::*; | ||
|
||
fn main() { | ||
let mut saw_zero = false; | ||
let mut saw_nonzero = false; | ||
fn ensure_both_happen(f: impl Fn() -> bool) -> bool { | ||
let mut saw_true = false; | ||
let mut saw_false = false; | ||
for _ in 0..50 { | ||
let a = std::hint::black_box(0.1_f64); | ||
let b = std::hint::black_box(0.2); | ||
let c = std::hint::black_box(-a * b); | ||
// It is unspecified whether the following operation is fused or not. The | ||
// following evaluates to 0.0 if unfused, and nonzero (-1.66e-18) if fused. | ||
let x = unsafe { fmuladdf64(a, b, c) }; | ||
if x == 0.0 { | ||
saw_zero = true; | ||
let b = f(); | ||
if b { | ||
saw_true = true; | ||
} else { | ||
saw_nonzero = true; | ||
saw_false = true; | ||
} | ||
if saw_true && saw_false { | ||
return true; | ||
} | ||
} | ||
false | ||
} | ||
|
||
fn main() { | ||
assert!( | ||
saw_zero && saw_nonzero, | ||
ensure_both_happen(|| { | ||
let a = std::hint::black_box(0.1_f64); | ||
let b = std::hint::black_box(0.2); | ||
let c = std::hint::black_box(-a * b); | ||
// It is unspecified whether the following operation is fused or not. The | ||
// following evaluates to 0.0 if unfused, and nonzero (-1.66e-18) if fused. | ||
let x = unsafe { fmuladdf64(a, b, c) }; | ||
x == 0.0 | ||
}), | ||
"`fmuladdf64` failed to be evaluated as both fused and unfused" | ||
); | ||
|
||
let mut saw_zero = false; | ||
let mut saw_nonzero = false; | ||
for _ in 0..50 { | ||
let a = std::hint::black_box(0.1_f32); | ||
let b = std::hint::black_box(0.2); | ||
let c = std::hint::black_box(-a * b); | ||
// It is unspecified whether the following operation is fused or not. The | ||
// following evaluates to 0.0 if unfused, and nonzero (-8.1956386e-10) if fused. | ||
let x = unsafe { fmuladdf32(a, b, c) }; | ||
if x == 0.0 { | ||
saw_zero = true; | ||
} else { | ||
saw_nonzero = true; | ||
} | ||
} | ||
assert!( | ||
saw_zero && saw_nonzero, | ||
ensure_both_happen(|| { | ||
let a = std::hint::black_box(0.1_f32); | ||
let b = std::hint::black_box(0.2); | ||
let c = std::hint::black_box(-a * b); | ||
// It is unspecified whether the following operation is fused or not. The | ||
// following evaluates to 0.0 if unfused, and nonzero (-8.1956386e-10) if fused. | ||
let x = unsafe { fmuladdf32(a, b, c) }; | ||
x == 0.0 | ||
}), | ||
"`fmuladdf32` failed to be evaluated as both fused and unfused" | ||
); | ||
|
||
assert!( | ||
ensure_both_happen(|| { | ||
let a = f32x4::splat(std::hint::black_box(0.1)); | ||
let b = f32x4::splat(std::hint::black_box(0.2)); | ||
let c = std::hint::black_box(-a * b); | ||
let x = unsafe { simd_relaxed_fma(a, b, c) }; | ||
// Whether we fuse or not is a per-element decision, so sometimes these should be | ||
// the same and sometimes not. | ||
x[0] == x[1] | ||
}), | ||
"`simd_relaxed_fma` failed to be evaluated as both fused and unfused" | ||
); | ||
|
||
assert!( | ||
ensure_both_happen(|| { | ||
let a = f64x4::splat(std::hint::black_box(0.1)); | ||
let b = f64x4::splat(std::hint::black_box(0.2)); | ||
let c = std::hint::black_box(-a * b); | ||
let x = unsafe { simd_relaxed_fma(a, b, c) }; | ||
// Whether we fuse or not is a per-element decision, so sometimes these should be | ||
// the same and sometimes not. | ||
x[0] == x[1] | ||
}), | ||
"`simd_relaxed_fma` failed to be evaluated as both fused and unfused" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters