1 /*
   2  * Copyright (c) 2013, 2019, 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.replacements.SnippetTemplate.DEFAULT_REPLACER;
  28 
  29 import org.graalvm.compiler.api.replacements.Snippet;
  30 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
  31 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  32 import org.graalvm.compiler.debug.DebugHandlersFactory;
  33 import org.graalvm.compiler.debug.GraalError;
  34 import org.graalvm.compiler.graph.Node.ConstantNodeParameter;
  35 import org.graalvm.compiler.graph.Node.NodeIntrinsic;
  36 import org.graalvm.compiler.nodes.extended.ForeignCallNode;
  37 import org.graalvm.compiler.nodes.spi.LoweringTool;
  38 import org.graalvm.compiler.options.OptionValues;
  39 import org.graalvm.compiler.phases.util.Providers;
  40 import org.graalvm.compiler.replacements.SnippetTemplate.AbstractTemplates;
  41 import org.graalvm.compiler.replacements.SnippetTemplate.Arguments;
  42 import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo;
  43 import org.graalvm.compiler.replacements.Snippets;
  44 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode;
  45 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation;
  46 
  47 import jdk.vm.ci.code.TargetDescription;
  48 
  49 public class AMD64X87MathSnippets implements Snippets {
  50 
  51     private static final double PI_4 = Math.PI / 4;
  52 
  53     @Snippet
  54     public static double sin(double input) {
  55         if (Math.abs(input) < PI_4) {
  56             return AMD64X87MathIntrinsicNode.compute(input, UnaryOperation.SIN);
  57         }
  58         return callDouble1(UnaryOperation.SIN.foreignCallDescriptor, input);
  59     }
  60 
  61     @Snippet
  62     public static double cos(double input) {
  63         if (Math.abs(input) < PI_4) {
  64             return AMD64X87MathIntrinsicNode.compute(input, UnaryOperation.COS);
  65         }
  66         return callDouble1(UnaryOperation.COS.foreignCallDescriptor, input);
  67     }
  68 
  69     @Snippet
  70     public static double tan(double input) {
  71         if (Math.abs(input) < PI_4) {
  72             return AMD64X87MathIntrinsicNode.compute(input, UnaryOperation.TAN);
  73         }
  74         return callDouble1(UnaryOperation.TAN.foreignCallDescriptor, input);
  75     }
  76 
  77     @NodeIntrinsic(value = ForeignCallNode.class)
  78     private static native double callDouble1(@ConstantNodeParameter ForeignCallDescriptor descriptor, double value);
  79 
  80     public static class Templates extends AbstractTemplates {
  81 
  82         private final SnippetInfo sin;
  83         private final SnippetInfo cos;
  84         private final SnippetInfo tan;
  85 
  86         public Templates(OptionValues options, Iterable<DebugHandlersFactory> factories, Providers providers, SnippetReflectionProvider snippetReflection, TargetDescription target) {
  87             super(options, factories, providers, snippetReflection, target);
  88 
  89             sin = snippet(AMD64X87MathSnippets.class, "sin");
  90             cos = snippet(AMD64X87MathSnippets.class, "cos");
  91             tan = snippet(AMD64X87MathSnippets.class, "tan");
  92         }
  93 
  94         public void lower(UnaryMathIntrinsicNode mathIntrinsicNode, LoweringTool tool) {
  95             SnippetInfo info;
  96 
  97             switch (mathIntrinsicNode.getOperation()) {
  98                 case SIN:
  99                     info = sin;
 100                     break;
 101                 case COS:
 102                     info = cos;
 103                     break;
 104                 case TAN:
 105                     info = tan;
 106                     break;
 107                 default:
 108                     throw GraalError.shouldNotReachHere("Snippet not found for math intrinsic " + mathIntrinsicNode.getOperation().name());
 109             }
 110 
 111             Arguments args = new Arguments(info, mathIntrinsicNode.graph().getGuardsStage(), tool.getLoweringStage());
 112             args.add("input", mathIntrinsicNode.getValue());
 113             template(mathIntrinsicNode, args).instantiate(providers.getMetaAccess(), mathIntrinsicNode, DEFAULT_REPLACER, tool, args);
 114             mathIntrinsicNode.safeDelete();
 115         }
 116     }
 117 }