Skip to content

Commit

Permalink
8294198: Implement isFinite intrinsic for RISC-V
Browse files Browse the repository at this point in the history
Reviewed-by: fyang, kvn
  • Loading branch information
Aleksei Voitylov authored and Vladimir Kozlov committed Sep 29, 2022
1 parent 5f6ad92 commit aeef3ec
Show file tree
Hide file tree
Showing 17 changed files with 316 additions and 48 deletions.
26 changes: 26 additions & 0 deletions src/hotspot/cpu/riscv/riscv.ad
Original file line number Diff line number Diff line change
Expand Up @@ -7261,6 +7261,32 @@ instruct isInfiniteD_reg_reg(iRegINoSp dst, fRegD src)
ins_pipe(fp_dop_reg_reg_d);
%}

// Float.isFinite
instruct isFiniteF_reg_reg(iRegINoSp dst, fRegF src)
%{
match(Set dst (IsFiniteF src));
format %{ "isFinite $dst, $src" %}
ins_encode %{
__ fclass_s(as_Register($dst$$reg), as_FloatRegister($src$$reg));
__ andi(as_Register($dst$$reg), as_Register($dst$$reg), 0b0001111110);
__ slt(as_Register($dst$$reg), zr, as_Register($dst$$reg));
%}
ins_pipe(fp_dop_reg_reg_s);
%}

// Double.isFinite
instruct isFiniteD_reg_reg(iRegINoSp dst, fRegD src)
%{
match(Set dst (IsFiniteD src));
format %{ "isFinite $dst, $src" %}
ins_encode %{
__ fclass_d(as_Register($dst$$reg), as_FloatRegister($src$$reg));
__ andi(as_Register($dst$$reg), as_Register($dst$$reg), 0b0001111110);
__ slt(as_Register($dst$$reg), zr, as_Register($dst$$reg));
%}
ins_pipe(fp_dop_reg_reg_d);
%}

