sdiv.carbon 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
  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. library "[[@TEST_NAME]]";
  14. fn Div(a: i32, b: i32) -> i32 = "int.sdiv";
  15. var arr: array(i32, Div(3, 2));
  16. let arr_p: array(i32, 1)* = &arr;
  17. fn RuntimeCallIsValid(a: i32, b: i32) -> i32 {
  18. //@dump-sem-ir-begin
  19. return Div(a, b);
  20. //@dump-sem-ir-end
  21. }
  22. // --- fail_overflow.carbon
  23. library "[[@TEST_NAME]]";
  24. fn Div(a: i32, b: i32) -> i32 = "int.sdiv";
  25. fn Sub(a: i32, b: i32) -> i32 = "int.ssub";
  26. fn Negate(a: i32) -> i32 = "int.snegate";
  27. // -0x7FFF_FFFF / -1 is OK.
  28. let a: i32 = Div(-0x7FFF_FFFF, -1);
  29. // -0x8000_0000 / 1 is OK.
  30. let b: i32 = Div(-0x8000_0000, 1);
  31. // -0x8000_0000 / -1 overflows.
  32. // CHECK:STDERR: fail_overflow.carbon:[[@LINE+4]]:14: error: integer overflow in calculation `-2147483648 / -1` [CompileTimeIntegerOverflow]
  33. // CHECK:STDERR: let c: i32 = Div(-0x8000_0000, -1);
  34. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~
  35. // CHECK:STDERR:
  36. let c: i32 = Div(-0x8000_0000, -1);
  37. // --- literal_no_overflow.carbon
  38. library "[[@TEST_NAME]]";
  39. fn Div(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.sdiv";
  40. class Expect(N:! Core.IntLiteral()) {}
  41. fn Test(N:! Core.IntLiteral()) -> Expect(N) { return {}; }
  42. fn F() {
  43. Test(Div(-0x8000_0000, -1)) as Expect(0x8000_0000);
  44. Test(Div(-0x8000_0000_0000_0000, -1)) as Expect(0x8000_0000_0000_0000);
  45. }
  46. // --- fail_div_by_zero.carbon
  47. library "[[@TEST_NAME]]";
  48. fn Div(a: i32, b: i32) -> i32 = "int.sdiv";
  49. fn DivLit(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.sdiv";
  50. // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:14: error: division by zero [CompileTimeDivisionByZero]
  51. // CHECK:STDERR: let a: i32 = Div(1, 0);
  52. // CHECK:STDERR: ^~~~~~~~~
  53. // CHECK:STDERR:
  54. let a: i32 = Div(1, 0);
  55. // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:14: error: division by zero [CompileTimeDivisionByZero]
  56. // CHECK:STDERR: let b: i32 = Div(0, 0);
  57. // CHECK:STDERR: ^~~~~~~~~
  58. // CHECK:STDERR:
  59. let b: i32 = Div(0, 0);
  60. // IntLiteral allows "overflow" by widening its representation, but not overflow to infinity.
  61. // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:28: error: division by zero [CompileTimeDivisionByZero]
  62. // CHECK:STDERR: let c: Core.IntLiteral() = DivLit(1, 0);
  63. // CHECK:STDERR: ^~~~~~~~~~~~
  64. // CHECK:STDERR:
  65. let c: Core.IntLiteral() = DivLit(1, 0);
  66. // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:28: error: division by zero [CompileTimeDivisionByZero]
  67. // CHECK:STDERR: let d: Core.IntLiteral() = DivLit(0, 0);
  68. // CHECK:STDERR: ^~~~~~~~~~~~
  69. // CHECK:STDERR:
  70. let d: Core.IntLiteral() = DivLit(0, 0);
  71. // CHECK:STDOUT: --- int_div.carbon
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: constants {
  74. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  75. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  76. // CHECK:STDOUT: %Div.type: type = fn_type @Div [concrete]
  77. // CHECK:STDOUT: %Div: %Div.type = struct_value () [concrete]
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: imports {
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: fn @RuntimeCallIsValid(%a.param: %i32, %b.param: %i32) -> %i32 {
  84. // CHECK:STDOUT: !entry:
  85. // CHECK:STDOUT: %Div.ref: %Div.type = name_ref Div, file.%Div.decl [concrete = constants.%Div]
  86. // CHECK:STDOUT: %a.ref: %i32 = name_ref a, %a
  87. // CHECK:STDOUT: %b.ref: %i32 = name_ref b, %b
  88. // CHECK:STDOUT: %Div.call: init %i32 = call %Div.ref(%a.ref, %b.ref)
  89. // CHECK:STDOUT: return %Div.call to %return
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: fn @__global_init() {
  93. // CHECK:STDOUT: !entry:
  94. // CHECK:STDOUT: <elided>
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT: