[Mesa-dev] [PATCH 04/10] glsl: Evaluate constant GLSL ES 3.00 pack/unpack operations

Chad Versace chad.versace at linux.intel.com
Thu Jan 10 10:57:15 PST 2013


Oliver,

Could you please clarify on the claim below in the comments to
bitcast_u2f: "[memcpy] is the only [method] supported by gcc" for
bitcasting uint to float.

On 01/10/2013 10:30 AM, Ian Romanick wrote:
> On 01/10/2013 12:10 AM, Chad Versace wrote:
>> That is, evaluate constant expressions of the following functions:
>>    packSnorm2x16  unpackSnorm2x16
>>    packUnorm2x16  unpackUnorm2x16
>>    packHalf2x16   unpackHalf2x16
> 
> I believe that there is already code somewhere in Mesa for doing float/half
> conversion.  Could we just re-use that?
> 
>> Signed-off-by: Chad Versace <chad.versace at linux.intel.com>
>> ---
>>   src/glsl/ir_constant_expression.cpp | 362 ++++++++++++++++++++++++++++++++++++
>>   1 file changed, 362 insertions(+)

I should have known that float/half conversions existed.

I just reviewed _mesa_half_to_float and _mesa_float_to_half, and they look like
suitable replacements except for one aspect: they use a union to cast between
uint and float, yet ir_constant_expression.cpp:bitcast_u2f contains this
comment:

/* This method is the only one supported by gcc.  Unions in particular
 * are iffy, and read-through-converted-pointer is killed by strict
 * aliasing.  OTOH, the compiler sees through the memcpy, so the
 * resulting asm is reasonable.
 */
static float
bitcast_u2f(unsigned int u)
{
   assert(sizeof(float) == sizeof(unsigned int));
   float f;
   memcpy(&f, &u, sizeof(f));
   return f;
}

The claim that memcpy "is the only [method] supported by gcc" for float/uint
conversion is news to me. Does that seem reasonable to you? I've CC'd the
author of the comment for clarification.

If the claim holds up, then I can easily update _mesa_float_to_half and
_mesa_half_to_float to use these functions.


More information about the mesa-dev mailing list