root/maint/gnulib/tests/test-intprops.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /* Test intprops.h.
   2    Copyright (C) 2011-2021 Free Software Foundation, Inc.
   3 
   4    This program is free software: you can redistribute it and/or modify
   5    it under the terms of the GNU General Public License as published by
   6    the Free Software Foundation; either version 3 of the License, or
   7    (at your option) any later version.
   8 
   9    This program is distributed in the hope that it will be useful,
  10    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12    GNU General Public License for more details.
  13 
  14    You should have received a copy of the GNU General Public License
  15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 /* Written by Paul Eggert.  */
  18 
  19 /* Tell gcc not to warn about the long expressions that the overflow
  20    macros expand to, or about the (X < 0) expressions.  */
  21 #if 4 < __GNUC__ + (3 <= __GNUC_MINOR__)
  22 # pragma GCC diagnostic ignored "-Woverlength-strings"
  23 # pragma GCC diagnostic ignored "-Wtype-limits"
  24 
  25 /* Work around a bug in GCC 6.1 and earlier; see:
  26    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68971  */
  27 # pragma GCC diagnostic ignored "-Woverflow"
  28 
  29 #endif
  30 
  31 #include <config.h>
  32 
  33 #include "intprops.h"
  34 #include "verify.h"
  35 
  36 #include <stdbool.h>
  37 #include <inttypes.h>
  38 #include <limits.h>
  39 
  40 #include "macros.h"
  41 
  42 /* Compile-time verification of expression X.
  43    In this file, we need it as a statement, rather than as a declaration.  */
  44 #define verify_stmt(x) do { verify (x); } while (0)
  45 
  46 /* VERIFY (X) uses a static assertion for compilers that are known to work,
  47    and falls back on a dynamic assertion for other compilers.
  48    These tests should be checkable via 'verify' rather than 'ASSERT', but
  49    using 'verify' would run into a bug with HP-UX 11.23 cc; see
  50    <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>.  */
  51 #if __GNUC__ || __clang__ || __SUNPRO_C
  52 # define VERIFY(x) verify_stmt (x)
  53 #else
  54 # define VERIFY(x) ASSERT (x)
  55 #endif
  56 
  57 #define DONTCARE __LINE__
  58 
  59 int int_minus_2 = -2;
  60 int int_1 = 1;
  61 
  62 int
  63 main (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  64 {
  65   /* Use VERIFY for tests that must be integer constant expressions,
  66      ASSERT otherwise.  */
  67 
  68   /* TYPE_IS_INTEGER.  */
  69   ASSERT (TYPE_IS_INTEGER (bool));
  70   ASSERT (TYPE_IS_INTEGER (char));
  71   ASSERT (TYPE_IS_INTEGER (signed char));
  72   ASSERT (TYPE_IS_INTEGER (unsigned char));
  73   ASSERT (TYPE_IS_INTEGER (short int));
  74   ASSERT (TYPE_IS_INTEGER (unsigned short int));
  75   ASSERT (TYPE_IS_INTEGER (int));
  76   ASSERT (TYPE_IS_INTEGER (unsigned int));
  77   ASSERT (TYPE_IS_INTEGER (long int));
  78   ASSERT (TYPE_IS_INTEGER (unsigned long int));
  79   ASSERT (TYPE_IS_INTEGER (intmax_t));
  80   ASSERT (TYPE_IS_INTEGER (uintmax_t));
  81   ASSERT (! TYPE_IS_INTEGER (float));
  82   ASSERT (! TYPE_IS_INTEGER (double));
  83   ASSERT (! TYPE_IS_INTEGER (long double));
  84 
  85   /* TYPE_SIGNED.  */
  86   /* VERIFY (! TYPE_SIGNED (bool)); // not guaranteed by gnulib substitute */
  87   VERIFY (TYPE_SIGNED (signed char));
  88   VERIFY (! TYPE_SIGNED (unsigned char));
  89   VERIFY (TYPE_SIGNED (short int));
  90   VERIFY (! TYPE_SIGNED (unsigned short int));
  91   VERIFY (TYPE_SIGNED (int));
  92   VERIFY (! TYPE_SIGNED (unsigned int));
  93   VERIFY (TYPE_SIGNED (long int));
  94   VERIFY (! TYPE_SIGNED (unsigned long int));
  95   VERIFY (TYPE_SIGNED (intmax_t));
  96   VERIFY (! TYPE_SIGNED (uintmax_t));
  97   ASSERT (TYPE_SIGNED (float));
  98   ASSERT (TYPE_SIGNED (double));
  99   ASSERT (TYPE_SIGNED (long double));
 100 
 101   /* Integer representation.  Check that it is two's complement.  */
 102   VERIFY (INT_MIN + INT_MAX < 0);
 103 
 104   /* TYPE_MINIMUM, TYPE_MAXIMUM.  */
 105   VERIFY (TYPE_MINIMUM (char) == CHAR_MIN);
 106   VERIFY (TYPE_MAXIMUM (char) == CHAR_MAX);
 107   VERIFY (TYPE_MINIMUM (unsigned char) == 0);
 108   VERIFY (TYPE_MAXIMUM (unsigned char) == UCHAR_MAX);
 109   VERIFY (TYPE_MINIMUM (signed char) == SCHAR_MIN);
 110   VERIFY (TYPE_MAXIMUM (signed char) == SCHAR_MAX);
 111   VERIFY (TYPE_MINIMUM (short int) == SHRT_MIN);
 112   VERIFY (TYPE_MAXIMUM (short int) == SHRT_MAX);
 113   VERIFY (TYPE_MINIMUM (unsigned short int) == 0);
 114   VERIFY (TYPE_MAXIMUM (unsigned short int) == USHRT_MAX);
 115   VERIFY (TYPE_MINIMUM (int) == INT_MIN);
 116   VERIFY (TYPE_MAXIMUM (int) == INT_MAX);
 117   VERIFY (TYPE_MINIMUM (unsigned int) == 0);
 118   VERIFY (TYPE_MAXIMUM (unsigned int) == UINT_MAX);
 119   VERIFY (TYPE_MINIMUM (long int) == LONG_MIN);
 120   VERIFY (TYPE_MAXIMUM (long int) == LONG_MAX);
 121   VERIFY (TYPE_MINIMUM (unsigned long int) == 0);
 122   VERIFY (TYPE_MAXIMUM (unsigned long int) == ULONG_MAX);
 123   #ifdef LLONG_MAX
 124    verify_stmt (TYPE_MINIMUM (long long int) == LLONG_MIN);
 125    verify_stmt (TYPE_MAXIMUM (long long int) == LLONG_MAX);
 126    verify_stmt (TYPE_MINIMUM (unsigned long long int) == 0);
 127    verify_stmt (TYPE_MAXIMUM (unsigned long long int) == ULLONG_MAX);
 128   #endif
 129   VERIFY (TYPE_MINIMUM (intmax_t) == INTMAX_MIN);
 130   VERIFY (TYPE_MAXIMUM (intmax_t) == INTMAX_MAX);
 131   VERIFY (TYPE_MINIMUM (uintmax_t) == 0);
 132   VERIFY (TYPE_MAXIMUM (uintmax_t) == UINTMAX_MAX);
 133 
 134   /* TYPE_WIDTH.  */
 135   #ifdef CHAR_WIDTH
 136    verify_stmt (TYPE_WIDTH (char) == CHAR_WIDTH);
 137    verify_stmt (TYPE_WIDTH (signed char) == SCHAR_WIDTH);
 138    verify_stmt (TYPE_WIDTH (unsigned char) == UCHAR_WIDTH);
 139    verify_stmt (TYPE_WIDTH (short int) == SHRT_WIDTH);
 140    verify_stmt (TYPE_WIDTH (unsigned short int) == USHRT_WIDTH);
 141    verify_stmt (TYPE_WIDTH (int) == INT_WIDTH);
 142    verify_stmt (TYPE_WIDTH (unsigned int) == UINT_WIDTH);
 143    verify_stmt (TYPE_WIDTH (long int) == LONG_WIDTH);
 144    verify_stmt (TYPE_WIDTH (unsigned long int) == ULONG_WIDTH);
 145    #ifdef LLONG_WIDTH
 146     verify_stmt (TYPE_WIDTH (long long int) == LLONG_WIDTH);
 147     verify_stmt (TYPE_WIDTH (unsigned long long int) == ULLONG_WIDTH);
 148    #endif
 149   #endif
 150 
 151   /* INT_BITS_STRLEN_BOUND.  */
 152   VERIFY (INT_BITS_STRLEN_BOUND (1) == 1);
 153   VERIFY (INT_BITS_STRLEN_BOUND (2620) == 789);
 154 
 155   /* INT_STRLEN_BOUND, INT_BUFSIZE_BOUND.  */
 156   #ifdef INT32_MAX /* POSIX guarantees int32_t; this ports to non-POSIX.  */
 157   VERIFY (INT_STRLEN_BOUND (int32_t) == sizeof ("-2147483648") - 1);
 158   VERIFY (INT_BUFSIZE_BOUND (int32_t) == sizeof ("-2147483648"));
 159   #endif
 160   #ifdef INT64_MAX
 161   VERIFY (INT_STRLEN_BOUND (int64_t) == sizeof ("-9223372036854775808") - 1);
 162   VERIFY (INT_BUFSIZE_BOUND (int64_t) == sizeof ("-9223372036854775808"));
 163   #endif
 164 
 165   /* All the INT_<op>_RANGE_OVERFLOW tests are equally valid as
 166      INT_<op>_OVERFLOW tests, so define macros to do both.  OP is the
 167      operation, OPNAME its symbolic name, A and B its operands, T the
 168      result type, V the overflow flag, and VRES the result if V and if
 169      two's complement.  CHECK_BINOP is for most binary operatinos,
 170      CHECK_SBINOP for binary +, -, * when the result type is signed,
 171      and CHECK_UNOP for unary operations.  */
 172   #define CHECK_BINOP(op, opname, a, b, t, v, vres)                       \
 173     VERIFY (INT_##opname##_RANGE_OVERFLOW (a, b, TYPE_MINIMUM (t),        \
 174                                            TYPE_MAXIMUM (t))              \
 175             == (v));                                                      \
 176     VERIFY (INT_##opname##_OVERFLOW (a, b) == (v))
 177   #define CHECK_SBINOP(op, opname, a, b, t, v, vres)                      \
 178     CHECK_BINOP(op, opname, a, b, t, v, vres);                            \
 179     {                                                                     \
 180       t result;                                                           \
 181       ASSERT (INT_##opname##_WRAPV (a, b, &result) == (v));               \
 182       ASSERT (result == ((v) ? (vres) : ((a) op (b))));                   \
 183     }
 184   #define CHECK_UNOP(op, opname, a, t, v)                                 \
 185     VERIFY (INT_##opname##_RANGE_OVERFLOW (a, TYPE_MINIMUM (t),           \
 186                                            TYPE_MAXIMUM (t))              \
 187             == (v));                                                      \
 188     VERIFY (INT_##opname##_OVERFLOW (a) == (v))
 189 
 190   /* INT_<op>_RANGE_OVERFLOW, INT_<op>_OVERFLOW.  */
 191   VERIFY (INT_ADD_RANGE_OVERFLOW (INT_MAX, 1, INT_MIN, INT_MAX));
 192   VERIFY (INT_ADD_OVERFLOW (INT_MAX, 1));
 193 
 194   CHECK_SBINOP (+, ADD, INT_MAX, 1, int, true, INT_MIN);
 195   CHECK_SBINOP (+, ADD, INT_MAX, -1, int, false, INT_MAX - 1);
 196   CHECK_SBINOP (+, ADD, INT_MIN, 1, int, false, INT_MIN + 1);
 197   CHECK_SBINOP (+, ADD, INT_MIN, -1, int, true, INT_MAX);
 198   CHECK_BINOP (+, ADD, UINT_MAX, 1u, unsigned int, true, 0u);
 199   CHECK_BINOP (+, ADD, 0u, 1u, unsigned int, false, 1u);
 200 
 201   CHECK_SBINOP (-, SUBTRACT, INT_MAX, 1, int, false, INT_MAX - 1);
 202   CHECK_SBINOP (-, SUBTRACT, INT_MAX, -1, int, true, INT_MIN);
 203   CHECK_SBINOP (-, SUBTRACT, INT_MIN, 1, int, true, INT_MAX);
 204   CHECK_SBINOP (-, SUBTRACT, INT_MIN, -1, int, false, INT_MIN - -1);
 205   CHECK_BINOP (-, SUBTRACT, UINT_MAX, 1u, unsigned int, false, UINT_MAX - 1u);
 206   CHECK_BINOP (-, SUBTRACT, 0u, 1u, unsigned int, true, 0u - 1u);
 207 
 208   CHECK_UNOP (-, NEGATE, INT_MIN, int, true);
 209   CHECK_UNOP (-, NEGATE, 0, int, false);
 210   CHECK_UNOP (-, NEGATE, INT_MAX, int, false);
 211   CHECK_UNOP (-, NEGATE, 0u, unsigned int, false);
 212   CHECK_UNOP (-, NEGATE, 1u, unsigned int, true);
 213   CHECK_UNOP (-, NEGATE, UINT_MAX, unsigned int, true);
 214 
 215   CHECK_SBINOP (*, MULTIPLY, INT_MAX, INT_MAX, int, true, 1);
 216   CHECK_SBINOP (*, MULTIPLY, INT_MAX, INT_MIN, int, true, INT_MIN);
 217   CHECK_SBINOP (*, MULTIPLY, INT_MIN, INT_MAX, int, true, INT_MIN);
 218   CHECK_SBINOP (*, MULTIPLY, INT_MIN, INT_MIN, int, true, 0);
 219   CHECK_SBINOP (*, MULTIPLY, -1, INT_MIN, int,
 220                 INT_NEGATE_OVERFLOW (INT_MIN), INT_MIN);
 221 #if !defined __HP_cc
 222   CHECK_SBINOP (*, MULTIPLY, LONG_MIN / INT_MAX, (long int) INT_MAX,
 223                 long int, false, LONG_MIN - LONG_MIN % INT_MAX);
 224 #endif
 225 
 226   CHECK_BINOP (/, DIVIDE, INT_MIN, -1, int,
 227                INT_NEGATE_OVERFLOW (INT_MIN), INT_MIN);
 228   CHECK_BINOP (/, DIVIDE, INT_MAX, 1, int, false, INT_MAX);
 229   CHECK_BINOP (/, DIVIDE, (unsigned int) INT_MIN, -1u, unsigned int,
 230                false, INT_MIN / -1u);
 231 
 232   CHECK_BINOP (%, REMAINDER, INT_MIN, -1, int, INT_NEGATE_OVERFLOW (INT_MIN), 0);
 233   CHECK_BINOP (%, REMAINDER, INT_MAX, 1, int, false, 0);
 234   CHECK_BINOP (%, REMAINDER, (unsigned int) INT_MIN, -1u, unsigned int,
 235                false, INT_MIN % -1u);
 236 
 237   CHECK_BINOP (<<, LEFT_SHIFT, UINT_MAX, 1, unsigned int, true, UINT_MAX << 1);
 238   CHECK_BINOP (<<, LEFT_SHIFT, UINT_MAX / 2 + 1, 1, unsigned int, true,
 239                (UINT_MAX / 2 + 1) << 1);
 240   CHECK_BINOP (<<, LEFT_SHIFT, UINT_MAX / 2, 1, unsigned int, false,
 241                (UINT_MAX / 2) << 1);
 242 
 243   /* INT_<op>_OVERFLOW and INT_<op>_WRAPV with mixed types.  */
 244   #define CHECK_SUM(a, b, t, v, vres)                                     \
 245     CHECK_SUM1 (a, b, t, v, vres);                                        \
 246     CHECK_SUM1 (b, a, t, v, vres)
 247   #define CHECK_SUM_WRAPV(a, b, t, v, vres, okres)                        \
 248     CHECK_SUM_WRAPV1 (a, b, t, v, vres, okres);                           \
 249     CHECK_SUM_WRAPV1 (b, a, t, v, vres, okres)
 250   #define CHECK_SUM1(a, b, t, v, vres)                                    \
 251     VERIFY (INT_ADD_OVERFLOW (a, b) == (v));                              \
 252     CHECK_SUM_WRAPV1 (a, b, t, v, vres, (a) + (b))
 253   #define CHECK_SUM_WRAPV1(a, b, t, v, vres, okres)                       \
 254     {                                                                     \
 255       t result;                                                           \
 256       ASSERT (INT_ADD_WRAPV (a, b, &result) == (v));                      \
 257       ASSERT (result == ((v) ? (vres) : (okres)));                        \
 258     }
 259   CHECK_SUM (-1, LONG_MIN, long int, true, LONG_MAX);
 260   CHECK_SUM (-1, UINT_MAX, unsigned int, false, DONTCARE);
 261   CHECK_SUM (-1L, INT_MIN, long int, INT_MIN == LONG_MIN,
 262               INT_MIN == LONG_MIN ? INT_MAX : DONTCARE);
 263   CHECK_SUM (0u, -1, unsigned int, true, 0u + -1);
 264   CHECK_SUM (0u, 0, unsigned int, false, DONTCARE);
 265   CHECK_SUM (0u, 1, unsigned int, false, DONTCARE);
 266   CHECK_SUM (1, LONG_MAX, long int, true, LONG_MIN);
 267   CHECK_SUM (1, UINT_MAX, unsigned int, true, 0u);
 268   CHECK_SUM (1L, INT_MAX, long int, INT_MAX == LONG_MAX,
 269               INT_MAX == LONG_MAX ? INT_MIN : DONTCARE);
 270   CHECK_SUM (1u, INT_MAX, unsigned int, INT_MAX == UINT_MAX, 1u + INT_MAX);
 271   CHECK_SUM (1u, INT_MIN, unsigned int, true, 1u + INT_MIN);
 272   CHECK_SUM_WRAPV (-1, 1u, int, false, DONTCARE, 0);
 273   CHECK_SUM_WRAPV (-1, 1ul, int, false, DONTCARE, 0);
 274   CHECK_SUM_WRAPV (-1l, 1u, int, false, DONTCARE, 0);
 275   CHECK_SUM_WRAPV (-100, 1000u, int, false, DONTCARE, 900);
 276   CHECK_SUM_WRAPV (INT_MIN, UINT_MAX, int, false, DONTCARE, INT_MAX);
 277   CHECK_SUM_WRAPV (1u, INT_MAX, int, true, INT_MIN, DONTCARE);
 278   CHECK_SUM_WRAPV (INT_MAX, 1, long int, LONG_MAX <= INT_MAX, INT_MIN,
 279                    INT_MAX + 1L);
 280   CHECK_SUM_WRAPV (UINT_MAX, 1, long int, LONG_MAX <= UINT_MAX, 0,
 281                    UINT_MAX + 1L);
 282   CHECK_SUM_WRAPV (INT_MAX, 1, unsigned long int, ULONG_MAX <= INT_MAX, 0,
 283                    INT_MAX + 1uL);
 284   CHECK_SUM_WRAPV (UINT_MAX, 1, unsigned long int, ULONG_MAX <= UINT_MAX, 0,
 285                    UINT_MAX + 1uL);
 286 
 287   {
 288     long int result;
 289     ASSERT (INT_ADD_WRAPV (1, INT_MAX, &result) == (INT_MAX == LONG_MAX));
 290     ASSERT (INT_ADD_WRAPV (-1, INT_MIN, &result) == (INT_MIN == LONG_MIN));
 291   }
 292 
 293   #define CHECK_DIFFERENCE(a, b, t, v, vres)                              \
 294     VERIFY (INT_SUBTRACT_OVERFLOW (a, b) == (v))
 295   #define CHECK_SDIFFERENCE(a, b, t, v, vres)                             \
 296     CHECK_DIFFERENCE (a, b, t, v, vres);                                  \
 297     CHECK_SDIFFERENCE_WRAPV (a, b, t, v, vres)
 298   #define CHECK_SDIFFERENCE_WRAPV(a, b, t, v, vres)                       \
 299     {                                                                     \
 300       t result;                                                           \
 301       ASSERT (INT_SUBTRACT_WRAPV (a, b, &result) == (v));                 \
 302       ASSERT (result == ((v) ? (vres) : ((a) - (b))));                    \
 303     }
 304   CHECK_DIFFERENCE (INT_MAX, 1u, unsigned int, UINT_MAX < INT_MAX - 1,
 305                     INT_MAX - 1u);
 306   CHECK_DIFFERENCE (UINT_MAX, 1, unsigned int, false, UINT_MAX - 1);
 307   CHECK_DIFFERENCE (0u, -1, unsigned int, false, 0u - -1);
 308   CHECK_DIFFERENCE (UINT_MAX, -1, unsigned int, true, UINT_MAX - -1);
 309   CHECK_DIFFERENCE (INT_MIN, 1u, unsigned int, true, INT_MIN - 1u);
 310   CHECK_DIFFERENCE (-1, 0u, unsigned int, true, -1 - 0u);
 311   CHECK_SDIFFERENCE (-1, INT_MIN, int, false, -1 - INT_MIN);
 312   CHECK_SDIFFERENCE (-1, INT_MAX, int, false, -1 - INT_MAX);
 313   CHECK_SDIFFERENCE (0, INT_MIN, int, INT_MIN < -INT_MAX, INT_MIN);
 314   CHECK_SDIFFERENCE (0, INT_MAX, int, false, 0 - INT_MAX);
 315   CHECK_SDIFFERENCE_WRAPV (-1, 1u, int, false, DONTCARE);
 316   CHECK_SDIFFERENCE_WRAPV (-1, 1ul, int, false, DONTCARE);
 317   CHECK_SDIFFERENCE_WRAPV (-1l, 1u, int, false, DONTCARE);
 318   CHECK_SDIFFERENCE_WRAPV (0u, INT_MAX, int, false, DONTCARE);
 319   CHECK_SDIFFERENCE_WRAPV (1u, INT_MIN, int, true, 1u - INT_MIN);
 320   {
 321     long int result;
 322     ASSERT (INT_SUBTRACT_WRAPV (INT_MAX, -1, &result) == (INT_MAX == LONG_MAX));
 323     ASSERT (INT_SUBTRACT_WRAPV (INT_MIN, 1, &result) == (INT_MAX == LONG_MAX));
 324   }
 325 
 326   #define CHECK_PRODUCT(a, b, t, v, vres)                                 \
 327     CHECK_PRODUCT1 (a, b, t, v, vres);                                    \
 328     CHECK_PRODUCT1 (b, a, t, v, vres)
 329   #define CHECK_SPRODUCT(a, b, t, v, vres)                                \
 330     CHECK_SPRODUCT1 (a, b, t, v, vres);                                   \
 331     CHECK_SPRODUCT1 (b, a, t, v, vres)
 332   #define CHECK_SPRODUCT_WRAPV(a, b, t, v, vres)                          \
 333     CHECK_SPRODUCT_WRAPV1 (a, b, t, v, vres);                             \
 334     CHECK_SPRODUCT_WRAPV1 (b, a, t, v, vres)
 335   #define CHECK_PRODUCT1(a, b, t, v, vres)                                \
 336     VERIFY (INT_MULTIPLY_OVERFLOW (a, b) == (v))
 337   #define CHECK_SPRODUCT1(a, b, t, v, vres)                               \
 338     CHECK_PRODUCT1 (a, b, t, v, vres);                                    \
 339     CHECK_SPRODUCT_WRAPV1 (a, b, t, v, vres)
 340   #define CHECK_SPRODUCT_WRAPV1(a, b, t, v, vres)                         \
 341     {                                                                     \
 342       t result;                                                           \
 343       ASSERT (INT_MULTIPLY_WRAPV (a, b, &result) == (v));                 \
 344       ASSERT (result == ((v) ? (vres) : ((a) * (b))));                    \
 345     }
 346   CHECK_PRODUCT (-1, 1u, unsigned int, true, -1 * 1u);
 347   CHECK_SPRODUCT (-1, INT_MIN, int, INT_NEGATE_OVERFLOW (INT_MIN), INT_MIN);
 348   CHECK_PRODUCT (-1, UINT_MAX, unsigned int, true, -1 * UINT_MAX);
 349   CHECK_SPRODUCT (-32768, LONG_MAX / -32768 - 1, long int, true, LONG_MIN);
 350   CHECK_SPRODUCT (-12345, LONG_MAX / -12345, long int, false, DONTCARE);
 351   CHECK_SPRODUCT (0, -1, int, false, DONTCARE);
 352   CHECK_SPRODUCT (0, 0, int, false, DONTCARE);
 353   CHECK_PRODUCT (0, 0u, unsigned int, false, DONTCARE);
 354   CHECK_SPRODUCT (0, 1, int, false, DONTCARE);
 355   CHECK_SPRODUCT (0, INT_MAX, int, false, DONTCARE);
 356   CHECK_SPRODUCT (0, INT_MIN, int, false, DONTCARE);
 357   CHECK_PRODUCT (0, UINT_MAX, unsigned int, false, DONTCARE);
 358   CHECK_PRODUCT (0u, -1, unsigned int, false, DONTCARE);
 359   CHECK_PRODUCT (0u, 0, unsigned int, false, DONTCARE);
 360   CHECK_PRODUCT (0u, 0u, unsigned int, false, DONTCARE);
 361   CHECK_PRODUCT (0u, 1, unsigned int, false, DONTCARE);
 362   CHECK_PRODUCT (0u, INT_MAX, unsigned int, false, DONTCARE);
 363   CHECK_PRODUCT (0u, INT_MIN, unsigned int, false, DONTCARE);
 364   CHECK_PRODUCT (0u, UINT_MAX, unsigned int, false, DONTCARE);
 365   CHECK_SPRODUCT (1, INT_MAX, int, false, DONTCARE);
 366   CHECK_SPRODUCT (1, INT_MIN, int, false, DONTCARE);
 367   CHECK_PRODUCT (1, UINT_MAX, unsigned int, false, DONTCARE);
 368   CHECK_PRODUCT (1u, INT_MIN, unsigned int, true, 1u * INT_MIN);
 369   CHECK_PRODUCT (1u, INT_MAX, unsigned int, UINT_MAX < INT_MAX, 1u * INT_MAX);
 370   CHECK_PRODUCT (INT_MAX, UINT_MAX, unsigned int, true, INT_MAX * UINT_MAX);
 371   CHECK_PRODUCT (INT_MAX, ULONG_MAX, unsigned long int, true,
 372                  INT_MAX * ULONG_MAX);
 373 #if !defined __HP_cc
 374   CHECK_SPRODUCT (INT_MIN, LONG_MAX / INT_MIN - 1, long int, true, LONG_MIN);
 375   CHECK_SPRODUCT (INT_MIN, LONG_MAX / INT_MIN, long int, false, DONTCARE);
 376 #endif
 377   CHECK_PRODUCT (INT_MIN, UINT_MAX, unsigned int, true, INT_MIN * UINT_MAX);
 378   CHECK_PRODUCT (INT_MIN, ULONG_MAX, unsigned long int, true,
 379                  INT_MIN * ULONG_MAX);
 380   CHECK_SPRODUCT_WRAPV (-1, INT_MAX + 1u, int, false, DONTCARE);
 381   CHECK_SPRODUCT_WRAPV (-1, 1u, int, false, DONTCARE);
 382   CHECK_SPRODUCT (0, ULONG_MAX, int, false, DONTCARE);
 383   CHECK_SPRODUCT (0u, LONG_MIN, int, false, DONTCARE);
 384   {
 385     long int result;
 386     ASSERT (INT_MULTIPLY_WRAPV (INT_MAX, INT_MAX, &result)
 387             == (LONG_MAX / INT_MAX < INT_MAX));
 388     ASSERT (INT_MULTIPLY_WRAPV (INT_MAX, INT_MAX, &result)
 389             || result == INT_MAX * (long int) INT_MAX);
 390     ASSERT (INT_MULTIPLY_WRAPV (INT_MIN, INT_MIN, &result)
 391             || result == INT_MIN * (long int) INT_MIN);
 392   }
 393 
 394 # ifdef LLONG_MAX
 395   {
 396     long long int result;
 397     ASSERT (INT_MULTIPLY_WRAPV (LONG_MAX, LONG_MAX, &result)
 398             == (LLONG_MAX / LONG_MAX < LONG_MAX));
 399     ASSERT (INT_MULTIPLY_WRAPV (LONG_MAX, LONG_MAX, &result)
 400             || result == LONG_MAX * (long long int) LONG_MAX);
 401     ASSERT (INT_MULTIPLY_WRAPV (LONG_MIN, LONG_MIN, &result)
 402             || result == LONG_MIN * (long long int) LONG_MIN);
 403   }
 404 # endif
 405 
 406   /* Check for GCC bug 91450.  */
 407   {
 408     unsigned long long result;
 409     ASSERT (INT_MULTIPLY_WRAPV (int_minus_2, int_1, &result) && result == -2);
 410   }
 411 
 412   #define CHECK_QUOTIENT(a, b, v) VERIFY (INT_DIVIDE_OVERFLOW (a, b) == (v))
 413 
 414   CHECK_QUOTIENT (INT_MIN, -1L, INT_MIN == LONG_MIN);
 415   CHECK_QUOTIENT (INT_MIN, UINT_MAX, false);
 416   CHECK_QUOTIENT (INTMAX_MIN, UINTMAX_MAX, false);
 417   CHECK_QUOTIENT (INTMAX_MIN, UINT_MAX, false);
 418   CHECK_QUOTIENT (-11, 10u, true);
 419   CHECK_QUOTIENT (-10, 10u, true);
 420   CHECK_QUOTIENT (-9, 10u, false);
 421   CHECK_QUOTIENT (11u, -10, true);
 422   CHECK_QUOTIENT (10u, -10, true);
 423   CHECK_QUOTIENT (9u, -10, false);
 424 
 425   #define CHECK_REMAINDER(a, b, v) VERIFY (INT_REMAINDER_OVERFLOW (a, b) == (v))
 426 
 427   CHECK_REMAINDER (INT_MIN, -1L, INT_MIN == LONG_MIN);
 428   CHECK_REMAINDER (-1, UINT_MAX, true);
 429   CHECK_REMAINDER ((intmax_t) -1, UINTMAX_MAX, true);
 430   CHECK_REMAINDER (INTMAX_MIN, UINT_MAX,
 431                    (INTMAX_MAX < UINT_MAX
 432                     && - (unsigned int) INTMAX_MIN % UINT_MAX != 0));
 433   CHECK_REMAINDER (INT_MIN, ULONG_MAX, INT_MIN % ULONG_MAX != 1);
 434   CHECK_REMAINDER (1u, -1, false);
 435   CHECK_REMAINDER (37*39u, -39, false);
 436   CHECK_REMAINDER (37*39u + 1, -39, true);
 437   CHECK_REMAINDER (37*39u - 1, -39, true);
 438   CHECK_REMAINDER (LONG_MAX, -INT_MAX, false);
 439 
 440   return 0;
 441 }

/* [previous][next][first][last][top][bottom][index][help] */