1030 inline bool is_power_of_2_long(jlong x) { |
1030 inline bool is_power_of_2_long(jlong x) { |
1031 return ((x != NoLongBits) && (mask_long_bits(x, x - 1) == NoLongBits)); |
1031 return ((x != NoLongBits) && (mask_long_bits(x, x - 1) == NoLongBits)); |
1032 } |
1032 } |
1033 |
1033 |
1034 // Returns largest i such that 2^i <= x. |
1034 // Returns largest i such that 2^i <= x. |
1035 // If x < 0, the function returns 31 on a 32-bit machine and 63 on a 64-bit machine. |
|
1036 // If x == 0, the function returns -1. |
1035 // If x == 0, the function returns -1. |
1037 inline int log2_intptr(uintptr_t x) { |
1036 inline int log2_intptr(uintptr_t x) { |
1038 int i = -1; |
1037 int i = -1; |
1039 uintptr_t p = 1; |
1038 uintptr_t p = 1; |
1040 while (p != 0 && p <= x) { |
1039 while (p != 0 && p <= x) { |
1045 // If p = 0, overflow has occurred and i = 31 or i = 63 (depending on the machine word size). |
1044 // If p = 0, overflow has occurred and i = 31 or i = 63 (depending on the machine word size). |
1046 return i; |
1045 return i; |
1047 } |
1046 } |
1048 |
1047 |
1049 //* largest i such that 2^i <= x |
1048 //* largest i such that 2^i <= x |
1050 // A negative value of 'x' will return '63' |
1049 inline int log2_long(julong x) { |
1051 inline int log2_long(unsigned long x) { |
|
1052 int i = -1; |
1050 int i = -1; |
1053 julong p = 1; |
1051 julong p = 1; |
1054 while (p != 0 && p <= x) { |
1052 while (p != 0 && p <= x) { |
1055 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) |
1053 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) |
1056 i++; p *= 2; |
1054 i++; p *= 2; |
1058 // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) |
1056 // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) |
1059 // (if p = 0 then overflow occurred and i = 63) |
1057 // (if p = 0 then overflow occurred and i = 63) |
1060 return i; |
1058 return i; |
1061 } |
1059 } |
1062 |
1060 |
|
1061 // If x < 0, the function returns 31 on a 32-bit machine and 63 on a 64-bit machine. |
1063 inline int log2_intptr(intptr_t x) { |
1062 inline int log2_intptr(intptr_t x) { |
1064 return log2_intptr((uintptr_t)x); |
1063 return log2_intptr((uintptr_t)x); |
1065 } |
1064 } |
1066 |
1065 |
1067 inline int log2_intptr(int x) { |
1066 inline int log2_int(int x) { |
|
1067 STATIC_ASSERT(sizeof(int) <= sizeof(uintptr_t)); |
1068 return log2_intptr((uintptr_t)x); |
1068 return log2_intptr((uintptr_t)x); |
1069 } |
1069 } |
1070 |
1070 |
1071 inline int log2_intptr(uint x) { |
1071 inline int log2_jint(jint x) { |
|
1072 STATIC_ASSERT(sizeof(jint) <= sizeof(uintptr_t)); |
1072 return log2_intptr((uintptr_t)x); |
1073 return log2_intptr((uintptr_t)x); |
1073 } |
1074 } |
1074 |
1075 |
1075 inline int log2_long(jlong x) { |
1076 inline int log2_uint(uint x) { |
1076 return log2_long((unsigned long)x); |
1077 STATIC_ASSERT(sizeof(uint) <= sizeof(uintptr_t)); |
|
1078 return log2_intptr((uintptr_t)x); |
|
1079 } |
|
1080 |
|
1081 // A negative value of 'x' will return '63' |
|
1082 inline int log2_jlong(jlong x) { |
|
1083 STATIC_ASSERT(sizeof(jlong) <= sizeof(julong)); |
|
1084 return log2_long((julong)x); |
1077 } |
1085 } |
1078 |
1086 |
1079 //* the argument must be exactly a power of 2 |
1087 //* the argument must be exactly a power of 2 |
1080 inline int exact_log2(intptr_t x) { |
1088 inline int exact_log2(intptr_t x) { |
1081 assert(is_power_of_2(x), "x must be a power of 2: " INTPTR_FORMAT, x); |
1089 assert(is_power_of_2(x), "x must be a power of 2: " INTPTR_FORMAT, x); |