< prev index next >

src/hotspot/share/utilities/globalDefinitions.hpp

Print this page

        

*** 1041,1076 **** } // Returns largest i such that 2^i <= x. // If x < 0, the function returns 31 on a 32-bit machine and 63 on a 64-bit machine. // If x == 0, the function returns -1. ! inline int log2_intptr(intptr_t x) { int i = -1; uintptr_t p = 1; ! while (p != 0 && p <= (uintptr_t)x) { // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) i++; p *= 2; } // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) // If p = 0, overflow has occurred and i = 31 or i = 63 (depending on the machine word size). return i; } //* largest i such that 2^i <= x // A negative value of 'x' will return '63' ! inline int log2_long(jlong x) { int i = -1; julong p = 1; ! while (p != 0 && p <= (julong)x) { // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) i++; p *= 2; } // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) // (if p = 0 then overflow occurred and i = 63) return i; } //* the argument must be exactly a power of 2 inline int exact_log2(intptr_t x) { assert(is_power_of_2(x), "x must be a power of 2: " INTPTR_FORMAT, x); return log2_intptr(x); } --- 1041,1092 ---- } // Returns largest i such that 2^i <= x. // If x < 0, the function returns 31 on a 32-bit machine and 63 on a 64-bit machine. // If x == 0, the function returns -1. ! inline int log2_intptr(uintptr_t x) { int i = -1; uintptr_t p = 1; ! while (p != 0 && p <= x) { // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) i++; p *= 2; } // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) // If p = 0, overflow has occurred and i = 31 or i = 63 (depending on the machine word size). return i; } //* largest i such that 2^i <= x // A negative value of 'x' will return '63' ! inline int log2_long(unsigned long x) { int i = -1; julong p = 1; ! while (p != 0 && p <= x) { // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) i++; p *= 2; } // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) // (if p = 0 then overflow occurred and i = 63) return i; } + inline int log2_intptr(intptr_t x) { + return log2_intptr((uintptr_t)x); + } + + inline int log2_intptr(int x) { + return log2_intptr((uintptr_t)x); + } + + inline int log2_intptr(uint x) { + return log2_intptr((uintptr_t)x); + } + + inline int log2_long(long x) { + return log2_long((unsigned long)x); + } + //* the argument must be exactly a power of 2 inline int exact_log2(intptr_t x) { assert(is_power_of_2(x), "x must be a power of 2: " INTPTR_FORMAT, x); return log2_intptr(x); }
*** 1082,1091 **** --- 1098,1130 ---- } inline bool is_odd (intx x) { return x & 1; } inline bool is_even(intx x) { return !is_odd(x); } + // abs methods which cannot overflow and so are well-defined across + // the entire domain of integer types. + static inline unsigned int uabs(unsigned int n) { + union { + unsigned int result; + int value; + }; + result = n; + if (value < 0) result = -result; + return result; + } + static inline unsigned long uabs(unsigned long n) { + union { + unsigned long result; + long value; + }; + result = n; + if (value < 0) result = -result; + return result; + } + static inline unsigned long uabs(long n) { return uabs((unsigned long)n); } + static inline unsigned int uabs(int n) { return uabs((unsigned int)n); } + // "to" should be greater than "from." inline intx byte_size(void* from, void* to) { return (address)to - (address)from; }
< prev index next >