-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #94452 - workingjubilee:sync-simd-bitmasks, r=working…
…jubilee Sync portable-simd for bitmasks &c. In the ideal case, where everything works easily and nothing has to be rearranged, it is as simple as: - `git subtree pull -P library/portable-simd https://github.com/rust-lang/portable-simd - ${branch}` - write the commit message - `python x.py test --stage 1` to make sure it runs - `git push` to your PR-to-rustc branch If anything borks up this flow, you can fix it with sufficient git wizardry but you are usually better off going back to the source, fixing it, and starting over, before you open the PR. r? `@calebzulawski`
- Loading branch information
Showing
21 changed files
with
438 additions
and
123 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
library/portable-simd/crates/core_simd/examples/spectral_norm.rs
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#![feature(portable_simd)] | ||
|
||
use core_simd::simd::*; | ||
|
||
fn a(i: usize, j: usize) -> f64 { | ||
((i + j) * (i + j + 1) / 2 + i + 1) as f64 | ||
} | ||
|
||
fn mult_av(v: &[f64], out: &mut [f64]) { | ||
assert!(v.len() == out.len()); | ||
assert!(v.len() % 2 == 0); | ||
|
||
for (i, out) in out.iter_mut().enumerate() { | ||
let mut sum = f64x2::splat(0.0); | ||
|
||
let mut j = 0; | ||
while j < v.len() { | ||
let b = f64x2::from_slice(&v[j..]); | ||
let a = f64x2::from_array([a(i, j), a(i, j + 1)]); | ||
sum += b / a; | ||
j += 2 | ||
} | ||
*out = sum.horizontal_sum(); | ||
} | ||
} | ||
|
||
fn mult_atv(v: &[f64], out: &mut [f64]) { | ||
assert!(v.len() == out.len()); | ||
assert!(v.len() % 2 == 0); | ||
|
||
for (i, out) in out.iter_mut().enumerate() { | ||
let mut sum = f64x2::splat(0.0); | ||
|
||
let mut j = 0; | ||
while j < v.len() { | ||
let b = f64x2::from_slice(&v[j..]); | ||
let a = f64x2::from_array([a(j, i), a(j + 1, i)]); | ||
sum += b / a; | ||
j += 2 | ||
} | ||
*out = sum.horizontal_sum(); | ||
} | ||
} | ||
|
||
fn mult_atav(v: &[f64], out: &mut [f64], tmp: &mut [f64]) { | ||
mult_av(v, tmp); | ||
mult_atv(tmp, out); | ||
} | ||
|
||
pub fn spectral_norm(n: usize) -> f64 { | ||
assert!(n % 2 == 0, "only even lengths are accepted"); | ||
|
||
let mut u = vec![1.0; n]; | ||
let mut v = u.clone(); | ||
let mut tmp = u.clone(); | ||
|
||
for _ in 0..10 { | ||
mult_atav(&u, &mut v, &mut tmp); | ||
mult_atav(&v, &mut u, &mut tmp); | ||
} | ||
(dot(&u, &v) / dot(&v, &v)).sqrt() | ||
} | ||
|
||
fn dot(x: &[f64], y: &[f64]) -> f64 { | ||
// This is auto-vectorized: | ||
x.iter().zip(y).map(|(&x, &y)| x * y).sum() | ||
} | ||
|
||
#[cfg(test)] | ||
#[test] | ||
fn test() { | ||
assert_eq!(&format!("{:.9}", spectral_norm(100)), "1.274219991"); | ||
} | ||
|
||
fn main() { | ||
// Empty main to make cargo happy | ||
} |
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
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
Oops, something went wrong.