1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package com.oracle.graal.compiler.ptx.test;
  24 
  25 import java.lang.reflect.Method;
  26 
  27 import org.junit.*;
  28 
  29 
  30 /* PTX ISA 3.1 - 8.7.3 Floating-Point Instructions */
  31 public class FloatPTXTest extends PTXTestBase {
  32         
  33     @Test
  34     public void testAdd() {
  35         compile("testAdd2F");
  36         compile("testAdd2D");
  37         compile("testAddFConst");
  38         compile("testAddConstF");
  39         compile("testAddDConst");
  40         compile("testAddConstD");
  41     }
  42     
  43     public static float testAdd2F(float a, float b) {
  44         return a + b;
  45     }
  46 
  47     public static double testAdd2D(double a, double b) {
  48         return a + b;
  49     }
  50 
  51     public static float testAddFConst(float a) {
  52         return a + 32.0F;
  53     }
  54 
  55     public static float testAddConstF(float a) {
  56         return 32.0F + a;
  57     }
  58 
  59     public static double testAddDConst(double a) {
  60         return a + 32.0;
  61     }
  62 
  63     public static double testAddConstD(double a) {
  64         return 32.0 + a;
  65     }
  66 
  67     @Test
  68     public void testSub() {
  69         compile("testSub2F");
  70         compile("testSub2D");
  71         compile("testSubFConst");
  72         compile("testSubConstF");
  73         compile("testSubDConst");
  74         compile("testSubConstD");
  75     }
  76     
  77     public static float testSub2F(float a, float b) {
  78         return a - b;
  79     }
  80 
  81     public static double testSub2D(double a, double b) {
  82         return a - b;
  83     }
  84 
  85     public static float testSubFConst(float a) {
  86         return a - 32.0F;
  87     }
  88 
  89     public static float testSubConstF(float a) {
  90         return 32.0F - a;
  91     }
  92 
  93     public static double testSubDConst(double a) {
  94         return a - 32.0;
  95     }
  96 
  97     public static double testSubConstD(double a) {
  98         return 32.0 - a;
  99     }
 100 
 101     @Test
 102     public void testMul() {
 103         compile("testMul2F");
 104         compile("testMul2D");
 105         compile("testMulFConst");
 106         compile("testMulConstF");
 107         compile("testMulDConst");
 108         compile("testMulConstD");
 109     }
 110     
 111     public static float testMul2F(float a, float b) {
 112         return a * b;
 113     }
 114 
 115     public static double testMul2D(double a, double b) {
 116         return a * b;
 117     }
 118     
 119     public static float testMulFConst(float a) {
 120         return a * 32.0F;
 121     }
 122 
 123     public static float testMulConstF(float a) {
 124         return 32.0F * a;
 125     }
 126 
 127     public static double testMulDConst(double a) {
 128         return a * 32.0;
 129     }
 130 
 131     public static double testMulConstD(double a) {
 132         return 32.0 * a;
 133     }
 134 
 135     @Test
 136     public void testDiv() {
 137         compile("testDiv2F");
 138         compile("testDiv2D");
 139         compile("testDivFConst");
 140         compile("testDivConstF");
 141         compile("testDivDConst");
 142         compile("testDivConstD");
 143     }
 144     
 145     public static float testDiv2F(float a, float b) {
 146         return a / b;
 147     }
 148 
 149     public static double testDiv2D(double a, double b) {
 150         return a / b;
 151     }
 152 
 153     public static float testDivFConst(float a) {
 154         return a / 32.0F;
 155     }
 156 
 157     public static float testDivConstF(float a) {
 158         return 32.0F / a;
 159     }
 160 
 161     public static double testDivDConst(double a) {
 162         return a / 32.0;
 163     }
 164 
 165     public static double testDivConstD(double a) {
 166         return 32.0 / a;
 167     }
 168 
 169     @Test
 170     public void testNeg() {
 171         compile("testNeg2F");
 172         compile("testNeg2D");
 173     }
 174     
 175     public static float testNeg2F(float a) {
 176         return -a;
 177     }
 178 
 179     public static double testNeg2D(double a) {
 180         return -a;
 181     }
 182 
 183     @Test
 184     public void testRem() {
 185         // need linkage to PTX remainder()
 186         // compile("testRem2F");
 187         // compile("testRem2D");
 188     }
 189     
 190     public static float testRem2F(float a, float b) {
 191         return a % b;
 192     }
 193 
 194     public static double testRem2D(double a, double b) {
 195         return a % b;
 196     }
 197    
 198     @Test
 199     public void testFloatConversion() {
 200         compile("testF2I");
 201         compile("testF2L");
 202         compile("testF2D");
 203         compile("testD2I");
 204         compile("testD2L");
 205         compile("testD2F");
 206     }
 207     
 208     public static int testF2I(float a) {
 209         return (int)a;
 210     }
 211 
 212     public static long testF2L(float a) {
 213         return (long)a;
 214     }
 215 
 216     public static double testF2D(float a) {
 217         return (double)a;
 218     }
 219 
 220     public static int testD2I(double a) {
 221         return (int)a;
 222     }
 223 
 224     public static long testD2L(double a) {
 225         return (long)a;
 226     }
 227 
 228     public static float testD2F(double a) {
 229         return (float)a;
 230     }
 231 
 232     public static void main(String[] args) {
 233         FloatPTXTest test = new FloatPTXTest();
 234         for (Method m : FloatPTXTest.class.getMethods()) {
 235             String name = m.getName();
 236             if (m.getAnnotation(Test.class) == null && 
 237                     name.startsWith("test") && 
 238                     name.startsWith("testRem") == false) {
 239                 System.out.println(name + ": \n" + new String(test.compile(name).getTargetCode()));             
 240             }
 241         }
 242     }
 243 }