Thanks to visit codestin.com
Credit goes to github.com

Skip to content

WiP Redesign the encoding of Longs in JavaScript. #5205

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
34 changes: 18 additions & 16 deletions javalib/src/main/scala/java/lang/Long.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ object Long {
if (radix == 10 || radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
toString(i)
else
toStringImpl(i, radix)
toStringImpl(i.toInt, (i >>> 32).toInt, radix)
}

@inline // because radix is almost certainly constant at call site
Expand All @@ -133,48 +133,50 @@ object Long {
val radix1 =
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) 10
else radix
toUnsignedStringImpl(i, radix1)
toUnsignedStringImpl(i.toInt, (i >>> 32).toInt, radix1)
}
}

// Intrinsic
@inline def toString(i: scala.Long): String = "" + i

@inline def toUnsignedString(i: scala.Long): String =
toUnsignedStringImpl(i, 10)
toUnsignedStringImpl(i.toInt, (i >>> 32).toInt, 10)

// Must be called only with valid radix
private def toStringImpl(i: scala.Long, radix: Int): String = {
val lo = i.toInt
val hi = (i >>> 32).toInt
private def toStringImpl(lo: Int, hi: Int, radix: Int): String = {
import js.JSNumberOps.enableJSNumberOps

if (lo >> 31 == hi) {
// It's a signed int32
import js.JSNumberOps.enableJSNumberOps
lo.toString(radix)
} else if (hi < 0) {
val neg = -i
"-" + toUnsignedStringInternalLarge(neg.toInt, (neg >>> 32).toInt, radix)
} else if (((hi ^ (hi >> 10)) & 0xffe00000) == 0) { // see RuntimeLong.isSignedSafeDouble
// (lo, hi) is small enough to be a Double, so toDouble is exact
makeLongFromLoHi(lo, hi).toDouble.toString(radix)
} else {
toUnsignedStringInternalLarge(lo, hi, radix)
val abs = Math.abs(makeLongFromLoHi(lo, hi))
val s = toUnsignedStringInternalLarge(abs.toInt, (abs >>> 32).toInt, radix)
if (hi < 0) "-" + s else s
}
}

// Must be called only with valid radix
private def toUnsignedStringImpl(i: scala.Long, radix: Int): String = {
val lo = i.toInt
val hi = (i >>> 32).toInt
private def toUnsignedStringImpl(lo: Int, hi: Int, radix: Int): String = {
import js.JSNumberOps.enableJSNumberOps

if (hi == 0) {
// It's an unsigned int32
import js.JSNumberOps.enableJSNumberOps
Integer.toUnsignedDouble(lo).toString(radix)
} else if ((hi & 0xffe00000) == 0) { // see RuntimeLong.isUnsignedSafeDouble
// (lo, hi) is small enough to be a Double, so toDouble is exact
makeLongFromLoHi(lo, hi).toDouble.toString(radix)
} else {
toUnsignedStringInternalLarge(lo, hi, radix)
}
}

// Must be called only with valid radix and with (lo, hi) >= 2^30
// Must be called only with valid radix and with (lo, hi) >= 2^53
@inline // inlined twice: once in toStringImpl and once in toUnsignedStringImpl
private def toUnsignedStringInternalLarge(lo: Int, hi: Int, radix: Int): String = {
import js.JSNumberOps.enableJSNumberOps
import js.JSStringOps.enableJSStringOps
Expand Down
Loading