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) | 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 inline int log2_long(julong x) { 1153 int i = -1; 1154 julong p = 1; 1155 while (p != 0 && p <= x) { 1156 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) 1157 i++; p *= 2; 1158 } 1159 // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) 1160 // (if p = 0 then overflow occurred and i = 63) 1161 return i; 1162 } 1163 1164 inline int log2_intptr(intptr_t x) { 1165 return log2_intptr((uintptr_t)x); 1166 } 1167 1168 inline int log2_int(int x) { 1169 return log2_intptr((uintptr_t)x); 1170 } 1171 1172 inline int log2_jint(jint x) { 1173 return log2_intptr((uintptr_t)x); 1174 } 1175 1176 inline int log2_uint(uint x) { 1177 return log2_intptr((uintptr_t)x); 1178 } 1179 1180 // A negative value of 'x' will return '63' 1181 inline int log2_jlong(jlong x) { 1182 return log2_long((julong)x); 1183 } 1184 1185 //* the argument must be exactly a power of 2 1186 inline int exact_log2(intptr_t x) { 1187 #ifdef ASSERT 1188 if (!is_power_of_2(x)) basic_fatal("x must be a power of 2"); 1189 #endif 1190 return log2_intptr(x); 1191 } 1192 1193 //* the argument must be exactly a power of 2 1194 inline int exact_log2_long(jlong x) { 1195 #ifdef ASSERT 1196 if (!is_power_of_2_long(x)) basic_fatal("x must be a power of 2"); 1197 #endif 1198 return log2_long(x); 1199 } 1200 1201 1202 // returns integer round-up to the nearest multiple of s (s must be a power of two) |