[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / runtime / gmp / cre-conv-tab.c
1 /* cre-conv-tab.c -- Create conversion table in a wordsize-dependent way.
2
3 Copyright (C) 1991 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 The GNU MP Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with the GNU MP Library; see the file COPYING.  If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 #include "gmp.h"
22 #include "gmp-impl.h"
23 #include "longlong.h"
24
25 extern double floor ();
26 extern double log ();
27
28 static unsigned long int
29 upow (b, e)
30     unsigned long int b;
31     unsigned int e;
32 {
33   unsigned long int y = 1;
34
35   while (e != 0)
36     {
37       while ((e & 1) == 0)
38         {
39           b = b * b;
40           e >>= 1;
41         }
42       y = y * b;
43       e -= 1;
44     }
45
46   return y;
47 }
48
49 unsigned int
50 ulog2 (x)
51      unsigned long int x;
52 {
53   unsigned int i;
54   for (i = 0;  x != 0;  i++)
55     x >>= 1;
56   return i;
57 }
58
59 main ()
60 {
61   int i;
62   unsigned long idig;
63   unsigned long big_base, big_base_inverted;
64   double fdig;
65   int dummy;
66   int normalization_steps;
67
68   unsigned long int max_uli;
69   int bits_uli;
70
71   max_uli = 1;
72   for (i = 1; ; i++)
73     {
74       if ((max_uli << 1) == 0)
75         break;
76       max_uli <<= 1;
77     }
78   bits_uli = i;
79
80   puts ("/* __mp_bases -- Structure for conversion between internal binary");
81   puts ("   format and strings in base 2..36.  The fields are explained in");
82   puts ("   gmp-impl.h.");
83   puts ("");
84   puts ("   ***** THIS FILE WAS CREATED BY A PROGRAM.  DON'T EDIT IT! *****");
85   puts ("");
86   puts ("Copyright (C) 1991 Free Software Foundation, Inc.");
87   puts ("");
88   puts ("This file is part of the GNU MP Library.");
89   puts ("");
90   puts ("The GNU MP Library is free software; you can redistribute it and/or");
91   puts ("modify it under the terms of the GNU General Public License as");
92   puts ("published by the Free Software Foundation; either version 2, or");
93   puts ("(at your option) any later version.");
94   puts ("");
95   puts ("The GNU MP Library is distributed in the hope that it will be");
96   puts ("useful, but WITHOUT ANY WARRANTY; without even the implied warranty");
97   puts ("of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the");
98   puts ("GNU General Public License for more details.");
99   puts ("");
100   puts ("You should have received a copy of the GNU General Public License");
101   puts ("along with the GNU MP Library; see the file COPYING.  If not, write");
102   puts ("to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,");
103   puts ("USA.  */");
104   puts ("");
105   puts ("#include \"gmp.h\"");
106   puts ("#include \"gmp-impl.h\"");
107   puts ("");
108
109   puts ("const struct bases __mp_bases[37] =\n{");
110   puts ("  /*  0 */ {0, 0, 0, 0.0},");
111   puts ("  /*  1 */ {0, 0, 0, 0.0},");
112   for (i = 2; i <= 36; i++)
113     {
114       /* The weird expression here is because many /bin/cc compilers
115          generate incorrect code for conversions from large unsigned
116          integers to double.  */
117       fdig = log(2.0)/log((double) i);
118       idig = floor(bits_uli * fdig);
119       if ((i & (i - 1)) == 0)
120         {
121           big_base = ulog2 (i) - 1;
122           big_base_inverted = 0;
123         }
124       else
125         {
126           big_base = upow (i, idig);
127           for (normalization_steps = 0;
128                (long int) (big_base << normalization_steps) >= 0;
129                normalization_steps++)
130             ;
131           udiv_qrnnd (big_base_inverted, dummy,
132                       -(big_base << normalization_steps), 0,
133                       big_base << normalization_steps);
134         }
135       printf ("  /* %2u */ {%lu, 0x%lX, 0x%lX, %.8f},\n",
136               i, idig, big_base, big_base_inverted, fdig);
137     }
138   puts ("};");
139
140   exit (0);
141 }