Say I had a really performance-critical loop in my program where I need to check if a point was inside a rectangle, but I know at compile time that the lower bounds are always going to be 0, like the following: (x >= 0 && y >= 0 && x < width && y < height)
Could I eliminate the first two comparisons by type-punning x and y to unsigned integers (for instance with something like reinterpret_cast<>()
or a union
in C++), since the sign bit would guarantee any negative number would turn into an unsigned int
large enough to fail the bounds check? If so, how would you go about implementing this in C++ or another language? Could you gain any performance improvement by doing this?