public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Question on valid transformations for cbrt
@ 2004-03-06 18:54 Kaveh R. Ghazi
  2004-03-07 15:11 ` Roger Sayle
  0 siblings, 1 reply; 5+ messages in thread
From: Kaveh R. Ghazi @ 2004-03-06 18:54 UTC (permalink / raw)
  To: gcc; +Cc: roger

I'm in the process of writing some transformations for builtin cbrt.
However it occurs to me that certain combinations might be invalid
when converting to pow, because pow doesn't like negative values with
non-integral powers.  E.g.:

	cbrt(sqrt(x)) -> pow(x, 1/6)

can be considered "safe" with -ffast-math IMO because if x was
negative the original sqrt would have choked anyway.  However here:

	cbrt(pow(x,y)) -> pow(x,y/3)

if x is negative and y is say 5, the first form would succeed but the
second would get an error EDOM because 5/3 is non-integral.  So I
don't think the latter is a valid transformation even with
-ffast-math.

(Perhaps the latter is okay if x passes tree_expr_nonnegative_p, but
that might not be worth it.)

Thoughts?

		Thanks,
		--Kaveh
--
Kaveh R. Ghazi			ghazi@caip.rutgers.edu

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Question on valid transformations for cbrt
  2004-03-06 18:54 Question on valid transformations for cbrt Kaveh R. Ghazi
@ 2004-03-07 15:11 ` Roger Sayle
  2004-03-10 15:32   ` Segher Boessenkool
  2004-04-02  0:22   ` Geoff Keating
  0 siblings, 2 replies; 5+ messages in thread
From: Roger Sayle @ 2004-03-07 15:11 UTC (permalink / raw)
  To: Kaveh R. Ghazi; +Cc: gcc


On Sat, 6 Mar 2004, Kaveh R. Ghazi wrote:
> I'm in the process of writing some transformations for builtin cbrt.
> However it occurs to me that certain combinations might be invalid
> when converting to pow, because pow doesn't like negative values with
> non-integral powers.  E.g.:
>
> 	cbrt(sqrt(x)) -> pow(x, 1/6)
>
> can be considered "safe" with -ffast-math IMO because if x was
> negative the original sqrt would have choked anyway.  However here:
>
> 	cbrt(pow(x,y)) -> pow(x,y/3)
>
> if x is negative and y is say 5, the first form would succeed but the
> second would get an error EDOM because 5/3 is non-integral.  So I
> don't think the latter is a valid transformation even with
> -ffast-math.
>
> (Perhaps the latter is okay if x passes tree_expr_nonnegative_p, but
> that might not be worth it.)
>
> Thoughts?

I think you're right, and we can't in general simplify cbrt(pow(x,y))
even with -ffast-math.  I've had to give some thought to GCC's existing
sqrt(pow(x,y)) to see whether we need to disable that.  However I've
conviced myself that its safe with -ffast-math.  If x is negative,
y must be an integer to satisfy pow, and then must be an even integer
to satisfy sqrt.  Hence our existing transform is safe.

The problem with cbrt, unlike sqrt, is that its is more expressive than
pow.  i.e. cbrt(-29.7) = -3.1 but pow(-29.7,1.0/3.0) => EDOM.  Hence
we can't normalize cbrt(x) as pow(x,1.0/3.0), but we can safely go the
other way pow(x,1.0/3.0) => cbrt(x).

Roger
--

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Question on valid transformations for cbrt
  2004-03-07 15:11 ` Roger Sayle
@ 2004-03-10 15:32   ` Segher Boessenkool
  2004-03-10 16:06     ` Robert Dewar
  2004-04-02  0:22   ` Geoff Keating
  1 sibling, 1 reply; 5+ messages in thread
From: Segher Boessenkool @ 2004-03-10 15:32 UTC (permalink / raw)
  To: Roger Sayle; +Cc: gcc, Kaveh R. Ghazi

> The problem with cbrt, unlike sqrt, is that its is more expressive than
> pow.  i.e. cbrt(-29.7) = -3.1 but pow(-29.7,1.0/3.0) => EDOM.  Hence
> we can't normalize cbrt(x) as pow(x,1.0/3.0),

You can "normalize" cbrt(x) as  sign(x) * pow(abs(x), 1.0/3)  -- maybe 
that
helps some further optimizations?

> but we can safely go the
> other way pow(x,1.0/3.0) => cbrt(x).


Segher

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Question on valid transformations for cbrt
  2004-03-10 15:32   ` Segher Boessenkool
@ 2004-03-10 16:06     ` Robert Dewar
  0 siblings, 0 replies; 5+ messages in thread
From: Robert Dewar @ 2004-03-10 16:06 UTC (permalink / raw)
  To: Segher Boessenkool; +Cc: Roger Sayle, gcc, Kaveh R. Ghazi

Segher Boessenkool wrote:

>> The problem with cbrt, unlike sqrt, is that its is more expressive than
>> pow.  i.e. cbrt(-29.7) = -3.1 but pow(-29.7,1.0/3.0) => EDOM.  Hence
>> we can't normalize cbrt(x) as pow(x,1.0/3.0),
> 
> 
> You can "normalize" cbrt(x) as  sign(x) * pow(abs(x), 1.0/3)  -- maybe that
> helps some further optimizations?

Of course both these transformations are numerically horrible. I trust 
that this is understood.

>> but we can safely go the
>> other way pow(x,1.0/3.0) => cbrt(x).

Well I don't like transformations like this which change the computation
so radically and give completely different results. This is not real
arithmetic, and the compiler really has no business making 
substituations that are not meaning preserving.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Question on valid transformations for cbrt
  2004-03-07 15:11 ` Roger Sayle
  2004-03-10 15:32   ` Segher Boessenkool
@ 2004-04-02  0:22   ` Geoff Keating
  1 sibling, 0 replies; 5+ messages in thread
From: Geoff Keating @ 2004-04-02  0:22 UTC (permalink / raw)
  To: Roger Sayle; +Cc: gcc

Roger Sayle <roger@eyesopen.com> writes:

> i.e. cbrt(-29.7) = -3.1 but pow(-29.7,1.0/3.0) => EDOM.

one reason for this is because 1.0 / 3.0, as a double, gets rounded to
6004799503160661 / 2 ^ 54, and so the result of the pow() is imaginary
and not representable as a 'double'.  (If we'd used base-3 arithmetic,
pow() could always provide a result with negative inputs.)

(This also means the same transformations with sqrt() will usually be
safe.)

-- 
- Geoffrey Keating <geoffk@geoffk.org>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-04-02  0:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-06 18:54 Question on valid transformations for cbrt Kaveh R. Ghazi
2004-03-07 15:11 ` Roger Sayle
2004-03-10 15:32   ` Segher Boessenkool
2004-03-10 16:06     ` Robert Dewar
2004-04-02  0:22   ` Geoff Keating

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).