divtab-sh4.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Copyright (C) 2004-2015 Free Software Foundation, Inc.
  2. This file is free software; you can redistribute it and/or modify it
  3. under the terms of the GNU General Public License as published by the
  4. Free Software Foundation; either version 3, or (at your option) any
  5. later version.
  6. This file is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. General Public License for more details.
  10. Under Section 7 of GPL version 3, you are granted additional
  11. permissions described in the GCC Runtime Library Exception, version
  12. 3.1, as published by the Free Software Foundation.
  13. You should have received a copy of the GNU General Public License and
  14. a copy of the GCC Runtime Library Exception along with this program;
  15. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  16. <http://www.gnu.org/licenses/>. */
  17. /* Calculate division table for SH2..4 integer division
  18. Contributed by Joern Rernnecke
  19. joern.rennecke@superh.com */
  20. #include <stdio.h>
  21. #include <math.h>
  22. int
  23. main ()
  24. {
  25. int i, j;
  26. double q, r, err, max_err = 0, max_s_err = 0;
  27. puts("/* This table has been generated by divtab-sh4.c. */");
  28. puts ("\t.balign 4");
  29. puts ("LOCAL(div_table_clz):");
  30. /* output some dummy number for 1/0. */
  31. printf ("\t.byte\t%d\n", 0);
  32. for (i = 1; i <= 128; i++)
  33. {
  34. int n = 0;
  35. if (i == 128)
  36. puts ("\
  37. /* Lookup table translating positive divisor to index into table of\n\
  38. normalized inverse. N.B. the '0' entry is also the last entry of the\n\
  39. previous table, and causes an unaligned access for division by zero. */\n\
  40. LOCAL(div_table_ix):");
  41. for (j = i; j <= 128; j += j)
  42. n++;
  43. printf ("\t.byte\t%d\n", n - 7);
  44. }
  45. for (i = 1; i <= 128; i++)
  46. {
  47. j = i < 0 ? -i : i;
  48. while (j < 128)
  49. j += j;
  50. printf ("\t.byte\t%d\n", j * 2 - 96*4);
  51. }
  52. puts("\
  53. /* 1/64 .. 1/127, normalized. There is an implicit leading 1 in bit 32. */\n\
  54. .balign 4\n\
  55. LOCAL(zero_l):");
  56. for (i = 64; i < 128; i++)
  57. {
  58. if (i == 96)
  59. puts ("LOCAL(div_table):");
  60. q = 4.*(1<<30)*128/i;
  61. r = ceil (q);
  62. /* The value for 64 is actually differently scaled that it would
  63. appear from this calculation. The implicit part is %01, not 10.
  64. Still, since the value in the table is 0 either way, this
  65. doesn't matter here. Still, the 1/64 entry is effectively a 1/128
  66. entry. */
  67. printf ("\t.long\t0x%X\n", (unsigned) r);
  68. err = r - q;
  69. if (err > max_err)
  70. max_err = err;
  71. err = err * i / 128;
  72. if (err > max_s_err)
  73. max_s_err = err;
  74. }
  75. printf ("\t/* maximum error: %f scaled: %f*/\n", max_err, max_s_err);
  76. exit (0);
  77. }