source: trunk/src/mkhdr.c@ 70

Last change on this file since 70 was 1, checked in by katerina, 19 years ago

Initial import

File size: 4.9 KB
RevLine 
[1]1#include "config.h"
2
3#ifdef HAVE_BROKEN_INCLUDES
4#define _ANSI_C_SOURCE
5#define _POSIX_SOURCE
6#endif
7
8#include <stdio.h>
9
10extern void exit(int status);
11extern int fclose(FILE *stream);
12
13#ifndef TRUE
14#define TRUE (1 == 1)
15#endif
16
17#ifndef FALSE
18#define FALSE (1 != 1)
19#endif
20
21struct description_int
22{
23 int int_size;
24 char int_name[20];
25};
26struct description_int int_descrs[] =
27{
28#ifdef LONGLONG
29 { sizeof(unsigned long long), "unsigned long long" },
30#endif
31 { sizeof(unsigned long), "unsigned long" },
32 { sizeof(unsigned int), "unsigned int" },
33 { sizeof(unsigned short), "unsigned short" },
34 { sizeof(unsigned char), "unsigned char" }
35};
36int size_count = sizeof(int_descrs) / sizeof(struct description_int);
37
38struct trnc_data
39{
40 long a, b, q, r;
41};
42struct trnc_data trunc_tbl[] =
43{
44 { 13, 4, 3, 1 },
45 { 13, -4, -3, 1 },
46 { -13, 4, -3, -1 },
47 { -13, -4, 3, -1 },
48 { 15, 4, 3, 3 },
49 { 15, -4, -3, 3 },
50 { -15, 4, -3, -3 },
51 { -15, -4, 3, -3 }
52};
53int size_trunc_tbl = sizeof(trunc_tbl) / sizeof(struct trnc_data);
54
55struct rand_data
56{
57 unsigned long dig_size, a1, c1, a2, c2;
58};
59struct rand_data rand_tbl[] =
60{
61 { 8, 197, 11, 37, 37 },
62 { 16, 805, 345, 925, 767 },
63 { 18, 13405, 4801, 20325, 19777 },
64 { 32, 13405, 4801, 20325, 19777 }
65};
66int size_rand_tbl = sizeof(rand_tbl) / sizeof(struct rand_data);
67
68int digit_bits;
69
70int
71div_trunc_p(void)
72{
73 int i;
74
75 for (i = 0; i < size_trunc_tbl; i++)
76 {
77 if (trunc_tbl[i].a / trunc_tbl[i].b != trunc_tbl[i].q)
78 {
79 printf("%ld / %ld = %ld, should have been %ld\n",
80 trunc_tbl[i].a,
81 trunc_tbl[i].b,
82 trunc_tbl[i].a / trunc_tbl[i].b,
83 trunc_tbl[i].q);
84 printf("when division truncates towards zero.\n");
85 return FALSE;
86 }
87 if (trunc_tbl[i].a % trunc_tbl[i].b != trunc_tbl[i].r)
88 {
89 printf("%ld %% %ld = %ld, should have been %ld\n",
90 trunc_tbl[i].a,
91 trunc_tbl[i].b,
92 trunc_tbl[i].a % trunc_tbl[i].b,
93 trunc_tbl[i].r);
94 printf("when division truncates towards zero.\n");
95 printf("Here a != q*b + r. Very strange!\n");
96 return FALSE;
97 }
98 }
99 return TRUE;
100}
101
102int
103charsize(void)
104{
105 int i = 0;
106 unsigned char ch = 1;
107 unsigned char oldch = 0;
108
109 while (ch > oldch)
110 {
111 oldch = ch;
112 ch <<= 1;
113 i++;
114 }
115 return i;
116}
117
118void
119RandDefsOut(FILE *fpOut, int dig_size)
120{
121 int i = 0;
122
123 while ((i < size_rand_tbl) && (dig_size != (int)rand_tbl[i].dig_size))
124 {
125 i++;
126 }
127 if (i == size_rand_tbl)
128 {
129 if (dig_size < (int) rand_tbl[0].dig_size)
130 {
131 printf("Digit size %d too small.\n", dig_size);
132 exit(10);
133 }
134 while ((int) rand_tbl[--i].dig_size > dig_size)
135 {
136 /* Count down `i' */
137 }
138 }
139 fprintf(fpOut, "\n#define BIG_RAND_A1 %ld\n", rand_tbl[i].a1);
140 fprintf(fpOut, "#define BIG_RAND_C1 %ld\n", rand_tbl[i].c1);
141 fprintf(fpOut, "#define BIG_RAND_A2 %ld\n", rand_tbl[i].a2);
142 fprintf(fpOut, "#define BIG_RAND_C2 %ld\n", rand_tbl[i].c2);
143}
144
145int
146main(void)
147{
148 int i, j = 0, ints_found = FALSE, char_bits;
149 FILE *fpOut;
150
151 if (!div_trunc_p())
152 {
153 printf("Can't do division on this machine!\n");
154 exit(10);
155 }
156
157 if ((fpOut = fopen("internal.h", "w")) == NULL)
158 {
159 printf("Could not create \"internal.h\".\n");
160 exit(10);
161 }
162
163 fprintf(fpOut, "#ifndef _BIGNUM_INTERNAL_H_\n");
164 fprintf(fpOut, "#define _BIGNUM_INTERNAL_H_\n\n");
165
166 char_bits = charsize();
167
168 for (i = 0; (i < size_count - 1) && !ints_found; i++)
169 {
170 for (j = i + 1; (j < size_count) && !ints_found; j++)
171 {
172 if (int_descrs[i].int_size >= 2 * int_descrs[j].int_size)
173 {
174 fprintf(fpOut,
175 "#define BIGNUM_DIGIT %s\n",
176 int_descrs[j].int_name);
177 fprintf(fpOut,
178 "#define BIGNUM_TWO_DIGITS %s\n\n",
179 int_descrs[i].int_name);
180 ints_found = TRUE;
181 }
182 }
183 }
184
185 if (!ints_found)
186 {
187 fprintf(stderr, "Strange, no integer type was two times bigger ");
188 fprintf(stderr, "than another integer type.\n");
189 fprintf(stderr, "Can't create header file.\nExiting.\n");
190 exit(10);
191 }
192
193 if (i > 0) i--;
194 if (j > 0) j--;
195
196 digit_bits = char_bits * int_descrs[j].int_size;
197 fprintf(fpOut, "#define BIG_CHARBITS %d\n", char_bits);
198 fprintf(fpOut, "#define BIGNUM_DIGIT_BITS %d\n", digit_bits);
199 fprintf(fpOut, "#define BIGNUM_TWO_DIGITS_BITS %d\n\n",
200 char_bits * int_descrs[i].int_size);
201
202 fprintf(fpOut, "struct big_struct\n{\n int sign;\n");
203 fprintf(fpOut, " unsigned long dgs_alloc;\n");
204 fprintf(fpOut, " unsigned long dgs_used;\n");
205 fprintf(fpOut, " BIGNUM_DIGIT *dp;\n};\n");
206
207 RandDefsOut(fpOut, char_bits * int_descrs[j].int_size);
208
209 if (sizeof(long) == sizeof(int))
210 {
211 fprintf(fpOut, "\n#define MEMCPY_LONG_COUNTER\n");
212 }
213
214#ifdef BIG_SHORT_NAMES
215 fprintf(fpOut, "\n#define BIG_SHORT_NAMES\n");
216#endif
217
218 fprintf(fpOut, "\n#endif\n");
219 fclose(fpOut);
220 exit(0);
221 return 0; /* Keep gcc from complaining */
222}
Note: See TracBrowser for help on using the repository browser.