[Piglit] [PATCH piglit v2] teximage-color: Fix un_to_float for 32-bit builds

Neil Roberts neil at linux.intel.com
Wed Sep 17 08:06:47 PDT 2014

Actually I think this might be a slightly cleaner way to do the shift
because it doesn't depend on any particular size of unsigned int and
there are fewer ~s.

- Neil

------- >8 --------------- (use git am --scissors to automatically chop here)

The un_to_float function was trying to get the maximum value given a
number of bits by shifting ~0ul by the number of bits. For the
GL_UNSIGNED_INT type this function was also being used to get a
maximum value for a 32-bit quantity. However on a 32-bit build this
would mean that it is shifting a 32-bit integer (unsigned long is
32-bit) by 32 bits. The C spec leaves it undefined what happens if you
do a shift that is greater than the number of bits in the type. GCC
takes advantage of that to make the shift a no-op so the maximum was
ending up as zero and the test fails.

This patch makes it shift ~0u in the other direction so that it
doesn't matter what size unsigned int is and it won't try to shift by

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=83695
 tests/texturing/teximage-colors.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/texturing/teximage-colors.c b/tests/texturing/teximage-colors.c
index 815ef4d..0afb4b1 100644
--- a/tests/texturing/teximage-colors.c
+++ b/tests/texturing/teximage-colors.c
@@ -189,7 +189,7 @@ valid_combination(GLenum format, GLenum type)
 static float
 un_to_float(unsigned char bits, unsigned int color)
-	unsigned int max = ~(~0ul << bits);
+	unsigned int max = ~0u >> (sizeof max * 8 - bits);
 	return (float)color / (float)max;

More information about the Piglit mailing list