smul.carbon 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // EXTRA-ARGS: --no-dump-sem-ir
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/builtins/int/smul.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/smul.carbon
  12. // --- i32.carbon
  13. library "[[@TEST_NAME]]";
  14. fn Mul(a: i32, b: i32) -> i32 = "int.smul";
  15. class Expect(N:! i32) {}
  16. fn Test(N:! i32) -> Expect(N) { return {}; }
  17. fn F() {
  18. Test(Mul(0, 0)) as Expect(0);
  19. Test(Mul(0, 3)) as Expect(0);
  20. Test(Mul(1, 2)) as Expect(2);
  21. Test(Mul(3, 2)) as Expect(6);
  22. Test(Mul(0x7FFF_FFFF, 1)) as Expect(0x7FFF_FFFF);
  23. Test(Mul(0x7FFF_FFFF, -1)) as Expect(-0x7FFF_FFFF);
  24. }
  25. fn RuntimeCallIsValid(a: i32, b: i32) -> i32 {
  26. return Mul(a, b);
  27. }
  28. // --- literal.carbon
  29. library "[[@TEST_NAME]]";
  30. fn Mul(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.smul";
  31. class Expect(N:! Core.IntLiteral()) {}
  32. fn Test(N:! Core.IntLiteral()) -> Expect(N) { return {}; }
  33. fn F() {
  34. Test(Mul(0, 0)) as Expect(0);
  35. Test(Mul(0, 3)) as Expect(0);
  36. Test(Mul(1, 2)) as Expect(2);
  37. Test(Mul(3, 2)) as Expect(6);
  38. Test(Mul(0x7FFF_FFFF, 1)) as Expect(0x7FFF_FFFF);
  39. Test(Mul(0x7FFF_FFFF, -1)) as Expect(-0x7FFF_FFFF);
  40. // Test some cases that might -- but shouldn't -- overflow.
  41. Test(Mul(0x8000_0000_0000_0000, 1)) as Expect(0x8000_0000_0000_0000);
  42. Test(Mul(0x8000_0000_0000_0000, -1)) as Expect(-0x8000_0000_0000_0000);
  43. Test(Mul(-0x8000_0000_0000_0000, 1)) as Expect(-0x8000_0000_0000_0000);
  44. Test(Mul(-0x8000_0000_0000_0000, -1)) as Expect(0x8000_0000_0000_0000);
  45. Test(Mul(-0x8000_0000_0000_0000, -1)) as Expect(0x8000_0000_0000_0000);
  46. Test(Mul(-0x8000_0000_0000_0000, -0x8000_0000_0000_0000))
  47. as Expect(0x4000_0000_0000_0000_0000_0000_0000_0000);
  48. }
  49. // --- fail_overflow.carbon
  50. package FailOverflow;
  51. fn Mul(a: i32, b: i32) -> i32 = "int.smul";
  52. let a: i32 = Mul(0x7FFF, 0x10000);
  53. // CHECK:STDERR: fail_overflow.carbon:[[@LINE+4]]:14: error: integer overflow in calculation `32768 * 65536` [CompileTimeIntegerOverflow]
  54. // CHECK:STDERR: let b: i32 = Mul(0x8000, 0x10000);
  55. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
  56. // CHECK:STDERR:
  57. let b: i32 = Mul(0x8000, 0x10000);