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:date:message-id:to:cc:subject:from:in-reply-to :references:mime-version:content-type:content-transfer-encoding; q=dns; s=default; b=wKgXE72nYCfo2P1DnYwgHpFqzfhJgt1TnJG+FOip0oq khOb5aH34kLGfwHxQH8jaO93GPZSGT2ZH7tQj0nsU9d89xDcFRxzo9lKqZHT71TS ha99y6nw2SE4dQr0OZ5n2JKdVPER3FIHyjWVHj1V5BBlSWqYaIrwNTWNdCzPIPYA = 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:date:message-id:to:cc:subject:from:in-reply-to :references:mime-version:content-type:content-transfer-encoding; s=default; bh=82M28jufDtNU8co4yOG7UZvmJoM=; b=kMvh64+xBtj8BSEsS 8tO9AGvDJucYqagiQHzgFsnVZOCvzy+Drp5Blm1Z5VhSp2gMOSr2tAk1dW5Me/6K iJmwU1Ff8tlzLvluHF7nzRv2mtA82Lz20urKWuYjnLvXRU+lYCJ7DOLTDvGNXkkq VZSj5D75vRt6MOMRfwV9E3o6kI= Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , 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-Spam-SWARE-Status: No, score=-24.4 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,KAM_LAZY_DOMAIN_SECURITY autolearn=ham version=3.3.2 spammy=H*c:PHrt X-HELO: msc14.plala.or.jp Date: Wed, 15 Aug 2018 00:05:39 +0900 (JST) Message-Id: <20180815.000539.1012490218433540835.trueroad@trueroad.jp> To: newlib AT sourceware DOT org Cc: cygwin AT cygwin DOT com, corinna-cygwin AT cygwin DOT com, trueroad AT trueroad DOT jp Subject: strtod ("nan") returns negative NaN From: Masamichi Hosoda In-Reply-To: <20180814095618.GT3747@calimero.vinschen.de> References: <20180814 DOT 134527 DOT 917341694729989717 DOT trueroad AT trueroad DOT jp> <20180814_dot_134527_dot_917341694729989717_dot_trueroad_at_trueroad_dot_jp> <20180814095618 DOT GT3747 AT calimero DOT vinschen DOT de> Mime-Version: 1.0 Content-Type: Multipart/Mixed; boundary="--Next_Part(Wed_Aug_15_00_05_39_2018_675)--" Content-Transfer-Encoding: 7bit X-VirusScan: Outbound; mvir-ac14; Wed, 15 Aug 2018 00:05:45 +0900 ----Next_Part(Wed_Aug_15_00_05_39_2018_675)-- Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Hi I've found that strtod ("nan") returns negative NaN on Cygwin 64 bit. https://cygwin.com/ml/cygwin/2018-08/msg00168.html On Linux with glibc, both strtod ("nan") and strtod ("-nan") return positive NaN. So I've created the patch that behaves like glibc. Both strtod ("nan") and strtod ("-nan") return positive NaN. Sample code: ``` #include #include int main (void) { printf ("strtof (\"nan\", NULL) = %f\n", strtof ("nan", NULL)); printf ("strtof (\"-nan\", NULL) = %f\n", strtof ("-nan", NULL)); printf ("strtod (\"nan\", NULL) = %f\n", strtod ("nan", NULL)); printf ("strtod (\"-nan\", NULL) = %f\n", strtod ("-nan", NULL)); printf ("strtold (\"nan\", NULL) = %Lf\n", strtold ("nan", NULL)); printf ("strtold (\"-nan\", NULL) = %Lf\n", strtold ("-nan", NULL)); } ``` The result of Cygwin (newlib) without my patch: ``` strtof ("nan", NULL) = nan strtof ("-nan", NULL) = nan strtod ("nan", NULL) = -nan strtod ("-nan", NULL) = nan strtold ("nan", NULL) = -nan strtold ("-nan", NULL) = -nan ``` The result of Linux (glibc, Ubuntu 16.04): ``` strtof ("nan", NULL) = nan strtof ("-nan", NULL) = nan strtod ("nan", NULL) = nan strtod ("-nan", NULL) = nan strtold ("nan", NULL) = nan strtold ("-nan", NULL) = nan ``` The result of FreeBSD 10.1 (BSD libc): ``` strtof ("nan", NULL) = nan strtof ("-nan", NULL) = nan strtod ("nan", NULL) = nan strtod ("-nan", NULL) = nan strtold ("nan", NULL) = nan strtold ("-nan", NULL) = nan ``` The result of Cygwin (newlib) with my patch: ``` strtof ("nan", NULL) = nan strtof ("-nan", NULL) = nan strtod ("nan", NULL) = nan strtod ("-nan", NULL) = nan strtold ("nan", NULL) = nan strtold ("-nan", NULL) = nan ``` Thanks. ----Next_Part(Wed_Aug_15_00_05_39_2018_675)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="0001-Fix-strtod-nan-returns-negative-NaN.patch" From 91cf4a20e0773f4a38d6d56b0867fe3725859e5e Mon Sep 17 00:00:00 2001 From: Masamichi Hosoda Date: Tue, 14 Aug 2018 22:29:34 +0900 Subject: [PATCH 1/2] Fix strtod ("nan") returns negative NaN The definition of qNaN for x86_64 and i386 was wrong. So strtod ("nan") and strtold ("nan") returned negative NaN instead of positive NaN. strtof ("nan") returns positive NaN so it does not have this issue. This commit fixes definition of qNaN for x86_64 and i386. So strtod ("nan") and strtold ("nan") return positive NaN. --- newlib/libc/stdlib/gd_qnan.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/newlib/libc/stdlib/gd_qnan.h b/newlib/libc/stdlib/gd_qnan.h index b775f82..8b0726a 100644 --- a/newlib/libc/stdlib/gd_qnan.h +++ b/newlib/libc/stdlib/gd_qnan.h @@ -26,6 +26,20 @@ #elif defined(__IEEE_LITTLE_ENDIAN) #if !defined(__mips) +#if defined (__x86_64__) || defined (__i386__) +#define f_QNAN 0x7fc00000 +#define d_QNAN0 0x0 +#define d_QNAN1 0x7ff80000 +#define ld_QNAN0 0x0 +#define ld_QNAN1 0xc0000000 +#define ld_QNAN2 0x7fff +#define ld_QNAN3 0x0 +#define ldus_QNAN0 0x0 +#define ldus_QNAN1 0x0 +#define ldus_QNAN2 0x0 +#define ldus_QNAN3 0xc000 +#define ldus_QNAN4 0x7fff +#else #define f_QNAN 0xffc00000 #define d_QNAN0 0x0 #define d_QNAN1 0xfff80000 @@ -38,6 +52,7 @@ #define ldus_QNAN2 0x0 #define ldus_QNAN3 0xc000 #define ldus_QNAN4 0xffff +#endif #elif defined(__mips_nan2008) #define f_QNAN 0x7fc00000 #define d_QNAN0 0x0 -- 2.17.0 ----Next_Part(Wed_Aug_15_00_05_39_2018_675)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="0002-Fix-strtod-nan-returns-negative-NaN.patch" From 7256702e5034b016b5114dd1a6c4c1a689a17816 Mon Sep 17 00:00:00 2001 From: Masamichi Hosoda Date: Tue, 14 Aug 2018 23:12:49 +0900 Subject: [PATCH 2/2] Fix strtod ("-nan") returns negative NaN On Linux, glibc's strtod ("-nan") and strtold ("-nan") return positive NaN. But, newlib's strtod ("-nan") returns negative NaN because it inverted the sign with the presence of `-` character. And, newlib's srtold ("-nan") returns negative NaN because it set the sign bit with the presence of `-` character. newlib's strtof ("-nan") returns positive NaN same as Linux glibc's. This commit removes strtod's NaN sign inversion and removes strtold's NaN sign bit setting. So strtod ("-nan") and strtold ("-nan") return positive NaN same as Linux glibc. --- newlib/libc/stdlib/strtod.c | 1 + newlib/libc/stdlib/strtodg.c | 1 + 2 files changed, 2 insertions(+) diff --git a/newlib/libc/stdlib/strtod.c b/newlib/libc/stdlib/strtod.c index 0cfa9e6..3b9fd26 100644 --- a/newlib/libc/stdlib/strtod.c +++ b/newlib/libc/stdlib/strtod.c @@ -451,6 +451,7 @@ _strtod_l (struct _reent *ptr, const char *__restrict s00, char **__restrict se, #ifndef No_Hex_NaN } #endif + sign = 0; goto ret; } } diff --git a/newlib/libc/stdlib/strtodg.c b/newlib/libc/stdlib/strtodg.c index 4ac1f8e..cc2842b 100644 --- a/newlib/libc/stdlib/strtodg.c +++ b/newlib/libc/stdlib/strtodg.c @@ -585,6 +585,7 @@ _strtodg_l (struct _reent *p, const char *s00, char **se, FPI *fpi, Long *exp, if (*s == '(') /*)*/ irv = hexnan(&s, fpi, bits); #endif + sign = 0; goto infnanexp; } } -- 2.17.0 ----Next_Part(Wed_Aug_15_00_05_39_2018_675)-- Content-Type: text/plain; charset=us-ascii -- 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 ----Next_Part(Wed_Aug_15_00_05_39_2018_675)----