From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17244 invoked by alias); 9 Apr 2018 12:09:28 -0000 Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com Received: (qmail 14876 invoked by uid 89); 9 Apr 2018 12:09:27 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.8 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: limerock03.mail.cornell.edu Received: from limerock03.mail.cornell.edu (HELO limerock03.mail.cornell.edu) (128.84.13.243) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 09 Apr 2018 12:09:26 +0000 X-CornellRouted: This message has been Routed already. Received: from authusersmtp.mail.cornell.edu (granite4.serverfarm.cornell.edu [10.16.197.9]) by limerock03.mail.cornell.edu (8.14.4/8.14.4_cu) with ESMTP id w39C9OjB029496 for ; Mon, 9 Apr 2018 08:09:24 -0400 Received: from [192.168.0.15] (mta-68-175-129-7.twcny.rr.com [68.175.129.7] (may be forged)) (authenticated bits=0) by authusersmtp.mail.cornell.edu (8.14.4/8.12.10) with ESMTP id w39C9MMM005868 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NOT) for ; Mon, 9 Apr 2018 08:09:23 -0400 Subject: Re: Floating point exception in strtod() To: cygwin@cygwin.com References: <20180409094706.GO18998@calimero.vinschen.de> From: Ken Brown Message-ID: Date: Mon, 09 Apr 2018 12:09:00 -0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <20180409094706.GO18998@calimero.vinschen.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-PMX-Cornell-Gauge: Gauge=XXXXX X-PMX-CORNELL-AUTH-RESULTS: dkim-out=none; X-IsSubscribed: yes X-SW-Source: 2018-04/txt/msg00069.txt.bz2 On 4/9/2018 5:47 AM, Corinna Vinschen wrote: > On Apr 7 13:40, Ken Brown wrote: >> $ cat strtod_test.c >> #include >> #include >> #include >> >> int >> main () >> { >> /* The following number comes from /usr/share/asymptote/ode.asy. */ >> const char *str = "121645100408832000.0"; >> char *ptr; >> >> feenableexcept (FE_INVALID); >> strtod (str, &ptr); >> >> /* If there was an exception, the following will not get executed. */ >> printf ("No exception.\n"); >> } >> >> $ gcc strtod_test.c >> >> $ ./a >> Floating point exception (core dumped) >> >> [The above was on x86. On x86_64 there's simply no output.] >> >> I have no idea what's special about the number 121645100408832000.0, but the >> problem goes away if, for example, I replace the leading 1 by 2. > > GDB shows that the exception occurs in newlib/libc/stdlib/strtod.c > line 1189, in this statment, which looks rather inconspicious at > first glance: > > L = (Long)aadj; > > L is of type Long == int32_t, aadj is of type double. The > value of aadj at this time is 2529648000.0 == 0x96c75d80 which > appears to be perfectly valid for a 32 bit int. > > However, on 64 bit for example the assembler statement generating the FP > exception is > > cvttsd2si %xmm0,%eax > > It is documented that this statemnt may raise FE_INVALID or FE_INEXACT > exceptions. The problem is that the generated 32 bit value is a > negative signed int value, while the source operand is positive. So the > conversion is, in fact, invalid. > > I applied a fix to newlib's strtod, to always use 64 bit ints in this > place. This fixes the problem and no exception is raised. Thanks for the explanation and the quick fix. I'm sorry for causing confusion by also sending this to the newlib list. Ken -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple