diff --git a/src/hotspot/share/code/compressedStream.cpp b/src/hotspot/share/code/compressedStream.cpp index 5a21bc234e6..47eb2969c40 100644 --- a/src/hotspot/share/code/compressedStream.cpp +++ b/src/hotspot/share/code/compressedStream.cpp @@ -25,16 +25,12 @@ #include "precompiled.hpp" #include "code/compressedStream.hpp" #include "utilities/ostream.hpp" +#include "utilities/moveBits.hpp" // 32-bit self-inverse encoding of float bits // converts trailing zeroes (common in floats) to leading zeroes inline juint CompressedStream::reverse_int(juint i) { - // Hacker's Delight, Figure 7-1 - i = (i & 0x55555555) << 1 | ((i >> 1) & 0x55555555); - i = (i & 0x33333333) << 2 | ((i >> 2) & 0x33333333); - i = (i & 0x0f0f0f0f) << 4 | ((i >> 4) & 0x0f0f0f0f); - i = (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24); - return i; + return reverse_bits(i); } jint CompressedReadStream::read_signed_int() { diff --git a/src/hotspot/share/opto/subnode.cpp b/src/hotspot/share/opto/subnode.cpp index a687ea2b6cb..292717bdd1a 100644 --- a/src/hotspot/share/opto/subnode.cpp +++ b/src/hotspot/share/opto/subnode.cpp @@ -38,6 +38,7 @@ #include "opto/phaseX.hpp" #include "opto/subnode.hpp" #include "runtime/sharedRuntime.hpp" +#include "utilities/moveBits.hpp" // Portions of code courtesy of Clifford Click @@ -1900,13 +1901,6 @@ const Type* SqrtFNode::Value(PhaseGVN* phase) const { return TypeF::make( (float)sqrt( (double)f ) ); } -static jlong reverse_bits(jlong val) { - jlong res = ((val & 0xF0F0F0F0F0F0F0F0L) >> 4) | ((val & 0x0F0F0F0F0F0F0F0F) << 4); - res = ((res & 0xCCCCCCCCCCCCCCCCL) >> 2) | ((res & 0x3333333333333333L) << 2); - res = ((res & 0xAAAAAAAAAAAAAAAAL) >> 1) | ((res & 0x5555555555555555L) << 1); - return res; -} - const Type* ReverseINode::Value(PhaseGVN* phase) const { const Type *t1 = phase->type( in(1) ); if (t1 == Type::TOP) { @@ -1917,7 +1911,7 @@ const Type* ReverseINode::Value(PhaseGVN* phase) const { jint res = reverse_bits(t1int->get_con()); return TypeInt::make(res); } - return t1int; + return bottom_type(); } const Type* ReverseLNode::Value(PhaseGVN* phase) const { @@ -1927,10 +1921,10 @@ const Type* ReverseLNode::Value(PhaseGVN* phase) const { } const TypeLong* t1long = t1->isa_long(); if (t1long && t1long->is_con()) { - jint res = reverse_bits(t1long->get_con()); + jlong res = reverse_bits(t1long->get_con()); return TypeLong::make(res); } - return t1long; + return bottom_type(); } Node* ReverseINode::Identity(PhaseGVN* phase) { diff --git a/src/hotspot/share/utilities/moveBits.hpp b/src/hotspot/share/utilities/moveBits.hpp new file mode 100644 index 00000000000..35d30c0ddf9 --- /dev/null +++ b/src/hotspot/share/utilities/moveBits.hpp @@ -0,0 +1,99 @@ +/* + * 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. + * + */ + +#ifndef SHARE_UTILITIES_MOVEBITS_HPP +#define SHARE_UTILITIES_MOVEBITS_HPP + +#include "metaprogramming/conditional.hpp" +#include "metaprogramming/enableIf.hpp" +#include "utilities/globalDefinitions.hpp" +#include + +template +class ReverseBitsImpl { + static const size_t NB = sizeof(T) * BitsPerByte; + + static_assert((NB == 8) || (NB == 16) || (NB == 32) || (NB == 64), + "unsupported size"); + + // The unsigned integral type for calculations. + using I = typename Conditional::type; + + static const I rep_5555 = static_cast(UCONST64(0x5555555555555555)); + static const I rep_3333 = static_cast(UCONST64(0x3333333333333333)); + static const I rep_0F0F = static_cast(UCONST64(0x0F0F0F0F0F0F0F0F)); + static const I rep_00FF = static_cast(UCONST64(0x00FF00FF00FF00FF)); + static const I rep_FFFF = static_cast(UCONST64(0x0000FFFF0000FFFF)); + +public: + + static constexpr T reverse_bits_in_bytes(T v) { + // Based on Hacker's Delight Section 7-1 + auto x = static_cast(v); + x = ((x & rep_5555) << 1) | ((x >> 1) & rep_5555); + x = ((x & rep_3333) << 2) | ((x >> 2) & rep_3333); + x = ((x & rep_0F0F) << 4) | ((x >> 4) & rep_0F0F); + return static_cast(x); + } + + static constexpr T reverse_bytes(T v) { + // Based on Hacker's Delight Section 7-1 + // NB: Compilers are good at recognizing byte-swap code and transforming + // it into platform-specific instructions like x86 bswap. + auto x = static_cast(v); + switch (NB) { + case 64: + // The use of NB/2 rather than 32 avoids a warning in dead code when + // I is uint32_t, because shifting a 32bit type by 32 is UB. + x = (x << (NB/2)) | (x >> (NB/2)); + case 32: // fallthrough + x = ((x & rep_FFFF) << 16) | ((x >> 16) & rep_FFFF); + case 16: // fallthrough + x = ((x & rep_00FF) << 8) | ((x >> 8) & rep_00FF); + default: // fallthrough + return static_cast(x); + } + } +}; + +// Performs byte reversal of an integral type up to 64 bits. +template ::value)> +constexpr T reverse_bytes(T x) { + return ReverseBitsImpl::reverse_bytes(x); +} + +// Performs bytewise bit reversal of each byte of an integral +// type up to 64 bits. +template ::value)> +constexpr T reverse_bits_in_bytes(T x) { + return ReverseBitsImpl::reverse_bits_in_bytes(x); +} + +// Performs full bit reversal an integral type up to 64 bits. +template ::value)> +constexpr T reverse_bits(T x) { + return reverse_bytes(reverse_bits_in_bytes(x)); +} + +#endif // SHARE_UTILITIES_MOVEBITS_HPP diff --git a/test/hotspot/gtest/opto/test_moveBits.cpp b/test/hotspot/gtest/opto/test_moveBits.cpp new file mode 100644 index 00000000000..d25f18ddb68 --- /dev/null +++ b/test/hotspot/gtest/opto/test_moveBits.cpp @@ -0,0 +1,113 @@ +/* + * 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. + * + */ + +#include "precompiled.hpp" +#include "utilities/globalDefinitions.hpp" +#include "utilities/moveBits.hpp" +#include "unittest.hpp" + +template +inline void test_moveBits() { + const int NBIT = sizeof(T) * 8; + const bool IS_U = (T)-1 > 0; + const int XOR_REV_BITS = (NBIT - 1); + const int XOR_REV_BITS_IN_BYTES = 7; // only flip position in byte + const int XOR_REV_BYTES = XOR_REV_BITS ^ XOR_REV_BITS_IN_BYTES; + printf("testing %sint%d_t...\n", IS_U ? "u" : "", NBIT); + ASSERT_EQ(reverse_bits((T)0), (T)0); + ASSERT_EQ(reverse_bits((T)-1), (T)-1); + ASSERT_EQ(reverse_bytes((T)0), (T)0); + ASSERT_EQ(reverse_bytes((T)-1), (T)-1); + ASSERT_EQ(reverse_bits_in_bytes((T)0), (T)0); + ASSERT_EQ(reverse_bits_in_bytes((T)-1), (T)-1); + for (int i1 = 0; i1 < NBIT; i1++) { + T mask1 = (T)1 << i1; + T revm1 = (T)1 << (i1 ^ XOR_REV_BITS); + T rbym1 = (T)1 << (i1 ^ XOR_REV_BYTES); + T ribm1 = (T)1 << (i1 ^ XOR_REV_BITS_IN_BYTES); + for (int i2 = 0; i2 <= i1; i2++) { + T mask2 = (T)1 << i2; + T revm2 = (T)1 << (i2 ^ XOR_REV_BITS); + T rbym2 = (T)1 << (i2 ^ XOR_REV_BYTES); + T ribm2 = (T)1 << (i2 ^ XOR_REV_BITS_IN_BYTES); + T mask = mask1|mask2; +#define STUFF (IS_U?"u":"s") << NBIT << "@" << i1 << "," << i2 + ASSERT_EQ(reverse_bits(mask), revm1|revm2) << STUFF; + ASSERT_EQ((T)~reverse_bits((T)~mask), revm1|revm2) << STUFF; + ASSERT_EQ(reverse_bytes(mask), rbym1|rbym2) << STUFF; + ASSERT_EQ((T)~reverse_bytes((T)~mask), rbym1|rbym2) << STUFF; + ASSERT_EQ(reverse_bits_in_bytes(mask), ribm1|ribm2) << STUFF; + ASSERT_EQ((T)~reverse_bits_in_bytes((T)~mask), ribm1|ribm2) << STUFF; + } + } +} + +TEST_VM(opto, moveBits) { + test_moveBits(); + test_moveBits(); + test_moveBits(); + test_moveBits(); + test_moveBits(); + test_moveBits(); + test_moveBits(); + test_moveBits(); +} + +// Here is some object code to look at if we want to do a manual +// study. One could find the build file named test_moveBits.o.cmdline +// and hand-edit the command line to produce assembly code in +// test_moveBits.s. +// +// Or, given the two empty "fence functions", one could do a +// quick scan like this: +// +// $ objdump -D $(find build/*release -name test_moveBits.o) \ +// | sed -n '/start_code_quality/,$p;/end_code_quality/q' \ +// | egrep -B10 bswap # or grep -B20 cfi_endproc + +void start_code_quality_moveBits() { } + +int32_t code_quality_reverse_bits_32(int32_t x) { + return reverse_bits(x); +} + +int32_t code_quality_reverse_bytes_32(int32_t x) { + return reverse_bytes(x); +} + +int32_t code_quality_reverse_bits_in_bytes_32(int32_t x) { + return reverse_bits_in_bytes(x); +} + +int64_t code_quality_reverse_bits_64(int64_t x) { + return reverse_bits(x); +} + +int64_t code_quality_reverse_bytes_64(int64_t x) { + return reverse_bytes(x); +} + +int64_t code_quality_reverse_bits_in_bytes_64(int64_t x) { + return reverse_bits_in_bytes(x); +} diff --git a/test/jdk/ProblemList-Xcomp.txt b/test/jdk/ProblemList-Xcomp.txt index 7569ca406e9..df1b9da12b0 100644 --- a/test/jdk/ProblemList-Xcomp.txt +++ b/test/jdk/ProblemList-Xcomp.txt @@ -29,8 +29,3 @@ java/lang/invoke/MethodHandles/CatchExceptionTest.java 8146623 generic-all java/lang/ref/ReferenceEnqueue.java 8284236 generic-all - -java/lang/Integer/BitTwiddle.java 8291649 generic-x64 -java/lang/Long/BitTwiddle.java 8291649 generic-x64 -java/util/zip/TestCRC32C.java 8291649 generic-x64 -java/util/zip/TestChecksum.java 8291649 generic-x64