|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// VMOptions=--optimization-counter-threshold=50 --no-background-compilation |
| 6 | + |
| 7 | +import "package:expect/expect.dart"; |
| 8 | + |
| 9 | +@pragma("vm:never-inline") |
| 10 | +dynamic smi_add(dynamic x, dynamic y) => x + y; |
| 11 | + |
| 12 | +@pragma("vm:never-inline") |
| 13 | +dynamic smi_sub(dynamic x, dynamic y) => x - y; |
| 14 | + |
| 15 | +@pragma("vm:never-inline") |
| 16 | +dynamic smi_mul(dynamic x, dynamic y) => x * y; |
| 17 | + |
| 18 | +@pragma("vm:never-inline") |
| 19 | +dynamic smi_and(dynamic x, dynamic y) => x & y; |
| 20 | + |
| 21 | +@pragma("vm:never-inline") |
| 22 | +dynamic smi_or(dynamic x, dynamic y) => x | y; |
| 23 | + |
| 24 | +@pragma("vm:never-inline") |
| 25 | +dynamic smi_xor(dynamic x, dynamic y) => x ^ y; |
| 26 | + |
| 27 | +@pragma("vm:never-inline") |
| 28 | +dynamic smi_div(dynamic x, dynamic y) => x ~/ y; |
| 29 | + |
| 30 | +@pragma("vm:never-inline") |
| 31 | +dynamic smi_mod(dynamic x, dynamic y) => x % y; |
| 32 | + |
| 33 | +@pragma("vm:never-inline") |
| 34 | +dynamic smi_sll(dynamic x, dynamic y) => x << y; |
| 35 | + |
| 36 | +@pragma("vm:never-inline") |
| 37 | +dynamic smi_sra(dynamic x, dynamic y) => x >> y; |
| 38 | + |
| 39 | +@pragma("vm:never-inline") |
| 40 | +dynamic smi_srl(dynamic x, dynamic y) => x >>> y; |
| 41 | + |
| 42 | +testSmi() { |
| 43 | + Expect.equals(7, smi_add(3, 4)); |
| 44 | + Expect.equals(-1, smi_sub(3, 4)); |
| 45 | + Expect.equals(12, smi_mul(3, 4)); |
| 46 | + Expect.equals(0, smi_and(3, 4)); |
| 47 | + Expect.equals(7, smi_or(3, 4)); |
| 48 | + Expect.equals(7, smi_xor(3, 4)); |
| 49 | + Expect.equals(0, smi_div(3, 4)); |
| 50 | + Expect.equals(3, smi_mod(3, 4)); |
| 51 | + Expect.equals(48, smi_sll(3, 4)); |
| 52 | + Expect.equals(0, smi_sra(3, 4)); |
| 53 | + Expect.equals(0, smi_srl(3, 4)); |
| 54 | +} |
| 55 | + |
| 56 | +const maxSmi32 = 0x3FFFFFFF; |
| 57 | +const maxSmi64 = 0x3FFFFFFFFFFFFFFF; |
| 58 | +const minSmi32 = -0x80000000; |
| 59 | +const minSmi64 = -0x8000000000000000; |
| 60 | + |
| 61 | +testSmiDeopt() { |
| 62 | + Expect.equals(0x40000000, smi_add(maxSmi32, 1)); |
| 63 | + Expect.equals(0x4000000000000000, smi_add(maxSmi64, 1)); |
| 64 | + |
| 65 | + Expect.equals(0x40000000, smi_sub(maxSmi32, -1)); |
| 66 | + Expect.equals(0x4000000000000000, smi_sub(maxSmi64, -1)); |
| 67 | + |
| 68 | + Expect.equals(0x7FFFFFFE, smi_mul(maxSmi32, 2)); |
| 69 | + Expect.equals(0x7FFFFFFFFFFFFFFE, smi_mul(maxSmi64, 2)); |
| 70 | + |
| 71 | + Expect.equals(0x80000000, smi_div(minSmi32, -1)); |
| 72 | + Expect.equals(0x8000000000000000, smi_div(minSmi64, -1)); |
| 73 | + |
| 74 | + Expect.throws(() => smi_mod(minSmi32, 0)); |
| 75 | + Expect.throws(() => smi_mod(minSmi64, 0)); |
| 76 | + |
| 77 | + Expect.equals(0x7FFFFFFE, smi_sll(maxSmi32, 1)); |
| 78 | + Expect.equals(0x7FFFFFFFFFFFFFFE, smi_sll(maxSmi64, 1)); |
| 79 | + |
| 80 | + Expect.equals(0, smi_sra(maxSmi32, 65)); |
| 81 | + Expect.equals(0, smi_sra(maxSmi64, 65)); |
| 82 | + |
| 83 | + Expect.equals(0, smi_srl(minSmi32, 65)); |
| 84 | + Expect.equals(0, smi_srl(minSmi64, 65)); |
| 85 | +} |
| 86 | + |
| 87 | +main() { |
| 88 | + for (int i = 0; i < 200; i++) { |
| 89 | + testSmi(); |
| 90 | + } |
| 91 | + |
| 92 | + print("================================="); |
| 93 | + |
| 94 | + for (int i = 0; i < 200; i++) { |
| 95 | + testSmiDeopt(); |
| 96 | + } |
| 97 | +} |
0 commit comments