[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)"))
}
@@ -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.
*/
package kotlin.math
import kotlin.js.internal.BitUtils
/**
* 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")
public actual fun Double.withSign(sign: Double): Double {
val thisSignBit = BitUtils.doubleSignBit(this)
val newSignBit = BitUtils.doubleSignBit(sign)
val thisSignBit = doubleSignBit(this)
val newSignBit = doubleSignBit(sign)
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.
*/
package kotlin
import kotlin.js.internal.BitUtils
/**
* Returns a bit representation of the specified floating-point value as [Long]
* according to the IEEE 754 floating-point "double format" bit layout.
*/
@SinceKotlin("1.2")
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]
@@ -22,7 +20,7 @@ public actual fun Double.toBits(): Long =
*/
@SinceKotlin("1.2")
public actual fun Double.toRawBits(): Long =
BitUtils.doubleToRawBits(this)
doubleToRawBits(this)
/**
* Returns the [Double] value corresponding to a given bit representation.
@@ -30,7 +28,7 @@ public actual fun Double.toRawBits(): Long =
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
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]
@@ -41,7 +39,7 @@ public actual inline fun Double.Companion.fromBits(bits: Long): Double =
*/
@SinceKotlin("1.2")
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]
@@ -53,7 +51,7 @@ public actual fun Float.toBits(): Int =
*/
@SinceKotlin("1.2")
public actual fun Float.toRawBits(): Int =
BitUtils.floatToRawBits(this)
floatToRawBits(this)
/**
* Returns the [Float] value corresponding to a given bit representation.
@@ -61,4 +59,4 @@ public actual fun Float.toRawBits(): Int =
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public actual inline fun Float.Companion.fromBits(bits: Int): Float =
BitUtils.floatFromBits(bits)
floatFromBits(bits)