On Tue, Dec 01, 2015 at 05:26:23PM +0100, Nicolas George wrote:
> Le decadi 10 frimaire, an CCXXIV, Hendrik Leppkes a écrit :
> > We generally just use ints for boolean properties, any particular
> > reason this uses unsigned instead?
> Just a matter of personal habit, justified below. But as I look at it, I see
> it is rather inconsistent with other fields in the struct that are int
> although they really would be better unsigned. Will change.
> When it does not matter, I usually use unsigned instead of signed because
> that often leaves the compiler more room for optimization. For example,
> "type half(type x) { return x / 2; }" compiles into this for unsigned:
>         movl    %edi, %eax
>         shrl    %eax
>         ret
> And this for signed:
>         movl    %edi, %eax
>         movl    $2, %ecx
>         cltd
>         idivl   %ecx
>         ret
> In other words, "x / 2" can be optimized into "x >> 1" for unsigned, but not
> for signed because the rounding is not the same for negative.
> I suppose in fact you already knew that. Of course it does not make a
> difference here, but I prefer this habit instead of the other way around.
> For booleans, I feel that int gives the impression there is a third case,
> possibly error: think of ssize_t for the return value of syscalls. And if it
> becomes a counter, then it should naturally be unsigned.

There is at least one case where you actually want it signed, which is
when you loop over it. Typically, in the case of a width/height, you
actually want it signed because it indicates the compiler that an overflow
will not happen since a signed overflow is undefined.

  for (x = x_start; x < width; x++)

Here, if we are dealing with signed, the compiler can simply assume that x
will not overflow, go back to 0 and then reach width. See
-Wstrict-overflow compiler option.


Clément B.

Attachment: signature.asc
Description: PGP signature

ffmpeg-devel mailing list

Reply via email to