Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch > #33070 > unrolled thread

NR reciprocal/division without extended precision

Started byalready5chosen@yahoo.com
First post2016-01-24 08:36 -0800
Last post2016-01-29 07:38 +0100
Articles 7 — 4 participants

Back to article view | Back to comp.arch


Contents

  NR reciprocal/division without extended precision already5chosen@yahoo.com - 2016-01-24 08:36 -0800
    Re: NR reciprocal/division without extended precision "James Van Buskirk" <not_valid@comcast.net> - 2016-01-24 10:29 -0700
      Re: NR reciprocal/division without extended precision MitchAlsup <MitchAlsup@aol.com> - 2016-01-24 09:46 -0800
        Re: NR reciprocal/division without extended precision already5chosen@yahoo.com - 2016-01-28 01:16 -0800
          Re: NR reciprocal/division without extended precision Terje Mathisen <terje.mathisen@tmsw.no> - 2016-01-28 21:01 +0100
            Re: NR reciprocal/division without extended precision already5chosen@yahoo.com - 2016-01-28 14:49 -0800
              Re: NR reciprocal/division without extended precision Terje Mathisen <terje.mathisen@tmsw.no> - 2016-01-29 07:38 +0100

#33070 — NR reciprocal/division without extended precision

Fromalready5chosen@yahoo.com
Date2016-01-24 08:36 -0800
SubjectNR reciprocal/division without extended precision
Message-ID<0eb97c14-d5ac-4e93-b063-1d1eba7831e1@googlegroups.com>
I make it a separate thread.

me:
Another problem with pure software NR reciprocal that I found yesterday:
For y=(2^53-1)/2^53 NR converges to x=1 instead of correct value of x=(2^52+1)/2^52.
So, some preventive measures should be taken. For me, setting few LSBs to 1 before last NR iterations worked fine.
I suppose, it is known in the literature, but I didn't read literature, just saw James's post and started hacking.

James Van Buskirk:
Check your arithmetic above.  N-R is only out by 0.5 ulps actually,
not 1.0 ulps.  Getting from 0.5+epsilon down to 0.5-epsilon is
about as hard as it was to get the right answer in the first place.

I checked my arithmetic above. And indeed my emulation of FMA by means of Gnu float128 was broken. It suffered from double rounding.
I tested several implementations of C99/C++11 fma() library function under Windows/x64/IvyBridge. Unfortunately, all of them were broken. Some, like MINGW/MINGW64, were broken in rude obvious way. Others, like VS3013, were broken in more subtle manner. But I didn't find one that works. So, I rolled my own. It's possible that it is broken too, but I don't think so.

So, I tested my statement above with supposedly good FMA and it still appears correct.
That is, if, by chance, we hit a correct value (x=(2^52+1)/2^52) as an initial approximation then we will stay here. If, however, the initial value is anything else then NR iterations will converge to 1.0 which is incorrect.

To be more specific, I tested only form of NR steps used by James on the first and last steps, i.e:
err = fma(-y, x, 1.0);
x = fma(x, err, x);

[toc] | [next] | [standalone]


#33072

From"James Van Buskirk" <not_valid@comcast.net>
Date2016-01-24 10:29 -0700
Message-ID<n831ib$505$1@dont-email.me>
In reply to#33070
> wrote in message 
> news:0eb97c14-d5ac-4e93-b063-1d1eba7831e1@googlegroups.com...

> I make it a separate thread.

> me:
> Another problem with pure software NR reciprocal that I found
> yesterday: For y=(2^53-1)/2^53 NR converges to x=1 instead of
> correct value of x=(2^52+1)/2^52.  So, some preventive measures
> should be taken. For me, setting few LSBs to 1 before last NR
> iterations worked fine.  I suppose, it is known in the literature,
> but I didn't read literature, just saw James's post and started hacking.

> James Van Buskirk:
> Check your arithmetic above.  N-R is only out by 0.5 ulps actually,
> not 1.0 ulps.  Getting from 0.5+epsilon down to 0.5-epsilon is
> about as hard as it was to get the right answer in the first place.

> I checked my arithmetic above. And indeed my emulation of
> FMA by means of Gnu float128 was broken. It suffered from
> double rounding. I tested several implementations of
> C99/C++11 fma() library function under Windows/x64/IvyBridge.
> Unfortunately, all of them were broken. Some, like
> MINGW/MINGW64, were broken in rude obvious way. Others,
> like VS3013, were broken in more subtle manner. But I didn't
> find one that works. So, I rolled my own. It's possible that it is
> broken too, but I don't think so.

> So, I tested my statement above with supposedly good FMA
> and it still appears correct. That is, if, by chance, we hit a
> correct value (x=(2^52+1)/2^52) as an initial approximation
> then we will stay here. If, however, the initial value is anything
> else then NR iterations will converge to 1.0 which is incorrect.

