public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Steven Munroe <munroesj@linux.vnet.ibm.com>
To: Joseph Myers <joseph@codesourcery.com>
Cc: Steve Munroe <sjmunroe@us.ibm.com>,
	"libc-alpha@sourceware.org" <libc-alpha@sourceware.org>,
	Michael R Meissner <mrmeissn@us.ibm.com>,
	"Paul E. Murphy" <murphyp@linux.vnet.ibm.com>,
	Tulio Magno Quites Machado Filho <tuliom@linux.vnet.ibm.com>
Subject: Re: IEEE128 binary float to decimal float conversion routines
Date: Tue, 08 Dec 2015 17:16:00 -0000	[thread overview]
Message-ID: <1449594999.9274.45.camel@oc7878010663> (raw)
In-Reply-To: <alpine.DEB.2.10.1511182322260.26547@digraph.polyomino.org.uk>

On Wed, 2015-11-18 at 23:53 +0000, Joseph Myers wrote:
> On Wed, 18 Nov 2015, Steve Munroe wrote:
> 
> > > The problem I see is with the final "result = temp;" which converts
> > double
> > > to float.
> > >
> > > The earlier steps are probably accurate to within 1ulp.  But if temp (a
> > > double) is half way between two representable float values - while the
> > > original argument is very close to that half way value, but not exact -
> > > then the final conversion will round to even, which may or may not be
> > > correct depending on which side of that double value the original
> > > _Decimal128 value was.  (Much the same applies in other rounding modes
> > > when the double value equals a float value but the original value isn't
> > > exactly that float value.)
> > >
> > Would changing the the decimal to binary conversion to be round to odd,
> > offset the following round double to float?
> > 
> > http://www.exploringbinary.com/gcc-avoids-double-rounding-errors-with-round-to-odd/
> 
> No, because it would just offload the problem onto getting a conversion 
> from _Decimal128 to double that is correctly rounded to odd, which is no 
> easier (indeed, requires more work, not less) than the original problem of 
> converting to float.
> 
Joseph I think we are talking pass each other on this.

The PowerISA (2.05 and later ) Decimal Floating-point "Round to Prepare
for Shorter Precision" mode would not address the Decimal128
convert/truncate to shorter binary floating-point (double or float).

But it will address the Float128 convert/truncate to to shorter decimal
floating-pointer (_Decimal64 and _Decimal32).


> The existing code loses some of the original precision when taking just 15 
> digits of the mantissa for conversion to double (not OK when you want to 
> determine the exact value rounded to odd after further operations - in the 
> hard cases, the final decimal digit will affect the correct rounding).  
> Then the multiplications / divisions by precomputed powers of 10 use a 
> table of long double values - while that gives extra precision (though 
> probably not enough extra precision), it's also incompatible with doing 
> rounding to odd, since IBM long double doesn't give meaningful "inexact" 
> exceptions or work in non-default rounding modes, while rounding to odd 
> requires working in round-to-zero mode and then checking the "inexact" 
> flag.
> 
So in the case of TIMode or KFmode conversion to _Decimal64/_Decimal32
we can save the current rounding mode (fe_dec_getround()) then use
fe_dec_setround (DEC_ROUND_05UP) to set the "Round to Prepare for
Shorter Precision" before the multiply that converts the mantissa to the
target radix. Then just before the the instruction that rounds to the
final (_Decimal64 or _Decimal32) type, we restore the callers rounding
more and execute the final version in the correct rounding mode.

I believe that addresses you double rounding concern for these
conversions.

And now we are address the similar issues with float128 with the recent
public release of the PowerISA-3.0:
https://www.ibm.com/developerworks/community/blogs/fe313521-2e95-46f2-817d-44a4f27eba32/entry/Announcing_a_New_Era_of_Openness_with_Power_3_0?lang=en

The PowerISA-3.0 document is here: http://ibm.biz/power-isa3

As an addition to the Vector Scalar extents we add support for IEEE
128-bit binary floating point. This architecture provides the option to
override the default round-mode and force "round to odd" on a
instruction basis for all the key operators. This obviously includes
add/subtract/multiple/divide/sqrt and convert convert quad to double.

This will be sufficient to support the _Decimal128 to double and float
conversion that you mentioned as problematic.

While we are waiting for the hardware implementation of the
PowerISA-3.0, we need to address the specifics of these conversions in
the soft-fp implementation. 

My observation is that a common element of these conversion is a large
precision multiply (to convert the radix of the mantissa) then a
possible truncation (with rounding) to the final precision in the new
radix. 

