[FFmpeg-devel] [RFC] abs vs FFABS

Uoti Urpala uoti.urpala
Sat Jan 17 17:46:44 CET 2009

On Sat, 2009-01-17 at 11:26 -0500, Ronald S. Bultje wrote:
> On Sat, Jan 17, 2009 at 11:10 AM, Uoti Urpala <uoti.urpala at pp1.inet.fi> wrote:
> > The compiler should be able to optimize that whole function to
> > "return 0;", and current GCC does.
> The idea is that it doesn't because the input is (semi-)random. The
> abs()/FFABS() is done on a calculated number per cycle. I wish I could
> check assembler to ensure it does, but I don't think it can just be
> optimized to return 0, because every outcome is checked (see test_end)
> and it returns 1 if it is wrong.

The only use of the of the result is to test whether it is >= 0. The
compiler can see that it's not necessary to actually calculate the
result of the abs to return the right value from the function. Either
the abs input is -0x80000000 in which case the behavior is undefined and
anything is OK, or the input is something else and the result will be >=
0. So assuming that the condition is hardcoded to true will always give
correct behavior.

More information about the ffmpeg-devel mailing list