> To be more specific, I tested only form of NR steps used by
> James on the first and last steps, i.e:
> err = fma(-y, x, 1.0);
> x = fma(x, err, x);

There seems to be some sort of theorem that you need 2*n+1
bits of accuracy to get to n bit rounded perfectly.
http://www.informatik.uni-trier.de/Reports/TR-08-2004/rnc6_12_markstein.pdf
I haven't read that very carefully.  The error in your example is
almost exactly the same whichever way the final rounding goes,
0.5 ulps.  I'm not sure whether the above paper requires some
kind of extended precision to get the perfect rounding that you
desire.  Some might say that the perfect should not get in the
way of the pretty darned good, and in the only application I
have seen that is reciprocal throughput limited (polynomial
evaluation by barycentric interpolation in chebfun) perfect
rounding is not a requirement.

You might be able to capture 2*n+1st bit without extended
precision.  Take err from your equation above and from it build
capture by copying the exponent bits, maybe the sign bit, and
wiping out all the mantissa bits but setting the lowest manitssa
bit.  Then add err**2 to capture so that round to nearest even
will capture that low bit if it's there, then subtract the original
capture and finally add to err.  Then I think your final fma might
do what you want.

[toc] | [prev] | [next] | [standalone]


#33074

FromMitchAlsup <MitchAlsup@aol.com>
Date2016-01-24 09:46 -0800
Message-ID<0a991790-09f0-490f-897a-d138176da966@googlegroups.com>
In reply to#33072
On Sunday, January 24, 2016 at 11:30:29 AM UTC-6, James Van Buskirk wrote:

> There seems to be some sort of theorem that you need 2*n+1
> bits of accuracy to get to n bit rounded perfectly.

Its either 2n+2 or 2n+3 depending upon certain setups of the iteration
algorithm. And indeed Peter Markstein is the right literature reference.

[toc] | [prev] | [next] | [standalone]


#33213

Fromalready5chosen@yahoo.com
Date2016-01-28 01:16 -0800
Message-ID<960d8ecf-d251-40fc-993a-78fb8b3d25ba@googlegroups.com>
In reply to#33074
On Sunday, January 24, 2016 at 7:46:12 PM UTC+2, MitchAlsup wrote:
> On Sunday, January 24, 2016 at 11:30:29 AM UTC-6, James Van Buskirk wrote:
> 
> > There seems to be some sort of theorem that you need 2*n+1
> > bits of accuracy to get to n bit rounded perfectly.
> 
> Its either 2n+2 or 2n+3 depending upon certain setups of the iteration
> algorithm. And indeed Peter Markstein is the right literature reference.

First, let's agree on terminology: n bits of matissa + 1 hidden bit are considered n+1 bits. IEEE-754 binary64 = 53 bit.

According to my math, y = 1-2^(-n-1) is the only argument in range [0.5..1) that requires 2n+3 bits for convergence. 
y = 1-3*2^(-n-1) is another argument that can ask for 2n+2 bits, but only in unusual rounding modes. In the default 'round to nearest even' mode it will converge to correct value with 2n+1 bits.
It looks like 2n+1 bits are sufficient for the rest of the arguments in all rounding modes.

[toc] | [prev] | [next] | [standalone]


#33243

FromTerje Mathisen <terje.mathisen@tmsw.no>
Date2016-01-28 21:01 +0100
Message-ID<n8ds2n$1iq$1@gioia.aioe.org>
In reply to#33213
already5chosen@yahoo.com wrote:
> On Sunday, January 24, 2016 at 7:46:12 PM UTC+2, MitchAlsup wrote:
>> On Sunday, January 24, 2016 at 11:30:29 AM UTC-6, James Van Buskirk
>> wrote:
>>
>>> There seems to be some sort of theorem that you need 2*n+1 bits
>>> of accuracy to get to n bit rounded perfectly.
>>
>> Its either 2n+2 or 2n+3 depending upon certain setups of the
>> iteration algorithm. And indeed Peter Markstein is the right
>> literature reference.
>
> First, let's agree on terminology: n bits of matissa + 1 hidden bit
> are considered n+1 bits. IEEE-754 binary64 = 53 bit.

The IEEE double has a n==52-bit mantissa. :-)

Otherwise there wouldn't be room for an 11-bit exponent and a sign bit.
>
> According to my math, y = 1-2^(-n-1) is the only argument in range
> [0.5..1) that requires 2n+3 bits for convergence. y = 1-3*2^(-n-1) is
> another argument that can ask for 2n+2 bits, but only in unusual
> rounding modes. In the default 'round to nearest even' mode it will
> converge to correct value with 2n+1 bits. It looks like 2n+1 bits are
> sufficient for the rest of the arguments in all rounding modes.

So what you are saying is that it might be worth it to recognize an 
all-1 mantissa and specialcase it in order to avoid the need for two 
extra bits of intermediate precision?

