3 * Generate limit MPs for C types
5 * (c) 2006 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
36 #if __STDC_VERSION__ >= 199900l
38 # include <inttypes.h>
44 /*----- Data types --------------------------------------------------------*/
46 /* --- Hack for GCC --- *
48 * WG14 in their infinite wisdom decided not to use the GCC constant name.
51 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 91)
52 # define EXT __extension__
57 #if defined(LONG_LONG_MIN) && !defined(LLONG_MIN)
58 # define LLONG_MIN EXT LONG_LONG_MIN
61 #if defined(LONG_LONG_MAX) && !defined(LLONG_MAX)
62 # define LLONG_MAX EXT LONG_LONG_MAX
65 #if defined(ULONG_LONG_MAX) && !defined(ULLONG_MAX)
66 # define ULLONG_MAX EXT ULONG_LONG_MAX
69 /* --- Choose the largest integer type --- */
71 #if defined(INTMAX_MAX)
72 typedef intmax_t imax
;
73 #elif defined(LLONG_MAX)
74 EXT
typedef long long imax
;
79 #if defined(UINTMAX_MAX)
80 typedef uintmax_t umax
;
81 #elif defined(ULLONG_MAX)
82 EXT
typedef unsigned long long umax
;
84 typedef unsigned long umax
;
87 /*----- Main code ---------------------------------------------------------*/
91 enum { NEG
, POS
, NSIGN
};
94 int gmap
[TABSZ
][NSIGN
];
95 struct { int g
, s
; } qmap
[TABSZ
];
100 static void dump(mp
*x
)
105 w
= (MPW_BITS
+ 3)/4;
107 while (2 + 2 * n
* (4 + w
) < 72) n
<<= 1;
110 printf("0x%0*x", w
, x
->v
[i
]);
112 if (i
>= MP_LEN(x
)) break;
114 if (i
% n
) fputs(" ", stdout
); else fputs("\n ", stdout
);
119 static void doemit(umax c
, int s
, int *gg
, int *qq
)
124 for (i
= 0; i
< n
; i
++) {
132 gmap
[i
][POS
] = gmap
[i
][NEG
] = -1;
134 MP_FROMINT(x
, umax
, c
);
135 printf("static mpw guts_%d[] = {\n", q
);
137 fputs("};\n\n", stdout
);
143 if (gmap
[i
][s
] < 0) {
153 static void emit(imax c
, int *gg
, int *qq
)
158 if (c
>= 0) { uc
= c
; s
= POS
; }
159 else { uc
= -c
; s
= NEG
; }
160 doemit(uc
, s
, gg
, qq
);
163 static void uemit(umax c
, int *gg
, int *qq
) { doemit(c
, POS
, gg
, qq
); }
172 { "SCHAR", SCHAR_MIN
, SCHAR_MAX
},
173 { "CHAR", CHAR_MIN
, CHAR_MAX
},
174 { "UCHAR", 0, UCHAR_MAX
},
175 { "UINT8", 0, 0xff },
176 { "SHRT", SHRT_MIN
, SHRT_MAX
},
177 { "USHRT", 0, USHRT_MAX
},
178 { "UINT16", 0, 0xffff },
179 { "INT", INT_MIN
, INT_MAX
},
180 { "UINT", 0, UINT_MAX
},
181 { "LONG", LONG_MIN
, LONG_MAX
},
182 { "ULONG", 0, ULONG_MAX
},
183 { "UINT32", 0, 0xffffffff },
185 { "LLONG", LLONG_MIN
, LLONG_MAX
},
186 { "ULLONG", 0, ULLONG_MAX
},
188 { "SIZET", 0, ~(size_t)0 },
192 static void dogen(void)
196 for (i
= 0; tab
[i
].name
; i
++) {
198 emit(tab
[i
].min
, &tab
[i
].gmin
, &tab
[i
].qmin
);
199 uemit(tab
[i
].max
, &tab
[i
].gmax
, &tab
[i
].qmax
);
203 static void cgen(void)
210 * C integer limits [generated]\n\
213 #include \"mplimits.h\"\n\
215 #define N(x) (sizeof(x)/sizeof(*x))\n\
216 #define MPpos(x) { x, x + N(x), N(x), 0, MP_CONST, 0 }\n\
217 #define MPneg(x) { x, x + N(x), N(x), 0, MP_CONST|MP_NEG, 0 }\n\
223 fputs("mp mp_limits[] = {\n", stdout
);
224 for (i
= 0; i
< q
; i
++)
225 printf(" MP%s(guts_%d),\n", qmap
[i
].s ?
"pos" : "neg", qmap
[i
].g
);
226 fputs("};\n", stdout
);
229 static void hgen(void)
236 * C integer limits [generated]\n\
239 #ifndef CATACOMB_MPLIMITS_H\n\
240 #define CATACOMB_MPLIMITS_H\n\
242 #ifndef CATACOMB_MP_H\n\
243 # include \"mp.h\"\n\
246 extern mp mp_limits[];\n\
251 for (i
= 0; tab
[i
].name
; i
++) {
253 printf("#define MP_%s_MIN (&mp_limits[%d])\n",
254 tab
[i
].name
, gmap
[tab
[i
].qmin
][NEG
]);
256 printf("#define MP_%s_MAX (&mp_limits[%d])\n",
257 tab
[i
].name
, gmap
[tab
[i
].qmax
][POS
]);
259 fputs("\n#endif\n", stdout
);
262 int main(int argc
, char *argv
[])
264 const char *what
= argc
== 2 ? argv
[1] : "<bogus>";
267 case 'c': cgen(); break;
268 case 'h': hgen(); break;
270 fprintf(stderr
, "unknown action `%s'\n", what
);
273 if (fflush(stdout
) || fclose(stdout
)) {
274 fprintf(stderr
, "error writing output: %s\n", strerror(errno
));
280 /*----- That's all, folks -------------------------------------------------*/