teximage-colors: fix divide by zero issue

Submitted by Brian Paul on Sept. 17, 2014, 5:14 p.m.

Details

Message ID 1410974090-32066-1-git-send-email-brianp@vmware.com
State New
Headers show

Not browsing as part of any series.

Commit Message

Brian Paul Sept. 17, 2014, 5:14 p.m.
Not exactly sure why, but the max = ~(~0ul << bits) assignment resulted
in max=0 when bits==32 with MinGW.  This caused us to do a divide by zero
and many of the tests failed.

Special-case the 32-bit case and change the arithmetic to be a little
simpler (to me at least).  Change sn_to_float() to be similar.
---
 tests/texturing/teximage-colors.c |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/tests/texturing/teximage-colors.c b/tests/texturing/teximage-colors.c
index 815ef4d..b350b99 100644
--- a/tests/texturing/teximage-colors.c
+++ b/tests/texturing/teximage-colors.c
@@ -189,14 +189,21 @@  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;
+	if (bits == 32) {
+		max = 0xffffffffU;
+	}
+	else {
+		assert(bits < 32);
+		max = (1 << bits) - 1;
+	}
 	return (float)color / (float)max;
 }
 
 static float
 sn_to_float(unsigned char bits, int color)
 {
-	int max = ~(~0 << (bits-1));
+	int max = (1 << (bits - 1)) - 1;
 	if (color < -max)
 		color = -max;
 	return (float)color / (float)max;

Comments

Ugh, I see this has already been covered today.  I don't have a strong 
preference for which fix is used.

-Brian

On 09/17/2014 11:14 AM, Brian Paul wrote:
> Not exactly sure why, but the max = ~(~0ul << bits) assignment resulted
> in max=0 when bits==32 with MinGW.  This caused us to do a divide by zero
> and many of the tests failed.
>
> Special-case the 32-bit case and change the arithmetic to be a little
> simpler (to me at least).  Change sn_to_float() to be similar.
> ---
>   tests/texturing/teximage-colors.c |   11 +++++++++--
>   1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/tests/texturing/teximage-colors.c b/tests/texturing/teximage-colors.c
> index 815ef4d..b350b99 100644
> --- a/tests/texturing/teximage-colors.c
> +++ b/tests/texturing/teximage-colors.c
> @@ -189,14 +189,21 @@ 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;
> +	if (bits == 32) {
> +		max = 0xffffffffU;
> +	}
> +	else {
> +		assert(bits < 32);
> +		max = (1 << bits) - 1;
> +	}
>   	return (float)color / (float)max;
>   }
>
>   static float
>   sn_to_float(unsigned char bits, int color)
>   {
> -	int max = ~(~0 << (bits-1));
> +	int max = (1 << (bits - 1)) - 1;
>   	if (color < -max)
>   		color = -max;
>   	return (float)color / (float)max;
>