blob: c7883f37a3a200029e4aa63551b1389e786ef2bb [file] [log] [blame]
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Functional tests for SIMD vectorization. Note that this class provides a mere
* functional test, not a precise numerical verifier.
*/
public class Main {
static float[] a;
//
// Arithmetic operations.
//
/// CHECK-START: void Main.add(float) loop_optimization (before)
/// CHECK-DAG: ArrayGet loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none
//
/// CHECK-START-ARM64: void Main.add(float) loop_optimization (after)
/// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: VecAdd loop:<<Loop>> outer_loop:none
/// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none
//
/// CHECK-START-MIPS64: void Main.add(float) loop_optimization (after)
/// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: VecAdd loop:<<Loop>> outer_loop:none
/// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none
static void add(float x) {
for (int i = 0; i < 128; i++)
a[i] += x;
}
/// CHECK-START: void Main.sub(float) loop_optimization (before)
/// CHECK-DAG: ArrayGet loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none
//
/// CHECK-START-ARM64: void Main.sub(float) loop_optimization (after)
/// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: VecSub loop:<<Loop>> outer_loop:none
/// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none
//
/// CHECK-START-MIPS64: void Main.sub(float) loop_optimization (after)
/// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: VecSub loop:<<Loop>> outer_loop:none
/// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none
static void sub(float x) {
for (int i = 0; i < 128; i++)
a[i] -= x;
}
/// CHECK-START: void Main.mul(float) loop_optimization (before)
/// CHECK-DAG: ArrayGet loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none
//
/// CHECK-START-ARM64: void Main.mul(float) loop_optimization (after)
/// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: VecMul loop:<<Loop>> outer_loop:none
/// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none
//
/// CHECK-START-MIPS64: void Main.mul(float) loop_optimization (after)
/// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: VecMul loop:<<Loop>> outer_loop:none
/// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none
static void mul(float x) {
for (int i = 0; i < 128; i++)
a[i] *= x;
}
/// CHECK-START: void Main.div(float) loop_optimization (before)
/// CHECK-DAG: ArrayGet loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none
//
/// CHECK-START-ARM64: void Main.div(float) loop_optimization (after)
/// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: VecDiv loop:<<Loop>> outer_loop:none
/// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none
//
/// CHECK-START-MIPS64: void Main.div(float) loop_optimization (after)
/// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: VecDiv loop:<<Loop>> outer_loop:none
/// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none
static void div(float x) {
for (int i = 0; i < 128; i++)
a[i] /= x;
}
/// CHECK-START: void Main.neg() loop_optimization (before)
/// CHECK-DAG: ArrayGet loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none
//
/// CHECK-START-ARM64: void Main.neg() loop_optimization (after)
/// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: VecNeg loop:<<Loop>> outer_loop:none
/// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none
//
/// CHECK-START-MIPS64: void Main.neg() loop_optimization (after)
/// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: VecNeg loop:<<Loop>> outer_loop:none
/// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none
static void neg() {
for (int i = 0; i < 128; i++)
a[i] = -a[i];
}
/// CHECK-START: void Main.abs() loop_optimization (before)
/// CHECK-DAG: ArrayGet loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none
//
/// CHECK-START-MIPS64: void Main.abs() loop_optimization (after)
/// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: VecAbs loop:<<Loop>> outer_loop:none
/// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none
//
/// CHECK-START-ARM64: void Main.abs() loop_optimization (after)
/// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: VecAbs loop:<<Loop>> outer_loop:none
/// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none
static void abs() {
for (int i = 0; i < 128; i++)
a[i] = Math.abs(a[i]);
}
/// CHECK-START: void Main.conv(int[]) loop_optimization (before)
/// CHECK-DAG: ArrayGet loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: ArraySet loop:<<Loop>> outer_loop:none
//
/// CHECK-START-ARM64: void Main.conv(int[]) loop_optimization (after)
/// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: VecCnv loop:<<Loop>> outer_loop:none
/// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none
//
/// CHECK-START-MIPS64: void Main.conv(int[]) loop_optimization (after)
/// CHECK-DAG: VecLoad loop:<<Loop:B\d+>> outer_loop:none
/// CHECK-DAG: VecCnv loop:<<Loop>> outer_loop:none
/// CHECK-DAG: VecStore loop:<<Loop>> outer_loop:none
static void conv(int[] b) {
for (int i = 0; i < 128; i++)
a[i] = b[i];
}
//
// Loop bounds.
//
static void bounds() {
for (int i = 1; i < 127; i++)
a[i] += 11;
}
//
// Test Driver.
//
public static void main(String[] args) {
// Set up.
a = new float[128];
for (int i = 0; i < 128; i++) {
a[i] = i;
}
// Arithmetic operations.
add(2.0f);
for (int i = 0; i < 128; i++) {
expectEquals(i + 2, a[i], "add");
}
sub(2.0f);
for (int i = 0; i < 128; i++) {
expectEquals(i, a[i], "sub");
}
mul(2.0f);
for (int i = 0; i < 128; i++) {
expectEquals(i + i, a[i], "mul");
}
div(2.0f);
for (int i = 0; i < 128; i++) {
expectEquals(i, a[i], "div");
}
neg();
for (int i = 0; i < 128; i++) {
expectEquals(-i, a[i], "neg");
}
// Loop bounds.
bounds();
expectEquals(0, a[0], "bounds0");
for (int i = 1; i < 127; i++) {
expectEquals(11 - i, a[i], "bounds");
}
expectEquals(-127, a[127], "bounds127");
// Abs.
abs();
expectEquals(0, a[0], "abs0");
for (int i = 1; i <= 11; i++) {
expectEquals(11 - i, a[i], "abs_lo");
}
for (int i = 12; i < 127; i++) {
expectEquals(i - 11, a[i], "abs_hi");
}
expectEquals(127, a[127], "abs127");
// Conversion.
int[] b = new int[128];
for (int i = 0; i < 128; i++) {
b[i] = 1000 * i;
}
conv(b);
for (int i = 1; i < 127; i++) {
expectEquals(1000.0f * i, a[i], "conv");
}
// Done.
System.out.println("passed");
}
private static void expectEquals(float expected, float result, String action) {
if (expected != result) {
throw new Error("Expected: " + expected + ", found: " + result + " for " + action);
}
}
}