Terje

-- 
- <Terje.Mathisen at tmsw.no>
"almost all programming can be viewed as an exercise in caching"

[toc] | [prev] | [next] | [standalone]


#33252

Fromalready5chosen@yahoo.com
Date2016-01-28 14:49 -0800
Message-ID<39db337e-e09f-4d69-a860-53a26788788f@googlegroups.com>
In reply to#33243
On Thursday, January 28, 2016 at 10:01:33 PM UTC+2, Terje Mathisen wrote:
> already5chosen@yahoo.com wrote:
> > On Sunday, January 24, 2016 at 7:46:12 PM UTC+2, MitchAlsup wrote:
> >> On Sunday, January 24, 2016 at 11:30:29 AM UTC-6, James Van Buskirk
> >> wrote:
> >>
> >>> There seems to be some sort of theorem that you need 2*n+1 bits
> >>> of accuracy to get to n bit rounded perfectly.
> >>
> >> Its either 2n+2 or 2n+3 depending upon certain setups of the
> >> iteration algorithm. And indeed Peter Markstein is the right
> >> literature reference.
> >
> > First, let's agree on terminology: n bits of matissa + 1 hidden bit
> > are considered n+1 bits. IEEE-754 binary64 = 53 bit.
> 
> The IEEE double has a n==52-bit mantissa. :-)
>

Yes, 52 bits stored, but 53 bits significant.

But then I managed to fool myself with all +1s and -1s.
What I meant to say that for proper rounding of the worst case of IEEE-754 binary64 we need to take into account 107 significant bits. Which makes it 2n+1 rather than 2n+3 :(

> Otherwise there wouldn't be room for an 11-bit exponent and a sign bit.
> >
> > According to my math, y = 1-2^(-n-1) is the only argument in range
> > [0.5..1) that requires 2n+3 bits for convergence. y = 1-3*2^(-n-1) is
> > another argument that can ask for 2n+2 bits, but only in unusual
> > rounding modes. In the default 'round to nearest even' mode it will
> > converge to correct value with 2n+1 bits. It looks like 2n+1 bits are
> > sufficient for the rest of the arguments in all rounding modes.
> 
> So what you are saying is that it might be worth it to recognize an 
> all-1 mantissa and specialcase it in order to avoid the need for two 
> extra bits of intermediate precision?

I think, the best practical solution would be to generate an exact result for this bit pattern as an initial approximation. With table-driven initial approximation it will be really easy.
For example, 16-entry table addressed by 4 MS bit of mantissa:
0 => 32/sqrt(16*17), // [16/32..17/32)
1 => 32/sqrt(17*18), // [17/32..18/32)
2 => 32/sqrt(18*19), // [18/32..19/32)
...
14 =>32/sqrt(30*31), // [30/32..31/32)
15 =>1-2^-53,        // [31/32..1)

Pay attention that the worst case relative error in "asymmetric" last range is approximately the same as in symmetric first range, so # of required NR steps is not impacted by the trick.


> 
> Terje
> 
> -- 
> - <Terje.Mathisen at tmsw.no>
> "almost all programming can be viewed as an exercise in caching"

[toc] | [prev] | [next] | [standalone]


#33269

FromTerje Mathisen <terje.mathisen@tmsw.no>
Date2016-01-29 07:38 +0100
Message-ID<n8f1dl$1e7v$1@gioia.aioe.org>
In reply to#33252
already5chosen@yahoo.com wrote:
> On Thursday, January 28, 2016 at 10:01:33 PM UTC+2, Terje Mathisen
>> So what you are saying is that it might be worth it to recognize
>> an all-1 mantissa and specialcase it in order to avoid the need for
>> two extra bits of intermediate precision?
>
> I think, the best practical solution would be to generate an exact
> result for this bit pattern as an initial approximation. With
> table-driven initial approximation it will be really easy. For
> example, 16-entry table addressed by 4 MS bit of mantissa: 0 =>
> 32/sqrt(16*17), // [16/32..17/32) 1 => 32/sqrt(17*18), //
> [17/32..18/32) 2 => 32/sqrt(18*19), // [18/32..19/32) ... 14
> =>32/sqrt(30*31), // [30/32..31/32) 15 =>1-2^-53,        //
> [31/32..1)
>
> Pay attention that the worst case relative error in "asymmetric" last
> range is approximately the same as in symmetric first range, so # of
> required NR steps is not impacted by the trick.

Yeah, that would work. The only real drawback is the fact that you have 
to work with full double precision all the way, instead of starting with 
4-5 bits and doubling it in each stage.

The latter approach is more interesting for a sw emulation using 
unsigned integer operations!

Terje

-- 
- <Terje.Mathisen at tmsw.no>
"almost all programming can be viewed as an exercise in caching"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.arch


csiph-web