#![feature(test)] #![allow(bad_style)] extern crate test; use test::Bencher; use lokacore::*; use randomize::*; pub fn make_a_pcg32_somehow() -> PCG32 { let mut gen_state: [u64; 2] = [0, 0]; getrandom::getrandom(bytes_of_mut(&mut gen_state)).unwrap(); PCG32::seed(gen_state[0], gen_state[1]) } #[derive(Clone, Copy)] struct Vec2Plain { x: f32, y: f32, } impl Vec2Plain { fn add(self, rhs: Self) -> Self { Self { x: self.x + rhs.x, y: self.y + rhs.y, } } fn mul(self, rhs: Self) -> Self { Self { x: self.x * rhs.x, y: self.y * rhs.y, } } } #[derive(Clone, Copy)] struct Vec3Plain { x: f32, y: f32, z: f32, } impl Vec3Plain { fn add(self, rhs: Self) -> Self { Self { x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z, } } fn mul(self, rhs: Self) -> Self { Self { x: self.x * rhs.x, y: self.y * rhs.y, z: self.z * rhs.z, } } } #[derive(Clone, Copy)] struct Vec4Plain { x: f32, y: f32, z: f32, w: f32, } impl Vec4Plain { fn add(self, rhs: Self) -> Self { Self { x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z, w: self.w + rhs.w, } } fn mul(self, rhs: Self) -> Self { Self { x: self.x * rhs.x, y: self.y * rhs.y, z: self.z * rhs.z, w: self.w * rhs.w, } } } #[bench] fn bench_Vec2Plain_add(b: &mut Bencher) { let gen = &mut make_a_pcg32_somehow(); let mut v1 = Vec2Plain { x: f32::from_bits(gen.next_u32()), y: f32::from_bits(gen.next_u32()), }; let v2 = Vec2Plain { x: f32::from_bits(gen.next_u32()), y: f32::from_bits(gen.next_u32()), }; b.iter(|| { for _ in 0..1000 { v1 = test::black_box(v1).add(test::black_box(v2)); } }); println!("{}", v1.x); } #[bench] fn bench_Vec3Plain_add(b: &mut Bencher) { let gen = &mut make_a_pcg32_somehow(); let mut v1 = Vec3Plain { x: f32::from_bits(gen.next_u32()), y: f32::from_bits(gen.next_u32()), z: f32::from_bits(gen.next_u32()), }; let v2 = Vec3Plain { x: f32::from_bits(gen.next_u32()), y: f32::from_bits(gen.next_u32()), z: f32::from_bits(gen.next_u32()), }; b.iter(|| { for _ in 0..1000 { v1 = test::black_box(v1).add(test::black_box(v2)); } }); println!("{}", v1.x); } #[bench] fn bench_Vec4Plain_add(b: &mut Bencher) { let gen = &mut make_a_pcg32_somehow(); let mut v1 = Vec4Plain { x: f32::from_bits(gen.next_u32()), y: f32::from_bits(gen.next_u32()), z: f32::from_bits(gen.next_u32()), w: f32::from_bits(gen.next_u32()), }; let v2 = Vec4Plain { x: f32::from_bits(gen.next_u32()), y: f32::from_bits(gen.next_u32()), z: f32::from_bits(gen.next_u32()), w: f32::from_bits(gen.next_u32()), }; b.iter(|| { for _ in 0..1000 { v1 = test::black_box(v1).add(test::black_box(v2)); } }); println!("{}", v1.x); } #[bench] fn bench_Vec2Plain_mul(b: &mut Bencher) { let gen = &mut make_a_pcg32_somehow(); let mut v1 = Vec2Plain { x: f32::from_bits(gen.next_u32()), y: f32::from_bits(gen.next_u32()), }; let v2 = Vec2Plain { x: f32::from_bits(gen.next_u32()), y: f32::from_bits(gen.next_u32()), }; b.iter(|| { for _ in 0..1000 { v1 = test::black_box(v1).mul(test::black_box(v2)); } }); println!("{}", v1.x); } #[bench] fn bench_Vec3Plain_mul(b: &mut Bencher) { let gen = &mut make_a_pcg32_somehow(); let mut v1 = Vec3Plain { x: f32::from_bits(gen.next_u32()), y: f32::from_bits(gen.next_u32()), z: f32::from_bits(gen.next_u32()), }; let v2 = Vec3Plain { x: f32::from_bits(gen.next_u32()), y: f32::from_bits(gen.next_u32()), z: f32::from_bits(gen.next_u32()), }; b.iter(|| { for _ in 0..1000 { v1 = test::black_box(v1).mul(test::black_box(v2)); } }); println!("{}", v1.x); } #[bench] fn bench_Vec4Plain_mul(b: &mut Bencher) { let gen = &mut make_a_pcg32_somehow(); let mut v1 = Vec4Plain { x: f32::from_bits(gen.next_u32()), y: f32::from_bits(gen.next_u32()), z: f32::from_bits(gen.next_u32()), w: f32::from_bits(gen.next_u32()), }; let v2 = Vec4Plain { x: f32::from_bits(gen.next_u32()), y: f32::from_bits(gen.next_u32()), z: f32::from_bits(gen.next_u32()), w: f32::from_bits(gen.next_u32()), }; b.iter(|| { for _ in 0..1000 { v1 = test::black_box(v1).mul(test::black_box(v2)); } }); println!("{}", v1.x); }