1*8feb0f0bSmrg /* Copyright (C) 2004-2020 Free Software Foundation, Inc.
236ac495dSmrg
336ac495dSmrg This file is free software; you can redistribute it and/or modify it
436ac495dSmrg under the terms of the GNU General Public License as published by the
536ac495dSmrg Free Software Foundation; either version 3, or (at your option) any
636ac495dSmrg later version.
736ac495dSmrg
836ac495dSmrg This file is distributed in the hope that it will be useful, but
936ac495dSmrg WITHOUT ANY WARRANTY; without even the implied warranty of
1036ac495dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1136ac495dSmrg General Public License for more details.
1236ac495dSmrg
1336ac495dSmrg Under Section 7 of GPL version 3, you are granted additional
1436ac495dSmrg permissions described in the GCC Runtime Library Exception, version
1536ac495dSmrg 3.1, as published by the Free Software Foundation.
1636ac495dSmrg
1736ac495dSmrg You should have received a copy of the GNU General Public License and
1836ac495dSmrg a copy of the GCC Runtime Library Exception along with this program;
1936ac495dSmrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2036ac495dSmrg <http://www.gnu.org/licenses/>. */
2136ac495dSmrg
2236ac495dSmrg /* Calculate division table for SH2..4 integer division
2336ac495dSmrg Contributed by Joern Rernnecke
2436ac495dSmrg joern.rennecke@superh.com */
2536ac495dSmrg
2636ac495dSmrg #include <stdio.h>
2736ac495dSmrg #include <math.h>
2836ac495dSmrg
2936ac495dSmrg int
main()3036ac495dSmrg main ()
3136ac495dSmrg {
3236ac495dSmrg int i, j;
3336ac495dSmrg double q, r, err, max_err = 0, max_s_err = 0;
3436ac495dSmrg
3536ac495dSmrg puts("/* This table has been generated by divtab-sh4.c. */");
3636ac495dSmrg puts ("\t.balign 4");
3736ac495dSmrg puts ("LOCAL(div_table_clz):");
3836ac495dSmrg /* output some dummy number for 1/0. */
3936ac495dSmrg printf ("\t.byte\t%d\n", 0);
4036ac495dSmrg for (i = 1; i <= 128; i++)
4136ac495dSmrg {
4236ac495dSmrg int n = 0;
4336ac495dSmrg if (i == 128)
4436ac495dSmrg puts ("\
4536ac495dSmrg /* Lookup table translating positive divisor to index into table of\n\
4636ac495dSmrg normalized inverse. N.B. the '0' entry is also the last entry of the\n\
4736ac495dSmrg previous table, and causes an unaligned access for division by zero. */\n\
4836ac495dSmrg LOCAL(div_table_ix):");
4936ac495dSmrg for (j = i; j <= 128; j += j)
5036ac495dSmrg n++;
5136ac495dSmrg printf ("\t.byte\t%d\n", n - 7);
5236ac495dSmrg }
5336ac495dSmrg for (i = 1; i <= 128; i++)
5436ac495dSmrg {
5536ac495dSmrg j = i < 0 ? -i : i;
5636ac495dSmrg while (j < 128)
5736ac495dSmrg j += j;
5836ac495dSmrg printf ("\t.byte\t%d\n", j * 2 - 96*4);
5936ac495dSmrg }
6036ac495dSmrg puts("\
6136ac495dSmrg /* 1/64 .. 1/127, normalized. There is an implicit leading 1 in bit 32. */\n\
6236ac495dSmrg .balign 4\n\
6336ac495dSmrg LOCAL(zero_l):");
6436ac495dSmrg for (i = 64; i < 128; i++)
6536ac495dSmrg {
6636ac495dSmrg if (i == 96)
6736ac495dSmrg puts ("LOCAL(div_table):");
6836ac495dSmrg q = 4.*(1<<30)*128/i;
6936ac495dSmrg r = ceil (q);
7036ac495dSmrg /* The value for 64 is actually differently scaled that it would
7136ac495dSmrg appear from this calculation. The implicit part is %01, not 10.
7236ac495dSmrg Still, since the value in the table is 0 either way, this
7336ac495dSmrg doesn't matter here. Still, the 1/64 entry is effectively a 1/128
7436ac495dSmrg entry. */
7536ac495dSmrg printf ("\t.long\t0x%X\n", (unsigned) r);
7636ac495dSmrg err = r - q;
7736ac495dSmrg if (err > max_err)
7836ac495dSmrg max_err = err;
7936ac495dSmrg err = err * i / 128;
8036ac495dSmrg if (err > max_s_err)
8136ac495dSmrg max_s_err = err;
8236ac495dSmrg }
8336ac495dSmrg printf ("\t/* maximum error: %f scaled: %f*/\n", max_err, max_s_err);
8436ac495dSmrg exit (0);
8536ac495dSmrg }
86