1026 template<class T> inline T MAX3(T a, T b, T c) { return MAX2(MAX2(a, b), c); } 1027 template<class T> inline T MIN3(T a, T b, T c) { return MIN2(MIN2(a, b), c); } 1028 template<class T> inline T MAX4(T a, T b, T c, T d) { return MAX2(MAX3(a, b, c), d); } 1029 template<class T> inline T MIN4(T a, T b, T c, T d) { return MIN2(MIN3(a, b, c), d); } 1030 1031 template<class T> inline T ABS(T x) { return (x > 0) ? x : -x; } 1032 1033 // true if x is a power of 2, false otherwise 1034 inline bool is_power_of_2(intptr_t x) { 1035 return ((x != NoBits) && (mask_bits(x, x - 1) == NoBits)); 1036 } 1037 1038 // long version of is_power_of_2 1039 inline bool is_power_of_2_long(jlong x) { 1040 return ((x != NoLongBits) && (mask_long_bits(x, x - 1) == NoLongBits)); 1041 } 1042 1043 // Returns largest i such that 2^i <= x. 1044 // If x < 0, the function returns 31 on a 32-bit machine and 63 on a 64-bit machine. 1045 // If x == 0, the function returns -1. 1046 inline int log2_intptr(intptr_t x) { 1047 int i = -1; 1048 uintptr_t p = 1; 1049 while (p != 0 && p <= (uintptr_t)x) { 1050 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) 1051 i++; p *= 2; 1052 } 1053 // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) 1054 // If p = 0, overflow has occurred and i = 31 or i = 63 (depending on the machine word size). 1055 return i; 1056 } 1057 1058 //* largest i such that 2^i <= x 1059 // A negative value of 'x' will return '63' 1060 inline int log2_long(jlong x) { 1061 int i = -1; 1062 julong p = 1; 1063 while (p != 0 && p <= (julong)x) { 1064 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) 1065 i++; p *= 2; 1066 } 1067 // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) 1068 // (if p = 0 then overflow occurred and i = 63) 1069 return i; 1070 } 1071 1072 //* the argument must be exactly a power of 2 1073 inline int exact_log2(intptr_t x) { 1074 assert(is_power_of_2(x), "x must be a power of 2: " INTPTR_FORMAT, x); 1075 return log2_intptr(x); 1076 } 1077 1078 //* the argument must be exactly a power of 2 1079 inline int exact_log2_long(jlong x) { 1080 assert(is_power_of_2_long(x), "x must be a power of 2: " JLONG_FORMAT, x); 1081 return log2_long(x); 1082 } 1083 1084 inline bool is_odd (intx x) { return x & 1; } 1085 inline bool is_even(intx x) { return !is_odd(x); } 1086 1087 // "to" should be greater than "from." 1088 inline intx byte_size(void* from, void* to) { 1089 return (address)to - (address)from; 1090 } 1091 1092 //---------------------------------------------------------------------------------------------------- 1093 // Avoid non-portable casts with these routines (DEPRECATED) 1094 1095 // NOTE: USE Bytes class INSTEAD WHERE POSSIBLE 1096 // Bytes is optimized machine-specifically and may be much faster then the portable routines below. 1097 1098 // Given sequence of four bytes, build into a 32-bit word 1099 // following the conventions used in class files. 1100 // On the 386, this could be realized with a simple address cast. 1101 // 1102 1103 // This routine takes eight bytes: 1104 inline u8 build_u8_from( u1 c1, u1 c2, u1 c3, u1 c4, u1 c5, u1 c6, u1 c7, u1 c8 ) { 1105 return (( u8(c1) << 56 ) & ( u8(0xff) << 56 )) | 1026 template<class T> inline T MAX3(T a, T b, T c) { return MAX2(MAX2(a, b), c); } 1027 template<class T> inline T MIN3(T a, T b, T c) { return MIN2(MIN2(a, b), c); } 1028 template<class T> inline T MAX4(T a, T b, T c, T d) { return MAX2(MAX3(a, b, c), d); } 1029 template<class T> inline T MIN4(T a, T b, T c, T d) { return MIN2(MIN3(a, b, c), d); } 1030 1031 template<class T> inline T ABS(T x) { return (x > 0) ? x : -x; } 1032 1033 // true if x is a power of 2, false otherwise 1034 inline bool is_power_of_2(intptr_t x) { 1035 return ((x != NoBits) && (mask_bits(x, x - 1) == NoBits)); 1036 } 1037 1038 // long version of is_power_of_2 1039 inline bool is_power_of_2_long(jlong x) { 1040 return ((x != NoLongBits) && (mask_long_bits(x, x - 1) == NoLongBits)); 1041 } 1042 1043 // Returns largest i such that 2^i <= x. 1044 // If x < 0, the function returns 31 on a 32-bit machine and 63 on a 64-bit machine. 1045 // If x == 0, the function returns -1. 1046 inline int log2_intptr(uintptr_t x) { 1047 int i = -1; 1048 uintptr_t p = 1; 1049 while (p != 0 && p <= x) { 1050 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) 1051 i++; p *= 2; 1052 } 1053 // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) 1054 // If p = 0, overflow has occurred and i = 31 or i = 63 (depending on the machine word size). 1055 return i; 1056 } 1057 1058 //* largest i such that 2^i <= x 1059 // A negative value of 'x' will return '63' 1060 inline int log2_long(unsigned long x) { 1061 int i = -1; 1062 julong p = 1; 1063 while (p != 0 && p <= x) { 1064 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) 1065 i++; p *= 2; 1066 } 1067 // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) 1068 // (if p = 0 then overflow occurred and i = 63) 1069 return i; 1070 } 1071 1072 inline int log2_intptr(intptr_t x) { 1073 return log2_intptr((uintptr_t)x); 1074 } 1075 1076 inline int log2_intptr(int x) { 1077 return log2_intptr((uintptr_t)x); 1078 } 1079 1080 inline int log2_intptr(uint x) { 1081 return log2_intptr((uintptr_t)x); 1082 } 1083 1084 inline int log2_long(long x) { 1085 return log2_long((unsigned long)x); 1086 } 1087 1088 //* the argument must be exactly a power of 2 1089 inline int exact_log2(intptr_t x) { 1090 assert(is_power_of_2(x), "x must be a power of 2: " INTPTR_FORMAT, x); 1091 return log2_intptr(x); 1092 } 1093 1094 //* the argument must be exactly a power of 2 1095 inline int exact_log2_long(jlong x) { 1096 assert(is_power_of_2_long(x), "x must be a power of 2: " JLONG_FORMAT, x); 1097 return log2_long(x); 1098 } 1099 1100 inline bool is_odd (intx x) { return x & 1; } 1101 inline bool is_even(intx x) { return !is_odd(x); } 1102 1103 // abs methods which cannot overflow and so are well-defined across 1104 // the entire domain of integer types. 1105 static inline unsigned int uabs(unsigned int n) { 1106 union { 1107 unsigned int result; 1108 int value; 1109 }; 1110 result = n; 1111 if (value < 0) result = -result; 1112 return result; 1113 } 1114 static inline unsigned long uabs(unsigned long n) { 1115 union { 1116 unsigned long result; 1117 long value; 1118 }; 1119 result = n; 1120 if (value < 0) result = -result; 1121 return result; 1122 } 1123 static inline unsigned long uabs(long n) { return uabs((unsigned long)n); } 1124 static inline unsigned int uabs(int n) { return uabs((unsigned int)n); } 1125 1126 // "to" should be greater than "from." 1127 inline intx byte_size(void* from, void* to) { 1128 return (address)to - (address)from; 1129 } 1130 1131 //---------------------------------------------------------------------------------------------------- 1132 // Avoid non-portable casts with these routines (DEPRECATED) 1133 1134 // NOTE: USE Bytes class INSTEAD WHERE POSSIBLE 1135 // Bytes is optimized machine-specifically and may be much faster then the portable routines below. 1136 1137 // Given sequence of four bytes, build into a 32-bit word 1138 // following the conventions used in class files. 1139 // On the 386, this could be realized with a simple address cast. 1140 // 1141 1142 // This routine takes eight bytes: 1143 inline u8 build_u8_from( u1 c1, u1 c2, u1 c3, u1 c4, u1 c5, u1 c6, u1 c7, u1 c8 ) { 1144 return (( u8(c1) << 56 ) & ( u8(0xff) << 56 )) |