1 /* Preprocessor arithmetic semantic tests. */ 2 3 /* Copyright (C) 2002 Free Software Foundation, Inc. */ 4 /* Source: Neil Booth, 26 May 2002. */ 5 6 /* The file tests overflow warnings for, and values of, preprocessor 7 arithmetic that are dependent on target precision. 8 9 Please keep changes to arith-2.c and arith-3.c in sync. */ 10 11 /* { dg-do preprocess } */ 12 /* { dg-options "-std=c99 -fshow-column" } */ 13 14 #include <limits.h> 15 16 #define APPEND2(NUM, SUFF) NUM ## SUFF 17 #define APPEND(NUM, SUFF) APPEND2(NUM, SUFF) 18 19 #define TARGET_UTYPE_MAX ULLONG_MAX 20 21 /* The tests in this file depend only on the macros defined in this 22 #if block. Note that it is no good calculating these values, as 23 the intent is to test both the preprocessor's number parser and 24 arithmetic. */ 25 #if TARGET_UTYPE_MAX == 65535ULL 26 27 # define TARG_PRECISION 16 28 # define MAX_INT 32767 29 # define MAX_UINT 65535 30 31 # define TARG_MAX_HEX 0x7fff 32 # define TARG_MAX_OCT 077777 33 # define TARG_MAX_PLUS_1 32768L 34 # define TARG_MAX_PLUS_1_U 32768UL 35 # define TARG_MAX_PLUS_1_HEX 0x8000 36 # define TARG_MAX_PLUS_1_OCT 0100000 37 # define UTARG_MAX_HEX 0xffff 38 # define UTARG_MAX_OCT 0177777 39 # define UTARG_MAX_PLUS_1 65536L 40 # define UTARG_MAX_PLUS_1_HEX 0x10000 41 # define UTARG_MAX_PLUS_1_OCT 0200000 42 43 # define TARG_LOWPART_PLUS_1 256L 44 # define TARG_LOWPART_PLUS_1_U 256UL 45 46 /* Division and modulo; anything that uses the high half in both 47 dividend and divisor. */ 48 # define LONG_UDIVISION 61234UL / 260L 49 # define LONG_UDIVISION_ANSWER 235 50 # define LONG_SDIVISION -15000L / 299L 51 # define LONG_SDIVISION_ANSWER -50 52 # define LONG_UMODULO 61234UL % 260L 53 # define LONG_UMODULO_ANSWER 134 54 # define LONG_SMODULO -15000L % 299L 55 # define LONG_SMODULO_ANSWER -50 56 57 #elif TARGET_UTYPE_MAX == 4294967295ULL 58 59 # define TARG_PRECISION 32 60 # define MAX_INT 2147483647 61 # define MAX_UINT 4294967295 62 63 # define TARG_MAX_HEX 0x7fffffff 64 # define TARG_MAX_OCT 017777777777 65 # define TARG_MAX_PLUS_1 2147483648L 66 # define TARG_MAX_PLUS_1_U 2147483648UL 67 # define TARG_MAX_PLUS_1_HEX 0x80000000 68 # define TARG_MAX_PLUS_1_OCT 020000000000 69 # define UTARG_MAX_HEX 0xffffffff 70 # define UTARG_MAX_OCT 037777777777 71 # define UTARG_MAX_PLUS_1 4294967296L 72 # define UTARG_MAX_PLUS_1_HEX 0x100000000 73 # define UTARG_MAX_PLUS_1_OCT 040000000000 74 75 # define TARG_LOWPART_PLUS_1 65536 76 # define TARG_LOWPART_PLUS_1_U 65536UL 77 78 /* Division and modulo; anything that uses the high half in both 79 dividend and divisor. */ 80 # define LONG_UDIVISION 268335456UL / 70000L 81 # define LONG_UDIVISION_ANSWER 3833 82 # define LONG_SDIVISION -368335456L / 123456L 83 # define LONG_SDIVISION_ANSWER -2983 84 # define LONG_UMODULO 268335456UL % 70000L 85 # define LONG_UMODULO_ANSWER 25456 86 # define LONG_SMODULO -368335456L % 123456L 87 # define LONG_SMODULO_ANSWER -66208 88 89 #elif TARGET_UTYPE_MAX == 18446744073709551615ULL 90 91 # define TARG_PRECISION 64 92 # define MAX_INT 9223372036854775807 93 # define MAX_UINT 18446744073709551615 94 95 # define TARG_MAX_HEX 0x7fffffffffffffff 96 # define TARG_MAX_OCT 0777777777777777777777 97 # define TARG_MAX_PLUS_1 9223372036854775808L 98 # define TARG_MAX_PLUS_1_U 9223372036854775808UL 99 # define TARG_MAX_PLUS_1_HEX 0x8000000000000000 100 # define TARG_MAX_PLUS_1_OCT 01000000000000000000000 101 # define UTARG_MAX_HEX 0xffffffffffffffff 102 # define UTARG_MAX_OCT 01777777777777777777777 103 # define UTARG_MAX_PLUS_1 18446744073709551616L 104 # define UTARG_MAX_PLUS_1_HEX 0x10000000000000000 105 # define UTARG_MAX_PLUS_1_OCT 02000000000000000000000 106 107 # define TARG_LOWPART_PLUS_1 4294967296 108 # define TARG_LOWPART_PLUS_1_U 4294967296U 109 110 /* Division and modulo; anything that uses the high half in both 111 dividend and divisor. */ 112 # define LONG_UDIVISION 235184372088832UL / 17279869184L 113 # define LONG_UDIVISION_ANSWER 13610 114 # define LONG_SDIVISION -234582345927345L / 12345678901L 115 # define LONG_SDIVISION_ANSWER -19001 116 # define LONG_UMODULO 235184372088832UL % 17279869184L 117 # define LONG_UMODULO_ANSWER 5352494592L 118 # define LONG_SMODULO -234582345927345L % 12345678901L 119 # define LONG_SMODULO_ANSWER -2101129444L 120 121 #else 122 123 # error Please extend the macros here so that this file tests your target 124 125 #endif 126 127 /* Create more macros based on the above. */ 128 #define TARG_PART_BITS (TARG_PRECISION / 2) 129 #define TARG_MIN (-TARG_MAX - 1) 130 #define TARG_MAX APPEND (MAX_INT, L) 131 #define TARG_MAX_U APPEND (MAX_INT, UL) 132 #define UTARG_MAX APPEND (MAX_UINT, L) 133 #define UTARG_MAX_U APPEND (MAX_UINT, UL) 134 135 /* And now the tests. */ 136 137 #if TARG_MAX /* { dg-bogus "so large" } */ 138 #endif 139 #if TARG_MAX_PLUS_1_HEX /* { dg-bogus "so large" } */ 140 #endif 141 #if TARG_MAX_PLUS_1_OCT /* { dg-bogus "so large" } */ 142 #endif 143 144 #if UTARG_MAX /* { dg-warning "so large" } */ 145 #endif 146 #if UTARG_MAX_PLUS_1 /* { dg-warning "too large" } */ 147 #endif 148 #if UTARG_MAX_PLUS_1_HEX /* { dg-warning "too large" } */ 149 #endif 150 #if UTARG_MAX_HEX /* { dg-bogus "too large" } */ 151 #endif 152 #if UTARG_MAX_PLUS_1_OCT /* { dg-warning "too large" } */ 153 #endif 154 #if UTARG_MAX_OCT /* { dg-bogus "too large" } */ 155 #endif 156 157 #if TARG_MAX < 0 || TARG_MAX_PLUS_1 < 0 /* { dg-warning "so large" } */ 158 # error /* { dg-bogus "error" } */ 159 #endif 160 161 #if UTARG_MAX_HEX < 0 || TARG_MAX_HEX < 0 162 # error /* { dg-bogus "error" } */ 163 #endif 164 165 #if UTARG_MAX_OCT < 0 || TARG_MAX_OCT < 0 166 # error /* { dg-bogus "error" } */ 167 #endif 168 169 #if -1 != UTARG_MAX_U 170 # error /* { dg-bogus "error" } */ 171 #endif 172 173 174 175 176 /* Test each operator correctly warns of overflow conditions, and 177 gives the right answer. */ 178 179 /* Binary +. */ 180 #if TARG_MAX + 1 != TARG_MIN /* { dg-warning "overflow" } */ 181 # error /* { dg-bogus "error" } */ 182 #endif 183 184 #if -TARG_MAX + -2 != TARG_MAX /* { dg-warning "overflow" } */ 185 # error /* { dg-bogus "error" } */ 186 #endif 187 188 #if -TARG_MAX + -1 != TARG_MIN /* { dg-bogus "overflow" } */ 189 # error /* { dg-bogus "error" } */ 190 #endif 191 192 #if TARG_MAX_U + 1 != TARG_MIN /* { dg-bogus "overflow" } */ 193 # error /* { dg-bogus "error" } */ 194 #endif 195 196 #if -TARG_MAX_U + -2 != TARG_MAX /* { dg-bogus "overflow" } */ 197 # error /* { dg-bogus "error" } */ 198 #endif 199 200 201 202 203 /* Binary -. */ 204 #if TARG_MAX - -1 != TARG_MIN /* { dg-warning "overflow" } */ 205 # error /* { dg-bogus "error" } */ 206 #endif 207 208 #if -TARG_MAX - 2 != TARG_MAX /* { dg-warning "overflow" } */ 209 # error /* { dg-bogus "error" } */ 210 #endif 211 212 #if -TARG_MAX - 1 != TARG_MIN /* { dg-bogus "overflow" } */ 213 # error /* { dg-bogus "error" } */ 214 #endif 215 216 #if TARG_MAX_U - -1 != TARG_MIN /* { dg-bogus "overflow" } */ 217 # error /* { dg-bogus "error" } */ 218 #endif 219 220 #if -TARG_MAX_U - 2 != TARG_MAX /* { dg-bogus "overflow" } */ 221 # error /* { dg-bogus "error" } */ 222 #endif 223 224 225 226 227 228 /* Binary *. */ 229 #if TARG_LOWPART_PLUS_1 * (TARG_LOWPART_PLUS_1 >> 1) != TARG_MIN /* { dg-warning "overflow" } */ 230 # error /* { dg-bogus "error" } */ 231 #endif 232 233 #if (TARG_LOWPART_PLUS_1 >> 1) * TARG_LOWPART_PLUS_1 != TARG_MIN /* { dg-warning "overflow" } */ 234 # error /* { dg-bogus "error" } */ 235 #endif 236 237 #if (TARG_LOWPART_PLUS_1 << 1) * (TARG_LOWPART_PLUS_1 + 1) != (TARG_LOWPART_PLUS_1 << 1) /* { dg-warning "overflow" } */ 238 # error /* { dg-bogus "error" } */ 239 #endif 240 241 #if TARG_MAX * 1 != TARG_MAX /* { dg-bogus "overflow" } */ 242 # error /* { dg-bogus "error" } */ 243 #endif 244 245 #if (TARG_MAX >> 1) * 2 != TARG_MAX - 1 /* { dg-bogus "overflow" } */ 246 # error /* { dg-bogus "error" } */ 247 #endif 248 249 #if (TARG_LOWPART_PLUS_1_U + 61) * (TARG_LOWPART_PLUS_1 << 1) != 61 * (TARG_LOWPART_PLUS_1 << 1) /* { dg-bogus "overflow" } */ 250 # error /* { dg-bogus "error" } */ 251 #endif 252 253 #if (TARG_LOWPART_PLUS_1 >> 1) * TARG_LOWPART_PLUS_1_U != TARG_MIN /* { dg-bogus "overflow" } */ 254 # error /* { dg-bogus "error" } */ 255 #endif 256 257 #if 1 * TARG_MIN != TARG_MIN /* { dg-bogus "overflow" } */ 258 # error /* { dg-bogus "error" } */ 259 #endif 260 261 262 263 264 /* Binary /. */ 265 #if TARG_MIN / -1 != TARG_MIN /* { dg-warning "overflow" } */ 266 # error /* { dg-bogus "error" } */ 267 #endif 268 269 #if TARG_MIN / 1 != TARG_MIN /* { dg-bogus "overflow" } */ 270 # error /* { dg-bogus "error" } */ 271 #endif 272 273 #if -TARG_MAX_PLUS_1_U / -1 != 0 /* { dg-bogus "overflow" } */ 274 # error /* { dg-bogus "error" } */ 275 #endif 276 277 #if -5 / (2 - 2) /* { dg-error "13:division by zero" } */ 278 #endif 279 280 #if LONG_UDIVISION != LONG_UDIVISION_ANSWER 281 # error /* { dg-bogus "error" } */ 282 #endif 283 284 #if LONG_SDIVISION != LONG_SDIVISION_ANSWER 285 # error /* { dg-bogus "error" } */ 286 #endif 287 288 /* Binary %. Cannot overflow. */ 289 #if -5 % (2 - 2) /* { dg-error "13:division by zero" } */ 290 #endif 291 292 #if TARG_MIN % 1 /* { dg-bogus "overflow" } */ 293 # error /* { dg-bogus "error" } */ 294 #endif 295 296 #if LONG_UMODULO != LONG_UMODULO_ANSWER 297 # error /* { dg-bogus "error" } */ 298 #endif 299 300 #if LONG_SMODULO != LONG_SMODULO_ANSWER 301 # error /* { dg-bogus "error" } */ 302 #endif 303 304 #if 234 % -1U != 234 305 # error /* { dg-bogus "error" } */ 306 #endif 307 308 #if TARG_MIN % -1U != TARG_MIN 309 # error /* { dg-bogus "error" } */ 310 #endif 311 312 /* Binary << and Binary >>, the latter cannot overflow. */ 313 #if -1 >> 3 != -1 /* { dg-bogus "overflow" } */ 314 # error /* { dg-bogus "error" } */ 315 #endif 316 317 #if TARG_MAX >> 3 != TARG_MAX / 8 /* { dg-bogus "overflow" } */ 318 # error /* { dg-bogus "error" } */ 319 #endif 320 321 #if 0 << 256 != 0 /* { dg-bogus "overflow" } */ 322 # error /* { dg-bogus "error" } */ 323 #endif 324 325 #if 1 << 256 != 0 /* { dg-warning "overflow" } */ 326 # error /* { dg-bogus "error" } */ 327 #endif 328 329 #if 1U << 256 != 0 /* { dg-bogus "overflow" } */ 330 # error /* { dg-bogus "error" } */ 331 #endif 332 333 #if TARG_MAX << 1 != -2 /* { dg-warning "overflow" } */ 334 # error /* { dg-bogus "error" } */ 335 #endif 336 337 #if TARG_MAX_U << 1 != -2 /* { dg-bogus "overflow" } */ 338 # error /* { dg-bogus "error" } */ 339 #endif 340 341 #if TARG_LOWPART_PLUS_1 << TARG_PART_BITS != 0 /* { dg-warning "overflow" } */ 342 # error /* { dg-bogus "error" } */ 343 #endif 344 345 #if TARG_LOWPART_PLUS_1 << (TARG_PART_BITS - 1) != TARG_MIN /* { dg-warning "overflow" } */ 346 # error /* { dg-bogus "error" } */ 347 #endif 348 349 #if TARG_LOWPART_PLUS_1_U << (TARG_PART_BITS - 1) != TARG_MIN /* { dg-bogus "overflow" } */ 350 # error /* { dg-bogus "error" } */ 351 #endif 352 353 #if TARG_LOWPART_PLUS_1 << (TARG_PART_BITS - 2) != (TARG_MAX_PLUS_1_U >> 1) /* { dg-bogus "overflow" } */ 354 # error /* { dg-bogus "error" } */ 355 #endif 356 357 /* Test how the sign bit is handled. */ 358 #if (TARG_MIN << 1) != 0 /* { dg-warning "overflow" } */ 359 # error /* { dg-bogus "error" } */ 360 #endif 361 362 #if (TARG_MAX_PLUS_1_U << 1) != 0 /* { dg-bogus "overflow" } */ 363 # error /* { dg-bogus "error" } */ 364 #endif 365 366 #if (TARG_MIN >> 1) != 3U << (TARG_PRECISION - 2) /* { dg-bogus "overflow" } */ 367 # error /* { dg-bogus "error" } */ 368 #endif 369 370 #if (TARG_MAX_PLUS_1_U >> 1) != 1 << (TARG_PRECISION - 2) /* { dg-bogus "overflow" } */ 371 # error /* { dg-bogus "error" } */ 372 #endif 373 374 375 376 /* Unary -. It can overflow in just one case. */ 377 #if -TARG_MIN != TARG_MIN /* { dg-warning "overflow" } */ 378 # error /* { dg-bogus "error" } */ 379 #endif 380 381 #if - -TARG_MAX != TARG_MAX /* { dg-bogus "overflow" } */ 382 # error /* { dg-bogus "error" } */ 383 #endif 384 385 386 387 388 /* Unary +, ~, and !. They cannot overflow. */ 389 #if +TARG_MAX != TARG_MAX /* { dg-bogus "overflow" } */ 390 # error /* { dg-bogus "error" } */ 391 #endif 392 393 #if !TARG_MAX + !TARG_MIN != 0 /* { dg-bogus "overflow" } */ 394 # error /* { dg-bogus "error" } */ 395 #endif 396 397 #if ~TARG_MAX , ~TARG_MIN != TARG_MAX /* { dg-bogus "overflow" } */ 398 # error /* { dg-bogus "error" } */ 399 #endif 400 401 402 403 404 /* Bitwise &, ^, |. They cannot overflow. */ 405 #if (TARG_MAX & -1), (TARG_MIN & -1) != TARG_MIN /* { dg-bogus "overflow" } */ 406 # error /* { dg-bogus "error" } */ 407 #endif 408 409 #if TARG_MAX | -1, (TARG_MIN | -1) != -1 /* { dg-bogus "overflow" } */ 410 # error /* { dg-bogus "error" } */ 411 #endif 412 413 #if TARG_MAX ^ -1, (TARG_MIN ^ -1) != TARG_MAX /* { dg-bogus "overflow" } */ 414 # error /* { dg-bogus "error" } */ 415 #endif 416 417 418 419 420 /* Comparison operators. They cannot overflow. */ 421 #if -1 <= TARG_MAX, (TARG_MIN <= 1) != 1 /* { dg-bogus "overflow" } */ 422 # error /* { dg-bogus "error" } */ 423 #endif 424 425 #if -1 >= TARG_MAX, (TARG_MIN >= 1) != 0 /* { dg-bogus "overflow" } */ 426 # error /* { dg-bogus "error" } */ 427 #endif 428 429 #if -1 < TARG_MAX, (TARG_MIN < 1) != 1 /* { dg-bogus "overflow" } */ 430 # error /* { dg-bogus "error" } */ 431 #endif 432 433 #if -1 > TARG_MAX, (TARG_MIN > 1) != 0 /* { dg-bogus "overflow" } */ 434 # error /* { dg-bogus "error" } */ 435 #endif 436 437 438 439 440 /* Comma and ? : operators. They cannot overflow. */ 441 #if -1, TARG_MAX, TARG_MIN != TARG_MIN /* { dg-bogus "overflow" } */ 442 # error /* { dg-bogus "error" } */ 443 #endif 444 445 #if -1 ? TARG_MAX: TARG_MAX, 0 ? 1: TARG_MIN != TARG_MIN /* { dg-bogus "overflow" } */ 446 # error /* { dg-bogus "error" } */ 447 #endif 448