Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #![feature(test)]
  2. extern crate test;
  3.  
  4. #[cfg(target_arch = "x86")]
  5. use std::arch::x86::*;
  6. #[cfg(target_arch = "x86_64")]
  7. use std::arch::x86_64::*;
  8.  
  9. use std::time::Instant;
  10.  
  11. struct Vector(f32, f32, f32, f32);
  12. impl Vector {
  13. #[inline(always)]
  14. fn new(x: f32, y: f32, z: f32, w: f32) -> Vector {
  15. Vector(x, y, z, w)
  16. }
  17. #[inline(always)]
  18. fn mul(a: &Vector, b: &Vector) -> Vector {
  19. Vector(a.0 * b.0, a.1 * b.1, a.2 * b.2, a.3 * b.3)
  20. }
  21. #[inline(always)]
  22. fn dot(a: &Vector, b: &Vector) -> f32 {
  23. a.0 * b.0 +
  24. a.1 * b.1 +
  25. a.2 * b.2 +
  26. a.3 * b.3
  27. }
  28. }
  29. struct Vec4(__m128); impl Vec4 {
  30. #[inline(always)]
  31. fn new(x: f32, y: f32, z: f32, w: f32) -> Vec4 {
  32. unsafe {
  33. Vec4(_mm_set_ps(x, y, z, w))
  34. }
  35. }
  36. #[inline(always)]
  37. fn mul(a: &Vec4, b: &Vec4) -> Vec4 {
  38. unsafe {
  39. Vec4(_mm_mul_ps(a.0, b.0))
  40. }
  41. }
  42. #[inline(always)]
  43. fn dot(a: &Vec4, b: &Vec4) -> f32 {
  44. unsafe {
  45. let mut x = _mm_mul_ps(a.0, b.0);
  46. x = _mm_hadd_ps(x, x);
  47. x = _mm_hadd_ps(x, x);
  48. _mm_cvtss_f32(x)
  49. }
  50. }
  51. }
  52.  
  53. fn main() {
  54. let iterations = 128_000_000u64;
  55. // f32; 8
  56. {
  57. let a = Vector::new(1.0, 0.0, 0.0, 0.0);
  58. let b = Vector::new(0.0, 1.0, 0.0, 0.0);
  59. let now = Instant::now();
  60.  
  61. for _ in 0..iterations {
  62. let x = Vector::mul(&a, &b);
  63. test::black_box(x);
  64.  
  65. let d = Vector::dot(&a, &b);
  66. test::black_box(d);
  67. }
  68. println!("[f32: 4]: {}", now.elapsed().as_millis());
  69. }
  70. // simd
  71. {
  72. let a = Vec4::new(1.0, 0.0, 0.0, 0.0);
  73. let b = Vec4::new(0.0, 1.0, 0.0, 0.0);
  74. let now = Instant::now();
  75.  
  76. for _ in 0..iterations {
  77. let x = Vec4::mul(&a, &b);
  78. test::black_box(x);
  79.  
  80. let d = Vec4::dot(&a, &b);
  81. test::black_box(d);
  82. }
  83. println!("__m128 : {}", now.elapsed().as_millis());
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement