smod.carbon 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/smod.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/smod.carbon
  12. // --- int_div.carbon
  13. fn Mod(a: i32, b: i32) -> i32 = "int.smod";
  14. var arr: [i32; Mod(5, 3)];
  15. let arr_p: [i32; 2]* = &arr;
  16. fn RuntimeCallIsValid(a: i32, b: i32) -> i32 {
  17. return Mod(a, b);
  18. }
  19. // --- fail_overflow.carbon
  20. package FailOverflow;
  21. fn Mod(a: i32, b: i32) -> i32 = "int.smod";
  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 = Mod(Negate(0x7FFF_FFFF), Negate(1));
  26. // -0x8000_0000 % 1 is OK.
  27. let b: i32 = Mod(Sub(Negate(0x7FFF_FFFF), 1), 1);
  28. // -0x8000_0000 / -1 overflows, so -0x8000_0000 % -1 is disallowed, even though
  29. // its result is representable.
  30. // CHECK:STDERR: fail_overflow.carbon:[[@LINE+4]]:14: error: integer overflow in calculation `-2147483648 % -1` [CompileTimeIntegerOverflow]
  31. // CHECK:STDERR: let c: i32 = Mod(Sub(Negate(0x7FFF_FFFF), 1), Negate(1));
  32. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  33. // CHECK:STDERR:
  34. let c: i32 = Mod(Sub(Negate(0x7FFF_FFFF), 1), Negate(1));
  35. // --- fail_div_by_zero.carbon
  36. package FailDivByZero;
  37. fn Mod(a: i32, b: i32) -> i32 = "int.smod";
  38. // Remainder of division by zero is not defined.
  39. // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:14: error: division by zero [CompileTimeDivisionByZero]
  40. // CHECK:STDERR: let a: i32 = Mod(1, 0);
  41. // CHECK:STDERR: ^~~~~~~~~
  42. // CHECK:STDERR:
  43. let a: i32 = Mod(1, 0);
  44. // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:14: error: division by zero [CompileTimeDivisionByZero]
  45. // CHECK:STDERR: let b: i32 = Mod(0, 0);
  46. // CHECK:STDERR: ^~~~~~~~~
  47. // CHECK:STDERR:
  48. let b: i32 = Mod(0, 0);