1 /* Check the values of some constants.
3 Copyright 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
27 char *ulong_max_def = "defined";
29 char *ulong_max_def = "not defined";
32 char *long_max_def = "defined";
34 char *long_max_def = "not defined";
38 char *uint_max_def = "defined";
40 char *uint_max_def = "not defined";
43 char *int_max_def = "defined";
45 char *int_max_def = "not defined";
49 char *ushrt_max_def = "defined";
51 char *ushrt_max_def = "not defined";
54 char *shrt_max_def = "defined";
56 char *shrt_max_def = "not defined";
63 #ifdef _LONG_LONG_LIMB
69 #if __GMP_MP_SIZE_T_INT
76 #define CHECK_LIMB_S(x, xname, y, yname) \
80 printf (LL("%s == %lx, but %s == %lx\n", \
81 "%s == %llx, but %s == %llx\n"), \
82 xname, x, yname, y); \
87 #define CHECK_INT_S(x, xname, y, yname) \
91 printf ("%s == %d, but %s == %d\n", xname, x, yname, y); \
98 #define CHECK_CONDITION_S(x, xname) \
102 printf ("%s is false\n", xname); \
108 /* How many bits seem to work in the given type. */
109 #define CALC_BITS(result, type) \
120 #define CHECK_BITS_S(constant, constant_name, type) \
123 CALC_BITS (calculated, type); \
124 if (calculated != constant) \
126 printf ("%s == %d, but calculated %d\n", \
127 constant_name, constant, calculated); \
133 #define CHECK_HIGHBIT_S(value, value_name, type, format) \
138 printf ("%s == 0\n", value_name); \
144 printf ("%s << 1 = ", value_name); \
145 printf (format, n); \
146 printf (" != 0\n"); \
152 #define CHECK_MAX_S(max_val, max_name, min_val, min_name, type, format) \
154 type maxval = max_val; \
155 type minval = min_val; \
160 printf ("%s + 1 = ", max_name); \
161 printf (format, n); \
162 printf (" != %s = ", min_name); \
163 printf (format, minval); \
167 if (maxval <= minval) \
169 printf ("%s = ", max_name); \
170 printf (format, maxval); \
171 printf (" <= %s = ", min_name); \
172 printf (format, minval); \
180 #define CHECK_LIMB(x,y) CHECK_LIMB_S (x, #x, y, #y)
181 #define CHECK_INT(x,y) CHECK_INT_S (x, #x, y, #y)
182 #define CHECK_CONDITION(x) CHECK_CONDITION_S (x, #x)
183 #define CHECK_BITS(c,t) CHECK_BITS_S (c, #c, t)
184 #define CHECK_MAX(m,n,t,f) CHECK_MAX_S (m, #m, n, #n, t, f)
185 #define CHECK_HIGHBIT(n,t,f) CHECK_HIGHBIT_S (n, #n, t, f)
187 #define CHECK_LIMB(x,y) CHECK_LIMB_S (x, "x", y, "y")
188 #define CHECK_INT(x,y) CHECK_INT_S (x, "x", y, "y")
189 #define CHECK_CONDITION(x) CHECK_CONDITION_S (x, "x")
190 #define CHECK_BITS(c,t) CHECK_BITS_S (c, "c", t)
191 #define CHECK_MAX(m,n,t,f) CHECK_MAX_S (m, "m", n, "n", t, f)
192 #define CHECK_HIGHBIT(n,t,f) CHECK_HIGHBIT_S (n, "n", t, f)
196 /* The tests below marked "Bad!" fail on Cray T90 systems, where int, short
197 and mp_size_t are 48 bits or some such but don't wraparound in a plain
198 twos complement fashion. In particular,
200 INT_HIGHBIT << 1 = 0xFFFFC00000000000 != 0
201 INT_MAX + 1 = 35184372088832 != INT_MIN = -35184372088832
203 This is a bit bizarre, but doesn't matter because GMP doesn't rely on any
204 particular overflow behaviour for int or short, only for mp_limb_t. */
207 main (int argc, char *argv[])
211 CHECK_INT (BYTES_PER_MP_LIMB, (int) sizeof(mp_limb_t));
212 CHECK_INT (mp_bits_per_limb, GMP_LIMB_BITS);
214 CHECK_BITS (GMP_LIMB_BITS, mp_limb_t);
215 CHECK_BITS (BITS_PER_ULONG, unsigned long);
217 CHECK_HIGHBIT (GMP_LIMB_HIGHBIT, mp_limb_t, LL("0x%lX","0x%llX"));
218 CHECK_HIGHBIT (ULONG_HIGHBIT, unsigned long, "0x%lX");
219 CHECK_HIGHBIT (UINT_HIGHBIT, unsigned int, "0x%X");
220 CHECK_HIGHBIT (USHRT_HIGHBIT, unsigned short, "0x%hX");
221 CHECK_HIGHBIT (LONG_HIGHBIT, long, "0x%lX");
223 CHECK_HIGHBIT (INT_HIGHBIT, int, "0x%X");
224 CHECK_HIGHBIT (SHRT_HIGHBIT, short, "0x%hX");
228 CHECK_MAX (LONG_MAX, LONG_MIN, long, "%ld");
229 CHECK_MAX (INT_MAX, INT_MIN, int, "%d");
230 CHECK_MAX (SHRT_MAX, SHRT_MIN, short, "%hd");
232 CHECK_MAX (ULONG_MAX, 0, unsigned long, "%lu");
233 CHECK_MAX (UINT_MAX, 0, unsigned int, "%u");
234 CHECK_MAX (USHRT_MAX, 0, unsigned short, "%hu");
236 CHECK_MAX (MP_SIZE_T_MAX, MP_SIZE_T_MIN, mp_size_t, SS("%d","%ld"));
239 /* UHWtype should have at least enough bits for half a UWtype */
241 int bits_per_UWtype, bits_per_UHWtype;
242 CALC_BITS (bits_per_UWtype, UWtype);
243 CALC_BITS (bits_per_UHWtype, UHWtype);
244 CHECK_CONDITION (2*bits_per_UHWtype >= bits_per_UWtype);
247 ASSERT_ALWAYS_LIMB (MODLIMB_INVERSE_3);
249 mp_limb_t modlimb_inverse_3_calc;
250 binvert_limb (modlimb_inverse_3_calc, CNST_LIMB(3));
251 ASSERT_ALWAYS_LIMB (modlimb_inverse_3_calc);
252 CHECK_LIMB (MODLIMB_INVERSE_3, modlimb_inverse_3_calc);
255 mp_limb_t MODLIMB_INVERSE_3_times_3
256 = (MODLIMB_INVERSE_3 * CNST_LIMB(3)) & GMP_NUMB_MASK;
257 CHECK_LIMB (MODLIMB_INVERSE_3_times_3, CNST_LIMB(1));
262 hi = refmpn_umul_ppmm (&lo, GMP_NUMB_CEIL_MAX_DIV3-1,
263 CNST_LIMB(3) << GMP_NAIL_BITS);
266 printf ("GMP_NUMB_CEIL_MAX_DIV3 too big\n");
269 hi = refmpn_umul_ppmm (&lo, GMP_NUMB_CEIL_MAX_DIV3,
270 CNST_LIMB(3) << GMP_NAIL_BITS);
273 printf ("GMP_NUMB_CEIL_MAX_DIV3 too small\n");
280 hi = refmpn_umul_ppmm (&lo, GMP_NUMB_CEIL_2MAX_DIV3-1,
281 CNST_LIMB(3) << GMP_NAIL_BITS);
284 printf ("GMP_NUMB_CEIL_2MAX_DIV3 too big\n");
287 hi = refmpn_umul_ppmm (&lo, GMP_NUMB_CEIL_2MAX_DIV3,
288 CNST_LIMB(3) << GMP_NAIL_BITS);
291 printf ("GMP_NUMB_CEIL_2MAX_DIV3 too small\n");
298 mp_limb_t pp_inverted_calc;
299 invert_limb (pp_inverted_calc, PP);
300 CHECK_LIMB (PP_INVERTED, pp_inverted_calc);
304 if (argc >= 2 || error)
309 printf ("After gmp.h,\n");
310 printf (" ULONG_MAX %s\n", ulong_max_def);
311 printf (" LONG_MAX %s\n", long_max_def);
312 printf (" UINT_MAX %s\n", uint_max_def);
313 printf (" INT_MAX %s\n", int_max_def);
314 printf (" USHRT_MAX %s\n", ushrt_max_def);
315 printf (" SHRT_MAX %s\n", shrt_max_def);
319 printf ("_CRAY is defined, so limits.h is being used\n");
322 printf ("ULONG_MAX %lX\n", ULONG_MAX);
323 printf ("ULONG_HIGHBIT %lX\n", ULONG_HIGHBIT);
324 printf ("LONG_MAX %lX\n", LONG_MAX);
325 printf ("LONG_MIN %lX\n", LONG_MIN);
327 printf ("UINT_MAX %X\n", UINT_MAX);
328 printf ("UINT_HIGHBIT %X\n", UINT_HIGHBIT);
329 printf ("INT_MAX %X\n", INT_MAX);
330 printf ("INT_MIN %X\n", INT_MIN);
332 printf ("USHRT_MAX %hX\n", USHRT_MAX);
333 printf ("USHRT_HIGHBIT %hX\n", USHRT_HIGHBIT);
334 printf ("SHRT_MAX %hX\n", SHRT_MAX);
335 printf ("SHRT_MIN %hX\n", SHRT_MIN);
339 CALC_BITS (bits, long); printf (" long %d\n", bits);
340 CALC_BITS (bits, int); printf (" int %d\n", bits);
341 CALC_BITS (bits, short); printf (" short %d\n", bits);
342 CALC_BITS (bits, unsigned long); printf (" unsigned long %d\n", bits);
343 CALC_BITS (bits, unsigned int); printf (" unsigned int %d\n", bits);
344 CALC_BITS (bits, unsigned short); printf (" unsigned short %d\n", bits);
345 CALC_BITS (bits, mp_size_t); printf (" mp_size_t %d\n", bits);