FYI

-------- Forwarded Message --------
Subject:        RE: complex.h functions and errno
Date:   Thu, 9 Nov 2017 11:38:54 -0700
From:   Barry Hedquist <b...@peren.com>
Reply-To:       b...@peren.com
Organization:   Perennial, Inc
To:     'Jim Thomas' <jaswtho...@sbcglobal.net>, 'David Keaton' <d...@dmk.com>
CC:     'Hal Finkel' <hfin...@anl.gov>



Hal,

From Jim Thomas. I’ll leave it to you to forward this to Austin Group.

Barry

*From:* Jim Thomas [mailto:jaswtho...@sbcglobal.net]
*Sent:* Thursday, November 09, 2017 11:17 AM
*To:* David Keaton <d...@dmk.com>
*Cc:* Barry Hedquist <b...@peren.com>
*Subject:* Re: complex.h functions and errno



   On Nov 9, 2017, at 7:09 AM, David Keaton <d...@dmk.com
   <mailto:d...@dmk.com>> wrote:

   Barry,

        I'm looping in Jim Thomas.

        Hal is correct, and I think the response to him misses the
   point that he raised.

Agreed.



   What Hal was talking about was the fact that cabs(a + I*b) ==
   sqrt(a*a + b*b), and the squaring done by cabs() is what causes the
   overflow.

The C expressions cabs(a + I*b) and sqrt(a*a + b*b) aren’t equivalent because the computation of the argument to sqrt overflows in many cases where the corresponding cabs does not (e.g., a = DBL_MAX, b = 0, a+a + b*b = inf, sqrt = inf, cabs = DBL_MAX). However, cabs can overflow in extreme cases, like the one mentioned below: cabs(1.7e308+I*1.7e308).


        Most of those who are interested in optimization would really
   like for errno to go away, and I don't see any problem with POSIX
   specifying that it isn't set for cases where the C standard leaves
   it unspecified.

I think this gets to Hal’s point. The only problem I see (with the C-POSIX difference) is for implementation of cabs(a + bi) as hypot(a, b) if hypot sets errno. G.6#6 says

   Each of the functions cabs and carg is specified by a formula in
   terms of a real function (whose special cases are covered in annex F):

cabs(x + iy) = hypot(x, y)

carg(x + iy) = atan2(y, x)

Maybe one could argue that this specification does not apply to errno.

Jim


   David

   On 11/09/2017 06:52 AM, Barry Hedquist wrote:

       Do we, WG14, have an answer for this?  What does C11/C17 say
       about this?
       Thanks,
       Barry
       -----Original Message-----
       From: Don Cragun [mailto:dcra...@sonic.net]
       Sent: Wednesday, November 08, 2017 8:15 PM
       To: Hal Finkel <hfin...@anl.gov <mailto:hfin...@anl.gov>>
       Cc: austin-group-l <austin-group-l@opengroup.org
       <mailto:austin-group-l@opengroup.org>>
       Subject: Re: complex.h functions and errno
       Hi Hal,
       I agree with what others have already said in this thread, but I
       think there
       is a more basic issue at play here.  In the example you provided:
       cabs(1.7e308+I*1.7e308);
       the multiplication is not being performed by the function.  It
       is performed
       by the compiler evaluating arguments to be passed to the
       functipn.  So the
       floating point overflow will occur before cabs() has a chance to
       perform any
       calculations.
       The C99 standard (section 6.5 "Expressions") says:
       "If an exceptional condition occurs during the evaluation of an
       expression
       (that is, if the result is not mathematically defined or not in the
       range
       of representable values for its type), the behavior is undefined."
       which seems to be in conflict with the math error handling
       requirements.  (I
       haven't tried to determine if this has been fixed in C11 or
       later drafts.)
         - Don

           On Nov 7, 2017, at 3:22 PM, Hal Finkel <hfin...@anl.gov
           <mailto:hfin...@anl.gov>> wrote:

           Hi, everyone,

           My reading of the combined C and POSIX specifications leads
           me to believe

       that functions in complex.h don't set errno on POSIX systems.
       This came up
       recently during a code review for the Clang compiler [1]. It has
       been
       pointed out that, under glibc, cabs() can set errno (e.g.,
       cabs(1.7e308+I*1.7e308)) although POSIX says "No errors are
       defined" for
       cabs.


           To elaborate, in the C specification, 7.12 specifies the
           requirements for

       functions in math.h. For those functions, 7.12.1 (Treatment of error
       conditions) says that overflows do set ERANGE, and that it's
       implementation
       defined if the same is true for underflows. That's true for
       functions in
       math.h in general. 7.3 specifies the requirements for functions in
       complex.h. Here, 7.3.2 says, "An implementation may set errno
       but is not
       required to." That, as I understand it, applies to all functions in
       complex.h (unless otherwise noted, I presume). However, because
       setting
       errno is not required by C for functions in complex.h, when
       POSIX says "No
       errors are defined." that constrains the choice (POSIX is
       constraining the
       implementation choice allowed by C as it does in many other
       cases). As a
       result, under POSIX, the functions in complex.h don't set errno.


           Is my reading correct? If it is correct, is it intentional?
           I'd prefer

       that these functions don't set errno, because that makes compiler
       optimization easier, but my overriding concern is that the compiler
       interprets the requirements correctly.


           Thanks in advance,

           Hal

           [1] https://reviews.llvm.org/D39611

           --
           Hal Finkel
           Lead, Compiler Technology and Programming Languages Leadership
           Computing Facility Argonne National Laboratory


--
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

Reply via email to