From c5d4cb9602ed18907d321f2d61f30e1159f58dbf Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 6 Dec 2022 18:38:44 +0100 Subject: sqrt lookup --- evc/src/pixel.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'evc/src/pixel.rs') diff --git a/evc/src/pixel.rs b/evc/src/pixel.rs index b3ef841..4fb6772 100644 --- a/evc/src/pixel.rs +++ b/evc/src/pixel.rs @@ -21,12 +21,32 @@ impl Ser for Pixel { impl Pixel { pub const BLACK: Pixel = Pixel { r: 0, g: 0, b: 0 }; #[inline] - pub fn distance(a: Pixel, b: Pixel) -> f64 { + pub fn distance(a: Pixel, b: Pixel) -> usize { let (rd, gd, bd) = ( - a.r.abs_diff(b.r) as f64, - a.r.abs_diff(b.r) as f64, - a.r.abs_diff(b.r) as f64, + a.r.abs_diff(b.r) as usize, + a.r.abs_diff(b.r) as usize, + a.r.abs_diff(b.r) as usize, ); - (rd * rd + gd * gd + bd * bd).sqrt() + SQRT[rd + gd + bd] } } + +const SQRT: [usize; 256 * 3] = gen_sqrt_lookup(); + +const fn gen_sqrt_lookup() -> [usize; N] { + let mut arr = [0; N]; + let mut i = 0; + while i < N { + arr[i] = sqrt(i as f32) as usize; + i += 1; + } + arr +} + +const fn sqrt(x: f32) -> f32 { + let a = 1.0; + let a = (a + x / a) * 0.5; + let a = (a + x / a) * 0.5; + let a = (a + x / a) * 0.5; + a +} -- cgit v1.2.3-70-g09d2