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