diff options
| author | 2014-11-25 20:56:51 +0000 | |
|---|---|---|
| committer | 2014-11-26 16:49:23 +0000 | |
| commit | fd861249f31ab360c12dd1ffb131d50f02b0bfc6 (patch) | |
| tree | 1765db2b26337f8e96616ebfb769c95d7b421ad2 /test/432-optimizing-cmp/src/Main.java | |
| parent | fef1680241e85532919ecfaf42855d31ddb69361 (diff) | |
[optimizing compiler] Add CMP{L,G}_{FLOAT,DOUBLE}
- adds float comparison for arm, x86, x86_64 backends.
- adds ucomis{s,d} assembly to x86 and x86_64.
Change-Id: I232d2b6e9ecf373beb5cc63698dd97a658ff9c83
Diffstat (limited to 'test/432-optimizing-cmp/src/Main.java')
| -rw-r--r-- | test/432-optimizing-cmp/src/Main.java | 227 |
1 files changed, 227 insertions, 0 deletions
diff --git a/test/432-optimizing-cmp/src/Main.java b/test/432-optimizing-cmp/src/Main.java new file mode 100644 index 0000000000..f45b94db57 --- /dev/null +++ b/test/432-optimizing-cmp/src/Main.java @@ -0,0 +1,227 @@ +/* + * Copyright (C) 2014 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. + */ + +import java.lang.reflect.Method; + +public class Main { + + public static void main(String[] args) throws Exception { + cmpLong(); + cmpFloat(); + cmpDouble(); + } + + private static void cmpLong() throws Exception { + expectLt(3.1F, 5.1F); + expectGt(5.1F, 3.1F); + expectLt(Long.MIN_VALUE, Long.MAX_VALUE); + expectGt(Long.MAX_VALUE, Long.MIN_VALUE); + + expectEquals(0, smaliCmpLong(0L, 0L)); + expectEquals(0, smaliCmpLong(1L, 1L)); + expectEquals(-1, smaliCmpLong(1L, 2L)); + expectEquals(1, smaliCmpLong(2L, 1L)); + expectEquals(-1, smaliCmpLong(Long.MIN_VALUE, Long.MAX_VALUE)); + expectEquals(1, smaliCmpLong(Long.MAX_VALUE, Long.MIN_VALUE)); + expectEquals(0, smaliCmpLong(Long.MIN_VALUE, Long.MIN_VALUE)); + expectEquals(0, smaliCmpLong(Long.MAX_VALUE, Long.MAX_VALUE)); + } + + private static void cmpFloat() throws Exception { + expectLt(3.1F, 5.1F); + expectGt(5.1F, 3.1F); + expectLt(Float.MIN_VALUE, Float.MAX_VALUE); + expectGt(Float.MAX_VALUE, Float.MIN_VALUE); + expectFalse(3.1F, Float.NaN); + expectFalse(Float.NaN, 3.1F); + + expectEquals(0, smaliCmpGtFloat(0F, 0F)); + expectEquals(0, smaliCmpGtFloat(1F, 1F)); + expectEquals(-1, smaliCmpGtFloat(1.1F, 2.1F)); + expectEquals(1, smaliCmpGtFloat(2.1F, 1.1F)); + expectEquals(-1, smaliCmpGtFloat(Float.MIN_VALUE, Float.MAX_VALUE)); + expectEquals(1, smaliCmpGtFloat(Float.MAX_VALUE, Float.MIN_VALUE)); + expectEquals(0, smaliCmpGtFloat(Float.MIN_VALUE, Float.MIN_VALUE)); + expectEquals(0, smaliCmpGtFloat(Float.MAX_VALUE, Float.MAX_VALUE)); + expectEquals(1, smaliCmpGtFloat(5F, Float.NaN)); + expectEquals(1, smaliCmpGtFloat(Float.NaN, 5F)); + + expectEquals(0, smaliCmpLtFloat(0F, 0F)); + expectEquals(0, smaliCmpLtFloat(1F, 1F)); + expectEquals(-1, smaliCmpLtFloat(1.1F, 2.1F)); + expectEquals(1, smaliCmpLtFloat(2.1F, 1.1F)); + expectEquals(-1, smaliCmpLtFloat(Float.MIN_VALUE, Float.MAX_VALUE)); + expectEquals(1, smaliCmpLtFloat(Float.MAX_VALUE, Float.MIN_VALUE)); + expectEquals(0, smaliCmpLtFloat(Float.MIN_VALUE, Float.MIN_VALUE)); + expectEquals(0, smaliCmpLtFloat(Float.MAX_VALUE, Float.MAX_VALUE)); + expectEquals(-1, smaliCmpLtFloat(5F, Float.NaN)); + expectEquals(-1, smaliCmpLtFloat(Float.NaN, 5F)); + } + + private static void cmpDouble() throws Exception { + expectLt(3.1D, 5.1D); + expectGt(5.1D, 3.1D); + expectLt(Double.MIN_VALUE, Double.MAX_VALUE); + expectGt(Double.MAX_VALUE, Double.MIN_VALUE); + expectFalse(3.1D, Double.NaN); + expectFalse(Double.NaN, 3.1D); + + expectEquals(0, smaliCmpGtDouble(0D, 0D)); + expectEquals(0, smaliCmpGtDouble(1D, 1D)); + expectEquals(-1, smaliCmpGtDouble(1.1D, 2.1D)); + expectEquals(1, smaliCmpGtDouble(2.1D, 1.1D)); + expectEquals(-1, smaliCmpGtDouble(Double.MIN_VALUE, Double.MAX_VALUE)); + expectEquals(1, smaliCmpGtDouble(Double.MAX_VALUE, Double.MIN_VALUE)); + expectEquals(0, smaliCmpGtDouble(Double.MIN_VALUE, Double.MIN_VALUE)); + expectEquals(0, smaliCmpGtDouble(Double.MAX_VALUE, Double.MAX_VALUE)); + expectEquals(1, smaliCmpGtDouble(5D, Double.NaN)); + expectEquals(1, smaliCmpGtDouble(Double.NaN, 5D)); + + expectEquals(0, smaliCmpLtDouble(0D, 0D)); + expectEquals(0, smaliCmpLtDouble(1D, 1D)); + expectEquals(-1, smaliCmpLtDouble(1.1D, 2.1D)); + expectEquals(1, smaliCmpLtDouble(2.1D, 1.1D)); + expectEquals(-1, smaliCmpLtDouble(Double.MIN_VALUE, Double.MAX_VALUE)); + expectEquals(1, smaliCmpLtDouble(Double.MAX_VALUE, Double.MIN_VALUE)); + expectEquals(0, smaliCmpLtDouble(Double.MIN_VALUE, Double.MIN_VALUE)); + expectEquals(0, smaliCmpLtDouble(Double.MAX_VALUE, Double.MAX_VALUE)); + expectEquals(-1, smaliCmpLtDouble(5D, Double.NaN)); + expectEquals(-1, smaliCmpLtDouble(Float.NaN, 5D)); + } + + static boolean $opt$lt(long a, long b) { + return a < b; + } + + static boolean $opt$lt(float a, float b) { + return a < b; + } + + static boolean $opt$lt(double a, double b) { + return a < b; + } + + static boolean $opt$gt(long a, long b) { + return a > b; + } + + static boolean $opt$gt(float a, float b) { + return a > b; + } + + static boolean $opt$gt(double a, double b) { + return a > b; + } + + // Wrappers around methods located in file cmp.smali. + + private static int smaliCmpLong(long a, long b) throws Exception { + Class<?> c = Class.forName("TestCmp"); + Method m = c.getMethod("$opt$CmpLong", long.class, long.class); + int result = (Integer)m.invoke(null, a, b); + return result; + } + + private static int smaliCmpGtFloat(float a, float b) throws Exception { + Class<?> c = Class.forName("TestCmp"); + Method m = c.getMethod("$opt$CmpGtFloat", float.class, float.class); + int result = (Integer)m.invoke(null, a, b); + return result; + } + + private static int smaliCmpLtFloat(float a, float b) throws Exception { + Class<?> c = Class.forName("TestCmp"); + Method m = c.getMethod("$opt$CmpLtFloat", float.class, float.class); + int result = (Integer)m.invoke(null, a, b); + return result; + } + + private static int smaliCmpGtDouble(double a, double b) throws Exception { + Class<?> c = Class.forName("TestCmp"); + Method m = c.getMethod("$opt$CmpGtDouble", double.class, double.class); + int result = (Integer)m.invoke(null, a, b); + return result; + } + + private static int smaliCmpLtDouble(double a, double b) throws Exception { + Class<?> c = Class.forName("TestCmp"); + Method m = c.getMethod("$opt$CmpLtDouble", double.class, double.class); + int result = (Integer)m.invoke(null, a, b); + return result; + } + + public static void expectEquals(int expected, int result) { + if (expected != result) { + throw new Error("Expected: " + expected + ", found: " + result); + } + } + + public static void expectLt(long a, long b) { + if (!$opt$lt(a, b)) { + throw new Error("Expected: " + a + " < " + b); + } + } + + public static void expectGt(long a, long b) { + if (!$opt$gt(a, b)) { + throw new Error("Expected: " + a + " > " + b); + } + } + + public static void expectLt(float a, float b) { + if (!$opt$lt(a, b)) { + throw new Error("Expected: " + a + " < " + b); + } + } + + public static void expectGt(float a, float b) { + if (!$opt$gt(a, b)) { + throw new Error("Expected: " + a + " > " + b); + } + } + + public static void expectFalse(float a, float b) { + if ($opt$lt(a, b)) { + throw new Error("Not expecting: " + a + " < " + b); + } + if ($opt$gt(a, b)) { + throw new Error("Not expecting: " + a + " > " + b); + } + } + + public static void expectLt(double a, double b) { + if (!$opt$lt(a, b)) { + throw new Error("Expected: " + a + " < " + b); + } + } + + public static void expectGt(double a, double b) { + if (!$opt$gt(a, b)) { + throw new Error("Expected: " + a + " > " + b); + } + } + + public static void expectFalse(double a, double b) { + if ($opt$lt(a, b)) { + throw new Error("Not expecting: " + a + " < " + b); + } + if ($opt$gt(a, b)) { + throw new Error("Not expecting: " + a + " > " + b); + } + } + +} + |