[stdlib-js-ir] Remove BitUtils object and make all members toplevel

It works slightly better with IR DCE --
we get about 6% less code on box tests.
This commit is contained in:
Zalim Bashorov
2020-01-13 20:54:37 +03:00
parent 62014e6711
commit 9e7f72382f
4 changed files with 62 additions and 71 deletions
+50 -53
View File
@@ -1,65 +1,62 @@
/* /*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
package kotlin.js.internal package kotlin.js
// TODO use declarations from stdlib
private external class ArrayBuffer(size: Int) private external class ArrayBuffer(size: Int)
private external class Float64Array(buffer: ArrayBuffer) private external class Float64Array(buffer: ArrayBuffer)
private external class Float32Array(buffer: ArrayBuffer) private external class Float32Array(buffer: ArrayBuffer)
private external class Int32Array(buffer: ArrayBuffer) private external class Int32Array(buffer: ArrayBuffer)
@PublishedApi private val buf = ArrayBuffer(8)
internal object BitUtils { // TODO use one DataView instead of bunch of typed views.
private val buf = ArrayBuffer(8) private val bufFloat64 = Float64Array(buf).unsafeCast<DoubleArray>()
private val bufFloat64 = Float64Array(buf).unsafeCast<DoubleArray>() private val bufFloat32 = Float32Array(buf).unsafeCast<FloatArray>()
private val bufFloat32 = Float32Array(buf).unsafeCast<FloatArray>() private val bufInt32 = Int32Array(buf).unsafeCast<IntArray>()
private val bufInt32 = Int32Array(buf).unsafeCast<IntArray>()
private var lowIndex = 0
private var highIndex = 1
init { private val lowIndex = run {
bufFloat64[0] = -1.0 // bff00000_00000000 bufFloat64[0] = -1.0 // bff00000_00000000
if (bufInt32[lowIndex] != 0) { if (bufInt32[0] != 0) 1 else 0
lowIndex = 1 }
highIndex = 0 private val highIndex = 1 - lowIndex
}
} internal fun doubleToRawBits(value: Double): Long {
bufFloat64[0] = value
fun doubleToRawBits(value: Double): Long { return Long(bufInt32[lowIndex], bufInt32[highIndex])
bufFloat64[0] = value }
return Long(bufInt32[lowIndex], bufInt32[highIndex])
} @PublishedApi
internal fun doubleFromBits(value: Long): Double {
fun doubleFromBits(value: Long): Double { bufInt32[lowIndex] = value.low
bufInt32[lowIndex] = value.low bufInt32[highIndex] = value.high
bufInt32[highIndex] = value.high return bufFloat64[0]
return bufFloat64[0] }
}
internal fun floatToRawBits(value: Float): Int {
fun floatToRawBits(value: Float): Int { bufFloat32[0] = value
bufFloat32[0] = value return bufInt32[0]
return bufInt32[0] }
}
@PublishedApi
fun floatFromBits(value: Int): Float { internal fun floatFromBits(value: Int): Float {
bufInt32[0] = value bufInt32[0] = value
return bufFloat32[0] return bufFloat32[0]
} }
// returns zero value for number with positive sign bit and non-zero value for number with negative sign bit. // returns zero value for number with positive sign bit and non-zero value for number with negative sign bit.
fun doubleSignBit(value: Double): Int { internal fun doubleSignBit(value: Double): Int {
bufFloat64[0] = value bufFloat64[0] = value
return bufInt32[highIndex] and Int.MIN_VALUE return bufInt32[highIndex] and Int.MIN_VALUE
} }
fun getNumberHashCode(obj: Double): Int { internal fun getNumberHashCode(obj: Double): Int {
if (jsBitwiseOr(obj, 0).unsafeCast<Double>() === obj) { if (jsBitwiseOr(obj, 0).unsafeCast<Double>() === obj) {
return obj.toInt() return obj.toInt()
} }
bufFloat64[0] = obj bufFloat64[0] = obj
return bufInt32[highIndex] * 31 + bufInt32[lowIndex] return bufInt32[highIndex] * 31 + bufInt32[lowIndex]
}
} }
@@ -1,12 +1,10 @@
/* /*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
package kotlin.js package kotlin.js
import kotlin.js.internal.BitUtils
fun equals(obj1: dynamic, obj2: dynamic): Boolean { fun equals(obj1: dynamic, obj2: dynamic): Boolean {
if (obj1 == null) { if (obj1 == null) {
return obj2 == null return obj2 == null
@@ -49,7 +47,7 @@ fun hashCode(obj: dynamic): Int {
return when (jsTypeOf(obj)) { return when (jsTypeOf(obj)) {
"object" -> if ("function" === jsTypeOf(obj.hashCode)) (obj.hashCode)() else getObjectHashCode(obj) "object" -> if ("function" === jsTypeOf(obj.hashCode)) (obj.hashCode)() else getObjectHashCode(obj)
"function" -> getObjectHashCode(obj) "function" -> getObjectHashCode(obj)
"number" -> BitUtils.getNumberHashCode(obj) "number" -> getNumberHashCode(obj)
"boolean" -> if(obj.unsafeCast<Boolean>()) 1 else 0 "boolean" -> if(obj.unsafeCast<Boolean>()) 1 else 0
else -> getStringHashCode(js("String(obj)")) else -> getStringHashCode(js("String(obj)"))
} }
@@ -1,11 +1,9 @@
/* /*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
package kotlin.math package kotlin.math
import kotlin.js.internal.BitUtils
/** /**
* Returns this value with the sign bit same as of the [sign] value. * Returns this value with the sign bit same as of the [sign] value.
* *
@@ -13,7 +11,7 @@ import kotlin.js.internal.BitUtils
*/ */
@SinceKotlin("1.2") @SinceKotlin("1.2")
public actual fun Double.withSign(sign: Double): Double { public actual fun Double.withSign(sign: Double): Double {
val thisSignBit = BitUtils.doubleSignBit(this) val thisSignBit = doubleSignBit(this)
val newSignBit = BitUtils.doubleSignBit(sign) val newSignBit = doubleSignBit(sign)
return if (thisSignBit == newSignBit) this else -this return if (thisSignBit == newSignBit) this else -this
} }
@@ -1,19 +1,17 @@
/* /*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
package kotlin package kotlin
import kotlin.js.internal.BitUtils
/** /**
* Returns a bit representation of the specified floating-point value as [Long] * Returns a bit representation of the specified floating-point value as [Long]
* according to the IEEE 754 floating-point "double format" bit layout. * according to the IEEE 754 floating-point "double format" bit layout.
*/ */
@SinceKotlin("1.2") @SinceKotlin("1.2")
public actual fun Double.toBits(): Long = public actual fun Double.toBits(): Long =
BitUtils.doubleToRawBits(if (this.isNaN()) Double.NaN else this) doubleToRawBits(if (this.isNaN()) Double.NaN else this)
/** /**
* Returns a bit representation of the specified floating-point value as [Long] * Returns a bit representation of the specified floating-point value as [Long]
@@ -22,7 +20,7 @@ public actual fun Double.toBits(): Long =
*/ */
@SinceKotlin("1.2") @SinceKotlin("1.2")
public actual fun Double.toRawBits(): Long = public actual fun Double.toRawBits(): Long =
BitUtils.doubleToRawBits(this) doubleToRawBits(this)
/** /**
* Returns the [Double] value corresponding to a given bit representation. * Returns the [Double] value corresponding to a given bit representation.
@@ -30,7 +28,7 @@ public actual fun Double.toRawBits(): Long =
@SinceKotlin("1.2") @SinceKotlin("1.2")
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public actual inline fun Double.Companion.fromBits(bits: Long): Double = public actual inline fun Double.Companion.fromBits(bits: Long): Double =
BitUtils.doubleFromBits(bits) doubleFromBits(bits)
/** /**
* Returns a bit representation of the specified floating-point value as [Int] * Returns a bit representation of the specified floating-point value as [Int]
@@ -41,7 +39,7 @@ public actual inline fun Double.Companion.fromBits(bits: Long): Double =
*/ */
@SinceKotlin("1.2") @SinceKotlin("1.2")
public actual fun Float.toBits(): Int = public actual fun Float.toBits(): Int =
BitUtils.floatToRawBits(if (this.isNaN()) Float.NaN else this) floatToRawBits(if (this.isNaN()) Float.NaN else this)
/** /**
* Returns a bit representation of the specified floating-point value as [Int] * Returns a bit representation of the specified floating-point value as [Int]
@@ -53,7 +51,7 @@ public actual fun Float.toBits(): Int =
*/ */
@SinceKotlin("1.2") @SinceKotlin("1.2")
public actual fun Float.toRawBits(): Int = public actual fun Float.toRawBits(): Int =
BitUtils.floatToRawBits(this) floatToRawBits(this)
/** /**
* Returns the [Float] value corresponding to a given bit representation. * Returns the [Float] value corresponding to a given bit representation.
@@ -61,4 +59,4 @@ public actual fun Float.toRawBits(): Int =
@SinceKotlin("1.2") @SinceKotlin("1.2")
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public actual inline fun Float.Companion.fromBits(bits: Int): Float = public actual inline fun Float.Companion.fromBits(bits: Int): Float =
BitUtils.floatFromBits(bits) floatFromBits(bits)