instruct divF_reg_reg(fRegF dst, fRegF src1, fRegF src2) %{
match(Set dst (DivF src1 src2));

Expand Down
5 changes: 4 additions & 1 deletion src/hotspot/share/classfile/vmIntrinsics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,11 @@ class methodHandle;
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_name( isInfinite_name, "isInfinite") \
do_intrinsic(_floatIsFinite, java_lang_Float, isFinite_name, float_bool_signature, F_S) \
do_name( isFinite_name, "isFinite") \
do_intrinsic(_doubleIsInfinite, java_lang_Double, isInfinite_name, double_bool_signature, F_S) \
do_intrinsic(_doubleIsFinite, java_lang_Double, isFinite_name, double_bool_signature, F_S) \
\
do_intrinsic(_floatToRawIntBits, java_lang_Float, floatToRawIntBits_name, float_int_signature, F_SN) \
do_name( floatToRawIntBits_name, "floatToRawIntBits") \
Expand Down
6 changes: 6 additions & 0 deletions src/hotspot/share/opto/c2compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,15 @@ bool C2Compiler::is_intrinsic_supported(const methodHandle& method, bool is_virt
case vmIntrinsics::_floatIsInfinite:
if (!Matcher::match_rule_supported(Op_IsInfiniteF)) return false;
break;
case vmIntrinsics::_floatIsFinite:
if (!Matcher::match_rule_supported(Op_IsFiniteF)) return false;
break;
case vmIntrinsics::_doubleIsInfinite:
if (!Matcher::match_rule_supported(Op_IsInfiniteD)) return false;
break;
case vmIntrinsics::_doubleIsFinite:
if (!Matcher::match_rule_supported(Op_IsFiniteD)) return false;
break;
case vmIntrinsics::_hashCode:
case vmIntrinsics::_identityHashCode:
case vmIntrinsics::_getClass:
Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/opto/classes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ macro(MoveF2I)
macro(MoveL2D)
macro(MoveD2L)
macro(IsInfiniteF)
macro(IsFiniteF)
macro(IsInfiniteD)
macro(IsFiniteD)
macro(MulD)
macro(MulF)
macro(MulHiL)
Expand Down
18 changes: 18 additions & 0 deletions src/hotspot/share/opto/intrinsicnode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,22 @@ class IsInfiniteDNode : public Node {
virtual uint ideal_reg() const { return Op_RegI; }
};

//---------- IsFiniteFNode -----------------------------------------------------
class IsFiniteFNode : public Node {
public:
IsFiniteFNode(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; }
};

//---------- IsFiniteDNode -----------------------------------------------------
class IsFiniteDNode : public Node {
public:
IsFiniteDNode(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
8 changes: 8 additions & 0 deletions src/hotspot/share/opto/library_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,9 @@ bool LibraryCallKit::try_to_inline(int predicate) {
case vmIntrinsics::_doubleToLongBits:
case vmIntrinsics::_longBitsToDouble: return inline_fp_conversions(intrinsic_id());

case vmIntrinsics::_floatIsFinite:
case vmIntrinsics::_floatIsInfinite:
case vmIntrinsics::_doubleIsFinite:
case vmIntrinsics::_doubleIsInfinite: return inline_fp_range_check(intrinsic_id());

case vmIntrinsics::_numberOfLeadingZeros_i:
Expand Down Expand Up @@ -4537,9 +4539,15 @@ bool LibraryCallKit::inline_fp_range_check(vmIntrinsics::ID id) {
case vmIntrinsics::_floatIsInfinite:
result = new IsInfiniteFNode(arg);
break;
case vmIntrinsics::_floatIsFinite:
result = new IsFiniteFNode(arg);
break;
case vmIntrinsics::_doubleIsInfinite:
result = new IsInfiniteDNode(arg);
break;
case vmIntrinsics::_doubleIsFinite:
result = new IsFiniteDNode(arg);
break;
default:
fatal_unexpected_iid(id);
break;
Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/runtime/vmStructs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,8 @@
declare_c2_type(SignumFNode, Node) \
declare_c2_type(IsInfiniteFNode, Node) \
declare_c2_type(IsInfiniteDNode, Node) \
declare_c2_type(IsFiniteFNode, Node) \
declare_c2_type(IsFiniteDNode, Node) \
declare_c2_type(LoadVectorGatherNode, LoadVectorNode) \
declare_c2_type(StoreVectorScatterNode, StoreVectorNode) \
declare_c2_type(VectorLoadMaskNode, VectorNode) \
Expand Down
1 change: 1 addition & 0 deletions src/java.base/share/classes/java/lang/Double.java
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,7 @@ public static boolean isInfinite(double v) {
* floating-point value, {@code false} otherwise.
* @since 1.8
*/
@IntrinsicCandidate
public static boolean isFinite(double d) {
return Math.abs(d) <= Double.MAX_VALUE;
}
Expand Down
1 change: 1 addition & 0 deletions src/java.base/share/classes/java/lang/Float.java
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ public static boolean isInfinite(float v) {
* floating-point value, {@code false} otherwise.
* @since 1.8
*/
@IntrinsicCandidate
public static boolean isFinite(float f) {
return Math.abs(f) <= Float.MAX_VALUE;
}
Expand Down
24 changes: 0 additions & 24 deletions test/hotspot/jtreg/compiler/intrinsics/TestDoubleClassCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,7 @@
* questions.
*/

/**
* @test
* @summary Test intrinsics for Double methods isNaN, isFinite, isInfinite.
* @requires vm.cpu.features ~= ".*avx512dq.*" | os.arch == "riscv64"
* @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;

Expand All @@ -40,10 +31,6 @@ public class TestDoubleClassCheck {
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];
Expand All @@ -59,17 +46,6 @@ public TestDoubleClassCheck() {
}
}

@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);
Expand Down
50 changes: 50 additions & 0 deletions test/hotspot/jtreg/compiler/intrinsics/TestDoubleIsFinite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2022, BELLSOFT. All rights reserved.
* 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 intrinsic for Double.isFinite.
* @requires os.arch == "riscv64"
* @library /test/lib /
* @run driver compiler.intrinsics.TestDoubleIsFinite
*/

package compiler.intrinsics;
import compiler.lib.ir_framework.*;

public class TestDoubleIsFinite extends TestDoubleClassCheck {
public static void main(String args[]) {
TestFramework.run(TestDoubleIsFinite.class);
}

@Test // needs to be run in (fast) debug mode
@Warmup(10000)
@IR(counts = {"IsFiniteD", ">= 1"}) // At least one IsFiniteD node is generated if intrinsic is used
public void testIsFinite() {
for (int i = 0; i < BUFFER_SIZE; i++) {
outputs[i] = Double.isFinite(inputs[i]);
}
checkResult("isFinite");
}
}
50 changes: 50 additions & 0 deletions test/hotspot/jtreg/compiler/intrinsics/TestDoubleIsInfinite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2022, BELLSOFT. All rights reserved.
* 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 intrinsic for Double.isInfinite.
* @requires vm.cpu.features ~= ".*avx512dq.*" | os.arch == "riscv64"
* @library /test/lib /
* @run driver compiler.intrinsics.TestDoubleIsInfinite
*/

package compiler.intrinsics;
import compiler.lib.ir_framework.*;

public class TestDoubleIsInfinite extends TestDoubleClassCheck {
public static void main(String args[]) {
TestFramework.run(TestDoubleIsInfinite.class);
}

@Test // needs to be run in (fast) debug mode
@Warmup(10000)
@IR(counts = {"IsInfiniteD", ">= 1"}) // At least 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");
}
}
23 changes: 0 additions & 23 deletions test/hotspot/jtreg/compiler/intrinsics/TestFloatClassCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,7 @@
* questions.
*/

/**
* @test
* @summary Test intrinsics for Float methods isNaN, isFinite, isInfinite.
* @requires vm.cpu.features ~= ".*avx512dq.*" | os.arch == "riscv64"
* @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;

Expand All @@ -40,10 +31,6 @@ public class TestFloatClassCheck {
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];
Expand All @@ -59,16 +46,6 @@ public TestFloatClassCheck() {
}
}

@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);
Expand Down
50 changes: 50 additions & 0 deletions test/hotspot/jtreg/compiler/intrinsics/TestFloatIsFinite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2022, BELLSOFT. All rights reserved.
* 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 intrinsics for Float.isFinite.
* @requires os.arch == "riscv64"
* @library /test/lib /
* @run driver compiler.intrinsics.TestFloatIsFinite
*/

package compiler.intrinsics;
import compiler.lib.ir_framework.*;

public class TestFloatIsFinite extends TestFloatClassCheck {
public static void main(String args[]) {
TestFramework.run(TestFloatIsFinite.class);
}

@Test // needs to be run in (fast) debug mode
@Warmup(10000)
@IR(counts = {"IsFiniteF", ">= 1"}) // At least one IsFiniteF node is generated if intrinsic is used
public void testIsFinite() {
for (int i = 0; i < BUFFER_SIZE; i++) {
outputs[i] = Float.isFinite(inputs[i]);
}
checkResult("isFinite");
}
}

1 comment on commit aeef3ec

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.