Fastest way to convert a signed float to an integer, with rounding?
I guess this is more of a general performance question than something related to rust, but I just thought I'd ask here.
I have a program that has a lot of f32.round() as i8, so much so that roundf32 shows up a significant amount in linux perf profiles. I started thinking about other ways I could do this conversion, one idea I've had so far has been:
(value - 0.5 + ((value >= 0.0) as u8 as f32)) as i8
I've benchmarked this and it's over twice as fast on my machine (278.56us for rounding and casting 100,000 floats vs 120.22us for this optimization). It feels a little imprecise though, and could perhaps be faster. Does anyone know of a faster solution or have any other ideas?