< prev index next >

src/hotspot/share/utilities/globalDefinitions.hpp

Print this page
rev 56944 : imported patch 8233702-function-to-clamp-value-to-range


 932 #endif
 933 
 934 #ifdef min
 935 #undef min
 936 #endif
 937 
 938 // It is necessary to use templates here. Having normal overloaded
 939 // functions does not work because it is necessary to provide both 32-
 940 // and 64-bit overloaded functions, which does not work, and having
 941 // explicitly-typed versions of these routines (i.e., MAX2I, MAX2L)
 942 // will be even more error-prone than macros.
 943 template<class T> inline T MAX2(T a, T b)           { return (a > b) ? a : b; }
 944 template<class T> inline T MIN2(T a, T b)           { return (a < b) ? a : b; }
 945 template<class T> inline T MAX3(T a, T b, T c)      { return MAX2(MAX2(a, b), c); }
 946 template<class T> inline T MIN3(T a, T b, T c)      { return MIN2(MIN2(a, b), c); }
 947 template<class T> inline T MAX4(T a, T b, T c, T d) { return MAX2(MAX3(a, b, c), d); }
 948 template<class T> inline T MIN4(T a, T b, T c, T d) { return MIN2(MIN3(a, b, c), d); }
 949 
 950 template<class T> inline T ABS(T x)                 { return (x > 0) ? x : -x; }
 951 







 952 // true if x is a power of 2, false otherwise
 953 inline bool is_power_of_2(intptr_t x) {
 954   return ((x != NoBits) && (mask_bits(x, x - 1) == NoBits));
 955 }
 956 
 957 // long version of is_power_of_2
 958 inline bool is_power_of_2_long(jlong x) {
 959   return ((x != NoLongBits) && (mask_long_bits(x, x - 1) == NoLongBits));
 960 }
 961 
 962 // Returns largest i such that 2^i <= x.
 963 // If x == 0, the function returns -1.
 964 inline int log2_intptr(uintptr_t x) {
 965   int i = -1;
 966   uintptr_t p = 1;
 967   while (p != 0 && p <= x) {
 968     // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x)
 969     i++; p *= 2;
 970   }
 971   // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1))




 932 #endif
 933 
 934 #ifdef min
 935 #undef min
 936 #endif
 937 
 938 // It is necessary to use templates here. Having normal overloaded
 939 // functions does not work because it is necessary to provide both 32-
 940 // and 64-bit overloaded functions, which does not work, and having
 941 // explicitly-typed versions of these routines (i.e., MAX2I, MAX2L)
 942 // will be even more error-prone than macros.
 943 template<class T> inline T MAX2(T a, T b)           { return (a > b) ? a : b; }
 944 template<class T> inline T MIN2(T a, T b)           { return (a < b) ? a : b; }
 945 template<class T> inline T MAX3(T a, T b, T c)      { return MAX2(MAX2(a, b), c); }
 946 template<class T> inline T MIN3(T a, T b, T c)      { return MIN2(MIN2(a, b), c); }
 947 template<class T> inline T MAX4(T a, T b, T c, T d) { return MAX2(MAX3(a, b, c), d); }
 948 template<class T> inline T MIN4(T a, T b, T c, T d) { return MIN2(MIN3(a, b, c), d); }
 949 
 950 template<class T> inline T ABS(T x)                 { return (x > 0) ? x : -x; }
 951 
 952 // Return the given value clamped to the range [min ... max]
 953 template<typename T>
 954 inline T clamp(T value, T min, T max) {
 955   assert(min <= max, "must be");
 956   return MIN2(MAX2(value, min), max);
 957 }
 958 
 959 // true if x is a power of 2, false otherwise
 960 inline bool is_power_of_2(intptr_t x) {
 961   return ((x != NoBits) && (mask_bits(x, x - 1) == NoBits));
 962 }
 963 
 964 // long version of is_power_of_2
 965 inline bool is_power_of_2_long(jlong x) {
 966   return ((x != NoLongBits) && (mask_long_bits(x, x - 1) == NoLongBits));
 967 }
 968 
 969 // Returns largest i such that 2^i <= x.
 970 // If x == 0, the function returns -1.
 971 inline int log2_intptr(uintptr_t x) {
 972   int i = -1;
 973   uintptr_t p = 1;
 974   while (p != 0 && p <= x) {
 975     // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x)
 976     i++; p *= 2;
 977   }
 978   // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1))


< prev index next >