1 /* Copyright (C) 2003 Free Software Foundation, Inc.
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 2, or (at your option) any
8 In addition to the permissions in the GNU General Public License, the
9 Free Software Foundation gives you unlimited permission to link the
10 compiled version of this file into combinations with other programs,
11 and to distribute those combinations without any restriction coming
12 from the use of this file. (The General Public License restrictions
13 do apply in other respects; for example, they cover modification of
14 the file, and distribution when not linked into a combine
17 This file is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; see the file COPYING. If not, write to
24 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
25 Boston, MA 02110-1301, USA. */
27 /* Calculate division table for SH5Media integer division
28 Contributed by Joern Rennecke
29 joern.rennecke@superh.com */
35 #define N_ENTRIES (1 << BITS)
36 #define CUTOFF_BITS 20
40 double max_defect
= 0.;
43 double min_defect
= 1e9
;
46 double max_defect2
= 0.;
49 double min_defect2
= 0.;
52 double min_defect3
= 01e9
;
56 double max_defect3
= 0.;
60 static double note_defect3 (int val
, double d2
, double y2d
, double x
)
62 int cutoff_val
= val
>> CUTOFF_BITS
;
68 cutoff
= (cutoff_val
* (1<<CUTOFF_BITS
) - val
) * y2d
;
69 defect
= cutoff
+ val
* d2
;
72 if (defect
> max_defect3
)
76 max_defect3_val
= val
;
78 if (defect
< min_defect3
)
82 min_defect3_val
= val
;
86 /* This function assumes 32 bit integers. */
88 calc_defect (double x
, int constant
, int factor
)
90 double y0
= (constant
- (int) floor ((x
* factor
* 64.))) / 16384.;
91 double y1
= 2 * y0
-y0
* y0
* (x
+ BIAS
/ (1.*(1LL<<30)));
96 y1
= floor (y1
* (1024 * 1024 * 1024)) / (1024 * 1024 * 1024);
108 y2d0
= floor (y1
* x
* (1LL << 60-16));
109 y2d1
= (int) (long long) y2d0
;
110 y2d
= - floor ((y1
- y0
/ (1<<30-14)) * y2d1
) / (1LL<<44);
112 if (d2
> max_defect2
)
117 if (d2
< min_defect2
)
122 /* zero times anything is trivially zero. */
123 note_defect3 ((1 << CUTOFF_BITS
) - 1, d2
, y2d
, x
);
124 note_defect3 (1 << CUTOFF_BITS
, d2
, y2d
, x
);
125 note_defect3 ((1U << 31) - (1 << CUTOFF_BITS
), d2
, y2d
, x
);
126 note_defect3 ((1U << 31) - 1, d2
, y2d
, x
);
127 note_defect3 (-1, d2
, y2d
, x
);
128 note_defect3 (-(1 << CUTOFF_BITS
), d2
, y2d
, x
);
129 note_defect3 ((1U << 31) - (1 << CUTOFF_BITS
) + 1, d2
, y2d
, x
);
130 note_defect3 (-(1U << 31), d2
, y2d
, x
);
138 unsigned char factors
[N_ENTRIES
];
139 short constants
[N_ENTRIES
];
140 int steps
= N_ENTRIES
/ 2;
141 double step
= 1. / steps
;
142 double eps30
= 1. / (1024 * 1024 * 1024);
144 for (i
= 0; i
< N_ENTRIES
; i
++)
146 double x_low
= (i
< steps
? 1. : -3.) + i
* step
;
147 double x_high
= x_low
+ step
- eps30
;
149 int factor
, constant
;
150 double low_defect
, med_defect
, high_defect
, max_defect
;
152 factor
= (1./x_low
- 1./x_high
) / step
* 256. + 0.5;
156 /* Use minimum of error function for x_med. */
157 x_med
= sqrt (256./factor
);
160 low_defect
= 1. / x_low
+ x_low
* factor
/ 256.;
161 high_defect
= 1. / x_high
+ x_high
* factor
/ 256.;
162 med_defect
= 1. / x_med
+ x_med
* factor
/ 256.;
164 = ((low_defect
> high_defect
) ^ (x_med
< 0)) ? low_defect
: high_defect
;
165 constant
= (med_defect
+ max_defect
) * 0.5 * 16384. + 0.5;
166 if (constant
< -32768 || constant
> 32767)
168 constants
[i
] = constant
;
169 calc_defect (x_low
, constant
, factor
);
170 calc_defect (x_med
, constant
, factor
);
171 calc_defect (x_high
, constant
, factor
);
173 printf ("/* This table has been generated by divtab.c .\n");
174 printf ("Defects for bias %d:\n", BIAS
);
175 printf (" Max defect: %e at %e\n", max_defect
, max_defect_x
);
176 printf (" Min defect: %e at %e\n", min_defect
, min_defect_x
);
177 printf (" Max 2nd step defect: %e at %e\n", max_defect2
, max_defect2_x
);
178 printf (" Min 2nd step defect: %e at %e\n", min_defect2
, min_defect2_x
);
179 printf (" Max div defect: %e at %d:%e\n", max_defect3
, max_defect3_val
, max_defect3_x
);
180 printf (" Min div defect: %e at %d:%e\n", min_defect3
, min_defect3_val
, min_defect3_x
);
181 printf (" Defect at 1: %e\n",
182 calc_defect (1., constants
[0], factors
[0]));
183 printf (" Defect at -2: %e */\n",
184 calc_defect (-2., constants
[steps
], factors
[steps
]));
185 printf ("\t.section\t.rodata\n");
186 printf ("\t.balign 2\n");
187 printf ("/* negative division constants */\n");
188 for (i
= steps
; i
< 2 * steps
; i
++)
189 printf ("\t.word\t%d\n", constants
[i
]);
190 printf ("/* negative division factors */\n");
191 for (i
= steps
; i
< 2*steps
; i
++)
192 printf ("\t.byte\t%d\n", factors
[i
]);
193 printf ("\t.skip %d\n", steps
);
194 printf ("\t.global GLOBAL(div_table):\n");
195 printf ("GLOBAL(div_table):\n");
196 printf ("\t.skip %d\n", steps
);
197 printf ("/* positive division factors */\n");
198 for (i
= 0; i
< steps
; i
++)
199 printf ("\t.byte\t%d\n", factors
[i
]);
200 printf ("/* positive division constants */\n");
201 for (i
= 0; i
< steps
; i
++)
202 printf ("\t.word\t%d\n", constants
[i
]);