From owner-svn-src-head@FreeBSD.ORG Tue Feb 3 19:14:02 2015 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 977C6296; Tue, 3 Feb 2015 19:14:02 +0000 (UTC) Received: from troutmask.apl.washington.edu (troutmask.apl.washington.edu [128.95.76.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "troutmask", Issuer "troutmask" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 789D8768; Tue, 3 Feb 2015 19:14:02 +0000 (UTC) Received: from troutmask.apl.washington.edu (localhost [127.0.0.1]) by troutmask.apl.washington.edu (8.14.9/8.14.9) with ESMTP id t13JDt1c013865 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Tue, 3 Feb 2015 11:13:55 -0800 (PST) (envelope-from sgk@troutmask.apl.washington.edu) Received: (from sgk@localhost) by troutmask.apl.washington.edu (8.14.9/8.14.9/Submit) id t13JDtEY013864; Tue, 3 Feb 2015 11:13:55 -0800 (PST) (envelope-from sgk) Date: Tue, 3 Feb 2015 11:13:55 -0800 From: Steve Kargl To: Bruce Evans Subject: Re: svn commit: r278154 - head/lib/msun/src Message-ID: <20150203191355.GA13447@troutmask.apl.washington.edu> References: <201502031417.t13EHU9g074687@svn.freebsd.org> <20150204012614.T1972@besplex.bde.org> <20150203163329.GA12706@troutmask.apl.washington.edu> <20150204035337.Y2718@besplex.bde.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20150204035337.Y2718@besplex.bde.org> User-Agent: Mutt/1.5.23 (2014-03-12) Cc: svn-src-head@freebsd.org, svn-src-all@freebsd.org, "Pedro F. Giffuni" , src-committers@freebsd.org X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 03 Feb 2015 19:14:02 -0000 On Wed, Feb 04, 2015 at 04:37:14AM +1100, Bruce Evans wrote: > On Tue, 3 Feb 2015, Steve Kargl wrote: > > > -#include > > +#include > > #include > > > > +#define FLT_LARGE FLT_MAX_EXP - FLT_MIN_EXP + FLT_MANT_DIG > > +#define FLT_SMALL FLT_MIN_EXP - FLT_MAX_EXP > > +#define DBL_LARGE DBL_MAX_EXP - DBL_MIN_EXP + DBL_MANT_DIG > > +#define DBL_SMALL DBL_MIN_EXP - DBL_MAX_EXP > > +#define LDBL_LARGE LDBL_MAX_EXP - LDBL_MIN_EXP + LDBL_MANT_DIG > > +#define LDBL_SMALL LDBL_MIN_EXP - LDBL_MAX_EXP > > + > > This is a lot of code and bugs (missing parentheses) for negative gain. My crystal ball isn't function too well lately. > To work, it depends on all of the above expressions having results that > can be represented as an int. There is no guarantee of this in general. On all architecture that FreeBSD currently supports, there is a guarantee to the extent that src/sys/${MACHINE}/include/_limits.h shows #define __INT_MAX 0x7fffffff #define __INT_MIN (-0x7fffffff - 1) > However, we can show that the above values are within certain fixed > limits just as easily as we can show that they are within the INT32 > limits, and then hard-code the fixed limits. E,g, +-16384 might work, > and +-32768 surely works when the exponent field is limited to 8 bits. > Double a few more times for safety. Hard-coding a limit related to > an assumed maximum supported width for the exponent field is easier > to get right that the above expressions. I think the above are quite > buggy, and so are my +-16384 and +-32768. The full exponent range > plus more for denormals is needed in both directions. You only have > it for the LARGE direction. This can be hard-coded for a 15-bit > exponent field as +-32768 +- extras for denormals, -+ a couple for > special exponents. This needs strictly larger than 16-bit ints for > the denormals only. Make it +-65536 to avoid having to count carefully > to avoid off-by-1 errors. Double a few times for larger exponent fields, > or just add assertions to detect increases of the maxima, or just hard- > code the limits as the int32_t extrema. Given that the widest field width in IEEE754-2008 for binary128 is 15, which gives a maximum unbiased exponent of 2**15, then using +-65536 should cover our needs. The functions scalbn[fl] can then deal with values of n that may result in exponents that are too small or too large. Untested diff below. -- Steve Index: s_scalbln.c =================================================================== --- s_scalbln.c (revision 278165) +++ s_scalbln.c (working copy) @@ -27,17 +27,17 @@ #include __FBSDID("$FreeBSD$"); -#include #include +#define LARGE 65536 +#define SMALL -65536 + double scalbln (double x, long n) { int in; - in = (int)n; - if (in != n) - in = (n > 0) ? INT_MAX: INT_MIN; + in = n > LARGE ? LARGE : n < SMALL ? SMALL : n; return (scalbn(x, in)); } @@ -46,9 +46,7 @@ { int in; - in = (int)n; - if (in != n) - in = (n > 0) ? INT_MAX: INT_MIN; + in = n > LARGE ? LARGE : n < SMALL ? SMALL : n; return (scalbnf(x, in)); } @@ -57,8 +55,6 @@ { int in; - in = (int)n; - if (in != n) - in = (n > 0) ? INT_MAX: INT_MIN; + in = n > LARGE ? LARGE : n < SMALL ? SMALL : n; return (scalbnl(x, in)); }