@@ -1457,18 +1457,16 @@ final class JsonWriter private[jsoniter_scala](
1457
1457
}
1458
1458
ByteArrayAccess .setShort(buf, pos, m)
1459
1459
pos += 2
1460
- var q = 0
1461
- if (exp < 100000000 ) {
1462
- q = exp.toInt
1460
+ var q = exp
1461
+ if (exp < 100000000L ) {
1463
1462
pos += digitCount(exp)
1464
1463
count = pos
1465
1464
} else {
1466
- val q1 = (exp >> 8 ) * 1441151881 >> 49 // divide a small positive long by 100000000
1467
- q = q1.toInt
1468
- pos += digitCount(q1)
1469
- count = write8Digits(exp - q1 * 100000000 , pos, buf, ds)
1465
+ q = NativeMath .multiplyHigh(exp, 6189700196426901375L ) >>> 25 // divide a positive long by 100000000
1466
+ pos += digitCount(q)
1467
+ count = write8Digits(exp - q * 100000000L , pos, buf, ds)
1470
1468
}
1471
- writePositiveIntDigits(q, pos, buf, ds)
1469
+ writePositiveIntDigits(q.toInt , pos, buf, ds)
1472
1470
}
1473
1471
}
1474
1472
@@ -1611,18 +1609,17 @@ final class JsonWriter private[jsoniter_scala](
1611
1609
buf(pos) = '-'
1612
1610
pos += 1
1613
1611
}
1614
- var q = hours.toInt
1612
+ var q = hours
1615
1613
var lastPos = pos
1616
- if (hours == q ) {
1614
+ if (hours < 100000000L ) {
1617
1615
lastPos += digitCount(hours)
1618
1616
pos = lastPos
1619
1617
} else {
1620
- val q1 = NativeMath .multiplyHigh(hours, 6189700196426901375L ) >>> 25 // divide a positive long by 100000000
1621
- q = q1.toInt
1622
- lastPos += digitCount(q1)
1623
- pos = write8Digits(hours - q1 * 100000000 , lastPos, buf, ds)
1618
+ q = NativeMath .multiplyHigh(hours, 6189700196426901375L ) >>> 25 // divide a positive long by 100000000
1619
+ lastPos += digitCount(q)
1620
+ pos = write8Digits(hours - q * 100000000L , lastPos, buf, ds)
1624
1621
}
1625
- writePositiveIntDigits(q, lastPos, buf, ds)
1622
+ writePositiveIntDigits(q.toInt , lastPos, buf, ds)
1626
1623
ByteArrayAccess .setShort(buf, pos, 0x2248 )
1627
1624
pos += 1
1628
1625
}
@@ -2030,9 +2027,11 @@ final class JsonWriter private[jsoniter_scala](
2030
2027
2031
2028
private [this ] def write8Digits (x : Long , pos : Int , buf : Array [Byte ], ds : Array [Short ]): Int = {
2032
2029
val y1 = x * 140737489 // Based on James Anhalt's algorithm for 8 digits: https://jk-jeon.github.io/posts/2022/02/jeaiii-algorithm/
2033
- val y2 = (y1 & 0x7FFFFFFFFFFFL) * 100
2034
- val y3 = (y2 & 0x7FFFFFFFFFFFL) * 100
2035
- val y4 = (y3 & 0x7FFFFFFFFFFFL) * 100
2030
+ val m1 = 0x7FFFFFFFFFFFL
2031
+ val m2 = 100L
2032
+ val y2 = (y1 & m1) * m2
2033
+ val y3 = (y2 & m1) * m2
2034
+ val y4 = (y3 & m1) * m2
2036
2035
val d1 = ds((y1 >> 47 ).toInt)
2037
2036
val d2 = ds((y2 >> 47 ).toInt) << 16
2038
2037
val d3 = ds((y3 >> 47 ).toInt).toLong << 32
@@ -2042,10 +2041,11 @@ final class JsonWriter private[jsoniter_scala](
2042
2041
}
2043
2042
2044
2043
private [this ] def write18Digits (x : Long , pos : Int , buf : Array [Byte ], ds : Array [Short ]): Int = {
2045
- val q1 = Math .multiplyHigh(x, 6189700196426901375L ) >>> 25 // divide a positive long by 100000000
2046
- val q2 = (q1 >> 8 ) * 1441151881 >> 49 // divide a small positive long by 100000000
2044
+ val m1 = 6189700196426901375L
2045
+ val q1 = NativeMath .multiplyHigh(x, m1) >>> 25 // divide a positive long by 100000000
2046
+ val q2 = NativeMath .multiplyHigh(q1, m1) >>> 25 // divide a positive long by 100000000
2047
2047
ByteArrayAccess .setShort(buf, pos, ds(q2.toInt))
2048
- write8Digits(x - q1 * 100000000 , write8Digits(q1 - q2 * 100000000 , pos + 2 , buf, ds), buf, ds)
2048
+ write8Digits(x - q1 * 100000000L , write8Digits(q1 - q2 * 100000000L , pos + 2 , buf, ds), buf, ds)
2049
2049
}
2050
2050
2051
2051
private [this ] def writeShort (x : Short ): Unit = {
@@ -2124,27 +2124,27 @@ final class JsonWriter private[jsoniter_scala](
2124
2124
pos += 3
2125
2125
}
2126
2126
}
2127
- var q = 0
2127
+ val m1 = 100000000L
2128
+ var q2 = q0
2128
2129
var lastPos = pos
2129
- if (q0 < 100000000 ) {
2130
- q = q0.toInt
2130
+ if (q0 < m1) {
2131
2131
lastPos += digitCount(q0)
2132
2132
pos = lastPos
2133
2133
} else {
2134
- val q1 = NativeMath .multiplyHigh(q0, 6189700196426901375L ) >>> 25 // divide a positive long by 100000000
2135
- if (q1 < 100000000 ) {
2136
- q = q1.toInt
2134
+ val m2 = 6189700196426901375L
2135
+ val q1 = NativeMath .multiplyHigh(q0, m2) >>> 25 // divide a positive long by 100000000
2136
+ if (q1 < m1) {
2137
+ q2 = q1
2137
2138
lastPos += digitCount(q1)
2138
2139
pos = lastPos
2139
2140
} else {
2140
- val q2 = (q1 >> 8 ) * 1441151881 >> 49 // divide a small positive long by 100000000
2141
- q = q2.toInt
2141
+ q2 = NativeMath .multiplyHigh(q1, m2) >>> 25 // divide a small positive long by 100000000
2142
2142
lastPos += digitCount(q2)
2143
- pos = write8Digits(q1 - q2 * 100000000 , lastPos, buf, ds)
2143
+ pos = write8Digits(q1 - q2 * m1 , lastPos, buf, ds)
2144
2144
}
2145
- pos = write8Digits(q0 - q1 * 100000000 , pos, buf, ds)
2145
+ pos = write8Digits(q0 - q1 * m1 , pos, buf, ds)
2146
2146
}
2147
- writePositiveIntDigits(q , lastPos, buf, ds)
2147
+ writePositiveIntDigits(q2.toInt , lastPos, buf, ds)
2148
2148
pos
2149
2149
}
2150
2150
0 commit comments