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 all commits
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
@@ -2515,6 +2515,8 @@ ATTRIBUTE_ALIGNED(16) juint _static_const_table_pow[] =

};

ATTRIBUTE_ALIGNED(8) double _DOUBLE2 = 2.0;

//registers,
// input: xmm0, xmm1
// scratch: xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7
@@ -2538,17 +2540,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;
address DOUBLE2 = (address) &_DOUBLE2;

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(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);
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. 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
* @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 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 {
for (int i = 0; i < 10; i++) {
for (int j = 1; j < 100000; j++) {
test(j * 1.0);
test(1.0 / j);
}
}
}

}