8285868: x86 intrinsics for floating point method isInfinite

Reviewed-by: kvn, jbhateja
This commit is contained in:
vamsi-parasa 2022-06-02 17:42:42 +00:00 committed by Jatin Bhateja
parent 13596cdf69
commit 7f44f572ea
18 changed files with 513 additions and 0 deletions

View File

@ -10410,6 +10410,26 @@ void Assembler::vzeroupper_uncached() {
}
}
void Assembler::vfpclassss(KRegister kdst, XMMRegister src, uint8_t imm8) {
// Encoding: EVEX.LIG.66.0F3A.W0 67 /r ib
assert(VM_Version::supports_evex(), "");
assert(VM_Version::supports_avx512dq(), "");
InstructionAttr attributes(AVX_128bit, /* vex_w */ false, /* legacy_mode */ false, /* no_mask_reg */ false, /* uses_vl */ false);
attributes.set_is_evex_instruction();
int encode = vex_prefix_and_encode(kdst->encoding(), 0, src->encoding(), VEX_SIMD_66, VEX_OPCODE_0F_3A, &attributes);
emit_int24((unsigned char)0x67, (unsigned char)(0xC0 | encode), imm8);
}
void Assembler::vfpclasssd(KRegister kdst, XMMRegister src, uint8_t imm8) {
// Encoding: EVEX.LIG.66.0F3A.W1 67 /r ib
assert(VM_Version::supports_evex(), "");
assert(VM_Version::supports_avx512dq(), "");
InstructionAttr attributes(AVX_128bit, /* vex_w */ true, /* legacy_mode */ false, /* no_mask_reg */ false, /* uses_vl */ false);
attributes.set_is_evex_instruction();
int encode = vex_prefix_and_encode(kdst->encoding(), 0, src->encoding(), VEX_SIMD_66, VEX_OPCODE_0F_3A, &attributes);
emit_int24((unsigned char)0x67, (unsigned char)(0xC0 | encode), imm8);
}
void Assembler::fld_x(Address adr) {
InstructionMark im(this);
emit_int8((unsigned char)0xDB);

View File

@ -2746,6 +2746,10 @@ private:
void evpmovm2d(XMMRegister dst, KRegister src, int vector_len);
void evpmovm2q(XMMRegister dst, KRegister src, int vector_len);
// floating point class tests
void vfpclassss(KRegister kdst, XMMRegister src, uint8_t imm8);
void vfpclasssd(KRegister kdst, XMMRegister src, uint8_t imm8);
// Vector blends
void blendvps(XMMRegister dst, XMMRegister src);
void blendvpd(XMMRegister dst, XMMRegister src);

View File

@ -5316,3 +5316,4 @@ void C2_MacroAssembler::udivmodL(Register rax, Register divisor, Register rdx, R
bind(done);
}
#endif

View File

@ -1462,6 +1462,12 @@ const bool Matcher::match_rule_supported(int opcode) {
return false;
}
break;
case Op_IsInfiniteF:
case Op_IsInfiniteD:
if (!VM_Version::supports_avx512dq()) {
return false;
}
break;
case Op_SqrtVD:
case Op_SqrtVF:
case Op_VectorMaskCmp:
@ -10136,3 +10142,29 @@ instruct castVVLeg(legVec dst)
ins_cost(0);
ins_pipe(empty);
%}
instruct FloatClassCheck_reg_reg_vfpclass(rRegI dst, regF src, kReg ktmp, rFlagsReg cr)
%{
match(Set dst (IsInfiniteF src));
effect(TEMP ktmp, KILL cr);
format %{ "float_class_check $dst, $src" %}
ins_encode %{
__ vfpclassss($ktmp$$KRegister, $src$$XMMRegister, 0x18);
__ kmovbl($dst$$Register, $ktmp$$KRegister);
%}
ins_pipe(pipe_slow);
%}
instruct DoubleClassCheck_reg_reg_vfpclass(rRegI dst, regD src, kReg ktmp, rFlagsReg cr)
%{
match(Set dst (IsInfiniteD src));
effect(TEMP ktmp, KILL cr);
format %{ "double_class_check $dst, $src" %}
ins_encode %{
__ vfpclasssd($ktmp$$KRegister, $src$$XMMRegister, 0x18);
__ kmovbl($dst$$Register, $ktmp$$KRegister);
%}
ins_pipe(pipe_slow);
%}

View File

