Skip to content
Permalink
Browse files
8265917: Different values computed by C2 and interpreter/C1 for Math.…
…pow(x, 2.0) on x86_32

Reviewed-by: kvn, thartmann
  • Loading branch information
DamonFool committed Apr 28, 2021
1 parent e144104 commit 889d246681e937af1fa8d97f683c49043ddf2eda
Showing with 66 additions and 0 deletions.
  1. +13 −0 src/hotspot/cpu/x86/macroAssembler_x86_pow.cpp
  2. +53 −0 test/hotspot/jtreg/compiler/intrinsics/math/TestPow2Opt.java
@@ -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
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);
}
}
}

}

1 comment on commit 889d246

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 889d246 Apr 28, 2021

Please sign in to comment.