--- old/graal/com.oracle.graal.compiler.hsail.test.infra/src/com/oracle/graal/compiler/hsail/test/infra/KernelTester.java 2013-12-12 12:32:44.861342611 -0600 +++ new/graal/com.oracle.graal.compiler.hsail.test.infra/src/com/oracle/graal/compiler/hsail/test/infra/KernelTester.java 2013-12-12 12:32:44.765342611 -0600 @@ -284,6 +284,10 @@ * unit test. */ public boolean isEqualsFP(double first, double second) { + // Special case for checking whether expected and actual values are both NaNs. + if (Double.isNaN(first) && Double.isNaN(second)) { + return true; + } return first == second; } --- old/graal/com.oracle.graal.compiler.hsail/src/com/oracle/graal/compiler/hsail/HSAILLIRGenerator.java 2013-12-12 12:32:45.481342611 -0600 +++ new/graal/com.oracle.graal.compiler.hsail/src/com/oracle/graal/compiler/hsail/HSAILLIRGenerator.java 2013-12-12 12:32:45.397342611 -0600 @@ -648,11 +648,61 @@ throw GraalInternalError.unimplemented(); } + /** + * Emit the LIR code for the Math.Abs intrinsic. + * + * @param input the source operand + * @return Value representing the result of the operation + */ @Override public Value emitMathAbs(Value input) { - throw GraalInternalError.unimplemented(); + Variable result = newVariable(input.getPlatformKind()); + append(new Op1Stack(ABS, result, input)); + return result; + } + + /** + * Emit the LIR code for the Math.Ceil intrinsic. + * + * @param input the source operand + * @return Value representing the result of the operation + */ + public Value emitMathCeil(Value input) { + Variable result = newVariable(input.getPlatformKind()); + append(new Op1Stack(CEIL, result, input)); + return result; } + /** + * Emit the LIR code for the Math.Floor intrinsic. + * + * @param input the source operand + * @return Value representing the result of the operation + */ + public Value emitMathFloor(Value input) { + Variable result = newVariable(input.getPlatformKind()); + append(new Op1Stack(FLOOR, result, input)); + return result; + } + + /** + * Emit the LIR code for the Math.Rint intrinsic. + * + * @param input the source operand + * @return Value representing the result of the operation + */ + public Value emitMathRint(Value input) { + Variable result = newVariable(input.getPlatformKind()); + append(new Op1Stack(RINT, result, input)); + return result; + } + + /** + * Emit the LIR code for the Math.sqrt intrinsic. + * + * @param input the source operand + * @return value representing the result of the operation + */ @Override public Value emitMathSqrt(Value input) { Variable result = newVariable(input.getPlatformKind()); @@ -784,4 +834,5 @@ public void visitInfopointNode(InfopointNode i) { throw GraalInternalError.unimplemented(); } + } --- old/graal/com.oracle.graal.hotspot.hsail/src/com/oracle/graal/hotspot/hsail/HSAILHotSpotBackend.java 2013-12-12 12:32:45.993342610 -0600 +++ new/graal/com.oracle.graal.hotspot.hsail/src/com/oracle/graal/hotspot/hsail/HSAILHotSpotBackend.java 2013-12-12 12:32:45.909342610 -0600 @@ -41,6 +41,8 @@ import com.oracle.graal.lir.asm.*; import com.oracle.graal.lir.hsail.*; import com.oracle.graal.nodes.*; +import com.oracle.graal.nodes.spi.Replacements; +import com.oracle.graal.replacements.hsail.*; /** * HSAIL specific backend. @@ -63,12 +65,27 @@ return true; } + /** + * Completes the initialization of the HSAIL backend. This includes initializing the providers + * and registering any method substitutions specified by the HSAIL backend. + */ @Override public void completeInitialization() { final HotSpotProviders providers = getProviders(); HotSpotVMConfig config = getRuntime().getConfig(); + // Initialize the lowering provider. final HotSpotLoweringProvider lowerer = (HotSpotLoweringProvider) providers.getLowerer(); lowerer.initialize(providers, config); + // Register the replacements used by the HSAIL backend. + Replacements replacements = providers.getReplacements(); + try { + // Register the substitutions for java.lang.Math routines. + // replacements.registerSubstitutions(Class.forName("com.oracle.graal.replacements.hsail.HSAILMathSubstitutions")); + replacements.registerSubstitutions(HSAILMathSubstitutions.class); + + } catch (Exception e) { + e.printStackTrace(); + } } /** --- old/graal/com.oracle.graal.lir.hsail/src/com/oracle/graal/lir/hsail/HSAILArithmetic.java 2013-12-12 12:32:46.913342611 -0600 +++ new/graal/com.oracle.graal.lir.hsail/src/com/oracle/graal/lir/hsail/HSAILArithmetic.java 2013-12-12 12:32:46.821342610 -0600 @@ -29,14 +29,17 @@ import com.oracle.graal.asm.hsail.*; import com.oracle.graal.graph.*; import com.oracle.graal.lir.*; -import com.oracle.graal.lir.asm.*; +import com.oracle.graal.lir.asm.CompilationResultBuilder; /** * Defines arithmetic instruction nodes. */ public enum HSAILArithmetic { + ABS, CALL, + CEIL, FDIV, + FLOOR, FREM, DADD, DDIV, @@ -97,6 +100,7 @@ LUSUB, LXOR, OADD, + RINT, SQRT, UNDEF; @@ -289,10 +293,33 @@ } } + /** + * Emit the HSAIL code for an arithmetic operation taking one input parameter. + * + * @param crb the CompilationResultBuilder + * @param masm the HSAIL assembler + * @param opcode the opcode of the arithmetic operation + * @param dst the destination + * @param src the source parameter + * @param info structure that stores the LIRFrameState. Used for exception handling. + */ + public static void emit(CompilationResultBuilder crb, HSAILAssembler masm, HSAILArithmetic opcode, Value dst, Value src, LIRFrameState info) { int exceptionOffset = -1; if (isRegister(src)) { switch (opcode) { + case ABS: + masm.emit("abs", dst, src); + break; + case CEIL: + masm.emit("ceil", dst, src); + break; + case FLOOR: + masm.emit("floor", dst, src); + break; + case RINT: + masm.emit("rint", dst, src); + break; case SQRT: masm.emit("sqrt", dst, src); break; @@ -304,14 +331,12 @@ break; case INOT: case LNOT: - // Emit the HSAIL instruction for a bitwise not. masm.emitForceBitwise("not", dst, src); break; case INEG: case LNEG: case FNEG: case DNEG: - // Emit the HSAIL instruction for a negate operation. masm.emit("neg", dst, src); break; default: --- old/mx/projects 2013-12-12 12:32:47.353342611 -0600 +++ new/mx/projects 2013-12-12 12:32:47.257342611 -0600 @@ -162,7 +162,7 @@ # graal.hotspot.hsail project@com.oracle.graal.hotspot.hsail@subDir=graal project@com.oracle.graal.hotspot.hsail@sourceDirs=src -project@com.oracle.graal.hotspot.hsail@dependencies=com.oracle.graal.compiler.hsail,com.oracle.graal.hotspot +project@com.oracle.graal.hotspot.hsail@dependencies=com.oracle.graal.compiler.hsail,com.oracle.graal.hotspot,com.oracle.graal.replacements.hsail project@com.oracle.graal.hotspot.hsail@checkstyle=com.oracle.graal.graph project@com.oracle.graal.hotspot.hsail@annotationProcessors=com.oracle.graal.service.processor project@com.oracle.graal.hotspot.hsail@javaCompliance=1.7 @@ -311,6 +311,14 @@ project@com.oracle.graal.replacements.amd64@javaCompliance=1.7 project@com.oracle.graal.replacements.amd64@workingSets=Graal,Replacements,AMD64 +# graal.replacements.hsail +project@com.oracle.graal.replacements.hsail@subDir=graal +project@com.oracle.graal.replacements.hsail@sourceDirs=src +project@com.oracle.graal.replacements.hsail@dependencies=com.oracle.graal.replacements,com.oracle.graal.compiler.hsail +project@com.oracle.graal.replacements.hsail@checkstyle=com.oracle.graal.graph +project@com.oracle.graal.replacements.hsail@javaCompliance=1.7 +project@com.oracle.graal.replacements.hsail@workingSets=Graal,Replacements,HSAIL + # graal.replacements.test project@com.oracle.graal.replacements.test@subDir=graal project@com.oracle.graal.replacements.test@sourceDirs=src --- /dev/null 2013-12-11 15:38:07.845401995 -0600 +++ new/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/DoubleAbsTest.java 2013-12-12 12:32:47.693342611 -0600 @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.graal.compiler.hsail.test; + +import org.junit.Test; +import com.oracle.graal.compiler.hsail.test.infra.*; + +/** + * Tests intrinsic for calls to Math.abs(double). Generates a abs_f64 instruction. + */ +public class DoubleAbsTest extends GraalKernelTester { + + static final int num = 40; + // Output array storing the results of calling Math.abs(). + @Result protected double[] outArray = new double[num]; + + /** + * The static "kernel" method we will be testing. This method calls Math.abs() on an element of + * an input array and writes the result to the corresponding index of an output array. By + * convention the gid is the last parameter. + * + * @param out the output array. + * @param ina the input array. + * @param gid the parameter used to index into the input and output arrays. + */ + public static void run(double[] out, double[] ina, int gid) { + out[gid] = Math.abs(ina[gid]); + } + + /** + * Tests the HSAIL code generated for this unit test by comparing the result of executing this + * code with the result of executing a sequential Java version of this unit test. + */ + @Test + public void test() { + super.testGeneratedHsail(); + } + + /** + * Initializes the input and output arrays passed to the run routine. + * + * @param in the input array. + */ + void setupArrays(double[] in) { + for (int i = 0; i < num; i++) { + // Include positive and negative values as well as corner cases. + if (i == 1) { + in[i] = Double.NaN; + } else if (i == 2) { + in[i] = Double.NEGATIVE_INFINITY; + } else if (i == 3) { + in[i] = Double.POSITIVE_INFINITY; + } else if (i == 4) { + in[i] = -0.0; + } else { + in[i] = i < num / 2 ? i : -i; + } + outArray[i] = 0; + } + } + + /** + * Dispatches the HSAIL kernel for this test case. + */ + @Override + public void runTest() { + double[] inArray = new double[num]; + setupArrays(inArray); + dispatchMethodKernel(num, outArray, inArray); + } +} --- /dev/null 2013-12-11 15:38:07.845401995 -0600 +++ new/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/DoubleCeilTest.java 2013-12-12 12:32:48.081342611 -0600 @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.graal.compiler.hsail.test; + +import org.junit.Test; +import com.oracle.graal.compiler.hsail.test.infra.*; + +/** + * Tests intrinsic for calls to Math.ceil(). Generates a ceil_f64 instruction. + */ +public class DoubleCeilTest extends GraalKernelTester { + + static final int num = 40; + // Output array storing the results. + @Result protected double[] outArray = new double[num]; + + /** + * The static "kernel" method we will be testing. This method calls Math.ceil() on an element of + * an input array and writes the result to the corresponding index of an output array. By + * convention the gid is the last parameter. + * + * @param out the output array. + * @param ina the input array. + * @param gid the parameter used to index into the input and output arrays. + */ + public static void run(double[] out, double[] ina, int gid) { + out[gid] = Math.ceil(ina[gid]); + } + + /** + * Tests the HSAIL code generated for this unit test by comparing the result of executing this + * code with the result of executing a sequential Java version of this unit test. + */ + @Test + public void test() { + super.testGeneratedHsail(); + } + + /** + * Initializes the input and output arrays passed to the run routine. + * + * @param in the input array. + */ + void setupArrays(double[] in) { + // Initialize arrays with a mix of positive and negativ values and any corner cases. + for (int i = 0; i < num; i++) { + if (i == 0) { + in[i] = 0.0; + } else if (i == 1) { + in[i] = -0.0; + } else if (i == 2) { + in[i] = Double.NaN; + } else if (i == 3) { + in[i] = Double.NEGATIVE_INFINITY; + } else if (i == 4) { + in[i] = Double.POSITIVE_INFINITY; + } else { + in[i] = i < num / 2 ? i + 0.5 : -i - 0.5; + } + outArray[i] = 0; + } + } + + /** + * Dispatches the HSAIL kernel for this test case. + */ + @Override + public void runTest() { + double[] inArray = new double[num]; + setupArrays(inArray); + dispatchMethodKernel(num, outArray, inArray); + } +} --- /dev/null 2013-12-11 15:38:07.845401995 -0600 +++ new/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/DoubleFloorTest.java 2013-12-12 12:32:48.453342608 -0600 @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.graal.compiler.hsail.test; + +import org.junit.Test; +import com.oracle.graal.compiler.hsail.test.infra.*; + +/** + * Tests intrinsic for calls to Math.floor(). Generates a floor_f64 instruction. + */ +public class DoubleFloorTest extends GraalKernelTester { + + static final int num = 40; + // Output array storing the results. + @Result protected double[] outArray = new double[num]; + + /** + * The static "kernel" method we will be testing. This method calls Math.floor() on an element + * of an input array and writes the result to the corresponding index of an output array. By + * convention the gid is the last parameter. + * + * @param out the output array. + * @param ina the input array. + * @param gid the parameter used to index into the input and output arrays. + */ + public static void run(double[] out, double[] ina, int gid) { + out[gid] = Math.floor(ina[gid]); + } + + /** + * Tests the HSAIL code generated for this unit test by comparing the result of executing this + * code with the result of executing a sequential Java version of this unit test. + */ + @Test + public void test() { + super.testGeneratedHsail(); + } + + /** + * Initializes the input and output arrays passed to the run routine. + * + * @param in the input array. + */ + void setupArrays(double[] in) { + // Initialize input array with a mix of positive and negative values and any corner cases. + for (int i = 0; i < num; i++) { + if (i == 0) { + in[i] = 0.0; + } else if (i == 1) { + in[i] = -0.0; + } else if (i == 2) { + in[i] = Double.NaN; + } else if (i == 3) { + in[i] = Double.NEGATIVE_INFINITY; + } else if (i == 4) { + in[i] = Double.POSITIVE_INFINITY; + } else { + in[i] = i < num / 2 ? i + 0.5 : -i - 0.5; + } + outArray[i] = 0; + } + } + + /** + * Dispatches the HSAIL kernel for this test case. + */ + @Override + public void runTest() { + double[] inArray = new double[num]; + setupArrays(inArray); + dispatchMethodKernel(num, outArray, inArray); + } +} --- /dev/null 2013-12-11 15:38:07.845401995 -0600 +++ new/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/DoubleRintTest.java 2013-12-12 12:32:48.837342608 -0600 @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.graal.compiler.hsail.test; + +import org.junit.*; + +import com.oracle.graal.compiler.hsail.test.infra.*; + +/** + * Tests intrinsic for Math.rint(). Generates an rint_f64 instruction. + */ +public class DoubleRintTest extends GraalKernelTester { + + static final int size = 64; + @Result double[] out = new double[size]; + + /** + * The static "kernel" method we will be testing. This method calls Math.rint() on an element of + * an input array and writes the result to the corresponding index of an output array. By + * convention the gid is the last parameter. + * + * @param out the output array. + * @param in the input array. + * @param gid the parameter used to index into the input and output arrays. + */ + public static void run(double[] out, double[] in, int gid) { + out[gid] = Math.rint(in[gid]); + } + + /** + * Initialize input arrays. + * + * @param in the input array + */ + void setupArrays(double[] in) { + // Initialize input array with a mix of positive and negative values and corner cases. + for (int i = 0; i < size; i++) { + if (i == 1) { + in[i] = Double.NaN; + } else if (i == 2) { + in[i] = Double.NEGATIVE_INFINITY; + } else if (i == 3) { + in[i] = Double.POSITIVE_INFINITY; + + } else if (i == 4) { + in[i] = 0.0; + } else if (i == 5) { + in[i] = -0.0; + } else { + in[i] = i < size / 2 ? i + 0.5 : -i - 0.6; + } + out[i] = 0; + } + } + + /** + * Dispatches the HSAIL kernel for this test case. + */ + @Override + public void runTest() { + double[] inArray = new double[size]; + setupArrays(inArray); + dispatchMethodKernel(size, out, inArray); + } + + /** + * Tests the HSAIL code generated for this unit test by comparing the result of executing this + * code with the result of executing a sequential Java version of this unit test. + */ + @Test + public void test() { + testGeneratedHsail(); + } +} --- /dev/null 2013-12-11 15:38:07.845401995 -0600 +++ new/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/DoubleSqrtTest.java 2013-12-12 12:32:49.229342608 -0600 @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.graal.compiler.hsail.test; + +import org.junit.*; + +import com.oracle.graal.compiler.hsail.test.infra.*; + +/** + * Tests intrinsic for call to Math.sqrt(double). Generates a sqrt_f64 instruction. + */ +public class DoubleSqrtTest extends GraalKernelTester { + + static final int size = 64; + @Result double[] out = new double[size]; + + /** + * The static "kernel" method we will be testing. This method calls Math.sqrt() on an element of + * an input array and writes the result to the corresponding index of an output array. By + * convention the gid is the last parameter. + * + * @param out the output array. + * @param in the input array. + * @param gid the parameter used to index into the input and output arrays. + */ + public static void run(double[] in, double[] out, int gid) { + out[gid] = Math.sqrt(in[gid]); + } + + /** + * Initializes the input and output arrays passed to the run routine. + * + * @param in the input array. + */ + void setupArrays(double[] in) { + for (int i = 0; i < size; i++) { + // Include positive and negative values as well as corner cases. + if (i == 1) { + in[i] = Double.NaN; + } else if (i == 2) { + in[i] = Double.NEGATIVE_INFINITY; + } else if (i == 3) { + in[i] = Double.POSITIVE_INFINITY; + } else if (i == 4) { + in[i] = -0.0; + } else if (i > 5 && i < 10) { + in[i] = i + 0.5; + } else { + in[i] = i < size / 2 ? i : -i; + } + out[i] = 0; + } + } + + /** + * Dispatches the HSAIL kernel for this test case. + */ + @Override + public void runTest() { + double[] inArray = new double[size]; + setupArrays(inArray); + dispatchMethodKernel(size, inArray, out); + } + + /** + * Tests the HSAIL code generated for this unit test by comparing the result of executing this + * code with the result of executing a sequential Java version of this unit test. + */ + @Test + public void test() { + testGeneratedHsail(); + } +} --- /dev/null 2013-12-11 15:38:07.845401995 -0600 +++ new/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/FloatAbsTest.java 2013-12-12 12:32:49.617342609 -0600 @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.graal.compiler.hsail.test; + +import org.junit.Test; +import com.oracle.graal.compiler.hsail.test.infra.*; + +/** + * Tests intrinsic for calls to Math.abs(float). Generates an abs_f32 instruction. + */ +public class FloatAbsTest extends GraalKernelTester { + + static final int num = 40; + // Output array storing the results of calling Math.abs(). + @Result protected float[] outArray = new float[num]; + + /** + * The static "kernel" method we will be testing. This method calls Math.abs() on an element of + * an input array and writes the result to the corresponding index of an output array. By + * convention the gid is the last parameter. + * + * @param out the output array. + * @param ina the input array. + * @param gid the parameter used to index into the input and output arrays. + */ + public static void run(float[] out, float[] ina, int gid) { + out[gid] = Math.abs(ina[gid]); + } + + /** + * Tests the HSAIL code generated for this unit test by comparing the result of executing this + * code with the result of executing a sequential Java version of this unit test. + */ + @Test + public void test() { + super.testGeneratedHsail(); + } + + /** + * Initializes the input and output arrays passed to the run routine. + * + * @param in the input array. + */ + void setupArrays(float[] in) { + for (int i = 0; i < num; i++) { + // Initialize array with positive and negative values as well as corner cases. + if (i == 1) { + in[i] = Float.NaN; + } else if (i == 2) { + in[i] = Float.NEGATIVE_INFINITY; + } else if (i == 3) { + in[i] = Float.POSITIVE_INFINITY; + } else if (i == 4) { + in[i] = -0; + } else { + in[i] = i < num / 2 ? i : -i; + } + outArray[i] = 0; + } + } + + /** + * Dispatches the HSAIL kernel for this test case. + */ + @Override + public void runTest() { + float[] inArray = new float[num]; + setupArrays(inArray); + dispatchMethodKernel(num, outArray, inArray); + } +} --- /dev/null 2013-12-11 15:38:07.845401995 -0600 +++ new/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/IntAbsTest.java 2013-12-12 12:32:50.009342609 -0600 @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.graal.compiler.hsail.test; + +import org.junit.Test; +import com.oracle.graal.compiler.hsail.test.infra.*; + +/** + * Tests intrinsic for call to Math.abs(int). Generates an abs_s32 instruction. + */ +public class IntAbsTest extends GraalKernelTester { + + static final int num = 20; + // Output array storing the results of negation operations. + @Result protected int[] outArray = new int[num]; + + /** + * The static "kernel" method we will be testing. This method calls Math.abs( ) on an element of + * an input array and writes the result to the corresponding index of an output array. By + * convention the gid is the last parameter. + * + * @param out the output array. + * @param ina the input array. + * @param gid the parameter used to index into the input and output arrays. + */ + public static void run(int[] out, int[] ina, int gid) { + out[gid] = Math.abs(ina[gid]); + } + + /** + * Tests the HSAIL code generated for this unit test by comparing the result of executing this + * code with the result of executing a sequential Java version of this unit test. + */ + @Test + public void test() { + super.testGeneratedHsail(); + } + + /** + * Initializes the input and output arrays passed to the run routine. + * + * @param in the input array. + */ + void setupArrays(int[] in) { + // initialize input array with a mix of positive and negative values and any corner cases. + for (int i = 0; i < num; i++) { + if (i == 1) { + in[i] = Integer.MIN_VALUE; + } else { + in[i] = i < num / 2 ? i : -i; + } + outArray[i] = 0; + } + } + + /** + * Dispatches the HSAIL kernel for this test case. + */ + @Override + public void runTest() { + int[] inArray = new int[num]; + setupArrays(inArray); + dispatchMethodKernel(num, outArray, inArray); + } +} --- /dev/null 2013-12-11 15:38:07.845401995 -0600 +++ new/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/LongAbsTest.java 2013-12-12 12:32:50.405342609 -0600 @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.graal.compiler.hsail.test; + +import org.junit.Test; +import com.oracle.graal.compiler.hsail.test.infra.*; + +/** + * Tests intrinsic for call to Math.abs(long). Generates an abs_s64 instruction. + */ +public class LongAbsTest extends GraalKernelTester { + + static final int num = 20; + // Output array storing the results of the operations. + @Result protected long[] outArray = new long[num]; + + /** + * The static "kernel" method we will be testing. This method calls Math.abs( ) on an element of + * an input array and writes the result to the corresponding index of an output array. By + * convention the gid is the last parameter. + * + * @param out the output array. + * @param ina the input array. + * @param gid the parameter used to index into the input and output arrays. + */ + public static void run(long[] out, long[] ina, int gid) { + out[gid] = Math.abs(ina[gid]); + } + + /** + * Tests the HSAIL code generated for this unit test by comparing the result of executing this + * code with the result of executing a sequential Java version of this unit test. + */ + @Test + public void test() { + super.testGeneratedHsail(); + } + + /** + * Initializes the input and output arrays passed to the run routine. + * + * @param in the input array. + */ + void setupArrays(long[] in) { + // Initialize input array with a mix of positive and negative values and any corner cases. + for (int i = 0; i < num; i++) { + if (i == 1) { + in[i] = Long.MIN_VALUE; + } else { + in[i] = i < num / 2 ? i : -i; + } + outArray[i] = 0; + } + } + + /** + * Dispatches the HSAIL kernel for this test case. + */ + @Override + public void runTest() { + long[] inArray = new long[num]; + setupArrays(inArray); + dispatchMethodKernel(num, outArray, inArray); + } +} --- /dev/null 2013-12-11 15:38:07.845401995 -0600 +++ new/graal/com.oracle.graal.replacements.hsail/src/com/oracle/graal/replacements/hsail/HSAILMathIntrinsicsNode.java 2013-12-12 12:32:50.857342606 -0600 @@ -0,0 +1,194 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.graal.replacements.hsail; + +import com.oracle.graal.api.meta.Constant; +import com.oracle.graal.api.meta.Value; +import com.oracle.graal.compiler.hsail.HSAILLIRGenerator; +import com.oracle.graal.graph.GraalInternalError; +import com.oracle.graal.graph.Node; +import com.oracle.graal.graph.spi.Canonicalizable; +import com.oracle.graal.graph.spi.CanonicalizerTool; +import com.oracle.graal.lir.hsail.HSAILArithmetic; +import com.oracle.graal.nodes.calc.FloatingNode; +import com.oracle.graal.nodes.ConstantNode; +import com.oracle.graal.nodes.ValueNode; +import com.oracle.graal.nodes.spi.ArithmeticLIRGenerator; +import com.oracle.graal.nodes.spi.ArithmeticLIRLowerable; +import com.oracle.graal.nodes.type.StampFactory; + +/** + * + * This node implements HSAIL intrinsics for specific java.lang.Math routines. + */ +public class HSAILMathIntrinsicsNode extends FloatingNode implements Canonicalizable, ArithmeticLIRLowerable { + + /** + * The parameter passed to the Math operation that this Node represents. + */ + @Input private ValueNode param; + + /** + * The math operation that this Node represents. + */ + private final HSAILArithmetic operation; + + /** + * Gets the paraneter passed to the Math operation that this node represents. + * + * @return the parameter + */ + public ValueNode getParameter() { + return param; + } + + /** + * Returns the math operation represented by this node. + * + * @return the operation + */ + public HSAILArithmetic operation() { + return operation; + } + + /** + * Creates a new HSAILMathIntrinsicNode. + * + * @param x the argument to the math operation + * @param op the math operation + */ + public HSAILMathIntrinsicsNode(ValueNode x, HSAILArithmetic op) { + super(StampFactory.forKind(x.kind())); + this.param = x; + this.operation = op; + } + + /** + * Generates the LIR instructions for the Math operation represented by this node. + */ + @Override + public void generate(ArithmeticLIRGenerator gen) { + Value input = gen.operand(getParameter()); + Value result; + switch (operation()) { + case ABS: + result = gen.emitMathAbs(input); + break; + case CEIL: + result = ((HSAILLIRGenerator) (gen)).emitMathCeil(input); + break; + case FLOOR: + result = ((HSAILLIRGenerator) (gen)).emitMathFloor(input); + break; + case RINT: + result = ((HSAILLIRGenerator) (gen)).emitMathRint(input); + break; + case SQRT: + result = gen.emitMathSqrt(input); + break; + + default: + throw GraalInternalError.shouldNotReachHere(); + } + gen.setResult(this, result); + } + + /** + * Converts a constant to a boxed double. + */ + public Constant evalConst(Constant... inputs) { + assert inputs.length == 1; + return Constant.forDouble(compute(inputs[0].asDouble(), operation())); + } + + /** + * Converts the result of the Math operation to a boxed Double constant Node. + */ + @Override + public Node canonical(CanonicalizerTool tool) { + if (getParameter().isConstant()) { + return ConstantNode.forPrimitive(evalConst(getParameter().asConstant()), graph()); + } + return this; + } + + /** + * NodeIntrinsics for java.lang.Math routines. + * + * The routines below are treated as NodeIntrinsics. A call to any of these ruotines triggers + * the creation of a HSAILMathIntrinsicNode to handle the Math library operation represented by + * the Operation argument. The bodies of these routines are disregarded. They are only provided + * here to keep the Java source compiler happy. + * + */ + + /** + * NodeIntrinsic for java.lang.Math routines taking a single int parameter. + * + * @param value + * @param op + * @return the result of the operation + */ + @NodeIntrinsic + public static int compute(int value, @ConstantNodeParameter HSAILArithmetic op) { + return 0; + } + + /** + * NodeIntrinsic for java.lang.Math routines taking a single double parameter. + * + * @param value the input parameter + * @param op the operation + * @return the result of the operation + */ + @NodeIntrinsic + public static long compute(long value, @ConstantNodeParameter HSAILArithmetic op) { + return 0; + } + + /** + * NodeIntrinsic for java.lang.Math routines taking a single float parameter. + * + * @param value the input parameter + * @param op the Math operation + * @return the result of the operation + */ + @NodeIntrinsic + public static float compute(float value, @ConstantNodeParameter HSAILArithmetic op) { + return 0; + } + + /** + * NodeIntrinsic for java.lang.Math routines taking a single double parameter. + * + * @param value the input parameter + * @param op the math operation + * + * @return the result of the operation + */ + @NodeIntrinsic + public static double compute(double value, @ConstantNodeParameter HSAILArithmetic op) { + return 0; + } + +} --- /dev/null 2013-12-11 15:38:07.845401995 -0600 +++ new/graal/com.oracle.graal.replacements.hsail/src/com/oracle/graal/replacements/hsail/HSAILMathSubstitutions.java 2013-12-12 12:32:51.237342606 -0600 @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.graal.replacements.hsail; + +import com.oracle.graal.api.replacements.ClassSubstitution; +import com.oracle.graal.api.replacements.MethodSubstitution; +import com.oracle.graal.lir.hsail.HSAILArithmetic; + +/** + * Substitutions for {@link java.lang.Math} methods. For any calls to the java.lang.Math routines + * listed below and annotated with @MethodSubstitution, Graal replaces the call with a call to + * HSAILMathIntrinsicNode.compute() which triggers the creation of an HSAILMathIntrinsicNode to + * intrinsify that java.lang.Math routine. + */ +@ClassSubstitution(java.lang.Math.class) +public class HSAILMathSubstitutions { + + /** + * Substitution for Math.abs(int). + * + * @param x the input + * @return the result of the computation + */ + @MethodSubstitution + public static int abs(int x) { + return HSAILMathIntrinsicsNode.compute(x, HSAILArithmetic.ABS); + } + + /** + * Substitution for Math.abs(long). + * + * @param x the input + * @return the result of the computation + */ + @MethodSubstitution + public static long abs(long x) { + return HSAILMathIntrinsicsNode.compute(x, HSAILArithmetic.ABS); + } + + /** + * Substitution for Math.abs(float). + * + * @param x the input + * @return the result of the computation + */ + @MethodSubstitution + public static float abs(float x) { + return HSAILMathIntrinsicsNode.compute(x, HSAILArithmetic.ABS); + } + + /** + * Substitution for Math.abs(double). + * + * @param x the input + * @return the result of the computation + */ + @MethodSubstitution + public static double abs(double x) { + return HSAILMathIntrinsicsNode.compute(x, HSAILArithmetic.ABS); + } + + /** + * Substitution for Math.ceil(double). + * + * @param x the input + * @return the result of the computation + */ + @MethodSubstitution + public static double ceil(double x) { + return HSAILMathIntrinsicsNode.compute(x, HSAILArithmetic.CEIL); + } + + /** + * Substitution for Math.floor(double). + * + * @param x the input + * @return the result of the computation + */ + @MethodSubstitution + public static double floor(double x) { + return HSAILMathIntrinsicsNode.compute(x, HSAILArithmetic.FLOOR); + } + + /** + * Substitution for Math.rint(double). + * + * @param x the input + * @return the result of the computation + */ + @MethodSubstitution + public static double rint(double x) { + return HSAILMathIntrinsicsNode.compute(x, HSAILArithmetic.RINT); + } + + /** + * Substitution for Math.sqrt(double). + * + * @param x the input + * @return the result of the computation + */ + + @MethodSubstitution + public static double sqrt(double x) { + return HSAILMathIntrinsicsNode.compute(x, HSAILArithmetic.SQRT); + } + +} --- old/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/FloatSqrtTest.java 2013-12-12 12:32:51.717342606 -0600 +++ /dev/null 2013-12-11 15:38:07.845401995 -0600 @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package com.oracle.graal.compiler.hsail.test; - -import org.junit.*; - -import com.oracle.graal.compiler.hsail.test.infra.*; - -/** - * Tests floating point square root. - */ -public class FloatSqrtTest extends GraalKernelTester { - - static final int size = 64; - float[] input = new float[size]; - @Result float[] output = new float[size]; - { - for (int i = 0; i < size; i++) { - input[i] = i; - output[i] = -1.0f; - } - - } - - public static void run(float[] input1, float[] output1, int gid) { - output1[gid] = (float) Math.sqrt(input1[gid]); - } - - @Override - public void runTest() { - dispatchMethodKernel(size, input, output); - } - - @Test - public void test() { - testGeneratedHsail(); - } -}