comparison gcc/config/sh/divtab.c @ 0:a06113de4d67

first commit
author kent <kent@cr.ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2009 14:47:48 +0900
parents
children 04ced10e8804
comparison
equal deleted inserted replaced
-1:000000000000 0:a06113de4d67
1 /* Copyright (C) 2003, 2009 Free Software Foundation, Inc.
2
3 This file is free software; you can redistribute it and/or modify it
4 under the terms of the GNU General Public License as published by the
5 Free Software Foundation; either version 3, or (at your option) any
6 later version.
7
8 This file is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
12
13 Under Section 7 of GPL version 3, you are granted additional
14 permissions described in the GCC Runtime Library Exception, version
15 3.1, as published by the Free Software Foundation.
16
17 You should have received a copy of the GNU General Public License and
18 a copy of the GCC Runtime Library Exception along with this program;
19 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
20 <http://www.gnu.org/licenses/>. */
21
22
23 /* Calculate division table for SH5Media integer division
24 Contributed by Joern Rennecke
25 joern.rennecke@superh.com */
26
27 #include <stdio.h>
28 #include <math.h>
29
30 #define BITS 5
31 #define N_ENTRIES (1 << BITS)
32 #define CUTOFF_BITS 20
33
34 #define BIAS (-330)
35
36 double max_defect = 0.;
37 double max_defect_x;
38
39 double min_defect = 1e9;
40 double min_defect_x;
41
42 double max_defect2 = 0.;
43 double max_defect2_x;
44
45 double min_defect2 = 0.;
46 double min_defect2_x;
47
48 double min_defect3 = 01e9;
49 double min_defect3_x;
50 int min_defect3_val;
51
52 double max_defect3 = 0.;
53 double max_defect3_x;
54 int max_defect3_val;
55
56 static double note_defect3 (int val, double d2, double y2d, double x)
57 {
58 int cutoff_val = val >> CUTOFF_BITS;
59 double cutoff;
60 double defect;
61
62 if (val < 0)
63 cutoff_val++;
64 cutoff = (cutoff_val * (1<<CUTOFF_BITS) - val) * y2d;
65 defect = cutoff + val * d2;
66 if (val < 0)
67 defect = - defect;
68 if (defect > max_defect3)
69 {
70 max_defect3 = defect;
71 max_defect3_x = x;
72 max_defect3_val = val;
73 }
74 if (defect < min_defect3)
75 {
76 min_defect3 = defect;
77 min_defect3_x = x;
78 min_defect3_val = val;
79 }
80 }
81
82 /* This function assumes 32-bit integers. */
83 static double
84 calc_defect (double x, int constant, int factor)
85 {
86 double y0 = (constant - (int) floor ((x * factor * 64.))) / 16384.;
87 double y1 = 2 * y0 -y0 * y0 * (x + BIAS / (1.*(1LL<<30)));
88 double y2d0, y2d;
89 int y2d1;
90 double d, d2;
91
92 y1 = floor (y1 * (1024 * 1024 * 1024)) / (1024 * 1024 * 1024);
93 d = y1 - 1 / x;
94 if (d > max_defect)
95 {
96 max_defect = d;
97 max_defect_x = x;
98 }
99 if (d < min_defect)
100 {
101 min_defect = d;
102 min_defect_x = x;
103 }
104 y2d0 = floor (y1 * x * (1LL << 60-16));
105 y2d1 = (int) (long long) y2d0;
106 y2d = - floor ((y1 - y0 / (1<<30-14)) * y2d1) / (1LL<<44);
107 d2 = y1 + y2d - 1/x;
108 if (d2 > max_defect2)
109 {
110 max_defect2 = d2;
111 max_defect2_x = x;
112 }
113 if (d2 < min_defect2)
114 {
115 min_defect2 = d2;
116 min_defect2_x = x;
117 }
118 /* zero times anything is trivially zero. */
119 note_defect3 ((1 << CUTOFF_BITS) - 1, d2, y2d, x);
120 note_defect3 (1 << CUTOFF_BITS, d2, y2d, x);
121 note_defect3 ((1U << 31) - (1 << CUTOFF_BITS), d2, y2d, x);
122 note_defect3 ((1U << 31) - 1, d2, y2d, x);
123 note_defect3 (-1, d2, y2d, x);
124 note_defect3 (-(1 << CUTOFF_BITS), d2, y2d, x);
125 note_defect3 ((1U << 31) - (1 << CUTOFF_BITS) + 1, d2, y2d, x);
126 note_defect3 (-(1U << 31), d2, y2d, x);
127 return d;
128 }
129
130 int
131 main ()
132 {
133 int i;
134 unsigned char factors[N_ENTRIES];
135 short constants[N_ENTRIES];
136 int steps = N_ENTRIES / 2;
137 double step = 1. / steps;
138 double eps30 = 1. / (1024 * 1024 * 1024);
139
140 for (i = 0; i < N_ENTRIES; i++)
141 {
142 double x_low = (i < steps ? 1. : -3.) + i * step;
143 double x_high = x_low + step - eps30;
144 double x_med;
145 int factor, constant;
146 double low_defect, med_defect, high_defect, max_defect;
147
148 factor = (1./x_low- 1./x_high) / step * 256. + 0.5;
149 if (factor == 256)
150 factor = 255;
151 factors[i] = factor;
152 /* Use minimum of error function for x_med. */
153 x_med = sqrt (256./factor);
154 if (x_low < 0)
155 x_med = - x_med;
156 low_defect = 1. / x_low + x_low * factor / 256.;
157 high_defect = 1. / x_high + x_high * factor / 256.;
158 med_defect = 1. / x_med + x_med * factor / 256.;
159 max_defect
160 = ((low_defect > high_defect) ^ (x_med < 0)) ? low_defect : high_defect;
161 constant = (med_defect + max_defect) * 0.5 * 16384. + 0.5;
162 if (constant < -32768 || constant > 32767)
163 abort ();
164 constants[i] = constant;
165 calc_defect (x_low, constant, factor);
166 calc_defect (x_med, constant, factor);
167 calc_defect (x_high, constant, factor);
168 }
169 printf ("/* This table has been generated by divtab.c .\n");
170 printf ("Defects for bias %d:\n", BIAS);
171 printf (" Max defect: %e at %e\n", max_defect, max_defect_x);
172 printf (" Min defect: %e at %e\n", min_defect, min_defect_x);
173 printf (" Max 2nd step defect: %e at %e\n", max_defect2, max_defect2_x);
174 printf (" Min 2nd step defect: %e at %e\n", min_defect2, min_defect2_x);
175 printf (" Max div defect: %e at %d:%e\n", max_defect3, max_defect3_val, max_defect3_x);
176 printf (" Min div defect: %e at %d:%e\n", min_defect3, min_defect3_val, min_defect3_x);
177 printf (" Defect at 1: %e\n",
178 calc_defect (1., constants[0], factors[0]));
179 printf (" Defect at -2: %e */\n",
180 calc_defect (-2., constants[steps], factors[steps]));
181 printf ("\t.section\t.rodata\n");
182 printf ("\t.balign 2\n");
183 printf ("/* negative division constants */\n");
184 for (i = steps; i < 2 * steps; i++)
185 printf ("\t.word\t%d\n", constants[i]);
186 printf ("/* negative division factors */\n");
187 for (i = steps; i < 2*steps; i++)
188 printf ("\t.byte\t%d\n", factors[i]);
189 printf ("\t.skip %d\n", steps);
190 printf ("\t.global GLOBAL(div_table):\n");
191 printf ("GLOBAL(div_table):\n");
192 printf ("\t.skip %d\n", steps);
193 printf ("/* positive division factors */\n");
194 for (i = 0; i < steps; i++)
195 printf ("\t.byte\t%d\n", factors[i]);
196 printf ("/* positive division constants */\n");
197 for (i = 0; i < steps; i++)
198 printf ("\t.word\t%d\n", constants[i]);
199 exit (0);
200 }