This is the mail archive of the
`gcc-patches@gcc.gnu.org`
mailing list for the GCC project.

# Re: [tree-ssa] fold-const trick for gzip

*From*: Ayal Zaks <ZAKS at il dot ibm dot com>
*To*: Andrew Pinski <pinskia at physics dot uc dot edu>, Roger Sayle <roger at eyesopen dot com>
*Cc*: <gcc-patches at gcc dot gnu dot org>
*Date*: Sat, 13 Dec 2003 22:59:48 +0200
*Subject*: Re: [tree-ssa] fold-const trick for gzip

Andrew Pinski writes:
>In gzip, there is a place where the code could be using unconditional code
>instead of conditional code for speed and size (at least on PPC).
>
>It turns (A > (2^n)-1) ? (A - 2^n) : 0 into (-(A >> n))&(A-2^n) where n+1
>is the size of the conditional type in bits.
Roger Sayle writes:
>The second is that any conditional of the form "((signed) A < 0) ? B : 0"
>can be transformed into "B & X" where X is "(signed) A >> n" or in your
>example above, "-((unsigned) A >> n)" which requires an additional
>negation.
So the original ((A > (2^n)-1) ? (A - 2^n) : 0) can be if-converted
into ((unsigned) A >> n) & (A - 2^n).
I'd just like to point out that this is a special case of
unsigned-subtract-and-saturate: (A >= C ? A - C : 0), or in other words
the form "((signed) A-C < 0) ? 0 : A-C", which can be if-converted into
(A - C) & !((unsigned) (A-C) >> (wordlength-1)).
This works for any C, not necessarily constant, but is slightly less
efficient than the special case of C == 2^{wordlength-1}, where
((unsigned) (A-C) >> (wordlength-1)) can indeed be simplified into
!((unsigned) A >> (wordlength-1)).
Note that a target may directly support unsigned-subtract-and-saturate,
which is most efficient.
MTCW, Ayal.