changeset 10570:f2265fd2eada

8150617: nth_bit and friends are broken Reviewed-by: shade, tschatzl, vlivanov
author stefank
date Thu, 25 Feb 2016 13:08:19 +0100
parents 7d5870c06812
children 8d5487c9746d
files src/share/vm/utilities/globalDefinitions.cpp src/share/vm/utilities/globalDefinitions.hpp
diffstat 2 files changed, 16 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/vm/utilities/globalDefinitions.cpp	Wed Mar 09 15:37:33 2016 -0800
+++ b/src/share/vm/utilities/globalDefinitions.cpp	Thu Feb 25 13:08:19 2016 +0100
@@ -358,6 +358,20 @@
     return size_t(result);
 }
 
+
+// Test that nth_bit macro and friends behave as
+// expected, even with low-precedence operators.
+
+STATIC_ASSERT(nth_bit(3)   == 0x8);
+STATIC_ASSERT(nth_bit(1|2) == 0x8);
+
+STATIC_ASSERT(right_n_bits(3)   == 0x7);
+STATIC_ASSERT(right_n_bits(1|2) == 0x7);
+
+STATIC_ASSERT(left_n_bits(3)   == (intptr_t) LP64_ONLY(0xE000000000000000) NOT_LP64(0xE0000000));
+STATIC_ASSERT(left_n_bits(1|2) == (intptr_t) LP64_ONLY(0xE000000000000000) NOT_LP64(0xE0000000));
+
+
 #ifndef PRODUCT
 // For unit testing only
 class GlobalDefinitions {
--- a/src/share/vm/utilities/globalDefinitions.hpp	Wed Mar 09 15:37:33 2016 -0800
+++ b/src/share/vm/utilities/globalDefinitions.hpp	Thu Feb 25 13:08:19 2016 +0100
@@ -1083,9 +1083,9 @@
 
 // get a word with the n.th or the right-most or left-most n bits set
 // (note: #define used only so that they can be used in enum constant definitions)
-#define nth_bit(n)        (n >= BitsPerWord ? 0 : OneBit << (n))
+#define nth_bit(n)        (((n) >= BitsPerWord) ? 0 : (OneBit << (n)))
 #define right_n_bits(n)   (nth_bit(n) - 1)
-#define left_n_bits(n)    (right_n_bits(n) << (n >= BitsPerWord ? 0 : (BitsPerWord - n)))
+#define left_n_bits(n)    (right_n_bits(n) << (((n) >= BitsPerWord) ? 0 : (BitsPerWord - (n))))
 
 // bit-operations using a mask m
 inline void   set_bits    (intptr_t& x, intptr_t m) { x |= m; }