Add __builtin_clrsb, similar to clz/ctz

Richard Henderson rth@redhat.com
Thu Jun 16 17:03:00 GMT 2011


On 06/16/2011 05:44 AM, Bernd Schmidt wrote:
> +@deftypefn {Built-in Function} int __builtin_clrsb (unsigned int x)
> +Returns the number of leading redundant sign bits in @var{x}, starting
> +at the most significant bit position.
> +@end deftypefn

Do we want a signed argument, since we're talking about signs?

It would seem that unlike clz, this function is not undefined for zero.
What about INT_MIN?  Do all cpus handle those edge cases the same way?
Both should probably be documented both for the builtin and the rtx code.

> +  if (x == 0 || x == (Wtype)-1)
> +    return W_TYPE_SIZE - 1;
> +  if (x > 0)
> +    count_leading_zeros (ret, x);
> +  else
> +    count_leading_zeros (ret, ~x);
> +  return ret - 1;

Do you get smaller code in general from

  if (x < 0)
    x = ~x;
  if (x == 0)
    return W_TYPE_SIZE - 1;
  count_leading_zeros(ret, x);
  return ret - 1;

?

> -(define_insn "signbitssi2"
> +(define_insn "clrsbsi2"
>    [(set (match_operand:HI 0 "register_operand" "=d")
>  	(if_then_else:HI
>  	 (lt (match_operand:SI 1 "register_operand" "d") (const_int 0))

No use of the new rtx code?  Not that this couldn't be handled
with a different patch, but I don't see how you're testing the
code in simplify-rtx without at least one such use.


r~



More information about the Gcc-patches mailing list