1 /*
   2  * Copyright (c) 2011, 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 jdk.vm.ci.code.ValueUtil.asRegister;
  26 
  27 import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
  28 import org.graalvm.compiler.debug.GraalError;
  29 import org.graalvm.compiler.lir.LIRInstructionClass;
  30 import org.graalvm.compiler.lir.Opcode;
  31 import org.graalvm.compiler.lir.amd64.AMD64LIRInstruction;
  32 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  33 
  34 import jdk.vm.ci.amd64.AMD64Kind;
  35 import jdk.vm.ci.meta.Value;
  36 
  37 /**
  38  * This provides the default implementation expected by some HotSpot based lowerings of Math
  39  * intrinsics. Depending on the release different patterns might be used.
  40  */
  41 public final class AMD64HotSpotMathIntrinsicOp extends AMD64LIRInstruction {
  42     public static final LIRInstructionClass<AMD64HotSpotMathIntrinsicOp> TYPE = LIRInstructionClass.create(AMD64HotSpotMathIntrinsicOp.class);
  43 
  44     public enum IntrinsicOpcode {
  45         SIN,
  46         COS,
  47         TAN,
  48         LOG,
  49         LOG10
  50     }
  51 
  52     @Opcode private final IntrinsicOpcode opcode;
  53     @Def protected Value result;
  54     @Use protected Value input;
  55 
  56     public AMD64HotSpotMathIntrinsicOp(IntrinsicOpcode opcode, Value result, Value input) {
  57         super(TYPE);
  58         this.opcode = opcode;
  59         this.result = result;
  60         this.input = input;
  61     }
  62 
  63     @Override
  64     public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
  65         switch (opcode) {
  66             case LOG:
  67                 masm.flog(asRegister(result, AMD64Kind.DOUBLE), asRegister(input, AMD64Kind.DOUBLE), false);
  68                 break;
  69             case LOG10:
  70                 masm.flog(asRegister(result, AMD64Kind.DOUBLE), asRegister(input, AMD64Kind.DOUBLE), true);
  71                 break;
  72             case SIN:
  73                 masm.fsin(asRegister(result, AMD64Kind.DOUBLE), asRegister(input, AMD64Kind.DOUBLE));
  74                 break;
  75             case COS:
  76                 masm.fcos(asRegister(result, AMD64Kind.DOUBLE), asRegister(input, AMD64Kind.DOUBLE));
  77                 break;
  78             case TAN:
  79                 masm.ftan(asRegister(result, AMD64Kind.DOUBLE), asRegister(input, AMD64Kind.DOUBLE));
  80                 break;
  81             default:
  82                 throw GraalError.shouldNotReachHere();
  83         }
  84     }
  85 }