Skip to content

Commit 6fcff37

Browse files
committed
Code clean up
1 parent 112ba92 commit 6fcff37

File tree

2 files changed

+64
-64
lines changed
  • jsoniter-scala-core

2 files changed

+64
-64
lines changed

jsoniter-scala-core/jvm/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonWriter.scala

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,18 +1457,16 @@ final class JsonWriter private[jsoniter_scala](
14571457
}
14581458
ByteArrayAccess.setShort(buf, pos, m)
14591459
pos += 2
1460-
var q = 0
1461-
if (exp < 100000000) {
1462-
q = exp.toInt
1460+
var q = exp
1461+
if (exp < 100000000L) {
14631462
pos += digitCount(exp)
14641463
count = pos
14651464
} 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 = Math.multiplyHigh(exp, 6189700196426901375L) >>> 25 // divide a positive long by 100000000
1466+
pos += digitCount(q)
1467+
count = write8Digits(exp - q * 100000000L, pos, buf, ds)
14701468
}
1471-
writePositiveIntDigits(q, pos, buf, ds)
1469+
writePositiveIntDigits(q.toInt, pos, buf, ds)
14721470
}
14731471
}
14741472

@@ -1611,18 +1609,17 @@ final class JsonWriter private[jsoniter_scala](
16111609
buf(pos) = '-'
16121610
pos += 1
16131611
}
1614-
var q = hours.toInt
1612+
var q = hours
16151613
var lastPos = pos
1616-
if (hours == q) {
1614+
if (hours < 100000000L) {
16171615
lastPos += digitCount(hours)
16181616
pos = lastPos
16191617
} else {
1620-
val q1 = Math.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 = Math.multiplyHigh(hours, 6189700196426901375L) >>> 25 // divide a positive long by 100000000
1619+
lastPos += digitCount(q)
1620+
pos = write8Digits(hours - q * 100000000L, lastPos, buf, ds)
16241621
}
1625-
writePositiveIntDigits(q, lastPos, buf, ds)
1622+
writePositiveIntDigits(q.toInt, lastPos, buf, ds)
16261623
ByteArrayAccess.setShort(buf, pos, 0x2248)
16271624
pos += 1
16281625
}
@@ -2030,9 +2027,11 @@ final class JsonWriter private[jsoniter_scala](
20302027

20312028
private[this] def write8Digits(x: Long, pos: Int, buf: Array[Byte], ds: Array[Short]): Int = {
20322029
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
20362035
val d1 = ds((y1 >> 47).toInt)
20372036
val d2 = ds((y2 >> 47).toInt) << 16
20382037
val d3 = ds((y3 >> 47).toInt).toLong << 32
@@ -2042,10 +2041,11 @@ final class JsonWriter private[jsoniter_scala](
20422041
}
20432042

20442043
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 = Math.multiplyHigh(x, m1) >>> 25 // divide a positive long by 100000000
2046+
val q2 = Math.multiplyHigh(q1, m1) >>> 25 // divide a positive long by 100000000
20472047
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)
20492049
}
20502050

20512051
private[this] def writeShort(x: Short): Unit = {
@@ -2124,27 +2124,27 @@ final class JsonWriter private[jsoniter_scala](
21242124
pos += 3
21252125
}
21262126
}
2127-
var q = 0
2127+
val m1 = 100000000L
2128+
var q2 = q0
21282129
var lastPos = pos
2129-
if (q0 < 100000000) {
2130-
q = q0.toInt
2130+
if (q0 < m1) {
21312131
lastPos += digitCount(q0)
21322132
pos = lastPos
21332133
} else {
2134-
val q1 = Math.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 = Math.multiplyHigh(q0, m2) >>> 25 // divide a positive long by 100000000
2136+
if (q1 < m1) {
2137+
q2 = q1
21372138
lastPos += digitCount(q1)
21382139
pos = lastPos
21392140
} else {
2140-
val q2 = (q1 >> 8) * 1441151881 >> 49 // divide a small positive long by 100000000
2141-
q = q2.toInt
2141+
q2 = Math.multiplyHigh(q1, m2) >>> 25 // divide a small positive long by 100000000
21422142
lastPos += digitCount(q2)
2143-
pos = write8Digits(q1 - q2 * 100000000, lastPos, buf, ds)
2143+
pos = write8Digits(q1 - q2 * m1, lastPos, buf, ds)
21442144
}
2145-
pos = write8Digits(q0 - q1 * 100000000, pos, buf, ds)
2145+
pos = write8Digits(q0 - q1 * m1, pos, buf, ds)
21462146
}
2147-
writePositiveIntDigits(q, lastPos, buf, ds)
2147+
writePositiveIntDigits(q2.toInt, lastPos, buf, ds)
21482148
pos
21492149
}
21502150

