1119 template<class T> inline T MIN2(T a, T b) { return (a < b) ? a : b; } 1120 template<class T> inline T MAX3(T a, T b, T c) { return MAX2(MAX2(a, b), c); } 1121 template<class T> inline T MIN3(T a, T b, T c) { return MIN2(MIN2(a, b), c); } 1122 template<class T> inline T MAX4(T a, T b, T c, T d) { return MAX2(MAX3(a, b, c), d); } 1123 template<class T> inline T MIN4(T a, T b, T c, T d) { return MIN2(MIN3(a, b, c), d); } 1124 1125 template<class T> inline T ABS(T x) { return (x > 0) ? x : -x; } 1126 1127 // true if x is a power of 2, false otherwise 1128 inline bool is_power_of_2(intptr_t x) { 1129 return ((x != NoBits) && (mask_bits(x, x - 1) == NoBits)); 1130 } 1131 1132 // long version of is_power_of_2 1133 inline bool is_power_of_2_long(jlong x) { 1134 return ((x != NoLongBits) && (mask_long_bits(x, x - 1) == NoLongBits)); 1135 } 1136 1137 //* largest i such that 2^i <= x 1138 // A negative value of 'x' will return '31' 1139 inline int log2_intptr(intptr_t x) { 1140 int i = -1; 1141 uintptr_t p = 1; 1142 while (p != 0 && p <= (uintptr_t)x) { 1143 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) 1144 i++; p *= 2; 1145 } 1146 // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) 1147 // (if p = 0 then overflow occurred and i = 31) 1148 return i; 1149 } 1150 1151 //* largest i such that 2^i <= x 1152 // A negative value of 'x' will return '63' 1153 inline int log2_long(jlong x) { 1154 int i = -1; 1155 julong p = 1; 1156 while (p != 0 && p <= (julong)x) { 1157 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) 1158 i++; p *= 2; 1159 } 1160 // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) 1161 // (if p = 0 then overflow occurred and i = 63) 1162 return i; 1163 } 1164 1165 //* the argument must be exactly a power of 2 1166 inline int exact_log2(intptr_t x) { 1167 #ifdef ASSERT 1168 if (!is_power_of_2(x)) basic_fatal("x must be a power of 2"); 1169 #endif 1170 return log2_intptr(x); 1171 } 1172 1173 //* the argument must be exactly a power of 2 1174 inline int exact_log2_long(jlong x) { 1175 #ifdef ASSERT 1176 if (!is_power_of_2_long(x)) basic_fatal("x must be a power of 2"); 1177 #endif 1178 return log2_long(x); 1179 } 1180 1181 1182 // returns integer round-up to the nearest multiple of s (s must be a power of two) 1183 inline intptr_t round_to(intptr_t x, uintx s) { 1184 #ifdef ASSERT 1185 if (!is_power_of_2(s)) basic_fatal("s must be a power of 2"); 1186 #endif 1187 const uintx m = s - 1; 1188 return mask_bits(x + m, ~m); 1189 } 1190 1191 // returns integer round-down to the nearest multiple of s (s must be a power of two) 1192 inline intptr_t round_down(intptr_t x, uintx s) { 1193 #ifdef ASSERT 1194 if (!is_power_of_2(s)) basic_fatal("s must be a power of 2"); 1195 #endif 1196 const uintx m = s - 1; 1197 return mask_bits(x, ~m); 1198 } 1199 1200 1201 inline bool is_odd (intx x) { return x & 1; } 1202 inline bool is_even(intx x) { return !is_odd(x); } 1203 1204 // "to" should be greater than "from." 1205 inline intx byte_size(void* from, void* to) { 1206 return (address)to - (address)from; 1207 } 1208 1209 //---------------------------------------------------------------------------------------------------- 1210 // Avoid non-portable casts with these routines (DEPRECATED) 1211 1212 // NOTE: USE Bytes class INSTEAD WHERE POSSIBLE 1213 // Bytes is optimized machine-specifically and may be much faster then the portable routines below. 1214 1215 // Given sequence of four bytes, build into a 32-bit word 1216 // following the conventions used in class files. 1217 // On the 386, this could be realized with a simple address cast. 1218 // 1219 1220 // This routine takes eight bytes: 1221 inline u8 build_u8_from( u1 c1, u1 c2, u1 c3, u1 c4, u1 c5, u1 c6, u1 c7, u1 c8 ) { 1222 return (( u8(c1) << 56 ) & ( u8(0xff) << 56 )) | 1119 template<class T> inline T MIN2(T a, T b) { return (a < b) ? a : b; } 1120 template<class T> inline T MAX3(T a, T b, T c) { return MAX2(MAX2(a, b), c); } 1121 template<class T> inline T MIN3(T a, T b, T c) { return MIN2(MIN2(a, b), c); } 1122 template<class T> inline T MAX4(T a, T b, T c, T d) { return MAX2(MAX3(a, b, c), d); } 1123 template<class T> inline T MIN4(T a, T b, T c, T d) { return MIN2(MIN3(a, b, c), d); } 1124 1125 template<class T> inline T ABS(T x) { return (x > 0) ? x : -x; } 1126 1127 // true if x is a power of 2, false otherwise 1128 inline bool is_power_of_2(intptr_t x) { 1129 return ((x != NoBits) && (mask_bits(x, x - 1) == NoBits)); 1130 } 1131 1132 // long version of is_power_of_2 1133 inline bool is_power_of_2_long(jlong x) { 1134 return ((x != NoLongBits) && (mask_long_bits(x, x - 1) == NoLongBits)); 1135 } 1136 1137 //* largest i such that 2^i <= x 1138 // A negative value of 'x' will return '31' 1139 inline int log2_intptr(uintptr_t x) { 1140 int i = -1; 1141 uintptr_t p = 1; 1142 while (p != 0 && p <= x) { 1143 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) 1144 i++; p *= 2; 1145 } 1146 // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) 1147 // (if p = 0 then overflow occurred and i = 31) 1148 return i; 1149 } 1150 1151 //* largest i such that 2^i <= x 1152 // A negative value of 'x' will return '63' 1153 inline int log2_long(unsigned long x) { 1154 int i = -1; 1155 julong p = 1; 1156 while (p != 0 && p <= x) { 1157 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) 1158 i++; p *= 2; 1159 } 1160 // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) 1161 // (if p = 0 then overflow occurred and i = 63) 1162 return i; 1163 } 1164 1165 inline int log2_intptr(intptr_t x) { 1166 return log2_intptr((uintptr_t)x); 1167 } 1168 1169 inline int log2_intptr(int x) { 1170 return log2_intptr((uintptr_t)x); 1171 } 1172 1173 inline int log2_intptr(uint x) { 1174 return log2_intptr((uintptr_t)x); 1175 } 1176 1177 inline int log2_long(jlong x) { 1178 return log2_long((unsigned long)x); 1179 } 1180 1181 //* the argument must be exactly a power of 2 1182 inline int exact_log2(intptr_t x) { 1183 #ifdef ASSERT 1184 if (!is_power_of_2(x)) basic_fatal("x must be a power of 2"); 1185 #endif 1186 return log2_intptr(x); 1187 } 1188 1189 //* the argument must be exactly a power of 2 1190 inline int exact_log2_long(jlong x) { 1191 #ifdef ASSERT 1192 if (!is_power_of_2_long(x)) basic_fatal("x must be a power of 2"); 1193 #endif 1194 return log2_long(x); 1195 } 1196 1197 1198 // returns integer round-up to the nearest multiple of s (s must be a power of two) 1199 inline intptr_t round_to(intptr_t x, uintx s) { 1200 #ifdef ASSERT 1201 if (!is_power_of_2(s)) basic_fatal("s must be a power of 2"); 1202 #endif 1203 const uintx m = s - 1; 1204 return mask_bits(x + m, ~m); 1205 } 1206 1207 // returns integer round-down to the nearest multiple of s (s must be a power of two) 1208 inline intptr_t round_down(intptr_t x, uintx s) { 1209 #ifdef ASSERT 1210 if (!is_power_of_2(s)) basic_fatal("s must be a power of 2"); 1211 #endif 1212 const uintx m = s - 1; 1213 return mask_bits(x, ~m); 1214 } 1215 1216 1217 inline bool is_odd (intx x) { return x & 1; } 1218 inline bool is_even(intx x) { return !is_odd(x); } 1219 1220 // abs methods which cannot overflow and so are well-defined across 1221 // the entire domain of integer types. 1222 static inline unsigned int uabs(unsigned int n) { 1223 union { 1224 unsigned int result; 1225 int value; 1226 }; 1227 result = n; 1228 if (value < 0) result = 0-result; 1229 return result; 1230 } 1231 static inline unsigned long uabs(unsigned long n) { 1232 union { 1233 unsigned long result; 1234 long value; 1235 }; 1236 result = n; 1237 if (value < 0) result = 0-result; 1238 return result; 1239 } 1240 static inline unsigned long uabs(jlong n) { return uabs((unsigned long)n); } 1241 static inline unsigned int uabs(int n) { return uabs((unsigned int)n); } 1242 1243 // "to" should be greater than "from." 1244 inline intx byte_size(void* from, void* to) { 1245 return (address)to - (address)from; 1246 } 1247 1248 //---------------------------------------------------------------------------------------------------- 1249 // Avoid non-portable casts with these routines (DEPRECATED) 1250 1251 // NOTE: USE Bytes class INSTEAD WHERE POSSIBLE 1252 // Bytes is optimized machine-specifically and may be much faster then the portable routines below. 1253 1254 // Given sequence of four bytes, build into a 32-bit word 1255 // following the conventions used in class files. 1256 // On the 386, this could be realized with a simple address cast. 1257 // 1258 1259 // This routine takes eight bytes: 1260 inline u8 build_u8_from( u1 c1, u1 c2, u1 c3, u1 c4, u1 c5, u1 c6, u1 c7, u1 c8 ) { 1261 return (( u8(c1) << 56 ) & ( u8(0xff) << 56 )) |