@@ -31,20 +31,24 @@ protected LazyIntRope(int value, Encoding encoding, int length) {
31
31
assert Integer .toString (value ).length () == length : value + " " + length ;
32
32
}
33
33
34
- @ CompilationFinal (dimensions = 1 ) private static final int [] LENGTH_TABLE = {
35
- 9 ,
36
- 99 ,
37
- 999 ,
38
- 9999 ,
39
- 99999 ,
40
- 999999 ,
41
- 9999999 ,
42
- 99999999 ,
43
- 999999999 ,
44
- Integer .MAX_VALUE };
45
-
46
- // From https://lemire.me/blog/2021/05/28/computing-the-number-of-digits-of-an-integer-quickly/
47
- // and https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/9d4cd21d0a/2021/05/28/digitcount.java (license: public domain)
34
+ // @formatter:off
35
+ @ CompilationFinal (dimensions = 1 ) private static final long [] LENGTH_TABLE = {
36
+ 0x100000000L , 0x1FFFFFFF6L , 0x1FFFFFFF6L ,
37
+ 0x1FFFFFFF6L , 0x2FFFFFF9CL , 0x2FFFFFF9CL ,
38
+ 0x2FFFFFF9CL , 0x3FFFFFC18L , 0x3FFFFFC18L ,
39
+ 0x3FFFFFC18L , 0x4FFFFD8F0L , 0x4FFFFD8F0L ,
40
+ 0x4FFFFD8F0L , 0x4FFFFD8F0L , 0x5FFFE7960L ,
41
+ 0x5FFFE7960L , 0x5FFFE7960L , 0x6FFF0BDC0L ,
42
+ 0x6FFF0BDC0L , 0x6FFF0BDC0L , 0x7FF676980L ,
43
+ 0x7FF676980L , 0x7FF676980L , 0x7FF676980L ,
44
+ 0x8FA0A1F00L , 0x8FA0A1F00L , 0x8FA0A1F00L ,
45
+ 0x9C4653600L , 0x9C4653600L , 0x9C4653600L ,
46
+ 0xA00000000L , 0xA00000000L
47
+ };
48
+ // @formatter:on
49
+
50
+ // From https://lemire.me/blog/2021/06/03/computing-the-number-of-digits-of-an-integer-even-faster/
51
+ // and https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/4e6e171a7d/2021/06/03/digitcount.c (license: public domain)
48
52
private static int length (int value ) {
49
53
final int sign ;
50
54
if (CompilerDirectives .injectBranchProbability (CompilerDirectives .UNLIKELY_PROBABILITY , value < 0 )) {
@@ -61,13 +65,8 @@ private static int length(int value) {
61
65
}
62
66
63
67
final int bits = 31 - Integer .numberOfLeadingZeros (value | 1 );
64
- int digits = ((9 * bits ) >>> 5 );
65
-
66
- if (value > LENGTH_TABLE [digits ]) {
67
- digits += 1 ;
68
- }
69
-
70
- return sign + digits + 1 ;
68
+ int digits = (int ) ((value + LENGTH_TABLE [bits ]) >>> 32 );
69
+ return sign + digits ;
71
70
}
72
71
73
72
@ Override
0 commit comments