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

Ian Romanick idr at freedesktop.org
Thu Jan 10 11:44:54 PST 2013


On 01/10/2013 10:57 AM, Chad Versace wrote:
> 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.

I was also not aware of this.

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

That sounds like a good plan.



More information about the mesa-dev mailing list