Hi,

On Sat, Jul 21, 2012 at 5:19 PM, Måns Rullgård <m...@mansr.com> wrote:
> "Ronald S. Bultje" <rsbul...@gmail.com> writes:
>
>> From: "Ronald S. Bultje" <rsbul...@gmail.com>
>>
>> This removes some code duplication between the 3 different versions,
>> and aligns brackets in such a way that it is now possible to pull
>> this code through a naive pre-processor that doesn't necessarily have
>> to be aware of compiler-macros.
>> ---
>>  libavcodec/h264.c |   36 ++++++++++++++++++++----------------
>>  1 file changed, 20 insertions(+), 16 deletions(-)
>>
>> diff --git a/libavcodec/h264.c b/libavcodec/h264.c
>> index a4afcc8..20fa7c3 100644
>> --- a/libavcodec/h264.c
>> +++ b/libavcodec/h264.c
>> @@ -178,30 +178,34 @@ const uint8_t *ff_h264_decode_nal(H264Context *h, 
>> const uint8_t *src,
>>  #if HAVE_FAST_UNALIGNED
>>  #if HAVE_FAST_64BIT
>>  #define RS 7
>> -    for (i = 0; i + 1 < length; i += 9) {
>> -        if (!((~AV_RN64A(src + i) &
>> -               (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
>> +#define MASKCHECK \
>> +        if (!((~AV_RN64A(src + i) & \
>> +               (AV_RN64A(src + i) - 0x0100010001000101ULL)) & \
>>                0x8000800080008080ULL))
>>  #else
>>  #define RS 3
>> -    for (i = 0; i + 1 < length; i += 5) {
>> -        if (!((~AV_RN32A(src + i) &
>> -               (AV_RN32A(src + i) - 0x01000101U)) &
>> +#define MASKCHECK \
>> +        if (!((~AV_RN32A(src + i) & \
>> +               (AV_RN32A(src + i) - 0x01000101U)) & \
>>                0x80008080U))
>>  #endif
>> -            continue;
>> -        if (i > 0 && !src[i])
>> -            i--;
>> -        while (src[i])
>> -            i++;
>> +#define LOOPCHECK \
>> +        MASKCHECK \
>> +            continue; \
>> +        if (i > 0 && !src[i]) \
>> +            i--; \
>> +        while (src[i]) \
>> +            i++
>>  #else
>>  #define RS 0
>> -    for (i = 0; i + 1 < length; i += 2) {
>> -        if (src[i])
>> -            continue;
>> -        if (i > 0 && src[i - 1] == 0)
>> -            i--;
>> +#define LOOPCHECK \
>> +        if (src[i]) \
>> +            continue; \
>> +        if (i > 0 && src[i - 1] == 0) \
>> +            i--
>>  #endif
>> +    for (i = 0; i + 1 < length; i += RS + 2) {
>> +        LOOPCHECK;
>>          if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) {
>>              if (src[i + 2] != 3) {
>>                  /* startcode, so we must be past the end */
>> --
>
> This manner of splitting things is incredibly weird-looking.  Instead of
> trying to unify these rather different fragments, turning the second
> half of the loop into a macro and writing out separate loops, each
> calling the macro for the common part, would probably look much more
> sane.

#define LOOP_COMMON_PART \
        if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
            if (src[i + 2] != 3) { \
                /* startcode, so we must be past the end */ \
                length = i; \
            } \
            break; \
        }
#if HAVE_FAST_UNALIGNED
#define CHECK_COMMON_PART \
        if (i > 0 && !src[i]) \
            i--; \
        while (src[i]) \
            i++
#if HAVE_FAST_64BIT
    for (i = 0; i + 1 < length; i += 9) {
        if (!((~AV_RN64A(src + i) &
               (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
              0x8000800080008080ULL))
            continue;
        CHECK_COMMON_PART;
        LOOP_COMMON_PART;
        i -= 7;
    }
#else
    for (i = 0; i + 1 < length; i += 5) {
        if (!((~AV_RN32A(src + i) &
               (AV_RN32A(src + i) - 0x01000101U)) &
              0x80008080U))
            continue;
        CHECK_COMMON_PART;
        LOOP_COMMON_PART;
        i -= 3;
    }
#endif
#else
    for (i = 0; i + 1 < length; i += 2) {
        if (src[i])
            continue;
        if (i > 0 && src[i - 1] == 0)
            i--;
        LOOP_COMMON_PART;
    }
#endif

Pick your bet and commit whichever is nicer; both work with the
preprocessor. (I think the earlier one looks better.)

Ronald
_______________________________________________
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to