umod.carbon 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/umod.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/umod.carbon
  12. // --- int_div.carbon
  13. fn Mod(a: i32, b: i32) -> i32 = "int.umod";
  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. // --- overflow.carbon
  20. package Overflow;
  21. fn Mod(a: i32, b: i32) -> i32 = "int.umod";
  22. fn Sub(a: i32, b: i32) -> i32 = "int.usub";
  23. fn Negate(a: i32) -> i32 = "int.unegate";
  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 is OK.
  29. let c: i32 = Mod(Sub(Negate(0x7FFF_FFFF), 1), Negate(1));
  30. // --- fail_div_by_zero.carbon
  31. package FailDivByZero;
  32. fn Mod(a: i32, b: i32) -> i32 = "int.umod";
  33. // Remainder of division by zero is not defined.
  34. // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:14: error: division by zero [CompileTimeDivisionByZero]
  35. // CHECK:STDERR: let a: i32 = Mod(1, 0);
  36. // CHECK:STDERR: ^~~~~~~~~
  37. // CHECK:STDERR:
  38. let a: i32 = Mod(1, 0);
  39. // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:14: error: division by zero [CompileTimeDivisionByZero]
  40. // CHECK:STDERR: let b: i32 = Mod(0, 0);
  41. // CHECK:STDERR: ^~~~~~~~~
  42. // CHECK:STDERR:
  43. let b: i32 = Mod(0, 0);