@ -203,6 +203,10 @@ class methodHandle;
/* Special flavor of dsqrt intrinsic to handle the "native" method in StrictMath. Otherwise the same as in Math. */ \
do_intrinsic(_dsqrt_strict, java_lang_StrictMath, sqrt_name, double_double_signature, F_SN) \
\
do_intrinsic(_floatIsInfinite, java_lang_Float, isInfinite_name, float_bool_signature, F_S) \
do_name( isInfinite_name, "isInfinite") \
do_intrinsic(_doubleIsInfinite, java_lang_Double, isInfinite_name, double_bool_signature, F_S) \
\
do_intrinsic(_floatToRawIntBits, java_lang_Float, floatToRawIntBits_name, float_int_signature, F_SN) \
do_name( floatToRawIntBits_name, "floatToRawIntBits") \
do_intrinsic(_floatToIntBits, java_lang_Float, floatToIntBits_name, float_int_signature, F_S) \

View File

@ -555,6 +555,8 @@
template(char_char_signature, "(C)C") \
template(short_short_signature, "(S)S") \
template(int_bool_signature, "(I)Z") \
template(float_bool_signature, "(F)Z") \
template(double_bool_signature, "(D)Z") \
template(float_int_signature, "(F)I") \
template(double_long_signature, "(D)J") \
template(double_double_signature, "(D)D") \

View File