jsoniter-scala-core/native/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonWriter.scala

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,18 +1457,16 @@ final class JsonWriter private[jsoniter_scala](
14571457
}
14581458
ByteArrayAccess.setShort(buf, pos, m)
14591459
pos += 2
1460-
var q = 0
1461-
if (exp < 100000000) {
1462-
q = exp.toInt
1460+
var q = exp
1461+
if (exp < 100000000L) {
14631462
pos += digitCount(exp)
14641463
count = pos
14651464
} 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)
14701468
}
1471-
writePositiveIntDigits(q, pos, buf, ds)
1469+
writePositiveIntDigits(q.toInt, pos, buf, ds)
14721470
}
14731471
}
14741472

@@ -1611,18 +1609,17 @@ final class JsonWriter private[jsoniter_scala](
16111609
buf(pos) = '-'
16121610
pos += 1
16131611
}
1614-
var q = hours.toInt
1612+
var q = hours
16151613
var lastPos = pos
1616-
if (hours == q) {
1614+
if (hours < 100000000L) {
16171615
lastPos += digitCount(hours)
16181616
pos = lastPos
16191617
} 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)
16241621
}
1625-
writePositiveIntDigits(q, lastPos, buf, ds)
1622+
writePositiveIntDigits(q.toInt, lastPos, buf, ds)
16261623
ByteArrayAccess.setShort(buf, pos, 0x2248)
16271624
pos += 1
16281625
}
@@ -2030,9 +2027,11 @@ final class JsonWriter private[jsoniter_scala](
20302027

20312028
private[this] def write8Digits(x: Long, pos: Int, buf: Array[Byte], ds: Array[Short]): Int = {
20322029
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
20362035
val d1 = ds((y1 >> 47).toInt)
20372036
val d2 = ds((y2 >> 47).toInt) << 16
20382037
val d3 = ds((y3 >> 47).toInt).toLong << 32
@@ -2042,10 +2041,11 @@ final class JsonWriter private[jsoniter_scala](
20422041
}
20432042

20442043
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
20472047
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)
20492049
}
20502050

20512051
private[this] def writeShort(x: Short): Unit = {
@@ -2124,27 +2124,27 @@ final class JsonWriter private[jsoniter_scala](
21242124
pos += 3
21252125
}
21262126
}
2127-
var q = 0
2127+
val m1 = 100000000L
2128+
var q2 = q0
21282129
var lastPos = pos
2129-
if (q0 < 100000000) {
2130-
q = q0.toInt
2130+
if (q0 < m1) {
21312131
lastPos += digitCount(q0)
21322132
pos = lastPos
21332133
} 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
21372138
lastPos += digitCount(q1)
21382139
pos = lastPos
21392140
} 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
21422142
lastPos += digitCount(q2)
2143-
pos = write8Digits(q1 - q2 * 100000000, lastPos, buf, ds)
2143+
pos = write8Digits(q1 - q2 * m1, lastPos, buf, ds)
21442144
}
2145-
pos = write8Digits(q0 - q1 * 100000000, pos, buf, ds)
2145+
pos = write8Digits(q0 - q1 * m1, pos, buf, ds)
21462146
}
2147-
writePositiveIntDigits(q, lastPos, buf, ds)
2147+
writePositiveIntDigits(q2.toInt, lastPos, buf, ds)
21482148
pos
21492149
}
21502150

0 commit comments

Comments
 (0)