Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * Functional tests for SIMD vectorization. |
| 19 | */ |
| 20 | public class Main { |
| 21 | |
| 22 | static boolean[] a; |
| 23 | |
| 24 | // |
| 25 | // Arithmetic operations. |
| 26 | // |
| 27 | |
| 28 | /// CHECK-START: void Main.and(boolean) loop_optimization (before) |
Aart Bik | 8c6c357 | 2017-10-25 11:48:48 -0700 | [diff] [blame] | 29 | /// CHECK-DAG: ArrayGet loop:<<Loop:B\d+>> outer_loop:none |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 30 | /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none |
| 31 | // |
Aart Bik | 9cb7fe4 | 2017-11-30 11:46:45 -0800 | [diff] [blame] | 32 | /// CHECK-START-{ARM,ARM64,MIPS64}: void Main.and(boolean) loop_optimization (after) |
Aart Bik | 8c6c357 | 2017-10-25 11:48:48 -0700 | [diff] [blame] | 33 | /// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none |
Goran Jakovljevic | 19680d3 | 2017-05-11 10:38:36 +0200 | [diff] [blame] | 34 | /// CHECK-DAG: VecAnd loop:<<Loop>> outer_loop:none |
| 35 | /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 36 | static void and(boolean x) { |
| 37 | for (int i = 0; i < 128; i++) |
| 38 | a[i] &= x; // NOTE: bitwise and, not the common && |
| 39 | } |
| 40 | |
| 41 | /// CHECK-START: void Main.or(boolean) loop_optimization (before) |
Aart Bik | 8c6c357 | 2017-10-25 11:48:48 -0700 | [diff] [blame] | 42 | /// CHECK-DAG: ArrayGet loop:<<Loop:B\d+>> outer_loop:none |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 43 | /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none |
| 44 | // |
Aart Bik | 9cb7fe4 | 2017-11-30 11:46:45 -0800 | [diff] [blame] | 45 | /// CHECK-START-{ARM,ARM64,MIPS64}: void Main.or(boolean) loop_optimization (after) |
Aart Bik | 8c6c357 | 2017-10-25 11:48:48 -0700 | [diff] [blame] | 46 | /// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none |
Goran Jakovljevic | 19680d3 | 2017-05-11 10:38:36 +0200 | [diff] [blame] | 47 | /// CHECK-DAG: VecOr loop:<<Loop>> outer_loop:none |
| 48 | /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 49 | static void or(boolean x) { |
| 50 | for (int i = 0; i < 128; i++) |
| 51 | a[i] |= x; // NOTE: bitwise or, not the common || |
| 52 | } |
| 53 | |
| 54 | /// CHECK-START: void Main.xor(boolean) loop_optimization (before) |
Aart Bik | 8c6c357 | 2017-10-25 11:48:48 -0700 | [diff] [blame] | 55 | /// CHECK-DAG: ArrayGet loop:<<Loop:B\d+>> outer_loop:none |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 56 | /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none |
| 57 | // |
Aart Bik | 9cb7fe4 | 2017-11-30 11:46:45 -0800 | [diff] [blame] | 58 | /// CHECK-START-{ARM,ARM64,MIPS64}: void Main.xor(boolean) loop_optimization (after) |
Aart Bik | 8c6c357 | 2017-10-25 11:48:48 -0700 | [diff] [blame] | 59 | /// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none |
Goran Jakovljevic | 19680d3 | 2017-05-11 10:38:36 +0200 | [diff] [blame] | 60 | /// CHECK-DAG: VecXor loop:<<Loop>> outer_loop:none |
| 61 | /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 62 | static void xor(boolean x) { |
| 63 | for (int i = 0; i < 128; i++) |
| 64 | a[i] ^= x; // NOTE: bitwise xor |
| 65 | } |
| 66 | |
| 67 | /// CHECK-START: void Main.not() loop_optimization (before) |
Aart Bik | 8c6c357 | 2017-10-25 11:48:48 -0700 | [diff] [blame] | 68 | /// CHECK-DAG: ArrayGet loop:<<Loop:B\d+>> outer_loop:none |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 69 | /// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none |
| 70 | // |
Aart Bik | 9cb7fe4 | 2017-11-30 11:46:45 -0800 | [diff] [blame] | 71 | /// CHECK-START-{ARM,ARM64,MIPS64}: void Main.not() loop_optimization (after) |
Aart Bik | 8c6c357 | 2017-10-25 11:48:48 -0700 | [diff] [blame] | 72 | /// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none |
Goran Jakovljevic | 19680d3 | 2017-05-11 10:38:36 +0200 | [diff] [blame] | 73 | /// CHECK-DAG: VecNot loop:<<Loop>> outer_loop:none |
| 74 | /// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 75 | static void not() { |
| 76 | for (int i = 0; i < 128; i++) |
| 77 | a[i] = !a[i]; |
| 78 | } |
| 79 | |
| 80 | // |
| 81 | // Test Driver. |
| 82 | // |
| 83 | |
| 84 | public static void main(String[] args) { |
| 85 | // Set up. |
| 86 | a = new boolean[128]; |
| 87 | for (int i = 0; i < 128; i++) { |
| 88 | a[i] = (i & 1) == 0; |
| 89 | } |
| 90 | // Arithmetic operations. |
| 91 | and(true); |
| 92 | for (int i = 0; i < 128; i++) { |
| 93 | expectEquals((i & 1) == 0, a[i], "and-true"); |
| 94 | } |
| 95 | xor(true); |
| 96 | for (int i = 0; i < 128; i++) { |
| 97 | expectEquals((i & 1) != 0, a[i], "xor-true"); |
| 98 | } |
| 99 | xor(false); |
| 100 | for (int i = 0; i < 128; i++) { |
| 101 | expectEquals((i & 1) != 0, a[i], "xor-false"); |
| 102 | } |
| 103 | not(); |
| 104 | for (int i = 0; i < 128; i++) { |
| 105 | expectEquals((i & 1) == 0, a[i], "not"); |
| 106 | } |
| 107 | or(true); |
| 108 | for (int i = 0; i < 128; i++) { |
| 109 | expectEquals(true, a[i], "or-true"); |
| 110 | } |
| 111 | and(false); |
| 112 | for (int i = 0; i < 128; i++) { |
| 113 | expectEquals(false, a[i], "and-false"); |
| 114 | } |
| 115 | or(false); |
| 116 | for (int i = 0; i < 128; i++) { |
| 117 | expectEquals(false, a[i], "or-false"); |
| 118 | } |
| 119 | // Done. |
| 120 | System.out.println("passed"); |
| 121 | } |
| 122 | |
| 123 | private static void expectEquals(boolean expected, boolean result, String action) { |
| 124 | if (expected != result) { |
| 125 | throw new Error("Expected: " + expected + ", found: " + result + " for " + action); |
| 126 | } |
| 127 | } |
| 128 | } |