1 /* Copyright (C) 2008-2015 Free Software Foundation, Inc.
2 Contributor: Joern Rennecke <joern.rennecke@embecosm.com>
3 on behalf of Synopsys Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* We use a polynom similar to a Tchebycheff polynom to get an initial
27 seed, and then use a newton-raphson iteration step to get an
29 If this result can't be rounded to the exact result with confidence, we
30 round to the value between the two closest representable values, and
31 test if the correctly rounded value is above or below this value.
33 Because of the Newton-raphson iteration step, an error in the seed at X
34 is amplified by X. Therefore, we don't want a Tchebycheff polynom
35 or a polynom that is close to optimal according to the maximum norm
36 on the errro of the seed value; we want one that is close to optimal
37 according to the maximum norm on the error of the result, i.e. we
38 want the maxima of the polynom to increase linearily.
39 Given an interval [X0,X2) over which to approximate,
40 with X1 := (X0+X2)/2, D := X1-X0, F := 1/D, and S := D/X1 we have,
41 like for Tchebycheff polynoms:
45 P(2) := 2 * X^2 + S*D * X - D^2
47 P(n+1) := 2 * X * P(n) - D^2 * P (n-1)
53 long double T[5]; /* Taylor polynom */
56 long double X0, X1, X2, S;
57 long double inc = 1./64;
58 long double D = inc*0.5;
61 memset (P, 0, sizeof (P));
63 for (i = 1; i < 5; i++)
66 for (X0 = 1.; X0 < 2.; X0 += inc)
72 for (i = 1; i < 5; i++)
73 T[i] = T[i-1] * -T[0];
75 printf ("T %1.8f %f %f %f %f\n", (double)T[0], (double)T[1], (double)T[2],
76 (double)T[3], (double)T[4]);
80 for (i = 3; i < 5; i++)
82 P[i][0] = -D*D*P[i-2][0];
83 for (j = 1; j < i; j++)
84 P[i][j] = 2*P[i-1][j-1]-D*D*P[i-2][j];
87 printf ("P3 %1.8f %f %f %f %f\n", (double)P[3][0], (double)P[3][1], (double)P[3][2],
88 (double)P[3][3], (double)P[3][4]);
89 printf ("P4 %1.8f %f %f %f %f\n", (double)P[4][0], (double)P[4][1], (double)P[4][2],
90 (double)P[4][3], (double)P[4][4]);
92 for (i = 4; i > 1; i--)
94 long double a = T[i]/P[i][i];
96 for (j = 0; j < i; j++)
100 printf ("A %1.8f %f %f\n", (double)T[0], (double)T[1], (double)T[2]);
104 long double a = (T[2]-i/512.)/P[2][2];
105 for (j = 0; j < 2; j++)
110 for (i = 0, i0 = 0; i < 4; i++)
114 i1 = T[1]*8192. + i0 / (long double)(1 << 19) - 0.5;
115 i1 = - (-i1 & 0x1fff);
116 Ti1 = ((unsigned)(-i1 << 19) | i0) /-(long double)(1LL<<32LL);
117 T0 = T[0] - (T[1]-Ti1)/P[1][1] * P[1][0] - (X1 - 1) * Ti1;
118 i0 = T0 * 512 * 1024 + 0.5;
122 printf ("A %1.8f %f %f\n", (double)T[0], (double)T[1], (double)T[2]);
124 printf ("\t.long 0x%x\n", (-i1 << 19) | i0);