by chias · 6y ago · 0 comments
That said, I can't speak to whether the Rust compiler wouldn't just optimize that away -- it seems like unrolling exponentiation into multiplication for small constant powers would be a very safe and easy thing to do.
Not just small constant powers either. I tried .powi(1000000) and it compiled into a sequence of 25 vmulss instructions.
I suspect then that defining it at x*x is just just because it's easier to type than x.powi(2).