[FFmpeg-devel] [PATCH] all: simplify qsort comparators, and add const-correctness

Ganesh Ajjanagadde gajjanagadde at gmail.com
Sun Oct 25 14:46:54 CET 2015


On Sat, Oct 24, 2015 at 9:38 PM, Mark Harris <mark.hsj at gmail.com> wrote:
>>>
>>>  static int compare_int64(const void *a, const void *b)
>>>  {
>>> -    int64_t va = *(int64_t *)a, vb = *(int64_t *)b;
>>> -    return va < vb ? -1 : va > vb ? +1 : 0;
>>> +    return *(const int64_t *)a - *(const int64_t *)b;
>>>  }
>>>
>>
>> What if the result doesn't fit in int? The input is not int.
>
> Even for int this transformation is not valid assuming that the full
> range of int is possible.  For example if *a = INT_MAX and *b = -1
> then *a - *b = INT_MAX+1 which is negative when cast to an int.

point taken, but this will need to be checked more carefully: for
instance, sometimes the integers fit in only the lower 24/16 bits,
etc. I have decided the following: this patch will only address the
const-ness. The next patch will be a scan through the qsort
comparators, and appropriately modifying the comparator by fixing
existing issues (if any) related to your point, and avoiding branches
when possibles.


More information about the ffmpeg-devel mailing list