Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2014 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 // Note that $opt$ is a marker for the optimizing compiler to test
     18 // it does compile the method.
     19 public class Main {
     20 
     21   public static void expectEquals(int expected, int result) {
     22     if (expected != result) {
     23       throw new Error("Expected: " + expected + ", found: " + result);
     24     }
     25   }
     26 
     27   public static void expectEquals(long expected, long result) {
     28     if (expected != result) {
     29       throw new Error("Expected: " + expected + ", found: " + result);
     30     }
     31   }
     32 
     33   public static void expectEquals(float expected, float result) {
     34     if (expected != result) {
     35       throw new Error("Expected: " + expected + ", found: " + result);
     36     }
     37   }
     38 
     39   public static void expectEquals(double expected, double result) {
     40     if (expected != result) {
     41       throw new Error("Expected: " + expected + ", found: " + result);
     42     }
     43   }
     44 
     45   public static void expectApproxEquals(float a, float b, float maxDelta) {
     46     boolean aproxEquals = (a > b)
     47       ? ((a - b) < maxDelta)
     48       : ((b - a) < maxDelta);
     49     if (!aproxEquals) {
     50       throw new Error("Expected: " + a + ", found: " + b + ", with delta: " + maxDelta);
     51     }
     52   }
     53 
     54   public static void expectApproxEquals(double a, double b, double maxDelta) {
     55     boolean aproxEquals = (a > b)
     56       ? ((a - b) < maxDelta)
     57       : ((b - a) < maxDelta);
     58     if (!aproxEquals) {
     59       throw new Error("Expected: " + a + ", found: " + b + ", with delta: " + maxDelta);
     60     }
     61   }
     62 
     63   public static void expectNaN(float a) {
     64     if (a == a) {
     65       throw new Error("Expected NaN: " + a);
     66     }
     67   }
     68 
     69   public static void expectNaN(double a) {
     70     if (a == a) {
     71       throw new Error("Expected NaN: " + a);
     72     }
     73   }
     74 
     75   public static void main(String[] args) {
     76     mul();
     77   }
     78 
     79   public static void mul() {
     80     mulInt();
     81     mulLong();
     82     mulFloat();
     83     mulDouble();
     84   }
     85 
     86   private static void mulInt() {
     87     expectEquals(15, $opt$Mul(5, 3));
     88     expectEquals(0, $opt$Mul(0, 0));
     89     expectEquals(0, $opt$Mul(0, 3));
     90     expectEquals(0, $opt$Mul(3, 0));
     91     expectEquals(-3, $opt$Mul(1, -3));
     92     expectEquals(36, $opt$Mul(-12, -3));
     93     expectEquals(33, $opt$Mul(1, 3) * 11);
     94     expectEquals(671088645, $opt$Mul(134217729, 5)); // (2^27 + 1) * 5
     95   }
     96 
     97   private static void mulLong() {
     98     expectEquals(15L, $opt$Mul(5L, 3L));
     99     expectEquals(0L, $opt$Mul(0L, 0L));
    100     expectEquals(0L, $opt$Mul(0L, 3L));
    101     expectEquals(0L, $opt$Mul(3L, 0L));
    102     expectEquals(-3L, $opt$Mul(1L, -3L));
    103     expectEquals(36L, $opt$Mul(-12L, -3L));
    104     expectEquals(33L, $opt$Mul(1L, 3L) * 11L);
    105     expectEquals(240518168583L, $opt$Mul(34359738369L, 7L)); // (2^35 + 1) * 7
    106   }
    107 
    108   private static void mulFloat() {
    109     expectApproxEquals(15F, $opt$Mul(5F, 3F), 0.0001F);
    110     expectApproxEquals(0F, $opt$Mul(0F, 0F), 0.0001F);
    111     expectApproxEquals(0F, $opt$Mul(0F, 3F), 0.0001F);
    112     expectApproxEquals(0F, $opt$Mul(3F, 0F), 0.0001F);
    113     expectApproxEquals(-3F, $opt$Mul(1F, -3F), 0.0001F);
    114     expectApproxEquals(36F, $opt$Mul(-12F, -3F), 0.0001F);
    115     expectApproxEquals(33F, $opt$Mul(1F, 3F) * 11F, 0.0001F);
    116     expectApproxEquals(0.02F, 0.1F * 0.2F, 0.0001F);
    117     expectApproxEquals(-0.1F, -0.5F * 0.2F, 0.0001F);
    118 
    119     expectNaN($opt$Mul(0F, Float.POSITIVE_INFINITY));
    120     expectNaN($opt$Mul(0F, Float.NEGATIVE_INFINITY));
    121     expectNaN($opt$Mul(Float.NaN, 11F));
    122     expectNaN($opt$Mul(Float.NaN, -11F));
    123     expectNaN($opt$Mul(Float.NaN, Float.NEGATIVE_INFINITY));
    124     expectNaN($opt$Mul(Float.NaN, Float.POSITIVE_INFINITY));
    125 
    126     expectEquals(Float.POSITIVE_INFINITY, $opt$Mul(2F, 3.40282346638528860e+38F));
    127     expectEquals(Float.POSITIVE_INFINITY, $opt$Mul(2F, Float.POSITIVE_INFINITY));
    128     expectEquals(Float.NEGATIVE_INFINITY, $opt$Mul(-2F, Float.POSITIVE_INFINITY));
    129     expectEquals(Float.NEGATIVE_INFINITY, $opt$Mul(-2F, 3.40282346638528860e+38F));
    130     expectEquals(Float.NEGATIVE_INFINITY, $opt$Mul(2F, Float.NEGATIVE_INFINITY));
    131     expectEquals(Float.POSITIVE_INFINITY, $opt$Mul(-2F, Float.NEGATIVE_INFINITY));
    132     expectEquals(Float.NEGATIVE_INFINITY, $opt$Mul(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY));
    133     expectEquals(Float.POSITIVE_INFINITY, $opt$Mul(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY));
    134     expectEquals(Float.POSITIVE_INFINITY, $opt$Mul(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY));
    135   }
    136 
    137   private static void mulDouble() {
    138     expectApproxEquals(15D, $opt$Mul(5D, 3D), 0.0001D);
    139     expectApproxEquals(0D, $opt$Mul(0D, 0D), 0.0001D);
    140     expectApproxEquals(0D, $opt$Mul(0D, 3D), 0.0001D);
    141     expectApproxEquals(0D, $opt$Mul(3D, 0D), 0.0001D);
    142     expectApproxEquals(-3D, $opt$Mul(1D, -3D), 0.0001D);
    143     expectApproxEquals(36D, $opt$Mul(-12D, -3D), 0.0001D);
    144     expectApproxEquals(33D, $opt$Mul(1D, 3D) * 11D, 0.0001D);
    145     expectApproxEquals(0.02D, 0.1D * 0.2D, 0.0001D);
    146     expectApproxEquals(-0.1D, -0.5D * 0.2D, 0.0001D);
    147 
    148     expectNaN($opt$Mul(0D, Double.POSITIVE_INFINITY));
    149     expectNaN($opt$Mul(0D, Double.NEGATIVE_INFINITY));
    150     expectNaN($opt$Mul(Double.NaN, 11D));
    151     expectNaN($opt$Mul(Double.NaN, -11D));
    152     expectNaN($opt$Mul(Double.NaN, Double.NEGATIVE_INFINITY));
    153     expectNaN($opt$Mul(Double.NaN, Double.POSITIVE_INFINITY));
    154 
    155     expectEquals(Double.POSITIVE_INFINITY, $opt$Mul(2D, 1.79769313486231570e+308));
    156     expectEquals(Double.POSITIVE_INFINITY, $opt$Mul(2D, Double.POSITIVE_INFINITY));
    157     expectEquals(Double.NEGATIVE_INFINITY, $opt$Mul(-2D, Double.POSITIVE_INFINITY));
    158     expectEquals(Double.NEGATIVE_INFINITY, $opt$Mul(-2D, 1.79769313486231570e+308));
    159     expectEquals(Double.NEGATIVE_INFINITY, $opt$Mul(2D, Double.NEGATIVE_INFINITY));
    160     expectEquals(Double.POSITIVE_INFINITY, $opt$Mul(-2D, Double.NEGATIVE_INFINITY));
    161     expectEquals(Double.NEGATIVE_INFINITY, $opt$Mul(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY));
    162     expectEquals(Double.POSITIVE_INFINITY, $opt$Mul(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY));
    163     expectEquals(Double.POSITIVE_INFINITY, $opt$Mul(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY));
    164   }
    165 
    166   static int $opt$Mul(int a, int b) {
    167     return a * b;
    168   }
    169 
    170   static long $opt$Mul(long a, long b) {
    171     return a * b;
    172   }
    173 
    174   static float $opt$Mul(float a, float b) {
    175     return a * b;
    176   }
    177 
    178   static double $opt$Mul(double a, double b) {
    179     return a * b;
    180   }
    181 }
    182