1 /*
2  * Copyright (C) 2019 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 package other;
18 
19 /**
20  * Tests for dot product idiom vectorization: char and short case.
21  */
22 public class TestFloatDouble {
23 
24   public static final int ARRAY_SIZE = 1024;
25 
26 
27   /// CHECK-START-{X86_64}: float other.TestFloatDouble.testDotProdSimpleFloat(float[], float[]) loop_optimization (after)
28   /// CHECK-NOT:                 VecDotProd
testDotProdSimpleFloat(float[] a, float[] b)29   public static final float testDotProdSimpleFloat(float[] a, float[] b) {
30     float sum = 0;
31     for (int i = 0; i < b.length; i++) {
32       sum += a[i] * b[i];
33     }
34     return sum;
35   }
36 
37 
38   /// CHECK-START-{X86_64}: double other.TestFloatDouble.testDotProdSimpleDouble(double[], double[]) loop_optimization (after)
39   /// CHECK-NOT:                 VecDotProd
40 
testDotProdSimpleDouble(double[] a, double[] b)41   public static final double testDotProdSimpleDouble(double[] a, double[] b) {
42     double sum = 0;
43     for (int i = 0; i < b.length; i++) {
44       sum += a[i] * b[i];
45     }
46     return sum;
47   }
48 
expectEquals(float expected, float result)49   private static void expectEquals(float expected, float result) {
50     if (Float.compare(expected, result) != 0) {
51       throw new Error("Expected: " + expected + ", found: " + result);
52     }
53   }
54 
expectEquals(double expected, double result)55   private static void expectEquals(double expected, double result) {
56     if (Double.compare(expected, result) != 0) {
57       throw new Error("Expected: " + expected + ", found: " + result);
58     }
59   }
60 
run()61   public static void run() {
62     final float MAX_F = Float.MAX_VALUE;
63     final float MIN_F = Float.MIN_VALUE;
64     final double MAX_D = Double.MAX_VALUE;
65     final double MIN_D = Double.MIN_VALUE;
66 
67     double[] a = new double[1024];
68     for (int i = 0; i != 1024; ++i) a[i] = MAX_D;
69     double[] b = new double[1024];
70     for (int i = 0; i != 1024; ++i) b[i] = ((i & 1) == 0) ? 1.0 : -1.0;
71     expectEquals(0.0, testDotProdSimpleDouble(a,b));
72 
73     float[] f1_1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.33f, 0.125f, 3.0f, 0.25f};
74     float[] f2_1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6.125f, 2.25f, 1.213f, 0.5f};
75     expectEquals(24.4415f, testDotProdSimpleFloat(f1_1, f2_1));
76 
77     float [] f1_2 = { 0, 0, 0, 0, 0, 0, 0, 0,
78                       0, 0, 0, 0,  0.63671875f, 0.76953125f, 0.22265625f, 1.0f};
79     float [] f2_2 = { 0, 0, 0, 0, 0, 0, 0, 0,
80                       0, 0, 0, 0, MIN_F, MAX_F, MAX_F, MIN_F };
81     expectEquals(3.376239E38f, testDotProdSimpleFloat(f1_2, f2_2));
82 
83     float[] f1_3 = { 0xc0000000, 0xc015c28f, 0x411dd42c, 0, 0, 0, 0,
84                      0, 0, 0, 0, 0, 0, 0, MIN_F, MIN_F };
85     float[] f2_3 = { 0x3f4c779a, 0x408820c5, 0, 0, 0, 0, 0,
86                      0, 0, 0, 0, 0, 0x00000000, 0, MAX_F, MAX_F };
87     expectEquals(-2.30124471E18f, testDotProdSimpleFloat(f1_3, f2_3));
88   }
89 
main(String[] args)90   public static void main(String[] args) {
91     run();
92   }
93 }
94