It seem a simple effort to provide a soft-fp implementation that
combines the multiple and truncation, without intermediate rounding.

As the soft-fp implementation performs rounding in the FP_PACK
operation, it simple to avoid the intermediate PACK/UNPACK steps between
the FP_MUL and FP_TRUNC operations. The only trick is that arithmetic
operations user the canonical PACK/UNPACK operation while the Truncate
operations use the SEMIRAW PACK/UNPACK. Some adjustments of the exponent
are requires to flow the (unrounded) result of the FP_MUL directly into
the FP_TRUNC operation, but this does not seem to require a large
effort. The final FP_PACK operation following the FP_TRUNC will perform
the final and correct rounding to the require precision.

This seems sufficient to address the issues you have raised and seems
much simpler then wholesale additions of round to odd to the soft-fp
implementation. 


  parent reply	other threads:[~2015-12-08 17:16 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-30 19:18 RFC Migrating PowerPC to IEEE 128-bit Floating Point Steven Munroe
2015-09-30 21:15 ` Joseph Myers
2015-10-13 18:06   ` Steven Munroe
2015-10-13 18:53     ` Michael Meissner
2015-10-13 23:11     ` Joseph Myers
2015-10-14  7:38       ` Andreas Schwab
2015-10-14 12:13         ` Joseph Myers
2016-03-07 23:48       ` Paul E. Murphy
2016-03-08  1:31         ` Joseph Myers
2016-04-19 17:26           ` Steven Munroe
2016-04-21 20:47             ` Joseph Myers
2015-10-26 18:05   ` [PATCH 0/2] Add minimal code for IEEE 128-bit floating point Tulio Magno Quites Machado Filho
2015-10-26 18:12     ` Joseph Myers
2015-10-26 18:45       ` Michael Meissner
2015-10-26 19:51       ` Steven Munroe
2015-10-26 22:31         ` Joseph Myers
2015-10-26 18:06   ` [PATCH 1/2] soft-fp: Automatically create KF files from TF ones Tulio Magno Quites Machado Filho
2015-10-26 18:16     ` Joseph Myers
2015-10-26 18:44       ` Michael Meissner
2015-10-26 20:01       ` [PATCHv2] " Tulio Magno Quites Machado Filho
2015-10-26 22:32         ` Joseph Myers
2015-10-27 11:20           ` Tulio Magno Quites Machado Filho
2015-10-26 18:06   ` [PATCH 2/2] soft-fp: Add new KF routines Tulio Magno Quites Machado Filho
2015-10-26 18:34     ` Joseph Myers
2015-11-16 17:48 ` IEEE128 binary float to decimal float conversion routines Paul E. Murphy
2015-11-16 18:24   ` Joseph Myers
2015-11-16 18:40     ` Joseph Myers
2015-11-16 22:07     ` Christoph Lauter
2015-11-16 22:42       ` Joseph Myers
2015-12-18 21:12         ` Steven Munroe
2015-12-18 22:13           ` Joseph Myers
2015-12-19  5:03             ` Steven Munroe
2015-12-19 13:15               ` Joseph Myers
2015-12-19 16:40               ` Joseph Myers
2015-12-23 17:33                 ` Steven Munroe
2015-11-16 23:45     ` Paul E. Murphy
2015-11-17  0:07       ` Joseph Myers
     [not found]         ` <201511180131.tAI1Vs2L023118@d03av01.boulder.ibm.com>
2015-11-18  2:03           ` Joseph Myers
     [not found]             ` <201511182301.tAIN1Igc011083@d03av02.boulder.ibm.com>
2015-11-18 23:53               ` Joseph Myers
     [not found]                 ` <201511190052.tAJ0qd4x018924@d03av02.boulder.ibm.com>
2015-11-19  1:22                   ` Joseph Myers
2015-12-08 17:16                 ` Steven Munroe [this message]
2015-12-08 18:25                   ` Joseph Myers
2015-12-15 21:18                     ` Steven Munroe
2015-12-16  0:07                       ` Joseph Myers
2015-11-19 17:57             ` Paul E. Murphy
2015-11-19 18:14               ` Joseph Myers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1449594999.9274.45.camel@oc7878010663 \
    --to=munroesj@linux.vnet.ibm.com \
    --cc=joseph@codesourcery.com \
    --cc=libc-alpha@sourceware.org \
    --cc=mrmeissn@us.ibm.com \
    --cc=murphyp@linux.vnet.ibm.com \
    --cc=sjmunroe@us.ibm.com \
    --cc=tuliom@linux.vnet.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).