Alex Converse <[email protected]> writes:

> On Sat, Jun 18, 2011 at 3:11 PM, Alex Converse <[email protected]> 
> wrote:
>> On Sat, Jun 18, 2011 at 2:03 PM, Alex Converse <[email protected]> 
>> wrote:
> [...]
>>>
>>> Because signed integer representation (two's complement; ones's
>>> complement; sign magnitude) is implementation defined this also needs
>>> to to be implementation defined.
>>>
>>> See also:
>>>
>>> 6.5.4
>>>> Some operators (the unary operator ~, and the binary operators <<, >>, &, 
>>>> ^, and |,
>>>> collectively described as bitwise operators) are required to have operands 
>>>> that have
>>>> integer type. These operators yield values that depend on the internal 
>>>> representations of
>>>> integers, and have implementation-defined and undefined aspects for signed 
>>>> types.
>>>
>>> Yet we allow all of these
>>>
>>
>> Feel free to propose a better solution.
>
> ping?

A safe method (on 2's complement machines, which we assume) is to use a
union:

static inline int u2s(unsigned x)
{
    union { unsigned u; int i; } v = { x };
    return v.i;
}

GCC and a few other compilers I tested compile this to nothing, as expected.

-- 
Måns Rullgård
[email protected]
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to