1047 } 1048 1049 //* largest i such that 2^i <= x 1050 // A negative value of 'x' will return '63' 1051 inline int log2_long(unsigned long x) { 1052 int i = -1; 1053 julong p = 1; 1054 while (p != 0 && p <= x) { 1055 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) 1056 i++; p *= 2; 1057 } 1058 // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) 1059 // (if p = 0 then overflow occurred and i = 63) 1060 return i; 1061 } 1062 1063 inline int log2_intptr(intptr_t x) { 1064 return log2_intptr((uintptr_t)x); 1065 } 1066 1067 inline int log2_intptr(int x) { 1068 return log2_intptr((uintptr_t)x); 1069 } 1070 1071 inline int log2_intptr(uint x) { 1072 return log2_intptr((uintptr_t)x); 1073 } 1074 1075 inline int log2_long(jlong x) { 1076 return log2_long((unsigned long)x); 1077 } 1078 1079 //* the argument must be exactly a power of 2 1080 inline int exact_log2(intptr_t x) { 1081 assert(is_power_of_2(x), "x must be a power of 2: " INTPTR_FORMAT, x); 1082 return log2_intptr(x); 1083 } 1084 1085 //* the argument must be exactly a power of 2 1086 inline int exact_log2_long(jlong x) { 1087 assert(is_power_of_2_long(x), "x must be a power of 2: " JLONG_FORMAT, x); 1088 return log2_long(x); 1089 } 1090 1091 inline bool is_odd (intx x) { return x & 1; } 1092 inline bool is_even(intx x) { return !is_odd(x); } 1093 | 1047 } 1048 1049 //* largest i such that 2^i <= x 1050 // A negative value of 'x' will return '63' 1051 inline int log2_long(unsigned long x) { 1052 int i = -1; 1053 julong p = 1; 1054 while (p != 0 && p <= x) { 1055 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) 1056 i++; p *= 2; 1057 } 1058 // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) 1059 // (if p = 0 then overflow occurred and i = 63) 1060 return i; 1061 } 1062 1063 inline int log2_intptr(intptr_t x) { 1064 return log2_intptr((uintptr_t)x); 1065 } 1066 1067 #ifdef _LP64 1068 inline int log2_intptr(int x) { 1069 return log2_intptr((uintptr_t)x); 1070 } 1071 1072 inline int log2_intptr(uint x) { 1073 return log2_intptr((uintptr_t)x); 1074 } 1075 #endif 1076 1077 inline int log2_long(jlong x) { 1078 return log2_long((unsigned long)x); 1079 } 1080 1081 //* the argument must be exactly a power of 2 1082 inline int exact_log2(intptr_t x) { 1083 assert(is_power_of_2(x), "x must be a power of 2: " INTPTR_FORMAT, x); 1084 return log2_intptr(x); 1085 } 1086 1087 //* the argument must be exactly a power of 2 1088 inline int exact_log2_long(jlong x) { 1089 assert(is_power_of_2_long(x), "x must be a power of 2: " JLONG_FORMAT, x); 1090 return log2_long(x); 1091 } 1092 1093 inline bool is_odd (intx x) { return x & 1; } 1094 inline bool is_even(intx x) { return !is_odd(x); } 1095 |