1 /*
  2  * Copyright (c) 2021, 2022, 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 #ifndef SHARE_PRIMS_VECTORSUPPORT_HPP
 26 #define SHARE_PRIMS_VECTORSUPPORT_HPP
 27 
 28 #include "jni.h"
 29 #include "code/debugInfo.hpp"
 30 #include "memory/allStatic.hpp"
 31 #include "oops/typeArrayOop.hpp"
 32 #include "runtime/registerMap.hpp"
 33 #include "utilities/exceptions.hpp"
 34 
 35 extern "C" {
 36   void JNICALL JVM_RegisterVectorSupportMethods(JNIEnv* env, jclass vsclass);
 37 }
 38 
 39 class VectorSupport : AllStatic {
 40  private:
 41   static Handle allocate_vector_payload(InstanceKlass* ik, frame* fr, RegisterMap* reg_map, ScopeValue* payload, TRAPS);
 42   static Handle allocate_vector_payload_helper(InstanceKlass* ik, frame* fr, RegisterMap* reg_map, Location location, TRAPS);
 43 
 44   static void init_payload_element(typeArrayOop arr, BasicType elem_bt, int index, address addr);
 45 
 46   static BasicType klass2bt(InstanceKlass* ik);
 47   static jint klass2length(InstanceKlass* ik);
 48 
 49  public:
 50 
 51    // Should be aligned with constants in jdk.internal.vm.vector.VectorSupport
 52   enum VectorOperation {
 53     // Unary
 54     VECTOR_OP_ABS     = 0,
 55     VECTOR_OP_NEG     = 1,
 56     VECTOR_OP_SQRT    = 2,
 57     VECTOR_OP_BIT_COUNT = 3,
 58 
 59     // Binary
 60     VECTOR_OP_ADD     = 4,
 61     VECTOR_OP_SUB     = 5,
 62     VECTOR_OP_MUL     = 6,
 63     VECTOR_OP_DIV     = 7,
 64     VECTOR_OP_MIN     = 8,
 65     VECTOR_OP_MAX     = 9,
 66     VECTOR_OP_AND     = 10,
 67     VECTOR_OP_OR      = 11,
 68     VECTOR_OP_XOR     = 12,
 69 
 70     // Ternary
 71     VECTOR_OP_FMA     = 13,
 72 
 73     // Broadcast int
 74     VECTOR_OP_LSHIFT  = 14,
 75     VECTOR_OP_RSHIFT  = 15,
 76     VECTOR_OP_URSHIFT = 16,
 77 
 78     // Convert
 79     VECTOR_OP_CAST        = 17,
 80     VECTOR_OP_UCAST       = 18,
 81     VECTOR_OP_REINTERPRET = 19,
 82 
 83     // Mask manipulation operations
 84     VECTOR_OP_MASK_TRUECOUNT = 20,
 85     VECTOR_OP_MASK_FIRSTTRUE = 21,
 86     VECTOR_OP_MASK_LASTTRUE  = 22,
 87     VECTOR_OP_MASK_TOLONG    = 23,
 88 
 89     // Rotate operations
 90     VECTOR_OP_LROTATE = 24,
 91     VECTOR_OP_RROTATE = 25,
 92 
 93     VECTOR_OP_COMPRESS = 26,
 94     VECTOR_OP_EXPAND = 27,
 95     VECTOR_OP_MASK_COMPRESS = 28,
 96 
 97     VECTOR_OP_TZ_COUNT = 29,
 98     VECTOR_OP_LZ_COUNT = 30,
 99     VECTOR_OP_REVERSE  = 31,
100     VECTOR_OP_REVERSE_BYTES = 32,
101     VECTOR_OP_COMPRESS_BITS = 33,
102     VECTOR_OP_EXPAND_BITS = 34,
103 
104     // Vector Math Library
105     VECTOR_OP_TAN   = 101,
106     VECTOR_OP_TANH  = 102,
107     VECTOR_OP_SIN   = 103,
108     VECTOR_OP_SINH  = 104,
109     VECTOR_OP_COS   = 105,
110     VECTOR_OP_COSH  = 106,
111     VECTOR_OP_ASIN  = 107,
112     VECTOR_OP_ACOS  = 108,
113     VECTOR_OP_ATAN  = 109,
114     VECTOR_OP_ATAN2 = 110,
115     VECTOR_OP_CBRT  = 111,
116     VECTOR_OP_LOG   = 112,
117     VECTOR_OP_LOG10 = 113,
118     VECTOR_OP_LOG1P = 114,
119     VECTOR_OP_POW   = 115,
120     VECTOR_OP_EXP   = 116,
121     VECTOR_OP_EXPM1 = 117,
122     VECTOR_OP_HYPOT = 118,
123 
124     VECTOR_OP_SVML_START = VECTOR_OP_TAN,
125     VECTOR_OP_SVML_END   = VECTOR_OP_HYPOT,
126     NUM_SVML_OP = VECTOR_OP_SVML_END - VECTOR_OP_SVML_START + 1
127   };
128 
129   enum {
130     VEC_SIZE_64 = 0,
131     VEC_SIZE_128 = 1,
132     VEC_SIZE_256 = 2,
133     VEC_SIZE_512 = 3,
134     NUM_VEC_SIZES = 4
135   };
136 
137   enum {
138     MODE_BROADCAST = 0,
139     MODE_BITS_COERCED_LONG_TO_MASK = 1
140   };
141 
142   static const char* svmlname[VectorSupport::NUM_SVML_OP];
143 
144   static int vop2ideal(jint vop, BasicType bt);
145 
146   static instanceOop allocate_vector(InstanceKlass* holder, frame* fr, RegisterMap* reg_map, ObjectValue* sv, TRAPS);
147 
148   static bool is_vector(Klass* klass);
149   static bool is_vector_mask(Klass* klass);
150   static bool is_vector_shuffle(Klass* klass);
151 };
152 #endif // SHARE_PRIMS_VECTORSUPPORT_HPP