From: Josh V. <ho...@na...> - 2001-06-15 02:28:41
|
Here's a patch that fixes some type alias bugs that show up with -fstrict-aliasing on gcc (which will be the default for gcc 3.0). For the most part the patch just changes instances of *(xtype *)&y which could possibly read from the address &y before the value of y has been written out to memory due to C aliasing rules, with ((union { xtype xt; ytype yt;} *)&y)->xt which is safe for gcc since the created pointer can alias both xtype and ytype. Hopefully other compilers will also assume union pointers can alias all of their member types. Josh Index: src/mmath.h =================================================================== RCS file: /cvsroot/mesa3d/Mesa/src/mmath.h,v retrieving revision 1.38 diff -u -r1.38 mmath.h --- src/mmath.h 2001/06/04 14:33:33 1.38 +++ src/mmath.h 2001/06/15 00:59:44 @@ -206,6 +206,8 @@ +#define GET_FLOAT_BITS(x) ((union { GLfloat f; GLuint i; } *)&(x))->i + /* * Float -> Int conversions (rounding, floor, ceiling) */ @@ -406,7 +408,9 @@ #define CLAMPED_FLOAT_TO_UBYTE(b, f) \ UNCLAMPED_FLOAT_TO_UBYTE(b, f) -#define COPY_FLOAT( dst, src ) *(GLuint *)&(dst) = *(GLuint *)&(src) +#define COPY_FLOAT( dst, src ) \ + ((union { GLuint i; GLfloat f; } *)&(dst))->i = \ + ((union { GLuint i; GLfloat f; } *)&(src))->i #else /* USE_IEEE */ Index: src/texutil.c =================================================================== RCS file: /cvsroot/mesa3d/Mesa/src/texutil.c,v retrieving revision 1.24 diff -u -r1.24 texutil.c --- src/texutil.c 2001/05/02 21:02:38 1.24 +++ src/texutil.c 2001/06/15 00:59:45 @@ -415,7 +415,7 @@ dst = (s >> 1) | ((s & 1) << 15); } #define CONVERT_TEXEL_DWORD( dst, src ) \ - { const GLuint s = *(GLuint *)src; \ + { const GLuint s = ((union { GLuint i; GLushort s; } *)src)->i; \ dst = (((s & 0xfffefffe) >> 1) | \ ((s & 0x00010001) << 15)); } Index: src/tnl/t_imm_api.c =================================================================== RCS file: /cvsroot/mesa3d/Mesa/src/tnl/t_imm_api.c,v retrieving revision 1.15 diff -u -r1.15 t_imm_api.c --- src/tnl/t_imm_api.c 2001/06/04 16:09:28 1.15 +++ src/tnl/t_imm_api.c 2001/06/15 00:59:48 @@ -543,12 +543,15 @@ #define NORMALF( x, y, z ) \ { \ GLuint count; \ - GLint *normal; \ + typedef union { GLfloat f; GLint i; } fi_type; \ + fi_type *normal; \ GET_IMMEDIATE; \ count = IM->Count; \ IM->Flag[count] |= VERT_NORM; \ - normal = (GLint *)IM->Normal[count]; \ - ASSIGN_3V(normal, *(int*)&(x), *(int*)&(y), *(int*)&(z)); \ + normal = (fi_type *)IM->Normal[count]; \ + normal[0].i = ((fi_type *)&(x))->i; \ + normal[1].i = ((fi_type *)&(y))->i; \ + normal[2].i = ((fi_type *)&(z))->i; \ } #else #define NORMALF NORMAL @@ -616,18 +619,19 @@ } #if defined(USE_IEEE) -#define TEXCOORD2F(s,t) \ -{ \ - GLuint count; \ - GLint *tc; \ - GET_IMMEDIATE; \ - count = IM->Count; \ - IM->Flag[count] |= VERT_TEX0; \ - tc = (GLint *)IM->TexCoord0[count]; \ - tc[0] = *(GLint *)&(s); \ - tc[1] = *(GLint *)&(t); \ - tc[2] = 0; \ - tc[3] = IEEE_ONE; \ +#define TEXCOORD2F(s,t) \ +{ \ + GLuint count; \ + typedef union { GLfloat f; GLint i; } fi_type; \ + fi_type *tc; \ + GET_IMMEDIATE; \ + count = IM->Count; \ + IM->Flag[count] |= VERT_TEX0; \ + tc = (fi_type *)IM->TexCoord0[count]; \ + tc[0].i = ((fi_type *)&(s))->i; \ + tc[1].i = ((fi_type *)&(t))->i; \ + tc[2].i = 0; \ + tc[3].i = IEEE_ONE; \ } #else #define TEXCOORD2F TEXCOORD2 @@ -721,53 +725,56 @@ } #if defined(USE_IEEE) -#define VERTEX2F(IM, x, y) \ -{ \ - GLuint count = IM->Count++; \ - GLint *dest = (GLint *)IM->Obj[count]; \ - IM->Flag[count] |= VERT_OBJ; \ - dest[0] = *(GLint *)&(x); \ - dest[1] = *(GLint *)&(y); \ - dest[2] = 0; \ - dest[3] = IEEE_ONE; \ +#define VERTEX2F(IM, x, y) \ +{ \ + GLuint count = IM->Count++; \ + typedef union { GLfloat f; GLint i; } fi_type; \ + fi_type *dest = (fi_type *)IM->Obj[count]; \ + IM->Flag[count] |= VERT_OBJ; \ + dest[0].i = ((fi_type *)&(x))->i; \ + dest[1].i = ((fi_type *)&(y))->i; \ + dest[2].i = 0; \ + dest[3].i = IEEE_ONE; \ /* ASSERT(IM->Flag[IM->Count]==0); */ \ if (count == IMM_MAXDATA - 1) \ - _tnl_flush_immediate( IM ); \ + _tnl_flush_immediate( IM ); \ } #else #define VERTEX2F VERTEX2 #endif #if defined(USE_IEEE) -#define VERTEX3F(IM, x, y, z) \ -{ \ - GLuint count = IM->Count++; \ - GLint *dest = (GLint *)IM->Obj[count]; \ - IM->Flag[count] |= VERT_OBJ_23; \ - dest[0] = *(GLint *)&(x); \ - dest[1] = *(GLint *)&(y); \ - dest[2] = *(GLint *)&(z); \ - dest[3] = IEEE_ONE; \ -/* ASSERT(IM->Flag[IM->Count]==0); */ \ +#define VERTEX3F(IM, x, y, z) \ +{ \ + GLuint count = IM->Count++; \ + typedef union { GLfloat f; GLint i; } fi_type; \ + fi_type *dest = (fi_type *)IM->Obj[count]; \ + IM->Flag[count] |= VERT_OBJ_23; \ + dest[0].i = ((fi_type *)&(x))->i; \ + dest[1].i = ((fi_type *)&(y))->i; \ + dest[2].i = ((fi_type *)&(z))->i; \ + dest[3].i = IEEE_ONE; \ +/* ASSERT(IM->Flag[IM->Count]==0); */ \ if (count == IMM_MAXDATA - 1) \ - _tnl_flush_immediate( IM ); \ + _tnl_flush_immediate( IM ); \ } #else #define VERTEX3F VERTEX3 #endif #if defined(USE_IEEE) -#define VERTEX4F(IM, x, y, z, w) \ -{ \ - GLuint count = IM->Count++; \ - GLint *dest = (GLint *)IM->Obj[count]; \ - IM->Flag[count] |= VERT_OBJ_234; \ - dest[0] = *(GLint *)&(x); \ - dest[1] = *(GLint *)&(y); \ - dest[2] = *(GLint *)&(z); \ - dest[3] = *(GLint *)&(w); \ +#define VERTEX4F(IM, x, y, z, w) \ +{ \ + GLuint count = IM->Count++; \ + typedef union { GLfloat f; GLint i; } fi_type; \ + fi_type *dest = (fi_type *)IM->Obj[count]; \ + IM->Flag[count] |= VERT_OBJ_234; \ + dest[0].i = ((fi_type *)&(x))->i; \ + dest[1].i = ((fi_type *)&(y))->i; \ + dest[2].i = ((fi_type *)&(z))->i; \ + dest[3].i = ((fi_type *)&(w))->i; \ if (count == IMM_MAXDATA - 1) \ - _tnl_flush_immediate( IM ); \ + _tnl_flush_immediate( IM ); \ } #else #define VERTEX4F VERTEX4 @@ -886,12 +893,13 @@ GLuint texunit = target - GL_TEXTURE0_ARB; \ if (texunit < IM->MaxTextureUnits) { \ GLuint count = IM->Count; \ - GLint *tc = (GLint *)IM->TexCoord[texunit][count]; \ + typedef union { GLfloat f; GLint i; } fi_type; \ + fi_type *tc = (fi_type *)IM->TexCoord[texunit][count]; \ IM->Flag[count] |= VERT_TEX(texunit); \ - tc[0] = *(int *)&(s); \ - tc[1] = *(int *)&(t); \ - tc[2] = 0; \ - tc[3] = IEEE_ONE; \ + tc[0].i = ((fi_type *)&(s))->i; \ + tc[1].i = ((fi_type *)&(t))->i; \ + tc[2].i = 0; \ + tc[3].i = IEEE_ONE; \ } \ } #else Index: src/tnl/t_vb_render.c =================================================================== RCS file: /cvsroot/mesa3d/Mesa/src/tnl/t_vb_render.c,v retrieving revision 1.20 diff -u -r1.20 t_vb_render.c --- src/tnl/t_vb_render.c 2001/05/11 15:53:06 1.20 +++ src/tnl/t_vb_render.c 2001/06/15 00:59:48 @@ -69,8 +69,8 @@ #if defined(USE_IEEE) -#define NEGATIVE(x) ((*(GLuint *)&x) & (1<<31)) -#define DIFFERENT_SIGNS(x,y) (((*(GLuint *)&x)^(*(GLuint *)&y)) & (1<<31)) +#define NEGATIVE(x) (GET_FLOAT_BITS(x) & (1<<31)) +#define DIFFERENT_SIGNS(x,y) ((GET_FLOAT_BITS(x) ^ GET_FLOAT_BITS(y)) & (1<<31)) #else #define NEGATIVE(x) (x < 0) #define DIFFERENT_SIGNS(x,y) (x * y <= 0 && x - y != 0) |
From: Klaus N. <kl...@ma...> - 2001-06-15 09:54:00
|
Hi, Some time ago I wrote saying that I had problems with the square-root functions in the file 'src/mmath.c' when compiling with '-fstrict-aliasing' (on RedHat7.0), so I guess that a similar patch as Josh's should be applied to 'mmath.c' in lines like float f; unsigned int *fi = (unsigned int *)&f; /* to access the bits of a float in */ /* C quickly we must misuse pointers */ Greetings, Klaus |
From: Brian P. <br...@va...> - 2001-06-15 15:24:47
|
Klaus Niederkrueger wrote: > > Hi, > > Some time ago I wrote saying that I had problems with the square-root > functions in the file 'src/mmath.c' when compiling with > '-fstrict-aliasing' (on RedHat7.0), so I guess that a similar patch as > Josh's should be applied to 'mmath.c' in lines like > > float f; > unsigned int *fi = (unsigned int *)&f; > /* to access the bits of a float in */ > /* C quickly we must misuse pointers */ Can you try doing that on your system? Let me know if a change is needed. -Brian |
From: Josh V. <ho...@na...> - 2001-06-15 19:57:51
|
Brian Paul <br...@va...> writes: > Klaus Niederkrueger wrote: > > > > Hi, > > > > Some time ago I wrote saying that I had problems with the square-root > > functions in the file 'src/mmath.c' when compiling with > > '-fstrict-aliasing' (on RedHat7.0), so I guess that a similar patch as > > Josh's should be applied to 'mmath.c' in lines like > > > > float f; > > unsigned int *fi = (unsigned int *)&f; > > /* to access the bits of a float in */ > > /* C quickly we must misuse pointers */ > > Can you try doing that on your system? Let me know if a change is > needed. How does FAST_MATH get defined? I was going to fix that one until I realized it wasn't even being compiled on my system. I can't find any Makefile targets that define it. Josh |
From: Brian P. <br...@va...> - 2001-06-15 15:23:45
|
Josh Vanderhoof wrote: > > Here's a patch that fixes some type alias bugs that show up with > -fstrict-aliasing on gcc (which will be the default for gcc 3.0). For > the most part the patch just changes instances of > > *(xtype *)&y > > which could possibly read from the address &y before the value of y > has been written out to memory due to C aliasing rules, with > > ((union { xtype xt; ytype yt;} *)&y)->xt > > which is safe for gcc since the created pointer can alias both xtype > and ytype. Hopefully other compilers will also assume union pointers > can alias all of their member types. I've applied the patch. I put the typedef for fi_type in glheader.h so it can be easily used anywhere. -Brian |