[FFmpeg-devel] pre discussion around Blackfin dct_quantize_bfin routine

Måns Rullgård mans
Tue Jun 12 18:15:16 CEST 2007


Marc Hoffman wrote:
> Hey Mans,
>
> On 6/12/07, M?ns Rullg?rd <mans at mansr.com> wrote:
>>
>> Marc Hoffman wrote:
>>
>> > +static inline uint64_t read_time(void)
>> > +{
>> > +    union {
>> > +        struct {
>> > +            unsigned lo;
>> > +            unsigned hi;
>> > +        } p;
>> > +        unsigned long long c;
>> > +    } t;
>> > +    asm volatile ("%0=cycles; %1=cycles2;" : "=d" (t.p.lo), "=d" (t.p.hi));
>> > +    return t.c;
>> > +}
>>
>> Using unions like that isn't allowed by the C standard.  It usually works,
>> but there are no guarantees.  In fact, I've seen such things break in nasty
>> ways.
>>
>
> O really, I have never seen such a problem interesting.  Anyways I'm
> sure it exists, however this is for a specific machine which I know
> this works for.

It's the compiler, not the machine, that can cause trouble here.  The
optimiser is allowed to treat the different members of a union as
distinct objects such that reads from one do not depend on writes to
another.  In this case, it could result in t.c being read and placed
wherever the return value goes before hi and lo are written.

-- 
M?ns Rullg?rd
mans at mansr.com




More information about the ffmpeg-devel mailing list