[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.
*/
package kotlin.js.internal
package kotlin.js
// TODO use declarations from stdlib
private external class ArrayBuffer(size: Int)
private external class Float64Array(buffer: ArrayBuffer)
private external class Float32Array(buffer: ArrayBuffer)
private external class Int32Array(buffer: ArrayBuffer)
@PublishedApi
internal object BitUtils {
private val buf = ArrayBuffer(8)
private val bufFloat64 = Float64Array(buf).unsafeCast<DoubleArray>()
private val bufFloat32 = Float32Array(buf).unsafeCast<FloatArray>()
private val bufInt32 = Int32Array(buf).unsafeCast<IntArray>()
private var lowIndex = 0
private var highIndex = 1
private val buf = ArrayBuffer(8)
// TODO use one DataView instead of bunch of typed views.
private val bufFloat64 = Float64Array(buf).unsafeCast<DoubleArray>()
private val bufFloat32 = Float32Array(buf).unsafeCast<FloatArray>()
private val bufInt32 = Int32Array(buf).unsafeCast<IntArray>()
init {
bufFloat64[0] = -1.0 // bff00000_00000000
if (bufInt32[lowIndex] != 0) {
lowIndex = 1
highIndex = 0
}
}
fun doubleToRawBits(value: Double): Long {
bufFloat64[0] = value
return Long(bufInt32[lowIndex], bufInt32[highIndex])
}
fun doubleFromBits(value: Long): Double {
bufInt32[lowIndex] = value.low
bufInt32[highIndex] = value.high
return bufFloat64[0]
}
fun floatToRawBits(value: Float): Int {
bufFloat32[0] = value
return bufInt32[0]
}
fun floatFromBits(value: Int): Float {
bufInt32[0] = value
return bufFloat32[0]
}
// returns zero value for number with positive sign bit and non-zero value for number with negative sign bit.
fun doubleSignBit(value: Double): Int {
bufFloat64[0] = value
return bufInt32[highIndex] and Int.MIN_VALUE
}
fun getNumberHashCode(obj: Double): Int {
if (jsBitwiseOr(obj, 0).unsafeCast<Double>() === obj) {
return obj.toInt()
}
bufFloat64[0] = obj
return bufInt32[highIndex] * 31 + bufInt32[lowIndex]
}
private val lowIndex = run {
bufFloat64[0] = -1.0 // bff00000_00000000
if (bufInt32[0] != 0) 1 else 0
}
private val highIndex = 1 - lowIndex
internal fun doubleToRawBits(value: Double): Long {
bufFloat64[0] = value
return Long(bufInt32[lowIndex], bufInt32[highIndex])
}
@PublishedApi
internal fun doubleFromBits(value: Long): Double {
bufInt32[lowIndex] = value.low
bufInt32[highIndex] = value.high
return bufFloat64[0]
}
internal fun floatToRawBits(value: Float): Int {
bufFloat32[0] = value
return bufInt32[0]
}
@PublishedApi
internal fun floatFromBits(value: Int): Float {
bufInt32[0] = value
return bufFloat32[0]
}
// returns zero value for number with positive sign bit and non-zero value for number with negative sign bit.
internal fun doubleSignBit(value: Double): Int {
bufFloat64[0] = value
return bufInt32[highIndex] and Int.MIN_VALUE
}
internal fun getNumberHashCode(obj: Double): Int {
if (jsBitwiseOr(obj, 0).unsafeCast<Double>() === obj) {
return obj.toInt()
}
bufFloat64[0] = obj
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.
*/
package kotlin.js
import kotlin.js.internal.BitUtils
fun equals(obj1: dynamic, obj2: dynamic): Boolean {
if (obj1 == null) {
return obj2 == null
@@ -49,7 +47,7 @@ fun hashCode(obj: dynamic): Int {
return when (jsTypeOf(obj)) {
"object" -> if ("function" === jsTypeOf(obj.hashCode)) (obj.hashCode)() else getObjectHashCode(obj)
"function" -> getObjectHashCode(obj)
"number" -> BitUtils.getNumberHashCode(obj)
"number" -> getNumberHashCode(obj)
"boolean" -> if(obj.unsafeCast<Boolean>()) 1 else 0
else -> getStringHashCode(js("String(obj)"))
}