1 /*
   2  * Copyright (c) 2016, 2018, 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 
  25 package org.graalvm.compiler.hotspot.amd64;
  26 
  27 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_COS_STUB;
  28 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_EXP_STUB;
  29 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_LOG10_STUB;
  30 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_LOG_STUB;
  31 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_POW_STUB;
  32 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_SIN_STUB;
  33 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_TAN_STUB;
  34 
  35 import org.graalvm.compiler.api.replacements.Snippet;
  36 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  37 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage;
  38 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  39 import org.graalvm.compiler.hotspot.stubs.SnippetStub;
  40 import org.graalvm.compiler.options.OptionValues;
  41 import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode;
  42 import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation;
  43 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode;
  44 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation;
  45 
  46 /**
  47  * Stub called to support {@link Math}.
  48  */
  49 public class AMD64MathStub extends SnippetStub {
  50 
  51     public AMD64MathStub(ForeignCallDescriptor descriptor, OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) {
  52         super(snippetName(descriptor), options, providers, linkage);
  53     }
  54 
  55     private static String snippetName(ForeignCallDescriptor descriptor) {
  56         if (descriptor == ARITHMETIC_LOG_STUB) {
  57             return "log";
  58         }
  59         if (descriptor == ARITHMETIC_LOG10_STUB) {
  60             return "log10";
  61         }
  62         if (descriptor == ARITHMETIC_SIN_STUB) {
  63             return "sin";
  64         }
  65         if (descriptor == ARITHMETIC_COS_STUB) {
  66             return "cos";
  67         }
  68         if (descriptor == ARITHMETIC_TAN_STUB) {
  69             return "tan";
  70         }
  71         if (descriptor == ARITHMETIC_EXP_STUB) {
  72             return "exp";
  73         }
  74         if (descriptor == ARITHMETIC_POW_STUB) {
  75             return "pow";
  76         }
  77         throw new InternalError("Unknown operation " + descriptor);
  78     }
  79 
  80     @Snippet
  81     private static double log(double value) {
  82         return UnaryMathIntrinsicNode.compute(value, UnaryOperation.LOG);
  83     }
  84 
  85     @Snippet
  86     private static double log10(double value) {
  87         return UnaryMathIntrinsicNode.compute(value, UnaryOperation.LOG10);
  88     }
  89 
  90     @Snippet
  91     private static double sin(double value) {
  92         return UnaryMathIntrinsicNode.compute(value, UnaryOperation.SIN);
  93     }
  94 
  95     @Snippet
  96     private static double cos(double value) {
  97         return UnaryMathIntrinsicNode.compute(value, UnaryOperation.COS);
  98     }
  99 
 100     @Snippet
 101     private static double tan(double value) {
 102         return UnaryMathIntrinsicNode.compute(value, UnaryOperation.TAN);
 103     }
 104 
 105     @Snippet
 106     private static double exp(double value) {
 107         return UnaryMathIntrinsicNode.compute(value, UnaryOperation.EXP);
 108     }
 109 
 110     @Snippet
 111     private static double pow(double value1, double value2) {
 112         return BinaryMathIntrinsicNode.compute(value1, value2, BinaryOperation.POW);
 113     }
 114 }