chiark / gitweb /
Add some more vectors, and a whinge about how Skipjack test vectors are.
[catacomb] / mptypes.c
1 /* -*-c-*-
2  *
3  * $Id: mptypes.c,v 1.3 1999/12/10 23:29:48 mdw Exp $
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.
28  */
29
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: mptypes.c,v $
33  * Revision 1.3  1999/12/10 23:29:48  mdw
34  * Change header file guard names.
35  *
36  * Revision 1.2  1999/11/13 01:54:32  mdw
37  * Format source code properly ;-).  Attach suffixes to the `max'
38  * constants.
39  *
40  */
41
42 /*----- Header files ------------------------------------------------------*/
43
44 #define _GNU_SOURCE
45 #include <stdio.h>
46 #include <limits.h>
47 #if __STDC_VERSION__ >= 199900l
48 #  include <stdint.h>
49 #endif
50
51 /*----- Data types --------------------------------------------------------*/
52
53 /* --- Hack for GCC --- *
54  *
55  * WG14 in their infinite wisdom decided not to use the GCC constant name.
56  */
57
58 #if defined(ULONG_LONG_MAX) && !defined(ULLONG_MAX)
59 #  define ULLONG_MAX ULONG_LONG_MAX
60 #endif
61
62 /* --- Choose the largest integer type --- */
63
64 #if defined(UINTMAX_MAX)
65   typedef uintmax_t umax;
66 # define P_UMAX PRIuMAX
67 #elif defined(ULLONG_MAX)
68   typedef unsigned long long umax;
69 # define P_UMAX "%llu"
70 #else
71   typedef unsigned long umax;
72 # define P_UMAX "%lu"
73 #endif
74
75 /* --- Table of interesting types --- *
76  *
77  * These are in preference order.
78  */
79
80 enum {
81   f_stdint
82 };
83
84 struct itype {
85   const char *name;
86   const char *suff;
87   umax max;
88   unsigned flags;
89   unsigned bits;
90 } tytab[] = {
91   { "unsigned int", "u",        UINT_MAX,       0 },
92   { "unsigned short", "u",      USHRT_MAX,      0 },
93   { "unsigned long", "ul",      ULONG_MAX,      0 },
94 #ifdef ULLONG_MAX
95   { "unsigned long long", "ull", ULLONG_MAX,    0 },
96 #endif
97 #ifdef UINTMAX_MAX
98   { "uintmax_t", "u",           UINTMAX_MAX,    f_stdint },
99 #endif
100   { 0,                          0 },
101 };
102
103 typedef struct itype itype;
104
105 /*----- Main code ---------------------------------------------------------*/
106
107 int main(int argc, char *argv[])
108 {
109   itype *i;
110   itype *largest, *mpw, *mpd;
111
112   /* --- Find the bitcounts --- */
113
114   for (i = tytab; i->name; i++) {
115     unsigned bits;
116     umax u = i->max;
117     for (bits = 0; u; bits++)
118       u >>= 1;
119     i->bits = bits;
120   }
121
122   /* --- Now try to find the interesting types --- *
123    *
124    * The first thing to do is to find the largest type.  Then I find the
125    * `best' type which is less than half that size, and then the `best' type
126    * which is twice as big as that one.
127    */
128
129   largest = tytab;
130   for (i = tytab; i->name; i++) {
131     if (i->bits > largest->bits)
132       largest = i;
133   }
134   for (mpw = 0, i = tytab; i->name; i++) {
135     if (i->bits * 2 <= largest->bits && (!mpw || i->bits > mpw->bits))
136       mpw = i;
137   }
138   if (!mpw)
139     mpw = tytab;
140   for (mpd = 0, i = tytab; i->name; i++) {
141     if (i->bits >= mpw->bits * 2 && (!mpd || i->bits < mpd->bits))
142       mpd = i;
143   }
144   if (!mpd) {
145     static itype w, d;
146     d = w = *mpw;
147     w.bits /= 2; w.max = ~(~((umax)0) << w.bits);
148     d.bits = w.bits * 2; d.max = ~(~((umax)0) << d.bits);
149     mpw = &w; mpd = &d;
150   }    
151
152   /* --- Output time --- */
153
154   puts("\
155 /* -*-c-*-\n\
156  *\n\
157  * mptypes.h [generated]\n\
158  */\n\
159 \n\
160 #ifndef CATACOMB_MPTYPES_H\n\
161 #define CATACOMB_MPTYPES_H\n\
162 ");
163   if ((mpd->flags | mpw->flags) & f_stdint) {
164     puts("\
165 #if __STDC_VERSION__ >= 199900l\n\
166 #  include <stdint.h>\n\
167 #endif\n\
168 ");
169   }
170   printf("\
171 typedef %s mpw;\n\
172 #define MPW_BITS %u\n\
173 #define MPW_MAX " P_UMAX "%s\n\
174 \n\
175 typedef %s mpd;\n\
176 #define MPD_BITS %u\n\
177 #define MPD_MAX " P_UMAX "%s\n\
178 \n\
179 #endif\n\
180 ",
181   mpw->name, mpw->bits, mpw->max, mpw->suff,
182   mpd->name, mpd->bits, mpd->max, mpd->suff);
183
184   return (0);
185 }
186 /*----- That's all, folks -------------------------------------------------*/