delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2018/04/09/08:09:39

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:subject:to:references:from:message-id:date
:mime-version:in-reply-to:content-type
:content-transfer-encoding; q=dns; s=default; b=Mr8hqS98BmdUBjKK
dT2HYdb5eS89XUFVTocE4QkEn9fNXjTYEFwJ1WpSwrDSi/cNbrbZlIXcdC4I3Dhj
df7S9mfWb+k0NUpni3RzhfnfMd/H2QMY6pHdxgq0Q05lk05ZpmN1B2sQjl7OWGLN
hKVPBhMeVXEMHPLoEVxHIPutXg0=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:subject:to:references:from:message-id:date
:mime-version:in-reply-to:content-type
:content-transfer-encoding; s=default; bh=cqWD6ol4owoFvBpItPukZR
7Mwrw=; b=H8bJCzadY34iC72j3s0VqR78nnLbnzXAkmiA22HJs88hK8Zzns+kv4
y/ceYXYpU0exPZpAoY0VfUzkpuCLKxXVMrRDYIuopV6tpiOgTKUdn9DrHsiaya/Z
//JUX91Xm2owXYQZOfjVpXkYAxFWd6al9RT2ZtVMjuCJJJc9ryyPY=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
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
X-CornellRouted: This message has been Routed already.
Subject: Re: Floating point exception in strtod()
To: cygwin AT cygwin DOT com
References: <dd5e98ea-8f39-1c4b-116c-37968cb3a52d AT cornell DOT edu> <20180409094706 DOT GO18998 AT calimero DOT vinschen DOT de>
From: Ken Brown <kbrown AT cornell DOT edu>
Message-ID: <c4afb320-e963-adf8-ea74-4c625729336c@cornell.edu>
Date: Mon, 9 Apr 2018 08:09:20 -0400
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>
X-PMX-Cornell-Gauge: Gauge=XXXXX
X-PMX-CORNELL-AUTH-RESULTS: dkim-out=none;
X-IsSubscribed: yes

On 4/9/2018 5:47 AM, Corinna Vinschen wrote:
> On Apr  7 13:40, Ken Brown wrote:
>> $ cat strtod_test.c
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <fenv.h>
>>
>> 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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019