public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Dave Korn" <dave.korn@artimi.com>
To: "'Scott Robert Ladd'" <scott.ladd@coyotegulch.com>,
	"'Paul Koning'" <pkoning@equallogic.com>
Cc: <rth@redhat.com>, <gcc@gcc.gnu.org>
Subject: RE: Sine and Cosine Accuracy
Date: Thu, 26 May 2005 17:29:00 -0000	[thread overview]
Message-ID: <SERRANOiVklUVhjNIjm00000179@SERRANO.CAM.ARTIMI.COM> (raw)
In-Reply-To: <4295FA1C.2050103@coyotegulch.com>

----Original Message----
>From: Scott Robert Ladd
>Sent: 26 May 2005 17:32

> Paul Koning wrote:
>>  Scott> Yes, but within the defined mathematical ranges for sine and
>>  Scott> cosine -- [0, 2 * PI) -- the processor intrinsics are quite 
>> Scott> accurate. 
>> 
>> Huh?  Sine and consine are mathematically defined for all finite
>> inputs.
> 
> Defined, yes. However, I'm speaking as a mathematician in this case, not
> a programmer. Pick up an trig book, and it will have a statement similar
> to this one, taken from a text (Trigonometry Demystified, Gibilisco,
> McGraw-Hill, 2003) randomly grabbed from the shelf next to me:
> 
> "These trigonometric identities apply to angles in the *standard range*
> of 0 rad <= theta < 2 * PI rad. 

  It's difficult to tell from that quote, which lacks sufficient context,
but you *appear* at first glance  to be conflating the fundamental
trignometric *functions* with the trignometric *identities* that are
generally built up from those functions.  That is to say, you appear to be
quoting a statement that says

" Identities such as
            sin(x)^2 + cos(x)^2 === 1
  are only valid when 0 <= x <= 2*PI"

and interpreting it to imply that 

"           sin(x)
  is only valid when 0 <= x <= 2*PI"

which, while it may or may not be true for other reasons, certainly is a
non-sequitur from the statement above.

  And in fact, and in any case, this is a perfect illustration of the point,
because what we're discussing here is *not* the behaviour of the
mathematical sine and cosine functions, but the behaviour of the C runtime
library functions sin(...) and cos(...), which are defined by the language
spec rather than by the strictures of mathematics.  And that spec makes *no*
restriction on what values you may supply as inputs, so gcc had better
implement sin and cos in a way that doesn't require the programmer to have
reduced the arguments beforehand, or it won't be ANSI compliant.

  Not only that, but if you don't use -funsafe-math-optimisations, gcc emits
libcalls to sin/cos functions, which I'll bet *do* reduce their arguments to
that range before doing the computation, (and which might indeed even be
clever enough to use the intrinsic, and can encapsulate the knowledge that
that intrinsic can only be used on arguments within a more limited range
than are valid for the C library function which they are being used to
implement).

  When you use -funsafe-math-optimisations, one of those optimisations is to
assume that you're not going to be using the full range of arguments that
POSIX/ANSI say is valid for the sin/cos functions, but that you're going to
be using values that are already folded into the range around zero, and so
it optimises away the libcall and the reduction with it and just uses the
intrinsic to implement the function.  But the intrinsic does not actually
implement the function as specified by ANSI, since it doesn't accept the
same range of inputs, and therefore it is *not* a suitable transformation to
ever apply except when the user has explicitly specified that they want to
live dangerously.  So in terms of your earlier suggestion:

----------------<quote>----------------
May I be so bold as to suggest that -funsafe-math-optimizations be
reduced in scope to perform exactly what it's name implies:
transformations that may slightly alter the meanding of code. Then move
the use of hardware intrinsics to a new -fhardware-math switch.
----------------<quote>----------------

... I am obliged to point out that using the hardware intrinsics *IS* an
unsafe optimisation, at least in this case!

    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....

  reply	other threads:[~2005-05-26 17:01 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-26 16:05 Scott Robert Ladd
2005-05-26 16:09 ` Andrew Haley
2005-05-26 16:33   ` Scott Robert Ladd
2005-05-26 17:14     ` Andrew Haley
2005-05-26 17:01   ` Paolo Carlini
2005-05-26 17:23 ` Richard Henderson
2005-05-26 17:24   ` Scott Robert Ladd
2005-05-26 17:27     ` Paul Koning
2005-05-26 17:27       ` Scott Robert Ladd
2005-05-26 17:29         ` Dave Korn [this message]
2005-05-26 17:37           ` David Daney
2005-05-26 17:56             ` Dave Korn
2005-05-26 17:40           ` Scott Robert Ladd
2005-05-26 18:12             ` Paul Koning
2005-05-26 18:32               ` Scott Robert Ladd
2005-05-26 18:50                 ` Paul Koning
2005-05-26 19:14                   ` Andrew Pinski
2005-05-26 19:35                     ` Scott Robert Ladd
2005-05-29  6:22                   ` Geoffrey Keating
2005-05-31 14:34                     ` Paul Koning
2005-05-31 22:58                       ` Geoff Keating
2005-05-29 12:07                 ` Roger Sayle
2005-05-30 15:34                   ` Vincent Lefevre
2005-05-29  2:22         ` Kai Henningsen
2005-05-29 18:16         ` Marc Espie
2005-05-29 20:58           ` Georg Bauhaus
2005-05-30 15:19             ` Gabriel Dos Reis
2005-05-30 15:19             ` Marc Espie
2005-05-30 17:26               ` Scott Robert Ladd
2005-05-30 17:18                 ` Marc Espie
2005-05-30 18:11                   ` Scott Robert Ladd
2005-05-30 17:31               ` Scott Robert Ladd
2005-05-31  3:10                 ` chris jefferson
2005-05-31 12:17                   ` Andrew Haley
2005-05-31 12:46                   ` Scott Robert Ladd
2005-05-31 13:02                     ` Andrew Haley
2005-05-31 13:34                 ` Vincent Lefevre
2005-05-30 15:35             ` Bernhard R. Link
2005-05-30 18:59               ` Scott Robert Ladd
2005-05-30 19:16               ` Georg Bauhaus
2005-05-30 19:17                 ` Bernhard R. Link
2005-05-30 19:54                   ` Georg Bauhaus
2005-05-30 20:04                     ` Gabriel Dos Reis
2005-05-26 17:35       ` Kevin Handy
2005-05-26 17:41         ` Paul Koning
2005-05-26 20:26           ` Joseph S. Myers
2005-05-26 21:15     ` Gabriel Dos Reis
2005-05-26 21:17       ` Scott Robert Ladd
2005-05-26 23:25         ` Gabriel Dos Reis
2005-05-27  0:18           ` Scott Robert Ladd
2005-05-27  0:54             ` Gabriel Dos Reis
2005-05-27 11:29           ` Marcin Dalecki
2005-05-27  9:36         ` Marcin Dalecki
2005-05-27 10:48       ` Marcin Dalecki
2005-05-26 21:33     ` Richard Henderson
2005-05-27  0:05       ` Scott Robert Ladd
2005-05-27  0:43         ` Gabriel Dos Reis
2005-05-27  0:54           ` Scott Robert Ladd
2005-05-28 11:26             ` Russ Allbery
2005-05-27 13:56     ` Vincent Lefevre
2005-05-29  3:36   ` Kai Henningsen
2005-05-26 17:53 Morten Welinder
2005-05-26 18:10 ` Scott Robert Ladd
2005-05-26 18:22   ` Dave Korn
2005-05-26 18:49     ` Scott Robert Ladd
2005-05-26 19:28       ` Dave Korn
2005-05-26 18:38 Morten Welinder
2005-05-26 20:58 ` Andrew Haley
2005-05-26 23:31 Uros Bizjak
2005-05-26 23:52 ` Paul Koning
2005-05-26 23:56 ` Gabriel Dos Reis
2005-05-26 23:57   ` Steven Bosscher
2005-05-27 15:09   ` Olivier Galibert
2005-05-27 15:28     ` Vincent Lefevre
2005-05-27 18:27     ` Marcin Dalecki
2005-05-26 23:59 Menezes, Evandro
2005-05-27 15:19 ` Vincent Lefevre
2005-05-27  0:39 Menezes, Evandro
2005-05-27  0:54 ` Scott Robert Ladd
2005-05-27  0:54 Menezes, Evandro
2005-05-27  0:54 ` Scott Robert Ladd
2005-05-27 12:42 ` Scott Robert Ladd
2005-05-28  4:32 Menezes, Evandro
2005-05-28  5:02 ` Scott Robert Ladd
2005-05-28 10:44 ` Gary Funck
2005-05-28  6:42 Menezes, Evandro

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=SERRANOiVklUVhjNIjm00000179@SERRANO.CAM.ARTIMI.COM \
    --to=dave.korn@artimi.com \
    --cc=gcc@gcc.gnu.org \
    --cc=pkoning@equallogic.com \
    --cc=rth@redhat.com \
    --cc=scott.ladd@coyotegulch.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).