< prev index next >

src/hotspot/share/utilities/globalDefinitions.hpp

Print this page
rev 56881 : 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   return MIN2(MAX2(value, min), max);
 956 }
 957 
 958 // true if x is a power of 2, false otherwise
 959 inline bool is_power_of_2(intptr_t x) {
 960   return ((x != NoBits) && (mask_bits(x, x - 1) == NoBits));
 961 }
 962 
 963 // long version of is_power_of_2
 964 inline bool is_power_of_2_long(jlong x) {
 965   return ((x != NoLongBits) && (mask_long_bits(x, x - 1) == NoLongBits));
 966 }
 967 
 968 // Returns largest i such that 2^i <= x.
 969 // If x == 0, the function returns -1.
 970 inline int log2_intptr(uintptr_t x) {
 971   int i = -1;
 972   uintptr_t p = 1;
 973   while (p != 0 && p <= x) {
 974     // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x)
 975     i++; p *= 2;
 976   }
 977   // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1))


< prev index next >