Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8265917: Different values computed by C2 and interpreter/C1 for Math.pow(x, 2.0) on x86_32 #3677

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
@@ -2538,17 +2538,28 @@ void MacroAssembler::fast_pow(XMMRegister xmm0, XMMRegister xmm1, XMMRegister xm
Label L_2TAG_PACKET_48_0_2, L_2TAG_PACKET_49_0_2, L_2TAG_PACKET_50_0_2, L_2TAG_PACKET_51_0_2;
Label L_2TAG_PACKET_52_0_2, L_2TAG_PACKET_53_0_2, L_2TAG_PACKET_54_0_2, L_2TAG_PACKET_55_0_2;
Label L_2TAG_PACKET_56_0_2, L_2TAG_PACKET_57_0_2, L_2TAG_PACKET_58_0_2, start;
Label L_NOT_DOUBLE2;

assert_different_registers(tmp, eax, ecx, edx);

address static_const_table_pow = (address)_static_const_table_pow;
static double DOUBLE2 = 2.0;

This comment has been minimized.

Loading
@TobiHartmann

TobiHartmann Apr 27, 2021
Member Outdated

Constants in the 64-bit code and _static_const_table_pow above use ATTRIBUTE_ALIGNED. Should we add that for DOUBLE2 as well?

This comment has been minimized.

Loading
@DamonFool

DamonFool Apr 27, 2021
Author Member Outdated

Hi @TobiHartmann ,

Thanks for your review.

Yes, it seem better to be with ATTRIBUTE_ALIGNED.
Updated.


bind(start);
subl(rsp, 120);
movl(Address(rsp, 64), tmp);
lea(tmp, ExternalAddress(static_const_table_pow));
movsd(xmm0, Address(rsp, 128));
movsd(xmm1, Address(rsp, 136));

// Special case: pow(x, 2.0) => x * x

This comment has been minimized.

Loading
@TobiHartmann

TobiHartmann Apr 27, 2021
Member

64-bit code also handles Special case: pow(x, 0.5) => sqrt(x). Don't we need to add that as well for consistency?

This comment has been minimized.

Loading
@DamonFool

DamonFool Apr 27, 2021
Author Member

64-bit code also handles Special case: pow(x, 0.5) => sqrt(x). Don't we need to add that as well for consistency?

This is the bug fix for C2's optimization of pow(x, 2.0).

pow(x, 0.5) will be handled in JDK-8265940 .
Thanks.

ucomisd(xmm1, ExternalAddress((address) &DOUBLE2));
jccb(Assembler::notEqual, L_NOT_DOUBLE2);
jccb(Assembler::parity, L_NOT_DOUBLE2);
mulsd(xmm0, xmm0);
jmp(L_2TAG_PACKET_21_0_2);

bind(L_NOT_DOUBLE2);
xorpd(xmm2, xmm2);
movl(eax, 16368);
pinsrw(xmm2, eax, 3);
@@ -1639,12 +1639,10 @@ bool LibraryCallKit::inline_math_pow() {
const TypeD* d = _gvn.type(exp)->isa_double_constant();
if (d != NULL) {
if (d->getd() == 2.0) {
#if !(defined(X86) && !defined(_LP64)) // excludes x86_32
// Special case: pow(x, 2.0) => x * x
Node* base = round_double_node(argument(0));
set_result(_gvn.transform(new MulDNode(base, base)));
return true;
#endif
}
#if defined(X86) && defined(_LP64)
else if (d->getd() == 0.5 && Matcher::match_rule_supported(Op_SqrtD)) {
@@ -25,34 +25,29 @@
* @test
* @bug 8265917
* @summary test the optimization of pow(x, 2.0)
* @run main/othervm TestPow2Opt
* @run main/othervm -Xint TestPow2Opt
* @run main/othervm -Xbatch -XX:TieredStopAtLevel=1 TestPow2Opt
* @run main/othervm -Xbatch -XX:-TieredCompilation TestPow2Opt
*/

public class TestPow2Opt {

static double test(double a) {
return Math.pow(a, 2.0);
static void test(double a) {
double r1 = a * a;
double r2 = Math.pow(a, 2.0);
if (r1 != r2) {
throw new RuntimeException("pow(" + a + ", 2.0), expected: " + r1 + ", actual: " + r2);
}
}

public static void main(String[] args) throws Exception {
// v1: value returned by the interpreter
double v1 = test(1.0 / 2047);

// Warmup
double sum = 0.0;
for (int i = 1; i < 100000; i++) {
sum += test(i + 0.0);
}

// v2: value returned by the compiler
double v2 = test(1.0 / 2047);

if (v1 != v2) {
throw new RuntimeException("v1 should be equal to v2, actual: " + "v1 = " + v1 + " v2 = " + v2);
for (int i = 0; i < 10; i++) {
for (int j = 1; j < 100000; j++) {
test(j * 1.0);
test(1.0 / j);
}
}

System.out.println(sum);
}

}