github.com/ltltlt/go-source-code@v0.0.0-20190830023027-95be009773aa/cmd/compile/internal/gc/testdata/fp.go (about) 1 // run 2 3 // Copyright 2015 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // Tests floating point arithmetic expressions 8 9 package main 10 11 import "fmt" 12 13 // manysub_ssa is designed to tickle bugs that depend on register 14 // pressure or unfriendly operand ordering in registers (and at 15 // least once it succeeded in this). 16 //go:noinline 17 func manysub_ssa(a, b, c, d float64) (aa, ab, ac, ad, ba, bb, bc, bd, ca, cb, cc, cd, da, db, dc, dd float64) { 18 aa = a + 11.0 - a 19 ab = a - b 20 ac = a - c 21 ad = a - d 22 ba = b - a 23 bb = b + 22.0 - b 24 bc = b - c 25 bd = b - d 26 ca = c - a 27 cb = c - b 28 cc = c + 33.0 - c 29 cd = c - d 30 da = d - a 31 db = d - b 32 dc = d - c 33 dd = d + 44.0 - d 34 return 35 } 36 37 // fpspill_ssa attempts to trigger a bug where phis with floating point values 38 // were stored in non-fp registers causing an error in doasm. 39 //go:noinline 40 func fpspill_ssa(a int) float64 { 41 42 ret := -1.0 43 switch a { 44 case 0: 45 ret = 1.0 46 case 1: 47 ret = 1.1 48 case 2: 49 ret = 1.2 50 case 3: 51 ret = 1.3 52 case 4: 53 ret = 1.4 54 case 5: 55 ret = 1.5 56 case 6: 57 ret = 1.6 58 case 7: 59 ret = 1.7 60 case 8: 61 ret = 1.8 62 case 9: 63 ret = 1.9 64 case 10: 65 ret = 1.10 66 case 11: 67 ret = 1.11 68 case 12: 69 ret = 1.12 70 case 13: 71 ret = 1.13 72 case 14: 73 ret = 1.14 74 case 15: 75 ret = 1.15 76 case 16: 77 ret = 1.16 78 } 79 return ret 80 } 81 82 //go:noinline 83 func add64_ssa(a, b float64) float64 { 84 return a + b 85 } 86 87 //go:noinline 88 func mul64_ssa(a, b float64) float64 { 89 return a * b 90 } 91 92 //go:noinline 93 func sub64_ssa(a, b float64) float64 { 94 return a - b 95 } 96 97 //go:noinline 98 func div64_ssa(a, b float64) float64 { 99 return a / b 100 } 101 102 //go:noinline 103 func neg64_ssa(a, b float64) float64 { 104 return -a + -1*b 105 } 106 107 //go:noinline 108 func add32_ssa(a, b float32) float32 { 109 return a + b 110 } 111 112 //go:noinline 113 func mul32_ssa(a, b float32) float32 { 114 return a * b 115 } 116 117 //go:noinline 118 func sub32_ssa(a, b float32) float32 { 119 return a - b 120 } 121 122 //go:noinline 123 func div32_ssa(a, b float32) float32 { 124 return a / b 125 } 126 127 //go:noinline 128 func neg32_ssa(a, b float32) float32 { 129 return -a + -1*b 130 } 131 132 //go:noinline 133 func conv2Float64_ssa(a int8, b uint8, c int16, d uint16, 134 e int32, f uint32, g int64, h uint64, i float32) (aa, bb, cc, dd, ee, ff, gg, hh, ii float64) { 135 aa = float64(a) 136 bb = float64(b) 137 cc = float64(c) 138 hh = float64(h) 139 dd = float64(d) 140 ee = float64(e) 141 ff = float64(f) 142 gg = float64(g) 143 ii = float64(i) 144 return 145 } 146 147 //go:noinline 148 func conv2Float32_ssa(a int8, b uint8, c int16, d uint16, 149 e int32, f uint32, g int64, h uint64, i float64) (aa, bb, cc, dd, ee, ff, gg, hh, ii float32) { 150 aa = float32(a) 151 bb = float32(b) 152 cc = float32(c) 153 dd = float32(d) 154 ee = float32(e) 155 ff = float32(f) 156 gg = float32(g) 157 hh = float32(h) 158 ii = float32(i) 159 return 160 } 161 162 func integer2floatConversions() int { 163 fails := 0 164 { 165 a, b, c, d, e, f, g, h, i := conv2Float64_ssa(0, 0, 0, 0, 0, 0, 0, 0, 0) 166 fails += expectAll64("zero64", 0, a, b, c, d, e, f, g, h, i) 167 } 168 { 169 a, b, c, d, e, f, g, h, i := conv2Float64_ssa(1, 1, 1, 1, 1, 1, 1, 1, 1) 170 fails += expectAll64("one64", 1, a, b, c, d, e, f, g, h, i) 171 } 172 { 173 a, b, c, d, e, f, g, h, i := conv2Float32_ssa(0, 0, 0, 0, 0, 0, 0, 0, 0) 174 fails += expectAll32("zero32", 0, a, b, c, d, e, f, g, h, i) 175 } 176 { 177 a, b, c, d, e, f, g, h, i := conv2Float32_ssa(1, 1, 1, 1, 1, 1, 1, 1, 1) 178 fails += expectAll32("one32", 1, a, b, c, d, e, f, g, h, i) 179 } 180 { 181 // Check maximum values 182 a, b, c, d, e, f, g, h, i := conv2Float64_ssa(127, 255, 32767, 65535, 0x7fffffff, 0xffffffff, 0x7fffFFFFffffFFFF, 0xffffFFFFffffFFFF, 3.402823E38) 183 fails += expect64("a", a, 127) 184 fails += expect64("b", b, 255) 185 fails += expect64("c", c, 32767) 186 fails += expect64("d", d, 65535) 187 fails += expect64("e", e, float64(int32(0x7fffffff))) 188 fails += expect64("f", f, float64(uint32(0xffffffff))) 189 fails += expect64("g", g, float64(int64(0x7fffffffffffffff))) 190 fails += expect64("h", h, float64(uint64(0xffffffffffffffff))) 191 fails += expect64("i", i, float64(float32(3.402823E38))) 192 } 193 { 194 // Check minimum values (and tweaks for unsigned) 195 a, b, c, d, e, f, g, h, i := conv2Float64_ssa(-128, 254, -32768, 65534, ^0x7fffffff, 0xfffffffe, ^0x7fffFFFFffffFFFF, 0xffffFFFFffffF401, 1.5E-45) 196 fails += expect64("a", a, -128) 197 fails += expect64("b", b, 254) 198 fails += expect64("c", c, -32768) 199 fails += expect64("d", d, 65534) 200 fails += expect64("e", e, float64(^int32(0x7fffffff))) 201 fails += expect64("f", f, float64(uint32(0xfffffffe))) 202 fails += expect64("g", g, float64(^int64(0x7fffffffffffffff))) 203 fails += expect64("h", h, float64(uint64(0xfffffffffffff401))) 204 fails += expect64("i", i, float64(float32(1.5E-45))) 205 } 206 { 207 // Check maximum values 208 a, b, c, d, e, f, g, h, i := conv2Float32_ssa(127, 255, 32767, 65535, 0x7fffffff, 0xffffffff, 0x7fffFFFFffffFFFF, 0xffffFFFFffffFFFF, 3.402823E38) 209 fails += expect32("a", a, 127) 210 fails += expect32("b", b, 255) 211 fails += expect32("c", c, 32767) 212 fails += expect32("d", d, 65535) 213 fails += expect32("e", e, float32(int32(0x7fffffff))) 214 fails += expect32("f", f, float32(uint32(0xffffffff))) 215 fails += expect32("g", g, float32(int64(0x7fffffffffffffff))) 216 fails += expect32("h", h, float32(uint64(0xffffffffffffffff))) 217 fails += expect32("i", i, float32(float64(3.402823E38))) 218 } 219 { 220 // Check minimum values (and tweaks for unsigned) 221 a, b, c, d, e, f, g, h, i := conv2Float32_ssa(-128, 254, -32768, 65534, ^0x7fffffff, 0xfffffffe, ^0x7fffFFFFffffFFFF, 0xffffFFFFffffF401, 1.5E-45) 222 fails += expect32("a", a, -128) 223 fails += expect32("b", b, 254) 224 fails += expect32("c", c, -32768) 225 fails += expect32("d", d, 65534) 226 fails += expect32("e", e, float32(^int32(0x7fffffff))) 227 fails += expect32("f", f, float32(uint32(0xfffffffe))) 228 fails += expect32("g", g, float32(^int64(0x7fffffffffffffff))) 229 fails += expect32("h", h, float32(uint64(0xfffffffffffff401))) 230 fails += expect32("i", i, float32(float64(1.5E-45))) 231 } 232 return fails 233 } 234 235 func multiplyAdd() int { 236 fails := 0 237 { 238 // Test that a multiply-accumulate operation with intermediate 239 // rounding forced by a float32() cast produces the expected 240 // result. 241 // Test cases generated experimentally on a system (s390x) that 242 // supports fused multiply-add instructions. 243 var tests = [...]struct{ x, y, z, res float32 }{ 244 {0.6046603, 0.9405091, 0.6645601, 1.2332485}, // fused multiply-add result: 1.2332486 245 {0.67908466, 0.21855305, 0.20318687, 0.3516029}, // fused multiply-add result: 0.35160288 246 {0.29311424, 0.29708257, 0.752573, 0.8396522}, // fused multiply-add result: 0.8396521 247 {0.5305857, 0.2535405, 0.282081, 0.41660595}, // fused multiply-add result: 0.41660598 248 {0.29711226, 0.89436173, 0.097454615, 0.36318043}, // fused multiply-add result: 0.36318046 249 {0.6810783, 0.24151509, 0.31152245, 0.47601312}, // fused multiply-add result: 0.47601315 250 {0.73023146, 0.18292491, 0.4283571, 0.5619346}, // fused multiply-add result: 0.56193465 251 {0.89634174, 0.32208398, 0.7211478, 1.009845}, // fused multiply-add result: 1.0098451 252 {0.6280982, 0.12675293, 0.2813303, 0.36094356}, // fused multiply-add result: 0.3609436 253 {0.29400632, 0.75316125, 0.15096405, 0.3723982}, // fused multiply-add result: 0.37239823 254 } 255 check := func(s string, got, expected float32) int { 256 if got != expected { 257 fmt.Printf("multiplyAdd: %s, expected %g, got %g\n", s, expected, got) 258 return 1 259 } 260 return 0 261 } 262 for _, t := range tests { 263 fails += check( 264 fmt.Sprintf("float32(%v * %v) + %v", t.x, t.y, t.z), 265 func(x, y, z float32) float32 { 266 return float32(x*y) + z 267 }(t.x, t.y, t.z), 268 t.res) 269 270 fails += check( 271 fmt.Sprintf("%v += float32(%v * %v)", t.z, t.x, t.y), 272 func(x, y, z float32) float32 { 273 z += float32(x * y) 274 return z 275 }(t.x, t.y, t.z), 276 t.res) 277 } 278 } 279 { 280 // Test that a multiply-accumulate operation with intermediate 281 // rounding forced by a float64() cast produces the expected 282 // result. 283 // Test cases generated experimentally on a system (s390x) that 284 // supports fused multiply-add instructions. 285 var tests = [...]struct{ x, y, z, res float64 }{ 286 {0.4688898449024232, 0.28303415118044517, 0.29310185733681576, 0.42581369658590373}, // fused multiply-add result: 0.4258136965859037 287 {0.7886049150193449, 0.3618054804803169, 0.8805431227416171, 1.1658647029293308}, // fused multiply-add result: 1.1658647029293305 288 {0.7302314772948083, 0.18292491645390843, 0.4283570818068078, 0.5619346137829748}, // fused multiply-add result: 0.5619346137829747 289 {0.6908388315056789, 0.7109071952999951, 0.5637795958152644, 1.0549018919252924}, // fused multiply-add result: 1.0549018919252926 290 {0.4584424785756506, 0.6001655953233308, 0.02626515060968944, 0.3014065536855481}, // fused multiply-add result: 0.30140655368554814 291 {0.539210105890946, 0.9756748149873165, 0.7507630564795985, 1.2768567767840384}, // fused multiply-add result: 1.2768567767840386 292 {0.7830349733960021, 0.3932509992288867, 0.1304138461737918, 0.4383431318929343}, // fused multiply-add result: 0.43834313189293433 293 {0.6841751300974551, 0.6530402051353608, 0.524499759549865, 0.9712936268572192}, // fused multiply-add result: 0.9712936268572193 294 {0.3691117091643448, 0.826454125634742, 0.34768170859156955, 0.6527356034505334}, // fused multiply-add result: 0.6527356034505333 295 {0.16867966833433606, 0.33136826030698385, 0.8279280961505588, 0.8838231843956668}, // fused multiply-add result: 0.8838231843956669 296 } 297 check := func(s string, got, expected float64) int { 298 if got != expected { 299 fmt.Printf("multiplyAdd: %s, expected %g, got %g\n", s, expected, got) 300 return 1 301 } 302 return 0 303 } 304 for _, t := range tests { 305 fails += check( 306 fmt.Sprintf("float64(%v * %v) + %v", t.x, t.y, t.z), 307 func(x, y, z float64) float64 { 308 return float64(x*y) + z 309 }(t.x, t.y, t.z), 310 t.res) 311 312 fails += check( 313 fmt.Sprintf("%v += float64(%v * %v)", t.z, t.x, t.y), 314 func(x, y, z float64) float64 { 315 z += float64(x * y) 316 return z 317 }(t.x, t.y, t.z), 318 t.res) 319 } 320 } 321 { 322 // Test that a multiply-accumulate operation with intermediate 323 // rounding forced by a complex128() cast produces the expected 324 // result. 325 // Test cases generated experimentally on a system (s390x) that 326 // supports fused multiply-add instructions. 327 var tests = [...]struct { 328 x, y float64 329 res complex128 330 }{ 331 {0.6046602879796196, 0.9405090880450124, (2.754489951983871 + 3i)}, // fused multiply-add result: (2.7544899519838713 + 3i) 332 {0.09696951891448456, 0.30091186058528707, (0.5918204173287407 + 3i)}, // fused multiply-add result: (0.5918204173287408 + 3i) 333 {0.544155573000885, 0.27850762181610883, (1.910974340818764 + 3i)}, // fused multiply-add result: (1.9109743408187638 + 3i) 334 {0.9769168685862624, 0.07429099894984302, (3.0050416047086297 + 3i)}, // fused multiply-add result: (3.00504160470863 + 3i) 335 {0.9269868035744142, 0.9549454404167818, (3.735905851140024 + 3i)}, // fused multiply-add result: (3.7359058511400245 + 3i) 336 {0.7109071952999951, 0.5637795958152644, (2.69650118171525 + 3i)}, // fused multiply-add result: (2.6965011817152496 + 3i) 337 {0.7558235074915978, 0.40380328579570035, (2.671273808270494 + 3i)}, // fused multiply-add result: (2.6712738082704934 + 3i) 338 {0.13065111702897217, 0.9859647293402467, (1.3779180804271633 + 3i)}, // fused multiply-add result: (1.3779180804271631 + 3i) 339 {0.8963417453962161, 0.3220839705208817, (3.0111092067095298 + 3i)}, // fused multiply-add result: (3.01110920670953 + 3i) 340 {0.39998376285699544, 0.497868113342702, (1.697819401913688 + 3i)}, // fused multiply-add result: (1.6978194019136883 + 3i) 341 } 342 check := func(s string, got, expected complex128) int { 343 if got != expected { 344 fmt.Printf("multiplyAdd: %s, expected %v, got %v\n", s, expected, got) 345 return 1 346 } 347 return 0 348 } 349 for _, t := range tests { 350 fails += check( 351 fmt.Sprintf("complex128(complex(%v, 1)*3) + complex(%v, 0)", t.x, t.y), 352 func(x, y float64) complex128 { 353 return complex128(complex(x, 1)*3) + complex(y, 0) 354 }(t.x, t.y), 355 t.res) 356 357 fails += check( 358 fmt.Sprintf("z := complex(%v, 1); z += complex128(complex(%v, 1) * 3)", t.y, t.x), 359 func(x, y float64) complex128 { 360 z := complex(y, 0) 361 z += complex128(complex(x, 1) * 3) 362 return z 363 }(t.x, t.y), 364 t.res) 365 } 366 } 367 return fails 368 } 369 370 const ( 371 aa = 0x1000000000000000 372 ab = 0x100000000000000 373 ac = 0x10000000000000 374 ad = 0x1000000000000 375 ba = 0x100000000000 376 bb = 0x10000000000 377 bc = 0x1000000000 378 bd = 0x100000000 379 ca = 0x10000000 380 cb = 0x1000000 381 cc = 0x100000 382 cd = 0x10000 383 da = 0x1000 384 db = 0x100 385 dc = 0x10 386 dd = 0x1 387 ) 388 389 //go:noinline 390 func compares64_ssa(a, b, c, d float64) (lt, le, eq, ne, ge, gt uint64) { 391 if a < a { 392 lt += aa 393 } 394 if a < b { 395 lt += ab 396 } 397 if a < c { 398 lt += ac 399 } 400 if a < d { 401 lt += ad 402 } 403 404 if b < a { 405 lt += ba 406 } 407 if b < b { 408 lt += bb 409 } 410 if b < c { 411 lt += bc 412 } 413 if b < d { 414 lt += bd 415 } 416 417 if c < a { 418 lt += ca 419 } 420 if c < b { 421 lt += cb 422 } 423 if c < c { 424 lt += cc 425 } 426 if c < d { 427 lt += cd 428 } 429 430 if d < a { 431 lt += da 432 } 433 if d < b { 434 lt += db 435 } 436 if d < c { 437 lt += dc 438 } 439 if d < d { 440 lt += dd 441 } 442 443 if a <= a { 444 le += aa 445 } 446 if a <= b { 447 le += ab 448 } 449 if a <= c { 450 le += ac 451 } 452 if a <= d { 453 le += ad 454 } 455 456 if b <= a { 457 le += ba 458 } 459 if b <= b { 460 le += bb 461 } 462 if b <= c { 463 le += bc 464 } 465 if b <= d { 466 le += bd 467 } 468 469 if c <= a { 470 le += ca 471 } 472 if c <= b { 473 le += cb 474 } 475 if c <= c { 476 le += cc 477 } 478 if c <= d { 479 le += cd 480 } 481 482 if d <= a { 483 le += da 484 } 485 if d <= b { 486 le += db 487 } 488 if d <= c { 489 le += dc 490 } 491 if d <= d { 492 le += dd 493 } 494 495 if a == a { 496 eq += aa 497 } 498 if a == b { 499 eq += ab 500 } 501 if a == c { 502 eq += ac 503 } 504 if a == d { 505 eq += ad 506 } 507 508 if b == a { 509 eq += ba 510 } 511 if b == b { 512 eq += bb 513 } 514 if b == c { 515 eq += bc 516 } 517 if b == d { 518 eq += bd 519 } 520 521 if c == a { 522 eq += ca 523 } 524 if c == b { 525 eq += cb 526 } 527 if c == c { 528 eq += cc 529 } 530 if c == d { 531 eq += cd 532 } 533 534 if d == a { 535 eq += da 536 } 537 if d == b { 538 eq += db 539 } 540 if d == c { 541 eq += dc 542 } 543 if d == d { 544 eq += dd 545 } 546 547 if a != a { 548 ne += aa 549 } 550 if a != b { 551 ne += ab 552 } 553 if a != c { 554 ne += ac 555 } 556 if a != d { 557 ne += ad 558 } 559 560 if b != a { 561 ne += ba 562 } 563 if b != b { 564 ne += bb 565 } 566 if b != c { 567 ne += bc 568 } 569 if b != d { 570 ne += bd 571 } 572 573 if c != a { 574 ne += ca 575 } 576 if c != b { 577 ne += cb 578 } 579 if c != c { 580 ne += cc 581 } 582 if c != d { 583 ne += cd 584 } 585 586 if d != a { 587 ne += da 588 } 589 if d != b { 590 ne += db 591 } 592 if d != c { 593 ne += dc 594 } 595 if d != d { 596 ne += dd 597 } 598 599 if a >= a { 600 ge += aa 601 } 602 if a >= b { 603 ge += ab 604 } 605 if a >= c { 606 ge += ac 607 } 608 if a >= d { 609 ge += ad 610 } 611 612 if b >= a { 613 ge += ba 614 } 615 if b >= b { 616 ge += bb 617 } 618 if b >= c { 619 ge += bc 620 } 621 if b >= d { 622 ge += bd 623 } 624 625 if c >= a { 626 ge += ca 627 } 628 if c >= b { 629 ge += cb 630 } 631 if c >= c { 632 ge += cc 633 } 634 if c >= d { 635 ge += cd 636 } 637 638 if d >= a { 639 ge += da 640 } 641 if d >= b { 642 ge += db 643 } 644 if d >= c { 645 ge += dc 646 } 647 if d >= d { 648 ge += dd 649 } 650 651 if a > a { 652 gt += aa 653 } 654 if a > b { 655 gt += ab 656 } 657 if a > c { 658 gt += ac 659 } 660 if a > d { 661 gt += ad 662 } 663 664 if b > a { 665 gt += ba 666 } 667 if b > b { 668 gt += bb 669 } 670 if b > c { 671 gt += bc 672 } 673 if b > d { 674 gt += bd 675 } 676 677 if c > a { 678 gt += ca 679 } 680 if c > b { 681 gt += cb 682 } 683 if c > c { 684 gt += cc 685 } 686 if c > d { 687 gt += cd 688 } 689 690 if d > a { 691 gt += da 692 } 693 if d > b { 694 gt += db 695 } 696 if d > c { 697 gt += dc 698 } 699 if d > d { 700 gt += dd 701 } 702 703 return 704 } 705 706 //go:noinline 707 func compares32_ssa(a, b, c, d float32) (lt, le, eq, ne, ge, gt uint64) { 708 if a < a { 709 lt += aa 710 } 711 if a < b { 712 lt += ab 713 } 714 if a < c { 715 lt += ac 716 } 717 if a < d { 718 lt += ad 719 } 720 721 if b < a { 722 lt += ba 723 } 724 if b < b { 725 lt += bb 726 } 727 if b < c { 728 lt += bc 729 } 730 if b < d { 731 lt += bd 732 } 733 734 if c < a { 735 lt += ca 736 } 737 if c < b { 738 lt += cb 739 } 740 if c < c { 741 lt += cc 742 } 743 if c < d { 744 lt += cd 745 } 746 747 if d < a { 748 lt += da 749 } 750 if d < b { 751 lt += db 752 } 753 if d < c { 754 lt += dc 755 } 756 if d < d { 757 lt += dd 758 } 759 760 if a <= a { 761 le += aa 762 } 763 if a <= b { 764 le += ab 765 } 766 if a <= c { 767 le += ac 768 } 769 if a <= d { 770 le += ad 771 } 772 773 if b <= a { 774 le += ba 775 } 776 if b <= b { 777 le += bb 778 } 779 if b <= c { 780 le += bc 781 } 782 if b <= d { 783 le += bd 784 } 785 786 if c <= a { 787 le += ca 788 } 789 if c <= b { 790 le += cb 791 } 792 if c <= c { 793 le += cc 794 } 795 if c <= d { 796 le += cd 797 } 798 799 if d <= a { 800 le += da 801 } 802 if d <= b { 803 le += db 804 } 805 if d <= c { 806 le += dc 807 } 808 if d <= d { 809 le += dd 810 } 811 812 if a == a { 813 eq += aa 814 } 815 if a == b { 816 eq += ab 817 } 818 if a == c { 819 eq += ac 820 } 821 if a == d { 822 eq += ad 823 } 824 825 if b == a { 826 eq += ba 827 } 828 if b == b { 829 eq += bb 830 } 831 if b == c { 832 eq += bc 833 } 834 if b == d { 835 eq += bd 836 } 837 838 if c == a { 839 eq += ca 840 } 841 if c == b { 842 eq += cb 843 } 844 if c == c { 845 eq += cc 846 } 847 if c == d { 848 eq += cd 849 } 850 851 if d == a { 852 eq += da 853 } 854 if d == b { 855 eq += db 856 } 857 if d == c { 858 eq += dc 859 } 860 if d == d { 861 eq += dd 862 } 863 864 if a != a { 865 ne += aa 866 } 867 if a != b { 868 ne += ab 869 } 870 if a != c { 871 ne += ac 872 } 873 if a != d { 874 ne += ad 875 } 876 877 if b != a { 878 ne += ba 879 } 880 if b != b { 881 ne += bb 882 } 883 if b != c { 884 ne += bc 885 } 886 if b != d { 887 ne += bd 888 } 889 890 if c != a { 891 ne += ca 892 } 893 if c != b { 894 ne += cb 895 } 896 if c != c { 897 ne += cc 898 } 899 if c != d { 900 ne += cd 901 } 902 903 if d != a { 904 ne += da 905 } 906 if d != b { 907 ne += db 908 } 909 if d != c { 910 ne += dc 911 } 912 if d != d { 913 ne += dd 914 } 915 916 if a >= a { 917 ge += aa 918 } 919 if a >= b { 920 ge += ab 921 } 922 if a >= c { 923 ge += ac 924 } 925 if a >= d { 926 ge += ad 927 } 928 929 if b >= a { 930 ge += ba 931 } 932 if b >= b { 933 ge += bb 934 } 935 if b >= c { 936 ge += bc 937 } 938 if b >= d { 939 ge += bd 940 } 941 942 if c >= a { 943 ge += ca 944 } 945 if c >= b { 946 ge += cb 947 } 948 if c >= c { 949 ge += cc 950 } 951 if c >= d { 952 ge += cd 953 } 954 955 if d >= a { 956 ge += da 957 } 958 if d >= b { 959 ge += db 960 } 961 if d >= c { 962 ge += dc 963 } 964 if d >= d { 965 ge += dd 966 } 967 968 if a > a { 969 gt += aa 970 } 971 if a > b { 972 gt += ab 973 } 974 if a > c { 975 gt += ac 976 } 977 if a > d { 978 gt += ad 979 } 980 981 if b > a { 982 gt += ba 983 } 984 if b > b { 985 gt += bb 986 } 987 if b > c { 988 gt += bc 989 } 990 if b > d { 991 gt += bd 992 } 993 994 if c > a { 995 gt += ca 996 } 997 if c > b { 998 gt += cb 999 } 1000 if c > c { 1001 gt += cc 1002 } 1003 if c > d { 1004 gt += cd 1005 } 1006 1007 if d > a { 1008 gt += da 1009 } 1010 if d > b { 1011 gt += db 1012 } 1013 if d > c { 1014 gt += dc 1015 } 1016 if d > d { 1017 gt += dd 1018 } 1019 1020 return 1021 } 1022 1023 //go:noinline 1024 func le64_ssa(x, y float64) bool { 1025 return x <= y 1026 } 1027 1028 //go:noinline 1029 func ge64_ssa(x, y float64) bool { 1030 return x >= y 1031 } 1032 1033 //go:noinline 1034 func lt64_ssa(x, y float64) bool { 1035 return x < y 1036 } 1037 1038 //go:noinline 1039 func gt64_ssa(x, y float64) bool { 1040 return x > y 1041 } 1042 1043 //go:noinline 1044 func eq64_ssa(x, y float64) bool { 1045 return x == y 1046 } 1047 1048 //go:noinline 1049 func ne64_ssa(x, y float64) bool { 1050 return x != y 1051 } 1052 1053 //go:noinline 1054 func eqbr64_ssa(x, y float64) float64 { 1055 if x == y { 1056 return 17 1057 } 1058 return 42 1059 } 1060 1061 //go:noinline 1062 func nebr64_ssa(x, y float64) float64 { 1063 if x != y { 1064 return 17 1065 } 1066 return 42 1067 } 1068 1069 //go:noinline 1070 func gebr64_ssa(x, y float64) float64 { 1071 if x >= y { 1072 return 17 1073 } 1074 return 42 1075 } 1076 1077 //go:noinline 1078 func lebr64_ssa(x, y float64) float64 { 1079 if x <= y { 1080 return 17 1081 } 1082 return 42 1083 } 1084 1085 //go:noinline 1086 func ltbr64_ssa(x, y float64) float64 { 1087 if x < y { 1088 return 17 1089 } 1090 return 42 1091 } 1092 1093 //go:noinline 1094 func gtbr64_ssa(x, y float64) float64 { 1095 if x > y { 1096 return 17 1097 } 1098 return 42 1099 } 1100 1101 //go:noinline 1102 func le32_ssa(x, y float32) bool { 1103 return x <= y 1104 } 1105 1106 //go:noinline 1107 func ge32_ssa(x, y float32) bool { 1108 return x >= y 1109 } 1110 1111 //go:noinline 1112 func lt32_ssa(x, y float32) bool { 1113 return x < y 1114 } 1115 1116 //go:noinline 1117 func gt32_ssa(x, y float32) bool { 1118 return x > y 1119 } 1120 1121 //go:noinline 1122 func eq32_ssa(x, y float32) bool { 1123 return x == y 1124 } 1125 1126 //go:noinline 1127 func ne32_ssa(x, y float32) bool { 1128 return x != y 1129 } 1130 1131 //go:noinline 1132 func eqbr32_ssa(x, y float32) float32 { 1133 if x == y { 1134 return 17 1135 } 1136 return 42 1137 } 1138 1139 //go:noinline 1140 func nebr32_ssa(x, y float32) float32 { 1141 if x != y { 1142 return 17 1143 } 1144 return 42 1145 } 1146 1147 //go:noinline 1148 func gebr32_ssa(x, y float32) float32 { 1149 if x >= y { 1150 return 17 1151 } 1152 return 42 1153 } 1154 1155 //go:noinline 1156 func lebr32_ssa(x, y float32) float32 { 1157 if x <= y { 1158 return 17 1159 } 1160 return 42 1161 } 1162 1163 //go:noinline 1164 func ltbr32_ssa(x, y float32) float32 { 1165 if x < y { 1166 return 17 1167 } 1168 return 42 1169 } 1170 1171 //go:noinline 1172 func gtbr32_ssa(x, y float32) float32 { 1173 if x > y { 1174 return 17 1175 } 1176 return 42 1177 } 1178 1179 //go:noinline 1180 func F32toU8_ssa(x float32) uint8 { 1181 return uint8(x) 1182 } 1183 1184 //go:noinline 1185 func F32toI8_ssa(x float32) int8 { 1186 return int8(x) 1187 } 1188 1189 //go:noinline 1190 func F32toU16_ssa(x float32) uint16 { 1191 return uint16(x) 1192 } 1193 1194 //go:noinline 1195 func F32toI16_ssa(x float32) int16 { 1196 return int16(x) 1197 } 1198 1199 //go:noinline 1200 func F32toU32_ssa(x float32) uint32 { 1201 return uint32(x) 1202 } 1203 1204 //go:noinline 1205 func F32toI32_ssa(x float32) int32 { 1206 return int32(x) 1207 } 1208 1209 //go:noinline 1210 func F32toU64_ssa(x float32) uint64 { 1211 return uint64(x) 1212 } 1213 1214 //go:noinline 1215 func F32toI64_ssa(x float32) int64 { 1216 return int64(x) 1217 } 1218 1219 //go:noinline 1220 func F64toU8_ssa(x float64) uint8 { 1221 return uint8(x) 1222 } 1223 1224 //go:noinline 1225 func F64toI8_ssa(x float64) int8 { 1226 return int8(x) 1227 } 1228 1229 //go:noinline 1230 func F64toU16_ssa(x float64) uint16 { 1231 return uint16(x) 1232 } 1233 1234 //go:noinline 1235 func F64toI16_ssa(x float64) int16 { 1236 return int16(x) 1237 } 1238 1239 //go:noinline 1240 func F64toU32_ssa(x float64) uint32 { 1241 return uint32(x) 1242 } 1243 1244 //go:noinline 1245 func F64toI32_ssa(x float64) int32 { 1246 return int32(x) 1247 } 1248 1249 //go:noinline 1250 func F64toU64_ssa(x float64) uint64 { 1251 return uint64(x) 1252 } 1253 1254 //go:noinline 1255 func F64toI64_ssa(x float64) int64 { 1256 return int64(x) 1257 } 1258 1259 func floatsToInts(x float64, expected int64) int { 1260 y := float32(x) 1261 fails := 0 1262 fails += expectInt64("F64toI8", int64(F64toI8_ssa(x)), expected) 1263 fails += expectInt64("F64toI16", int64(F64toI16_ssa(x)), expected) 1264 fails += expectInt64("F64toI32", int64(F64toI32_ssa(x)), expected) 1265 fails += expectInt64("F64toI64", int64(F64toI64_ssa(x)), expected) 1266 fails += expectInt64("F32toI8", int64(F32toI8_ssa(y)), expected) 1267 fails += expectInt64("F32toI16", int64(F32toI16_ssa(y)), expected) 1268 fails += expectInt64("F32toI32", int64(F32toI32_ssa(y)), expected) 1269 fails += expectInt64("F32toI64", int64(F32toI64_ssa(y)), expected) 1270 return fails 1271 } 1272 1273 func floatsToUints(x float64, expected uint64) int { 1274 y := float32(x) 1275 fails := 0 1276 fails += expectUint64("F64toU8", uint64(F64toU8_ssa(x)), expected) 1277 fails += expectUint64("F64toU16", uint64(F64toU16_ssa(x)), expected) 1278 fails += expectUint64("F64toU32", uint64(F64toU32_ssa(x)), expected) 1279 fails += expectUint64("F64toU64", uint64(F64toU64_ssa(x)), expected) 1280 fails += expectUint64("F32toU8", uint64(F32toU8_ssa(y)), expected) 1281 fails += expectUint64("F32toU16", uint64(F32toU16_ssa(y)), expected) 1282 fails += expectUint64("F32toU32", uint64(F32toU32_ssa(y)), expected) 1283 fails += expectUint64("F32toU64", uint64(F32toU64_ssa(y)), expected) 1284 return fails 1285 } 1286 1287 func floatingToIntegerConversionsTest() int { 1288 fails := 0 1289 fails += floatsToInts(0.0, 0) 1290 fails += floatsToInts(0.5, 0) 1291 fails += floatsToInts(0.9, 0) 1292 fails += floatsToInts(1.0, 1) 1293 fails += floatsToInts(1.5, 1) 1294 fails += floatsToInts(127.0, 127) 1295 fails += floatsToInts(-1.0, -1) 1296 fails += floatsToInts(-128.0, -128) 1297 1298 fails += floatsToUints(0.0, 0) 1299 fails += floatsToUints(1.0, 1) 1300 fails += floatsToUints(255.0, 255) 1301 1302 for j := uint(0); j < 24; j++ { 1303 // Avoid hard cases in the construction 1304 // of the test inputs. 1305 v := int64(1<<62) | int64(1<<(62-j)) 1306 w := uint64(v) 1307 f := float32(v) 1308 d := float64(v) 1309 fails += expectUint64("2**62...", F32toU64_ssa(f), w) 1310 fails += expectUint64("2**62...", F64toU64_ssa(d), w) 1311 fails += expectInt64("2**62...", F32toI64_ssa(f), v) 1312 fails += expectInt64("2**62...", F64toI64_ssa(d), v) 1313 fails += expectInt64("2**62...", F32toI64_ssa(-f), -v) 1314 fails += expectInt64("2**62...", F64toI64_ssa(-d), -v) 1315 w += w 1316 f += f 1317 d += d 1318 fails += expectUint64("2**63...", F32toU64_ssa(f), w) 1319 fails += expectUint64("2**63...", F64toU64_ssa(d), w) 1320 } 1321 1322 for j := uint(0); j < 16; j++ { 1323 // Avoid hard cases in the construction 1324 // of the test inputs. 1325 v := int32(1<<30) | int32(1<<(30-j)) 1326 w := uint32(v) 1327 f := float32(v) 1328 d := float64(v) 1329 fails += expectUint32("2**30...", F32toU32_ssa(f), w) 1330 fails += expectUint32("2**30...", F64toU32_ssa(d), w) 1331 fails += expectInt32("2**30...", F32toI32_ssa(f), v) 1332 fails += expectInt32("2**30...", F64toI32_ssa(d), v) 1333 fails += expectInt32("2**30...", F32toI32_ssa(-f), -v) 1334 fails += expectInt32("2**30...", F64toI32_ssa(-d), -v) 1335 w += w 1336 f += f 1337 d += d 1338 fails += expectUint32("2**31...", F32toU32_ssa(f), w) 1339 fails += expectUint32("2**31...", F64toU32_ssa(d), w) 1340 } 1341 1342 for j := uint(0); j < 15; j++ { 1343 // Avoid hard cases in the construction 1344 // of the test inputs. 1345 v := int16(1<<14) | int16(1<<(14-j)) 1346 w := uint16(v) 1347 f := float32(v) 1348 d := float64(v) 1349 fails += expectUint16("2**14...", F32toU16_ssa(f), w) 1350 fails += expectUint16("2**14...", F64toU16_ssa(d), w) 1351 fails += expectInt16("2**14...", F32toI16_ssa(f), v) 1352 fails += expectInt16("2**14...", F64toI16_ssa(d), v) 1353 fails += expectInt16("2**14...", F32toI16_ssa(-f), -v) 1354 fails += expectInt16("2**14...", F64toI16_ssa(-d), -v) 1355 w += w 1356 f += f 1357 d += d 1358 fails += expectUint16("2**15...", F32toU16_ssa(f), w) 1359 fails += expectUint16("2**15...", F64toU16_ssa(d), w) 1360 } 1361 1362 fails += expectInt32("-2147483648", F32toI32_ssa(-2147483648), -2147483648) 1363 1364 fails += expectInt32("-2147483648", F64toI32_ssa(-2147483648), -2147483648) 1365 fails += expectInt32("-2147483647", F64toI32_ssa(-2147483647), -2147483647) 1366 fails += expectUint32("4294967295", F64toU32_ssa(4294967295), 4294967295) 1367 1368 fails += expectInt16("-32768", F64toI16_ssa(-32768), -32768) 1369 fails += expectInt16("-32768", F32toI16_ssa(-32768), -32768) 1370 1371 // NB more of a pain to do these for 32-bit because of lost bits in Float32 mantissa 1372 fails += expectInt16("32767", F64toI16_ssa(32767), 32767) 1373 fails += expectInt16("32767", F32toI16_ssa(32767), 32767) 1374 fails += expectUint16("32767", F64toU16_ssa(32767), 32767) 1375 fails += expectUint16("32767", F32toU16_ssa(32767), 32767) 1376 fails += expectUint16("65535", F64toU16_ssa(65535), 65535) 1377 fails += expectUint16("65535", F32toU16_ssa(65535), 65535) 1378 1379 return fails 1380 } 1381 1382 func fail64(s string, f func(a, b float64) float64, a, b, e float64) int { 1383 d := f(a, b) 1384 if d != e { 1385 fmt.Printf("For (float64) %v %v %v, expected %v, got %v\n", a, s, b, e, d) 1386 return 1 1387 } 1388 return 0 1389 } 1390 1391 func fail64bool(s string, f func(a, b float64) bool, a, b float64, e bool) int { 1392 d := f(a, b) 1393 if d != e { 1394 fmt.Printf("For (float64) %v %v %v, expected %v, got %v\n", a, s, b, e, d) 1395 return 1 1396 } 1397 return 0 1398 } 1399 1400 func fail32(s string, f func(a, b float32) float32, a, b, e float32) int { 1401 d := f(a, b) 1402 if d != e { 1403 fmt.Printf("For (float32) %v %v %v, expected %v, got %v\n", a, s, b, e, d) 1404 return 1 1405 } 1406 return 0 1407 } 1408 1409 func fail32bool(s string, f func(a, b float32) bool, a, b float32, e bool) int { 1410 d := f(a, b) 1411 if d != e { 1412 fmt.Printf("For (float32) %v %v %v, expected %v, got %v\n", a, s, b, e, d) 1413 return 1 1414 } 1415 return 0 1416 } 1417 1418 func expect64(s string, x, expected float64) int { 1419 if x != expected { 1420 println("F64 Expected", expected, "for", s, ", got", x) 1421 return 1 1422 } 1423 return 0 1424 } 1425 1426 func expect32(s string, x, expected float32) int { 1427 if x != expected { 1428 println("F32 Expected", expected, "for", s, ", got", x) 1429 return 1 1430 } 1431 return 0 1432 } 1433 1434 func expectUint64(s string, x, expected uint64) int { 1435 if x != expected { 1436 fmt.Printf("U64 Expected 0x%016x for %s, got 0x%016x\n", expected, s, x) 1437 return 1 1438 } 1439 return 0 1440 } 1441 1442 func expectInt64(s string, x, expected int64) int { 1443 if x != expected { 1444 fmt.Printf("%s: Expected 0x%016x, got 0x%016x\n", s, expected, x) 1445 return 1 1446 } 1447 return 0 1448 } 1449 1450 func expectUint32(s string, x, expected uint32) int { 1451 if x != expected { 1452 fmt.Printf("U32 %s: Expected 0x%08x, got 0x%08x\n", s, expected, x) 1453 return 1 1454 } 1455 return 0 1456 } 1457 1458 func expectInt32(s string, x, expected int32) int { 1459 if x != expected { 1460 fmt.Printf("I32 %s: Expected 0x%08x, got 0x%08x\n", s, expected, x) 1461 return 1 1462 } 1463 return 0 1464 } 1465 1466 func expectUint16(s string, x, expected uint16) int { 1467 if x != expected { 1468 fmt.Printf("U16 %s: Expected 0x%04x, got 0x%04x\n", s, expected, x) 1469 return 1 1470 } 1471 return 0 1472 } 1473 1474 func expectInt16(s string, x, expected int16) int { 1475 if x != expected { 1476 fmt.Printf("I16 %s: Expected 0x%04x, got 0x%04x\n", s, expected, x) 1477 return 1 1478 } 1479 return 0 1480 } 1481 1482 func expectAll64(s string, expected, a, b, c, d, e, f, g, h, i float64) int { 1483 fails := 0 1484 fails += expect64(s+":a", a, expected) 1485 fails += expect64(s+":b", b, expected) 1486 fails += expect64(s+":c", c, expected) 1487 fails += expect64(s+":d", d, expected) 1488 fails += expect64(s+":e", e, expected) 1489 fails += expect64(s+":f", f, expected) 1490 fails += expect64(s+":g", g, expected) 1491 return fails 1492 } 1493 1494 func expectAll32(s string, expected, a, b, c, d, e, f, g, h, i float32) int { 1495 fails := 0 1496 fails += expect32(s+":a", a, expected) 1497 fails += expect32(s+":b", b, expected) 1498 fails += expect32(s+":c", c, expected) 1499 fails += expect32(s+":d", d, expected) 1500 fails += expect32(s+":e", e, expected) 1501 fails += expect32(s+":f", f, expected) 1502 fails += expect32(s+":g", g, expected) 1503 return fails 1504 } 1505 1506 var ev64 [2]float64 = [2]float64{42.0, 17.0} 1507 var ev32 [2]float32 = [2]float32{42.0, 17.0} 1508 1509 func cmpOpTest(s string, 1510 f func(a, b float64) bool, 1511 g func(a, b float64) float64, 1512 ff func(a, b float32) bool, 1513 gg func(a, b float32) float32, 1514 zero, one, inf, nan float64, result uint) int { 1515 fails := 0 1516 fails += fail64bool(s, f, zero, zero, result>>16&1 == 1) 1517 fails += fail64bool(s, f, zero, one, result>>12&1 == 1) 1518 fails += fail64bool(s, f, zero, inf, result>>8&1 == 1) 1519 fails += fail64bool(s, f, zero, nan, result>>4&1 == 1) 1520 fails += fail64bool(s, f, nan, nan, result&1 == 1) 1521 1522 fails += fail64(s, g, zero, zero, ev64[result>>16&1]) 1523 fails += fail64(s, g, zero, one, ev64[result>>12&1]) 1524 fails += fail64(s, g, zero, inf, ev64[result>>8&1]) 1525 fails += fail64(s, g, zero, nan, ev64[result>>4&1]) 1526 fails += fail64(s, g, nan, nan, ev64[result>>0&1]) 1527 1528 { 1529 zero := float32(zero) 1530 one := float32(one) 1531 inf := float32(inf) 1532 nan := float32(nan) 1533 fails += fail32bool(s, ff, zero, zero, (result>>16)&1 == 1) 1534 fails += fail32bool(s, ff, zero, one, (result>>12)&1 == 1) 1535 fails += fail32bool(s, ff, zero, inf, (result>>8)&1 == 1) 1536 fails += fail32bool(s, ff, zero, nan, (result>>4)&1 == 1) 1537 fails += fail32bool(s, ff, nan, nan, result&1 == 1) 1538 1539 fails += fail32(s, gg, zero, zero, ev32[(result>>16)&1]) 1540 fails += fail32(s, gg, zero, one, ev32[(result>>12)&1]) 1541 fails += fail32(s, gg, zero, inf, ev32[(result>>8)&1]) 1542 fails += fail32(s, gg, zero, nan, ev32[(result>>4)&1]) 1543 fails += fail32(s, gg, nan, nan, ev32[(result>>0)&1]) 1544 } 1545 1546 return fails 1547 } 1548 1549 func expectCx128(s string, x, expected complex128) int { 1550 if x != expected { 1551 println("Cx 128 Expected", expected, "for", s, ", got", x) 1552 return 1 1553 } 1554 return 0 1555 } 1556 1557 func expectCx64(s string, x, expected complex64) int { 1558 if x != expected { 1559 println("Cx 64 Expected", expected, "for", s, ", got", x) 1560 return 1 1561 } 1562 return 0 1563 } 1564 1565 //go:noinline 1566 func cx128sum_ssa(a, b complex128) complex128 { 1567 return a + b 1568 } 1569 1570 //go:noinline 1571 func cx128diff_ssa(a, b complex128) complex128 { 1572 return a - b 1573 } 1574 1575 //go:noinline 1576 func cx128prod_ssa(a, b complex128) complex128 { 1577 return a * b 1578 } 1579 1580 //go:noinline 1581 func cx128quot_ssa(a, b complex128) complex128 { 1582 return a / b 1583 } 1584 1585 //go:noinline 1586 func cx128neg_ssa(a complex128) complex128 { 1587 return -a 1588 } 1589 1590 //go:noinline 1591 func cx128real_ssa(a complex128) float64 { 1592 return real(a) 1593 } 1594 1595 //go:noinline 1596 func cx128imag_ssa(a complex128) float64 { 1597 return imag(a) 1598 } 1599 1600 //go:noinline 1601 func cx128cnst_ssa(a complex128) complex128 { 1602 b := 2 + 3i 1603 return a * b 1604 } 1605 1606 //go:noinline 1607 func cx64sum_ssa(a, b complex64) complex64 { 1608 return a + b 1609 } 1610 1611 //go:noinline 1612 func cx64diff_ssa(a, b complex64) complex64 { 1613 return a - b 1614 } 1615 1616 //go:noinline 1617 func cx64prod_ssa(a, b complex64) complex64 { 1618 return a * b 1619 } 1620 1621 //go:noinline 1622 func cx64quot_ssa(a, b complex64) complex64 { 1623 return a / b 1624 } 1625 1626 //go:noinline 1627 func cx64neg_ssa(a complex64) complex64 { 1628 return -a 1629 } 1630 1631 //go:noinline 1632 func cx64real_ssa(a complex64) float32 { 1633 return real(a) 1634 } 1635 1636 //go:noinline 1637 func cx64imag_ssa(a complex64) float32 { 1638 return imag(a) 1639 } 1640 1641 //go:noinline 1642 func cx128eq_ssa(a, b complex128) bool { 1643 return a == b 1644 } 1645 1646 //go:noinline 1647 func cx128ne_ssa(a, b complex128) bool { 1648 return a != b 1649 } 1650 1651 //go:noinline 1652 func cx64eq_ssa(a, b complex64) bool { 1653 return a == b 1654 } 1655 1656 //go:noinline 1657 func cx64ne_ssa(a, b complex64) bool { 1658 return a != b 1659 } 1660 1661 func expectTrue(s string, b bool) int { 1662 if !b { 1663 println("expected true for", s, ", got false") 1664 return 1 1665 } 1666 return 0 1667 } 1668 func expectFalse(s string, b bool) int { 1669 if b { 1670 println("expected false for", s, ", got true") 1671 return 1 1672 } 1673 return 0 1674 } 1675 1676 func complexTest128() int { 1677 fails := 0 1678 var a complex128 = 1 + 2i 1679 var b complex128 = 3 + 6i 1680 sum := cx128sum_ssa(b, a) 1681 diff := cx128diff_ssa(b, a) 1682 prod := cx128prod_ssa(b, a) 1683 quot := cx128quot_ssa(b, a) 1684 neg := cx128neg_ssa(a) 1685 r := cx128real_ssa(a) 1686 i := cx128imag_ssa(a) 1687 cnst := cx128cnst_ssa(a) 1688 c1 := cx128eq_ssa(a, a) 1689 c2 := cx128eq_ssa(a, b) 1690 c3 := cx128ne_ssa(a, a) 1691 c4 := cx128ne_ssa(a, b) 1692 1693 fails += expectCx128("sum", sum, 4+8i) 1694 fails += expectCx128("diff", diff, 2+4i) 1695 fails += expectCx128("prod", prod, -9+12i) 1696 fails += expectCx128("quot", quot, 3+0i) 1697 fails += expectCx128("neg", neg, -1-2i) 1698 fails += expect64("real", r, 1) 1699 fails += expect64("imag", i, 2) 1700 fails += expectCx128("cnst", cnst, -4+7i) 1701 fails += expectTrue(fmt.Sprintf("%v==%v", a, a), c1) 1702 fails += expectFalse(fmt.Sprintf("%v==%v", a, b), c2) 1703 fails += expectFalse(fmt.Sprintf("%v!=%v", a, a), c3) 1704 fails += expectTrue(fmt.Sprintf("%v!=%v", a, b), c4) 1705 1706 return fails 1707 } 1708 1709 func complexTest64() int { 1710 fails := 0 1711 var a complex64 = 1 + 2i 1712 var b complex64 = 3 + 6i 1713 sum := cx64sum_ssa(b, a) 1714 diff := cx64diff_ssa(b, a) 1715 prod := cx64prod_ssa(b, a) 1716 quot := cx64quot_ssa(b, a) 1717 neg := cx64neg_ssa(a) 1718 r := cx64real_ssa(a) 1719 i := cx64imag_ssa(a) 1720 c1 := cx64eq_ssa(a, a) 1721 c2 := cx64eq_ssa(a, b) 1722 c3 := cx64ne_ssa(a, a) 1723 c4 := cx64ne_ssa(a, b) 1724 1725 fails += expectCx64("sum", sum, 4+8i) 1726 fails += expectCx64("diff", diff, 2+4i) 1727 fails += expectCx64("prod", prod, -9+12i) 1728 fails += expectCx64("quot", quot, 3+0i) 1729 fails += expectCx64("neg", neg, -1-2i) 1730 fails += expect32("real", r, 1) 1731 fails += expect32("imag", i, 2) 1732 fails += expectTrue(fmt.Sprintf("%v==%v", a, a), c1) 1733 fails += expectFalse(fmt.Sprintf("%v==%v", a, b), c2) 1734 fails += expectFalse(fmt.Sprintf("%v!=%v", a, a), c3) 1735 fails += expectTrue(fmt.Sprintf("%v!=%v", a, b), c4) 1736 1737 return fails 1738 } 1739 1740 func main() { 1741 1742 a := 3.0 1743 b := 4.0 1744 1745 c := float32(3.0) 1746 d := float32(4.0) 1747 1748 tiny := float32(1.5E-45) // smallest f32 denorm = 2**(-149) 1749 dtiny := float64(tiny) // well within range of f64 1750 1751 fails := 0 1752 fails += fail64("+", add64_ssa, a, b, 7.0) 1753 fails += fail64("*", mul64_ssa, a, b, 12.0) 1754 fails += fail64("-", sub64_ssa, a, b, -1.0) 1755 fails += fail64("/", div64_ssa, a, b, 0.75) 1756 fails += fail64("neg", neg64_ssa, a, b, -7) 1757 1758 fails += fail32("+", add32_ssa, c, d, 7.0) 1759 fails += fail32("*", mul32_ssa, c, d, 12.0) 1760 fails += fail32("-", sub32_ssa, c, d, -1.0) 1761 fails += fail32("/", div32_ssa, c, d, 0.75) 1762 fails += fail32("neg", neg32_ssa, c, d, -7) 1763 1764 // denorm-squared should underflow to zero. 1765 fails += fail32("*", mul32_ssa, tiny, tiny, 0) 1766 1767 // but should not underflow in float and in fact is exactly representable. 1768 fails += fail64("*", mul64_ssa, dtiny, dtiny, 1.9636373861190906e-90) 1769 1770 // Intended to create register pressure which forces 1771 // asymmetric op into different code paths. 1772 aa, ab, ac, ad, ba, bb, bc, bd, ca, cb, cc, cd, da, db, dc, dd := manysub_ssa(1000.0, 100.0, 10.0, 1.0) 1773 1774 fails += expect64("aa", aa, 11.0) 1775 fails += expect64("ab", ab, 900.0) 1776 fails += expect64("ac", ac, 990.0) 1777 fails += expect64("ad", ad, 999.0) 1778 1779 fails += expect64("ba", ba, -900.0) 1780 fails += expect64("bb", bb, 22.0) 1781 fails += expect64("bc", bc, 90.0) 1782 fails += expect64("bd", bd, 99.0) 1783 1784 fails += expect64("ca", ca, -990.0) 1785 fails += expect64("cb", cb, -90.0) 1786 fails += expect64("cc", cc, 33.0) 1787 fails += expect64("cd", cd, 9.0) 1788 1789 fails += expect64("da", da, -999.0) 1790 fails += expect64("db", db, -99.0) 1791 fails += expect64("dc", dc, -9.0) 1792 fails += expect64("dd", dd, 44.0) 1793 1794 fails += integer2floatConversions() 1795 1796 fails += multiplyAdd() 1797 1798 var zero64 float64 = 0.0 1799 var one64 float64 = 1.0 1800 var inf64 float64 = 1.0 / zero64 1801 var nan64 float64 = sub64_ssa(inf64, inf64) 1802 1803 fails += cmpOpTest("!=", ne64_ssa, nebr64_ssa, ne32_ssa, nebr32_ssa, zero64, one64, inf64, nan64, 0x01111) 1804 fails += cmpOpTest("==", eq64_ssa, eqbr64_ssa, eq32_ssa, eqbr32_ssa, zero64, one64, inf64, nan64, 0x10000) 1805 fails += cmpOpTest("<=", le64_ssa, lebr64_ssa, le32_ssa, lebr32_ssa, zero64, one64, inf64, nan64, 0x11100) 1806 fails += cmpOpTest("<", lt64_ssa, ltbr64_ssa, lt32_ssa, ltbr32_ssa, zero64, one64, inf64, nan64, 0x01100) 1807 fails += cmpOpTest(">", gt64_ssa, gtbr64_ssa, gt32_ssa, gtbr32_ssa, zero64, one64, inf64, nan64, 0x00000) 1808 fails += cmpOpTest(">=", ge64_ssa, gebr64_ssa, ge32_ssa, gebr32_ssa, zero64, one64, inf64, nan64, 0x10000) 1809 1810 { 1811 lt, le, eq, ne, ge, gt := compares64_ssa(0.0, 1.0, inf64, nan64) 1812 fails += expectUint64("lt", lt, 0x0110001000000000) 1813 fails += expectUint64("le", le, 0x1110011000100000) 1814 fails += expectUint64("eq", eq, 0x1000010000100000) 1815 fails += expectUint64("ne", ne, 0x0111101111011111) 1816 fails += expectUint64("ge", ge, 0x1000110011100000) 1817 fails += expectUint64("gt", gt, 0x0000100011000000) 1818 // fmt.Printf("lt=0x%016x, le=0x%016x, eq=0x%016x, ne=0x%016x, ge=0x%016x, gt=0x%016x\n", 1819 // lt, le, eq, ne, ge, gt) 1820 } 1821 { 1822 lt, le, eq, ne, ge, gt := compares32_ssa(0.0, 1.0, float32(inf64), float32(nan64)) 1823 fails += expectUint64("lt", lt, 0x0110001000000000) 1824 fails += expectUint64("le", le, 0x1110011000100000) 1825 fails += expectUint64("eq", eq, 0x1000010000100000) 1826 fails += expectUint64("ne", ne, 0x0111101111011111) 1827 fails += expectUint64("ge", ge, 0x1000110011100000) 1828 fails += expectUint64("gt", gt, 0x0000100011000000) 1829 } 1830 1831 fails += floatingToIntegerConversionsTest() 1832 fails += complexTest128() 1833 fails += complexTest64() 1834 1835 if fails > 0 { 1836 fmt.Printf("Saw %v failures\n", fails) 1837 panic("Failed.") 1838 } 1839 }