modernc.org/ccgo/v3@v3.16.14/lib/testdata/gcc-9.1.0/gcc/testsuite/gcc.c-torture/execute/conversion.c (about) 1 /* Test front-end conversions, optimizer conversions, and run-time 2 conversions between different arithmetic types. 3 4 Constants are specified in a non-obvious way to make them work for 5 any word size. Their value on a 32-bit machine is indicated in the 6 comments. 7 8 Note that this code is NOT intended for testing of accuracy of fp 9 conversions. */ 10 11 float 12 u2f(u) 13 unsigned int u; 14 { 15 return u; 16 } 17 18 double 19 u2d(u) 20 unsigned int u; 21 { 22 return u; 23 } 24 25 long double 26 u2ld(u) 27 unsigned int u; 28 { 29 return u; 30 } 31 32 float 33 s2f(s) 34 int s; 35 { 36 return s; 37 } 38 39 double 40 s2d(s) 41 int s; 42 { 43 return s; 44 } 45 46 long double 47 s2ld(s) 48 int s; 49 { 50 return s; 51 } 52 53 int 54 fnear (float x, float y) 55 { 56 float t = x - y; 57 return t == 0 || x / t > 1000000.0; 58 } 59 60 int 61 dnear (double x, double y) 62 { 63 double t = x - y; 64 return t == 0 || x / t > 100000000000000.0; 65 } 66 67 int 68 ldnear (long double x, long double y) 69 { 70 long double t = x - y; 71 return t == 0 || x / t > 100000000000000000000000000000000.0; 72 } 73 74 test_integer_to_float() 75 { 76 if (u2f(0U) != (float) 0U) /* 0 */ 77 abort(); 78 if (!fnear (u2f(~0U), (float) ~0U)) /* 0xffffffff */ 79 abort(); 80 if (!fnear (u2f((~0U) >> 1), (float) ((~0U) >> 1))) /* 0x7fffffff */ 81 abort(); 82 if (u2f(~((~0U) >> 1)) != (float) ~((~0U) >> 1)) /* 0x80000000 */ 83 abort(); 84 85 if (u2d(0U) != (double) 0U) /* 0 */ 86 abort(); 87 if (!dnear (u2d(~0U), (double) ~0U)) /* 0xffffffff */ 88 abort(); 89 if (!dnear (u2d((~0U) >> 1),(double) ((~0U) >> 1))) /* 0x7fffffff */ 90 abort(); 91 if (u2d(~((~0U) >> 1)) != (double) ~((~0U) >> 1)) /* 0x80000000 */ 92 abort(); 93 94 if (u2ld(0U) != (long double) 0U) /* 0 */ 95 abort(); 96 if (!ldnear (u2ld(~0U), (long double) ~0U)) /* 0xffffffff */ 97 abort(); 98 if (!ldnear (u2ld((~0U) >> 1),(long double) ((~0U) >> 1))) /* 0x7fffffff */ 99 abort(); 100 if (u2ld(~((~0U) >> 1)) != (long double) ~((~0U) >> 1)) /* 0x80000000 */ 101 abort(); 102 103 if (s2f(0) != (float) 0) /* 0 */ 104 abort(); 105 if (!fnear (s2f(~0), (float) ~0)) /* 0xffffffff */ 106 abort(); 107 if (!fnear (s2f((int)((~0U) >> 1)), (float)(int)((~0U) >> 1))) /* 0x7fffffff */ 108 abort(); 109 if (s2f((int)(~((~0U) >> 1))) != (float)(int)~((~0U) >> 1)) /* 0x80000000 */ 110 abort(); 111 112 if (s2d(0) != (double) 0) /* 0 */ 113 abort(); 114 if (!dnear (s2d(~0), (double) ~0)) /* 0xffffffff */ 115 abort(); 116 if (!dnear (s2d((int)((~0U) >> 1)), (double)(int)((~0U) >> 1))) /* 0x7fffffff */ 117 abort(); 118 if (s2d((int)~((~0U) >> 1)) != (double)(int)~((~0U) >> 1)) /* 0x80000000 */ 119 abort(); 120 121 if (s2ld(0) != (long double) 0) /* 0 */ 122 abort(); 123 if (!ldnear (s2ld(~0), (long double) ~0)) /* 0xffffffff */ 124 abort(); 125 if (!ldnear (s2ld((int)((~0U) >> 1)), (long double)(int)((~0U) >> 1))) /* 0x7fffffff */ 126 abort(); 127 if (s2ld((int)~((~0U) >> 1)) != (long double)(int)~((~0U) >> 1)) /* 0x80000000 */ 128 abort(); 129 } 130 131 #if __GNUC__ 132 float 133 ull2f(u) 134 unsigned long long int u; 135 { 136 return u; 137 } 138 139 double 140 ull2d(u) 141 unsigned long long int u; 142 { 143 return u; 144 } 145 146 long double 147 ull2ld(u) 148 unsigned long long int u; 149 { 150 return u; 151 } 152 153 float 154 sll2f(s) 155 long long int s; 156 { 157 return s; 158 } 159 160 double 161 sll2d(s) 162 long long int s; 163 { 164 return s; 165 } 166 167 long double 168 sll2ld(s) 169 long long int s; 170 { 171 return s; 172 } 173 174 test_longlong_integer_to_float() 175 { 176 if (ull2f(0ULL) != (float) 0ULL) /* 0 */ 177 abort(); 178 if (ull2f(~0ULL) != (float) ~0ULL) /* 0xffffffff */ 179 abort(); 180 if (ull2f((~0ULL) >> 1) != (float) ((~0ULL) >> 1)) /* 0x7fffffff */ 181 abort(); 182 if (ull2f(~((~0ULL) >> 1)) != (float) ~((~0ULL) >> 1)) /* 0x80000000 */ 183 abort(); 184 185 if (ull2d(0ULL) != (double) 0ULL) /* 0 */ 186 abort(); 187 #if __HAVE_68881__ 188 /* Some 68881 targets return values in fp0, with excess precision. 189 But the compile-time conversion to double works correctly. */ 190 if (! dnear (ull2d(~0ULL), (double) ~0ULL)) /* 0xffffffff */ 191 abort(); 192 if (! dnear (ull2d((~0ULL) >> 1), (double) ((~0ULL) >> 1))) /* 0x7fffffff */ 193 abort(); 194 #else 195 if (ull2d(~0ULL) != (double) ~0ULL) /* 0xffffffff */ 196 abort(); 197 if (ull2d((~0ULL) >> 1) != (double) ((~0ULL) >> 1)) /* 0x7fffffff */ 198 abort(); 199 #endif 200 if (ull2d(~((~0ULL) >> 1)) != (double) ~((~0ULL) >> 1)) /* 0x80000000 */ 201 abort(); 202 203 if (ull2ld(0ULL) != (long double) 0ULL) /* 0 */ 204 abort(); 205 if (ull2ld(~0ULL) != (long double) ~0ULL) /* 0xffffffff */ 206 abort(); 207 if (ull2ld((~0ULL) >> 1) != (long double) ((~0ULL) >> 1)) /* 0x7fffffff */ 208 abort(); 209 if (ull2ld(~((~0ULL) >> 1)) != (long double) ~((~0ULL) >> 1)) /* 0x80000000 */ 210 abort(); 211 212 if (sll2f(0LL) != (float) 0LL) /* 0 */ 213 abort(); 214 if (sll2f(~0LL) != (float) ~0LL) /* 0xffffffff */ 215 abort(); 216 if (! fnear (sll2f((long long int)((~0ULL) >> 1)), (float)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */ 217 abort(); 218 if (sll2f((long long int)(~((~0ULL) >> 1))) != (float)(long long int)~((~0ULL) >> 1)) /* 0x80000000 */ 219 abort(); 220 221 if (sll2d(0LL) != (double) 0LL) /* 0 */ 222 abort(); 223 if (sll2d(~0LL) != (double) ~0LL) /* 0xffffffff */ 224 abort(); 225 if (!dnear (sll2d((long long int)((~0ULL) >> 1)), (double)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */ 226 abort(); 227 if (! dnear (sll2d((long long int)~((~0ULL) >> 1)), (double)(long long int)~((~0ULL) >> 1))) /* 0x80000000 */ 228 abort(); 229 230 if (sll2ld(0LL) != (long double) 0LL) /* 0 */ 231 abort(); 232 if (sll2ld(~0LL) != (long double) ~0LL) /* 0xffffffff */ 233 abort(); 234 if (!ldnear (sll2ld((long long int)((~0ULL) >> 1)), (long double)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */ 235 abort(); 236 if (! ldnear (sll2ld((long long int)~((~0ULL) >> 1)), (long double)(long long int)~((~0ULL) >> 1))) /* 0x80000000 */ 237 abort(); 238 } 239 #endif 240 241 unsigned int 242 f2u(float f) 243 { 244 return (unsigned) f; 245 } 246 247 unsigned int 248 d2u(double d) 249 { 250 return (unsigned) d; 251 } 252 253 unsigned int 254 ld2u(long double d) 255 { 256 return (unsigned) d; 257 } 258 259 int 260 f2s(float f) 261 { 262 return (int) f; 263 } 264 265 int 266 d2s(double d) 267 { 268 return (int) d; 269 } 270 271 int 272 ld2s(long double d) 273 { 274 return (int) d; 275 } 276 277 test_float_to_integer() 278 { 279 if (f2u(0.0) != 0) 280 abort(); 281 if (f2u(0.999) != 0) 282 abort(); 283 if (f2u(1.0) != 1) 284 abort(); 285 if (f2u(1.99) != 1) 286 abort(); 287 #ifdef __SPU__ 288 /* SPU float rounds towards zero. */ 289 if (f2u((float) ((~0U) >> 1)) != 0x7fffff80) 290 abort(); 291 #else 292 if (f2u((float) ((~0U) >> 1)) != (~0U) >> 1 && /* 0x7fffffff */ 293 f2u((float) ((~0U) >> 1)) != ((~0U) >> 1) + 1) 294 abort(); 295 #endif 296 if (f2u((float) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */ 297 abort(); 298 299 /* These tests require double precision, so for hosts that don't offer 300 that much precision, just ignore these test. */ 301 if (sizeof (double) >= 8) { 302 if (d2u(0.0) != 0) 303 abort(); 304 if (d2u(0.999) != 0) 305 abort(); 306 if (d2u(1.0) != 1) 307 abort(); 308 if (d2u(1.99) != 1) 309 abort(); 310 if (d2u((double) (~0U)) != ~0U) /* 0xffffffff */ 311 abort(); 312 if (d2u((double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */ 313 abort(); 314 if (d2u((double) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */ 315 abort(); 316 } 317 318 /* These tests require long double precision, so for hosts that don't offer 319 that much precision, just ignore these test. */ 320 if (sizeof (long double) >= 8) { 321 if (ld2u(0.0) != 0) 322 abort(); 323 if (ld2u(0.999) != 0) 324 abort(); 325 if (ld2u(1.0) != 1) 326 abort(); 327 if (ld2u(1.99) != 1) 328 abort(); 329 if (ld2u((long double) (~0U)) != ~0U) /* 0xffffffff */ 330 abort(); 331 if (ld2u((long double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */ 332 abort(); 333 if (ld2u((long double) ~((~0U) >> 1)) != ~((~0U) >> 1)) /* 0x80000000 */ 334 abort(); 335 } 336 337 if (f2s(0.0) != 0) 338 abort(); 339 if (f2s(0.999) != 0) 340 abort(); 341 if (f2s(1.0) != 1) 342 abort(); 343 if (f2s(1.99) != 1) 344 abort(); 345 if (f2s(-0.999) != 0) 346 abort(); 347 if (f2s(-1.0) != -1) 348 abort(); 349 if (f2s(-1.99) != -1) 350 abort(); 351 if (f2s((float)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */ 352 abort(); 353 354 /* These tests require double precision, so for hosts that don't offer 355 that much precision, just ignore these test. */ 356 if (sizeof (double) >= 8) { 357 if (d2s(0.0) != 0) 358 abort(); 359 if (d2s(0.999) != 0) 360 abort(); 361 if (d2s(1.0) != 1) 362 abort(); 363 if (d2s(1.99) != 1) 364 abort(); 365 if (d2s(-0.999) != 0) 366 abort(); 367 if (d2s(-1.0) != -1) 368 abort(); 369 if (d2s(-1.99) != -1) 370 abort(); 371 if (d2s((double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */ 372 abort(); 373 if (d2s((double)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */ 374 abort(); 375 } 376 377 /* These tests require long double precision, so for hosts that don't offer 378 that much precision, just ignore these test. */ 379 if (sizeof (long double) >= 8) { 380 if (ld2s(0.0) != 0) 381 abort(); 382 if (ld2s(0.999) != 0) 383 abort(); 384 if (ld2s(1.0) != 1) 385 abort(); 386 if (ld2s(1.99) != 1) 387 abort(); 388 if (ld2s(-0.999) != 0) 389 abort(); 390 if (ld2s(-1.0) != -1) 391 abort(); 392 if (ld2s(-1.99) != -1) 393 abort(); 394 if (ld2s((long double) ((~0U) >> 1)) != (~0U) >> 1) /* 0x7fffffff */ 395 abort(); 396 if (ld2s((long double)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */ 397 abort(); 398 } 399 } 400 401 #if __GNUC__ 402 unsigned long long int 403 f2ull(float f) 404 { 405 return (unsigned long long int) f; 406 } 407 408 unsigned long long int 409 d2ull(double d) 410 { 411 return (unsigned long long int) d; 412 } 413 414 unsigned long long int 415 ld2ull(long double d) 416 { 417 return (unsigned long long int) d; 418 } 419 420 long long int 421 f2sll(float f) 422 { 423 return (long long int) f; 424 } 425 426 long long int 427 d2sll(double d) 428 { 429 return (long long int) d; 430 } 431 432 long long int 433 ld2sll(long double d) 434 { 435 return (long long int) d; 436 } 437 438 test_float_to_longlong_integer() 439 { 440 if (f2ull(0.0) != 0LL) 441 abort(); 442 if (f2ull(0.999) != 0LL) 443 abort(); 444 if (f2ull(1.0) != 1LL) 445 abort(); 446 if (f2ull(1.99) != 1LL) 447 abort(); 448 #ifdef __SPU__ 449 /* SPU float rounds towards zero. */ 450 if (f2ull((float) ((~0ULL) >> 1)) != 0x7fffff8000000000ULL) 451 abort(); 452 #else 453 if (f2ull((float) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */ 454 f2ull((float) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1) 455 abort(); 456 #endif 457 if (f2ull((float) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */ 458 abort(); 459 460 if (d2ull(0.0) != 0LL) 461 abort(); 462 if (d2ull(0.999) != 0LL) 463 abort(); 464 if (d2ull(1.0) != 1LL) 465 abort(); 466 if (d2ull(1.99) != 1LL) 467 abort(); 468 if (d2ull((double) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */ 469 d2ull((double) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1) 470 abort(); 471 if (d2ull((double) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */ 472 abort(); 473 474 if (ld2ull(0.0) != 0LL) 475 abort(); 476 if (ld2ull(0.999) != 0LL) 477 abort(); 478 if (ld2ull(1.0) != 1LL) 479 abort(); 480 if (ld2ull(1.99) != 1LL) 481 abort(); 482 if (ld2ull((long double) ((~0ULL) >> 1)) != (~0ULL) >> 1 && /* 0x7fffffff */ 483 ld2ull((long double) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1) 484 abort(); 485 if (ld2ull((long double) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */ 486 abort(); 487 488 489 if (f2sll(0.0) != 0LL) 490 abort(); 491 if (f2sll(0.999) != 0LL) 492 abort(); 493 if (f2sll(1.0) != 1LL) 494 abort(); 495 if (f2sll(1.99) != 1LL) 496 abort(); 497 if (f2sll(-0.999) != 0LL) 498 abort(); 499 if (f2sll(-1.0) != -1LL) 500 abort(); 501 if (f2sll(-1.99) != -1LL) 502 abort(); 503 if (f2sll((float)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */ 504 abort(); 505 506 if (d2sll(0.0) != 0LL) 507 abort(); 508 if (d2sll(0.999) != 0LL) 509 abort(); 510 if (d2sll(1.0) != 1LL) 511 abort(); 512 if (d2sll(1.99) != 1LL) 513 abort(); 514 if (d2sll(-0.999) != 0LL) 515 abort(); 516 if (d2sll(-1.0) != -1LL) 517 abort(); 518 if (d2sll(-1.99) != -1LL) 519 abort(); 520 if (d2sll((double)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */ 521 abort(); 522 523 if (ld2sll(0.0) != 0LL) 524 abort(); 525 if (ld2sll(0.999) != 0LL) 526 abort(); 527 if (ld2sll(1.0) != 1LL) 528 abort(); 529 if (ld2sll(1.99) != 1LL) 530 abort(); 531 if (ld2sll(-0.999) != 0LL) 532 abort(); 533 if (ld2sll(-1.0) != -1LL) 534 abort(); 535 if (ld2sll(-1.99) != -1LL) 536 abort(); 537 if (ld2sll((long double)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */ 538 abort(); 539 } 540 #endif 541 542 main() 543 { 544 test_integer_to_float(); 545 test_float_to_integer(); 546 #if __GNUC__ 547 test_longlong_integer_to_float(); 548 test_float_to_longlong_integer(); 549 #endif 550 exit(0); 551 }