1.1 --- a/src/share/vm/utilities/globalDefinitions.hpp Wed Jul 03 20:04:13 2019 +0800 1.2 +++ b/src/share/vm/utilities/globalDefinitions.hpp Wed Jul 03 20:42:37 2019 +0800 1.3 @@ -1145,10 +1145,10 @@ 1.4 1.5 //* largest i such that 2^i <= x 1.6 // A negative value of 'x' will return '31' 1.7 -inline int log2_intptr(intptr_t x) { 1.8 +inline int log2_intptr(uintptr_t x) { 1.9 int i = -1; 1.10 uintptr_t p = 1; 1.11 - while (p != 0 && p <= (uintptr_t)x) { 1.12 + while (p != 0 && p <= x) { 1.13 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) 1.14 i++; p *= 2; 1.15 } 1.16 @@ -1158,11 +1158,10 @@ 1.17 } 1.18 1.19 //* largest i such that 2^i <= x 1.20 -// A negative value of 'x' will return '63' 1.21 -inline int log2_long(jlong x) { 1.22 +inline int log2_long(julong x) { 1.23 int i = -1; 1.24 julong p = 1; 1.25 - while (p != 0 && p <= (julong)x) { 1.26 + while (p != 0 && p <= x) { 1.27 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) 1.28 i++; p *= 2; 1.29 } 1.30 @@ -1171,6 +1170,27 @@ 1.31 return i; 1.32 } 1.33 1.34 +inline int log2_intptr(intptr_t x) { 1.35 + return log2_intptr((uintptr_t)x); 1.36 +} 1.37 + 1.38 +inline int log2_int(int x) { 1.39 + return log2_intptr((uintptr_t)x); 1.40 +} 1.41 + 1.42 +inline int log2_jint(jint x) { 1.43 + return log2_intptr((uintptr_t)x); 1.44 +} 1.45 + 1.46 +inline int log2_uint(uint x) { 1.47 + return log2_intptr((uintptr_t)x); 1.48 +} 1.49 + 1.50 +// A negative value of 'x' will return '63' 1.51 +inline int log2_jlong(jlong x) { 1.52 + return log2_long((julong)x); 1.53 +} 1.54 + 1.55 //* the argument must be exactly a power of 2 1.56 inline int exact_log2(intptr_t x) { 1.57 #ifdef ASSERT 1.58 @@ -1210,6 +1230,29 @@ 1.59 inline bool is_odd (intx x) { return x & 1; } 1.60 inline bool is_even(intx x) { return !is_odd(x); } 1.61 1.62 +// abs methods which cannot overflow and so are well-defined across 1.63 +// the entire domain of integer types. 1.64 +static inline unsigned int uabs(unsigned int n) { 1.65 + union { 1.66 + unsigned int result; 1.67 + int value; 1.68 + }; 1.69 + result = n; 1.70 + if (value < 0) result = 0-result; 1.71 + return result; 1.72 +} 1.73 +static inline julong uabs(julong n) { 1.74 + union { 1.75 + julong result; 1.76 + jlong value; 1.77 + }; 1.78 + result = n; 1.79 + if (value < 0) result = 0-result; 1.80 + return result; 1.81 +} 1.82 +static inline julong uabs(jlong n) { return uabs((julong)n); } 1.83 +static inline unsigned int uabs(int n) { return uabs((unsigned int)n); } 1.84 + 1.85 // "to" should be greater than "from." 1.86 inline intx byte_size(void* from, void* to) { 1.87 return (address)to - (address)from; 1.88 @@ -1412,6 +1455,32 @@ 1.89 1.90 #define ARRAY_SIZE(array) (sizeof(array)/sizeof((array)[0])) 1.91 1.92 +//---------------------------------------------------------------------------------------------------- 1.93 +// Sum and product which can never overflow: they wrap, just like the 1.94 +// Java operations. Note that we don't intend these to be used for 1.95 +// general-purpose arithmetic: their purpose is to emulate Java 1.96 +// operations. 1.97 + 1.98 +// The goal of this code to avoid undefined or implementation-defined 1.99 +// behaviour. The use of an lvalue to reference cast is explicitly 1.100 +// permitted by Lvalues and rvalues [basic.lval]. [Section 3.10 Para 1.101 +// 15 in C++03] 1.102 +#define JAVA_INTEGER_OP(OP, NAME, TYPE, UNSIGNED_TYPE) \ 1.103 +inline TYPE NAME (TYPE in1, TYPE in2) { \ 1.104 + UNSIGNED_TYPE ures = static_cast<UNSIGNED_TYPE>(in1); \ 1.105 + ures OP ## = static_cast<UNSIGNED_TYPE>(in2); \ 1.106 + return reinterpret_cast<TYPE&>(ures); \ 1.107 +} 1.108 + 1.109 +JAVA_INTEGER_OP(+, java_add, jint, juint) 1.110 +JAVA_INTEGER_OP(-, java_subtract, jint, juint) 1.111 +JAVA_INTEGER_OP(*, java_multiply, jint, juint) 1.112 +JAVA_INTEGER_OP(+, java_add, jlong, julong) 1.113 +JAVA_INTEGER_OP(-, java_subtract, jlong, julong) 1.114 +JAVA_INTEGER_OP(*, java_multiply, jlong, julong) 1.115 + 1.116 +#undef JAVA_INTEGER_OP 1.117 + 1.118 // Dereference vptr 1.119 // All C++ compilers that we know of have the vtbl pointer in the first 1.120 // word. If there are exceptions, this function needs to be made compiler