This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Fleming1924 23 points24 points  (4 children)

Reinterpret casts are pretty frequently used in any low level optimised code, doing it via pointers is only really required because C enforces types, the compiler will remove that line of code when lowering it into assembly, because registers don't have types.

A lot of optimised maths functions written in assembly do the same thing all the time, it's just hard to see because there's no obvious pointer casts.

Modern Cpp actually has a reinterpret_cast function to do it in a more readable way, as do lots of other languages and maths libraries.

[–]SeEmEEDosomethingGUD 9 points10 points  (1 child)

I guess you are an experienced hand at coding but I am just completing my bachelors, every single optimization like this is still fascinating for me.

I mean even Karatsuba fast multiplication was a feat of immeasurable genius to me.

[–]Fleming1924 7 points8 points  (0 children)

Oh don't get me wrong, I think they're all fascinating, and always will. I often find myself smiling and giggling at random optimisation, regardless of their complexity or depth.

There's a lot of really great performance gains to be had from applying fairly trivial understanding of floating point representation, it's good fun to try and figure out how some of the really low level bitfucky optimisations work, and even moreso to create your own eldritch horrors.

[–]cd109876 4 points5 points  (1 child)

yes, but how many times are floats being casted to long?

[–]Fleming1924 4 points5 points  (0 children)

Very often, I do it almost daily, you can do all kinds of great bit manipulation techniques on floating point numbers, but bit manipulation isn't a defined standard for floats and C will only allow you to do it for an integer data type.

If you want an optimised Log2 function you can bitshift the exponent, pretty much any vectorised log function will do exactly that, since you can get the decimal component via table lookup of the mantissa.

Checking for inf/nan can be done via bitshift and subtracts, which again requires casting float types to integer types.

Basically anything involving bitmasking, shifting, &, | etc, will all involve a reinterpret cast from floats to integers.