1 /*
   2  * Copyright (c) 2009, 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 
  24 package com.oracle.graal.compiler.hsail.test;
  25 
  26 import org.junit.*;
  27 
  28 import com.oracle.graal.compiler.hsail.test.infra.*;
  29 
  30 /**
  31  * Tests intrinsic for call to Math.sqrt(double). Generates a sqrt_f64 instruction.
  32  */
  33 public class DoubleSqrtTest extends GraalKernelTester {
  34 
  35     static final int size = 64;
  36     @Result double[] out = new double[size];
  37 
  38     /**
  39      * The static "kernel" method we will be testing. This method calls Math.sqrt() on an element of
  40      * an input array and writes the result to the corresponding index of an output array. By
  41      * convention the gid is the last parameter.
  42      * 
  43      * @param out the output array.
  44      * @param in the input array.
  45      * @param gid the parameter used to index into the input and output arrays.
  46      */
  47     public static void run(double[] in, double[] out, int gid) {
  48         out[gid] = Math.sqrt(in[gid]);
  49     }
  50 
  51     /**
  52      * Initializes the input and output arrays passed to the run routine.
  53      * 
  54      * @param in the input array.
  55      */
  56     void setupArrays(double[] in) {
  57         for (int i = 0; i < size; i++) {
  58             // Include positive and negative values as well as corner cases.
  59             if (i == 1) {
  60                 in[i] = Double.NaN;
  61             } else if (i == 2) {
  62                 in[i] = Double.NEGATIVE_INFINITY;
  63             } else if (i == 3) {
  64                 in[i] = Double.POSITIVE_INFINITY;
  65             } else if (i == 4) {
  66                 in[i] = -0.0;
  67             } else if (i > 5 && i < 10) {
  68                 in[i] = i + 0.5;
  69             } else {
  70                 in[i] = i < size / 2 ? i : -i;
  71             }
  72             out[i] = 0;
  73         }
  74     }
  75 
  76     /**
  77      * Dispatches the HSAIL kernel for this test case.
  78      */
  79     @Override
  80     public void runTest() {
  81         double[] inArray = new double[size];
  82         setupArrays(inArray);
  83         dispatchMethodKernel(size, inArray, out);
  84     }
  85 
  86     /**
  87      * Tests the HSAIL code generated for this unit test by comparing the result of executing this
  88      * code with the result of executing a sequential Java version of this unit test.
  89      */
  90     @Test
  91     public void test() {
  92         testGeneratedHsail();
  93     }
  94 }