5084155b |
1 | /* -*-c-*- |
2 | * |
e8776ccd |
3 | * $Id$ |
0b181057 |
4 | * |
5 | * Generate `mptypes.h' header file for current architecture |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
5084155b |
28 | */ |
29 | |
0b181057 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
5084155b |
32 | #define _GNU_SOURCE |
33 | #include <stdio.h> |
34 | #include <limits.h> |
35 | #if __STDC_VERSION__ >= 199900l |
36 | # include <stdint.h> |
e8776ccd |
37 | # include <inttypes.h> |
5084155b |
38 | #endif |
39 | |
0b181057 |
40 | /*----- Data types --------------------------------------------------------*/ |
41 | |
5084155b |
42 | /* --- Hack for GCC --- * |
43 | * |
44 | * WG14 in their infinite wisdom decided not to use the GCC constant name. |
45 | */ |
46 | |
add9d49f |
47 | #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 91) |
48 | # define EXT __extension__ |
49 | #else |
50 | # define EXT |
51 | #endif |
52 | |
5084155b |
53 | #if defined(ULONG_LONG_MAX) && !defined(ULLONG_MAX) |
54 | # define ULLONG_MAX ULONG_LONG_MAX |
55 | #endif |
56 | |
57 | /* --- Choose the largest integer type --- */ |
58 | |
8282072d |
59 | #if defined(UINTMAX_MAX) && defined(PRIuMAX) |
5084155b |
60 | typedef uintmax_t umax; |
61 | # define P_UMAX PRIuMAX |
62 | #elif defined(ULLONG_MAX) |
add9d49f |
63 | __extension__ typedef unsigned long long umax; |
e8776ccd |
64 | # define P_UMAX "llu" |
5084155b |
65 | #else |
66 | typedef unsigned long umax; |
e8776ccd |
67 | # define P_UMAX "lu" |
5084155b |
68 | #endif |
69 | |
70 | /* --- Table of interesting types --- * |
71 | * |
72 | * These are in preference order. |
73 | */ |
74 | |
75 | enum { |
add9d49f |
76 | f_stdint = 1u, |
77 | f_ext = 2u |
5084155b |
78 | }; |
79 | |
80 | struct itype { |
81 | const char *name; |
0b181057 |
82 | const char *suff; |
5084155b |
83 | umax max; |
84 | unsigned flags; |
85 | unsigned bits; |
86 | } tytab[] = { |
add9d49f |
87 | { "unsigned int", "u", UINT_MAX, 0 }, |
88 | { "unsigned short", "u", USHRT_MAX, 0 }, |
89 | { "unsigned long", "ul", ULONG_MAX, 0 }, |
5084155b |
90 | #ifdef ULLONG_MAX |
add9d49f |
91 | { "unsigned long long", "ull", EXT ULLONG_MAX, f_ext }, |
5084155b |
92 | #endif |
93 | #ifdef UINTMAX_MAX |
add9d49f |
94 | { "uintmax_t", "u", UINTMAX_MAX, f_stdint }, |
5084155b |
95 | #endif |
96 | { 0, 0 }, |
97 | }; |
98 | |
99 | typedef struct itype itype; |
100 | |
0b181057 |
101 | /*----- Main code ---------------------------------------------------------*/ |
5084155b |
102 | |
103 | int main(int argc, char *argv[]) |
104 | { |
105 | itype *i; |
106 | itype *largest, *mpw, *mpd; |
add9d49f |
107 | const static char *extstr = "CATACOMB_MPTYPES_EXTENSION "; |
5084155b |
108 | |
109 | /* --- Find the bitcounts --- */ |
110 | |
111 | for (i = tytab; i->name; i++) { |
112 | unsigned bits; |
113 | umax u = i->max; |
114 | for (bits = 0; u; bits++) |
115 | u >>= 1; |
116 | i->bits = bits; |
117 | } |
118 | |
119 | /* --- Now try to find the interesting types --- * |
120 | * |
121 | * The first thing to do is to find the largest type. Then I find the |
122 | * `best' type which is less than half that size, and then the `best' type |
123 | * which is twice as big as that one. |
124 | */ |
125 | |
126 | largest = tytab; |
127 | for (i = tytab; i->name; i++) { |
128 | if (i->bits > largest->bits) |
129 | largest = i; |
130 | } |
131 | for (mpw = 0, i = tytab; i->name; i++) { |
132 | if (i->bits * 2 <= largest->bits && (!mpw || i->bits > mpw->bits)) |
133 | mpw = i; |
134 | } |
135 | if (!mpw) |
136 | mpw = tytab; |
137 | for (mpd = 0, i = tytab; i->name; i++) { |
138 | if (i->bits >= mpw->bits * 2 && (!mpd || i->bits < mpd->bits)) |
139 | mpd = i; |
140 | } |
141 | if (!mpd) { |
142 | static itype w, d; |
143 | d = w = *mpw; |
144 | w.bits /= 2; w.max = ~(~((umax)0) << w.bits); |
145 | d.bits = w.bits * 2; d.max = ~(~((umax)0) << d.bits); |
146 | mpw = &w; mpd = &d; |
147 | } |
148 | |
149 | /* --- Output time --- */ |
150 | |
151 | puts("\ |
152 | /* -*-c-*-\n\ |
153 | *\n\ |
154 | * mptypes.h [generated]\n\ |
155 | */\n\ |
156 | \n\ |
b3f05084 |
157 | #ifndef CATACOMB_MPTYPES_H\n\ |
158 | #define CATACOMB_MPTYPES_H\n\ |
5084155b |
159 | "); |
160 | if ((mpd->flags | mpw->flags) & f_stdint) { |
161 | puts("\ |
162 | #if __STDC_VERSION__ >= 199900l\n\ |
163 | # include <stdint.h>\n\ |
164 | #endif\n\ |
165 | "); |
166 | } |
add9d49f |
167 | if ((mpd->flags | mpw->flags) & f_ext) { |
168 | printf("\ |
169 | #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 91)\n\ |
170 | # define %s __extension__\n\ |
171 | #else\n\ |
172 | # define %s\n\ |
173 | #endif\n\ |
174 | ", extstr, extstr); |
175 | } |
5084155b |
176 | printf("\ |
add9d49f |
177 | %stypedef %s mpw;\n\ |
5084155b |
178 | #define MPW_BITS %u\n\ |
e8776ccd |
179 | #define MPW_MAX %s%" P_UMAX "%s\n\ |
5084155b |
180 | \n\ |
add9d49f |
181 | %stypedef %s mpd;\n\ |
5084155b |
182 | #define MPD_BITS %u\n\ |
e8776ccd |
183 | #define MPD_MAX %s%" P_UMAX "%s\n\ |
5084155b |
184 | \n\ |
185 | #endif\n\ |
186 | ", |
add9d49f |
187 | mpw->flags & f_ext ? extstr : "", mpw->name, |
188 | mpw->bits, |
189 | mpw->flags & f_ext ? extstr : "", mpw->max, mpw->suff, |
190 | mpd->flags & f_ext ? extstr : "", mpd->name, |
191 | mpd->bits, |
192 | mpd->flags & f_ext ? extstr : "", mpd->max, mpd->suff); |
5084155b |
193 | |
194 | return (0); |
195 | } |
add9d49f |
196 | |
0b181057 |
197 | /*----- That's all, folks -------------------------------------------------*/ |