@ -518,6 +518,12 @@ bool C2Compiler::is_intrinsic_supported(const methodHandle& method, bool is_virt
case vmIntrinsics::_fsignum:
if (!Matcher::match_rule_supported(Op_SignumF)) return false;
break;
case vmIntrinsics::_floatIsInfinite:
if (!Matcher::match_rule_supported(Op_IsInfiniteF)) return false;
break;
case vmIntrinsics::_doubleIsInfinite:
if (!Matcher::match_rule_supported(Op_IsInfiniteD)) return false;
break;
case vmIntrinsics::_hashCode:
case vmIntrinsics::_identityHashCode:
case vmIntrinsics::_getClass:

View File

@ -245,6 +245,8 @@ macro(MoveI2F)
macro(MoveF2I)
macro(MoveL2D)
macro(MoveD2L)
macro(IsInfiniteF)
macro(IsInfiniteD)
macro(MulD)
macro(MulF)
macro(MulHiL)

View File

@ -262,4 +262,22 @@ class SignumFNode : public Node {
virtual uint ideal_reg() const { return Op_RegF; }
};
//---------- IsInfiniteFNode -----------------------------------------------------
class IsInfiniteFNode : public Node {
public:
IsInfiniteFNode(Node* in1) : Node(0, in1) {}
virtual int Opcode() const;
const Type* bottom_type() const { return TypeInt::BOOL; }
virtual uint ideal_reg() const { return Op_RegI; }
};
//---------- IsInfiniteDNode -----------------------------------------------------
class IsInfiniteDNode : public Node {
public:
IsInfiniteDNode(Node* in1) : Node(0, in1) {}
virtual int Opcode() const;
const Type* bottom_type() const { return TypeInt::BOOL; }
virtual uint ideal_reg() const { return Op_RegI; }
};
#endif // SHARE_OPTO_INTRINSICNODE_HPP

View File

@ -516,6 +516,9 @@ bool LibraryCallKit::try_to_inline(int predicate) {
case vmIntrinsics::_doubleToLongBits:
case vmIntrinsics::_longBitsToDouble: return inline_fp_conversions(intrinsic_id());
case vmIntrinsics::_floatIsInfinite:
case vmIntrinsics::_doubleIsInfinite: return inline_fp_range_check(intrinsic_id());
case vmIntrinsics::_numberOfLeadingZeros_i:
case vmIntrinsics::_numberOfLeadingZeros_l:
case vmIntrinsics::_numberOfTrailingZeros_i:
@ -4642,6 +4645,25 @@ bool LibraryCallKit::inline_fp_conversions(vmIntrinsics::ID id) {
return true;
}
bool LibraryCallKit::inline_fp_range_check(vmIntrinsics::ID id) {
Node* arg = argument(0);
Node* result = NULL;
switch (id) {
case vmIntrinsics::_floatIsInfinite:
result = new IsInfiniteFNode(arg);
break;
case vmIntrinsics::_doubleIsInfinite:
result = new IsInfiniteDNode(arg);
break;
default:
fatal_unexpected_iid(id);
break;
}
set_result(_gvn.transform(result));
return true;
}
//----------------------inline_unsafe_copyMemory-------------------------
// public native void Unsafe.copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);

View File

@ -274,6 +274,7 @@ class LibraryCallKit : public GraphKit {
bool inline_unsafe_fence(vmIntrinsics::ID id);
bool inline_onspinwait();
bool inline_fp_conversions(vmIntrinsics::ID id);
bool inline_fp_range_check(vmIntrinsics::ID id);
bool inline_number_methods(vmIntrinsics::ID id);
bool inline_divmod_methods(vmIntrinsics::ID id);
bool inline_reference_get();

View File

@ -1847,6 +1847,8 @@
declare_c2_type(CopySignFNode, Node) \
declare_c2_type(SignumDNode, Node) \
declare_c2_type(SignumFNode, Node) \
declare_c2_type(IsInfiniteFNode, Node) \
declare_c2_type(IsInfiniteDNode, Node) \
declare_c2_type(LoadVectorGatherNode, LoadVectorNode) \
declare_c2_type(StoreVectorScatterNode, StoreVectorNode) \
declare_c2_type(VectorLoadMaskNode, VectorNode) \

View File

@ -754,6 +754,7 @@ public final class Double extends Number
* @return {@code true} if the value of the argument is positive
* infinity or negative infinity; {@code false} otherwise.
*/
@IntrinsicCandidate
public static boolean isInfinite(double v) {
return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
}

View File

@ -575,6 +575,7 @@ public final class Float extends Number
* @return {@code true} if the argument is positive infinity or
* negative infinity; {@code false} otherwise.
*/
@IntrinsicCandidate
public static boolean isInfinite(float v) {
return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
}

View File

@ -0,0 +1,96 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @summary Test x86_64 intrinsics for Double methods isNaN, isFinite, isInfinite.
* @requires vm.cpu.features ~= ".*avx512dq.*"
* @library /test/lib /
* @run driver compiler.intrinsics.TestDoubleClassCheck
*/
package compiler.intrinsics;
import compiler.lib.ir_framework.*;
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;
public class TestDoubleClassCheck {
RandomGenerator rng;
int BUFFER_SIZE = 1024;
double[] inputs;
boolean[] outputs;
public static void main(String args[]) {
TestFramework.run(TestDoubleClassCheck.class);
}
public TestDoubleClassCheck() {
outputs = new boolean[BUFFER_SIZE];
inputs = new double[BUFFER_SIZE];
RandomGenerator rng = RandomGeneratorFactory.getDefault().create(0);
double input;
for (int i = 0; i < BUFFER_SIZE; i++) {
if (i % 5 == 0) {
input = (i%2 == 0) ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
}
else if (i % 3 == 0) input = Double.NaN;
else input = rng.nextDouble();
inputs[i] = input;
}
}
@Test // needs to be run in (fast) debug mode
@Warmup(10000)
@IR(counts = {"IsInfiniteD", ">= 1"}) // Atleast one IsInfiniteD node is generated if intrinsic is used
public void testIsInfinite() {
for (int i = 0; i < BUFFER_SIZE; i++) {
outputs[i] = Double.isInfinite(inputs[i]);
}
checkResult("isInfinite");
}
public void checkResult(String method) {
for (int i=0; i < BUFFER_SIZE; i++) {
boolean expected = doubleClassCheck(inputs[i], method);
if (expected != outputs[i]) {
String errorMsg = "Correctness check failed for Double." + method +
"() for input = " + inputs[i];
throw new RuntimeException(errorMsg);
}
}
}
public boolean doubleClassCheck(double f, String method) {
long infBits = Double.doubleToRawLongBits(Double.POSITIVE_INFINITY);
long bits = Double.doubleToRawLongBits(f);
bits = bits & Long.MAX_VALUE;
switch (method) {
case "isFinite": return (bits < infBits);
case "isInfinite": return (bits == infBits);
case "isNaN": return (bits > infBits);
default: throw new IllegalArgumentException("incorrect method for Double");
}
}
}

View File

@ -0,0 +1,95 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @summary Test x86_64 intrinsics for Float methods isNaN, isFinite, isInfinite.
* @requires vm.cpu.features ~= ".*avx512dq.*"
* @library /test/lib /
* @run driver compiler.intrinsics.TestFloatClassCheck
*/
package compiler.intrinsics;
import compiler.lib.ir_framework.*;
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;
public class TestFloatClassCheck {
RandomGenerator rng;
int BUFFER_SIZE = 1024;
float[] inputs;
boolean[] outputs;
public static void main(String args[]) {
TestFramework.run(TestFloatClassCheck.class);
}
public TestFloatClassCheck() {
outputs = new boolean[BUFFER_SIZE];
inputs = new float[BUFFER_SIZE];
RandomGenerator rng = RandomGeneratorFactory.getDefault().create(0);
float input;
for (int i = 0; i < BUFFER_SIZE; i++) {
if (i % 5 == 0) {
input = (i%2 == 0) ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;
}
else if (i % 3 == 0) input = Float.NaN;
else input = rng.nextFloat();
inputs[i] = input;
}
}
@Test // needs to be run in (fast) debug mode
@Warmup(10000)
@IR(counts = {"IsInfiniteF", ">= 1"}) // Atleast one IsInfiniteF node is generated if intrinsic is used
public void testIsInfinite() {
for (int i = 0; i < BUFFER_SIZE; i++) {
outputs[i] = Float.isInfinite(inputs[i]);
}
checkResult("isInfinite");
}
public void checkResult(String method) {
for (int i=0; i < BUFFER_SIZE; i++) {
boolean expected = floatClassCheck(inputs[i], method);
if (expected != outputs[i]) {
String errorMsg = "Correctness check failed for Float." + method +
"() for input = " + inputs[i];
throw new RuntimeException(errorMsg);
}
}
}
public boolean floatClassCheck(float f, String method) {
int infBits = Float.floatToRawIntBits(Float.POSITIVE_INFINITY);
int bits = Float.floatToRawIntBits(f);
bits = bits & Integer.MAX_VALUE;
switch (method) {
case "isFinite": return (bits < infBits);
case "isInfinite": return (bits == infBits);
case "isNaN": return (bits > infBits);
default: throw new IllegalArgumentException("incorrect method for Float");
}
}
}

View File

@ -0,0 +1,102 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.java.lang;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.CompilerControl;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OperationsPerInvocation;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Param;
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
@Fork(1)
public class DoubleClassCheck {
RandomGenerator rng;
static final int BUFFER_SIZE = 1024;
double[] inputs;
boolean[] storeOutputs;
int[] cmovOutputs;
int[] branchOutputs;
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
static int call() {
return 1;
}
@Setup
public void setup() {
storeOutputs = new boolean[BUFFER_SIZE];
cmovOutputs = new int[BUFFER_SIZE];
branchOutputs = new int[BUFFER_SIZE];
inputs = new double[BUFFER_SIZE];
RandomGenerator rng = RandomGeneratorFactory.getDefault().create(0);
double input;
for (int i = 0; i < BUFFER_SIZE; i++) {
if (i % 5 == 0) {
input = (i%2 == 0) ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
}
else if (i % 3 == 0) input = Double.NaN;
else input = rng.nextDouble();
inputs[i] = input;
}
}
@Benchmark
@OperationsPerInvocation(BUFFER_SIZE)
public void testIsInfiniteStore() {
for (int i = 0; i < BUFFER_SIZE; i++) {
storeOutputs[i] = Double.isInfinite(inputs[i]);
}
}
@Benchmark
@OperationsPerInvocation(BUFFER_SIZE)
public void testIsInfiniteCMov() {
for (int i = 0; i < BUFFER_SIZE; i++) {
cmovOutputs[i] = Double.isInfinite(inputs[i]) ? 9 : 7;
}
}
@Benchmark
@OperationsPerInvocation(BUFFER_SIZE)
public void testIsInfiniteBranch() {
for (int i = 0; i < BUFFER_SIZE; i++) {
cmovOutputs[i] = Double.isInfinite(inputs[i]) ? call() : 7;
}
}
}

View File

@ -0,0 +1,104 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.java.lang;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.CompilerControl;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OperationsPerInvocation;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Param;
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
@Fork(1)
public class FloatClassCheck {
RandomGenerator rng;
static final int BUFFER_SIZE = 1024;
float[] inputs;
boolean[] storeOutputs;
int[] cmovOutputs;
int[] branchOutputs;
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
static int call() {
return 1;
}
@Setup
public void setup() {
storeOutputs = new boolean[BUFFER_SIZE];
cmovOutputs = new int[BUFFER_SIZE];
branchOutputs = new int[BUFFER_SIZE];
inputs = new float[BUFFER_SIZE];
RandomGenerator rng = RandomGeneratorFactory.getDefault().create(0);
float input;
for (int i = 0; i < BUFFER_SIZE; i++) {
if (i % 5 == 0) {
input = (i % 2 == 0) ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;
} else if (i % 3 == 0)
input = Float.NaN;
else
input = rng.nextFloat();
inputs[i] = input;
}
}
@Benchmark
@OperationsPerInvocation(BUFFER_SIZE)
public void testIsInfiniteStore() {
for (int i = 0; i < BUFFER_SIZE; i++) {
storeOutputs[i] = Float.isInfinite(inputs[i]);
}
}
@Benchmark
@OperationsPerInvocation(BUFFER_SIZE)
public void testIsInfiniteCMov() {
for (int i = 0; i < BUFFER_SIZE; i++) {
cmovOutputs[i] = Float.isInfinite(inputs[i]) ? 9 : 7;
}
}
@Benchmark
@OperationsPerInvocation(BUFFER_SIZE)
public void testIsInfiniteBranch() {
for (int i = 0; i < BUFFER_SIZE; i++) {
cmovOutputs[i] = Float.isInfinite(inputs[i]) ? call() : 7;
}
}
}