util: Make CLAMP turn NaN into MIN.
The previous implementation of CLAMP() allowed NaN to pass through
unscathed, by failing both comparisons. NaN isn't exactly a value
between MIN and MAX, which can break the assumptions of many callers.
This patch changes CLAMP to convert NaN to MIN, arbitrarily. Callers
that need NaN to be handled in a specific manner should probably open
code something, or use a macro specifically designed to do that.
Section 2.3.4.1 of the OpenGL 4.5 spec says:
"Any representable floating-point value is legal as input to a GL
command that requires floating-point data. The result of providing a
value that is not a floating-point number to such a command is
unspecified, but must not lead to GL interruption or termination.
In IEEE arithmetic, for example, providing a negative zero or a
denormalized number to a GL command yields predictable results,
while providing a NaN or an infinity yields unspecified results."
While CLAMP may apply to more than just GL inputs, it seems reasonable
to follow those rules, and allow MIN as an "unspecified result".
This prevents assertion failures in i965 when running the games
"XCOM: Enemy Unknown" and "XCOM: Enemy Within", which call
glTexEnv(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT,
-nan(0x7ffff3));
presumably unintentionally. i965 clamps the LOD bias to be in range,
and asserts that it's in the proper range when converting to fixed
point. NaN is not, so it crashed. We'd like to at least avoid that.
Reviewed-by: Roland Scheidegger <sroland@vmware.com>
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>