[FFmpeg-devel] [PATCH 10/10] Document how av_cmp_q() deal when one of the values to be compared is 0/0.

Stefano Sabatini stefano.sabatini-lala
Mon Oct 4 11:24:25 CEST 2010

```On date Monday 2010-10-04 01:54:21 +0200, Michael Niedermayer encoded:
> On Mon, Oct 04, 2010 at 12:14:34AM +0200, Stefano Sabatini wrote:
[...]
> > @@ -43,10 +44,13 @@ typedef struct AVRational{
> >   * Compare two rationals.
> >   * @param a first rational
> >   * @param b second rational
> > - * @return 0 if a==b, 1 if a>b and -1 if a<b
> > + * @return 0 if a==b, 1 if a>b, -1 if a<b, and INT_MIN if one of the
> > + * values is of the form 0/0
> >   */
> >  static inline int av_cmp_q(AVRational a, AVRational b){
> >      const int64_t tmp= a.num * (int64_t)b.den - b.num * (int64_t)a.den;
> > +    if (a.num == 0 && a.den == 0 || b.num == 0 && b.den == 0)
> > +        return INT_MIN;
> >
> >      if(tmp) return ((tmp ^ a.den ^ b.den)>>63)|1;
> >      else    return 0;
>
> this doesnt work with infinites
> i would suggest:
>
> Index: rational.h
> ===================================================================
> --- rational.h  (revision 25329)
> +++ rational.h  (working copy)
> @@ -49,7 +49,9 @@
>      const int64_t tmp= a.num * (int64_t)b.den - b.num * (int64_t)a.den;
>
>      if(tmp) return ((tmp ^ a.den ^ b.den)>>63)|1;
> -    else    return 0;
> +    else if(b.den && a.den) return 0;
> +    else if(a.num && b.num) return (a.num>>31) - (b.num>>31);
> +    else                    return INT_MIN;
>  }
>
>  /**
>
> if you agree ill commit that and leave the dox to you

OK, and patch updated with a test. Result is:

Testing av_cmp_q()
1/0 cmp 1/1 -> 1
-1/0 cmp 1/1 -> -1
1/0 cmp -1/1 -> 1
1/0 cmp 1/0 -> 0
2/0 cmp 1/0 -> 0
1/0 cmp -1/0 -> 1
-1/0 cmp 1/0 -> -1
-1/0 cmp -1/0 -> 0
0/0 cmp 1/1 -> -2147483648
0/0 cmp -1/1 -> -2147483648
0/0 cmp 1/0 -> -2147483648
1/0 cmp 0/0 -> -2147483648
0/0 cmp -1/0 -> -2147483648
0/0 cmp 0/0 -> -2147483648

Tell if you want to keep the test or apply the test as a separate
patch or apply the patch yourself (but in that case remember to bump
lavu micro version).

Regards.
--
FFmpeg = Freak & Funny Mysterious Practical Exciting Guru

```

More information about the ffmpeg-devel mailing list