Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More efficient encoding of doubles with Scala.js #1271

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions zio-json/js/src/main/scala/zio/json/internal/SafeNumbers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,16 @@ object SafeNumbers {
val s = vb >> 2
if (
s < 100 || {
dv = s / 10
var z = s
dv = s
dv = (dv >>> 1) + (dv >>> 2) // Based upon the divu10() code from Hacker's Delight 2nd Edition by Henry Warren
dv += dv >>> 4
dv += dv >>> 8
dv += dv >>> 16
dv += dv >>> 32
z -= dv & 0xfffffffffffffff8L
dv >>>= 3
if ((z - (dv << 1)).toInt >= 10) dv += 1L
val sp40 = (dv << 5) + (dv << 3)
val upin = (vbls - sp40).toInt
(((sp40 + vbrd).toInt + 40) ^ upin) >= 0 || {
Expand Down Expand Up @@ -293,15 +302,26 @@ object SafeNumbers {
q0 * 100000000L == x
}
) return stripTrailingZeros(q0).toLong
var y = x
var q1, r1 = 0L
var q1, y, z = x
var r1 = 0
while ({
q1 = y / 100
r1 = y - ((q1 << 6) + (q1 << 5) + (q1 << 2))
q1 = (q1 >>> 1) + (q1 >>> 2) // Based upon the divu10() code from Hacker's Delight 2nd Edition by Henry Warren
q1 += q1 >>> 4
q1 += q1 >>> 8
q1 += q1 >>> 16
q1 += q1 >>> 32
z -= q1 & 0xfffffffffffffff8L
q1 >>>= 3
r1 = (z - (q1 << 1)).toInt
if (r1 >= 10) {
q1 += 1L
r1 -= 10
}
r1 == 0
}) y = q1
q1 = y / 10
r1 = y - ((q1 << 3) + (q1 << 1))
}) {
y = q1
z = q1
}
if (r1 == 0) return q1
y
}
Expand Down