sdiv.carbon 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/sdiv.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/sdiv.carbon
  12. // --- int_div.carbon
  13. fn Div(a: i32, b: i32) -> i32 = "int.sdiv";
  14. var arr: [i32; Div(3, 2)];
  15. let arr_p: [i32; 1]* = &arr;
  16. fn RuntimeCallIsValid(a: i32, b: i32) -> i32 {
  17. return Div(a, b);
  18. }
  19. // --- fail_overflow.carbon
  20. package FailOverflow;
  21. fn Div(a: i32, b: i32) -> i32 = "int.sdiv";
  22. fn Sub(a: i32, b: i32) -> i32 = "int.ssub";
  23. fn Negate(a: i32) -> i32 = "int.snegate";
  24. // -0x7FFF_FFFF / -1 is OK.
  25. let a: i32 = Div(-0x7FFF_FFFF, -1);
  26. // -0x8000_0000 / 1 is OK.
  27. let b: i32 = Div(-0x8000_0000, 1);
  28. // -0x8000_0000 / -1 overflows.
  29. // CHECK:STDERR: fail_overflow.carbon:[[@LINE+4]]:14: error: integer overflow in calculation `-2147483648 / -1` [CompileTimeIntegerOverflow]
  30. // CHECK:STDERR: let c: i32 = Div(-0x8000_0000, -1);
  31. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~
  32. // CHECK:STDERR:
  33. let c: i32 = Div(-0x8000_0000, -1);
  34. // --- literal_no_overflow.carbon
  35. library "[[@TEST_NAME]]";
  36. fn Div(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.sdiv";
  37. class Expect(N:! Core.IntLiteral()) {}
  38. fn Test(N:! Core.IntLiteral()) -> Expect(N) { return {}; }
  39. fn F() {
  40. Test(Div(-0x8000_0000, -1)) as Expect(0x8000_0000);
  41. Test(Div(-0x8000_0000_0000_0000, -1)) as Expect(0x8000_0000_0000_0000);
  42. }
  43. // --- fail_div_by_zero.carbon
  44. package FailDivByZero;
  45. fn Div(a: i32, b: i32) -> i32 = "int.sdiv";
  46. fn DivLit(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.sdiv";
  47. // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:14: error: division by zero [CompileTimeDivisionByZero]
  48. // CHECK:STDERR: let a: i32 = Div(1, 0);
  49. // CHECK:STDERR: ^~~~~~~~~
  50. // CHECK:STDERR:
  51. let a: i32 = Div(1, 0);
  52. // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:14: error: division by zero [CompileTimeDivisionByZero]
  53. // CHECK:STDERR: let b: i32 = Div(0, 0);
  54. // CHECK:STDERR: ^~~~~~~~~
  55. // CHECK:STDERR:
  56. let b: i32 = Div(0, 0);
  57. // IntLiteral allows "overflow" by widening its representation, but not overflow to infinity.
  58. // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:28: error: division by zero [CompileTimeDivisionByZero]
  59. // CHECK:STDERR: let c: Core.IntLiteral() = DivLit(1, 0);
  60. // CHECK:STDERR: ^~~~~~~~~~~~
  61. // CHECK:STDERR:
  62. let c: Core.IntLiteral() = DivLit(1, 0);
  63. // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:28: error: division by zero [CompileTimeDivisionByZero]
  64. // CHECK:STDERR: let d: Core.IntLiteral() = DivLit(0, 0);
  65. // CHECK:STDERR: ^~~~~~~~~~~~
  66. // CHECK:STDERR:
  67. let d: Core.IntLiteral() = DivLit(0, 0);