| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
- // Exceptions. See /LICENSE for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- //
- // EXTRA-ARGS: --no-dump-sem-ir
- //
- // AUTOUPDATE
- // TIP: To test this file alone, run:
- // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/builtins/int/smod.carbon
- // TIP: To dump output, run:
- // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/smod.carbon
- // --- int_div.carbon
- fn Mod(a: i32, b: i32) -> i32 = "int.smod";
- var arr: [i32; Mod(5, 3)];
- let arr_p: [i32; 2]* = &arr;
- fn RuntimeCallIsValid(a: i32, b: i32) -> i32 {
- return Mod(a, b);
- }
- // --- fail_overflow.carbon
- package FailOverflow;
- fn Mod(a: i32, b: i32) -> i32 = "int.smod";
- fn Sub(a: i32, b: i32) -> i32 = "int.ssub";
- fn Negate(a: i32) -> i32 = "int.snegate";
- // -0x7FFF_FFFF % -1 is OK.
- let a: i32 = Mod(Negate(0x7FFF_FFFF), Negate(1));
- // -0x8000_0000 % 1 is OK.
- let b: i32 = Mod(Sub(Negate(0x7FFF_FFFF), 1), 1);
- // -0x8000_0000 / -1 overflows, so -0x8000_0000 % -1 is disallowed, even though
- // its result is representable.
- // CHECK:STDERR: fail_overflow.carbon:[[@LINE+4]]:14: error: integer overflow in calculation `-2147483648 % -1` [CompileTimeIntegerOverflow]
- // CHECK:STDERR: let c: i32 = Mod(Sub(Negate(0x7FFF_FFFF), 1), Negate(1));
- // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // CHECK:STDERR:
- let c: i32 = Mod(Sub(Negate(0x7FFF_FFFF), 1), Negate(1));
- // --- fail_div_by_zero.carbon
- package FailDivByZero;
- fn Mod(a: i32, b: i32) -> i32 = "int.smod";
- // Remainder of division by zero is not defined.
- // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:14: error: division by zero [CompileTimeDivisionByZero]
- // CHECK:STDERR: let a: i32 = Mod(1, 0);
- // CHECK:STDERR: ^~~~~~~~~
- // CHECK:STDERR:
- let a: i32 = Mod(1, 0);
- // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:14: error: division by zero [CompileTimeDivisionByZero]
- // CHECK:STDERR: let b: i32 = Mod(0, 0);
- // CHECK:STDERR: ^~~~~~~~~
- // CHECK:STDERR:
- let b: i32 = Mod(0, 0);
|