From 9e7f72382fbc109b5ba62901796d46087d1963fe Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Mon, 13 Jan 2020 20:54:37 +0300 Subject: [PATCH] [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. --- libraries/stdlib/js-ir/runtime/bitUtils.kt | 103 +++++++++--------- libraries/stdlib/js-ir/runtime/coreRuntime.kt | 6 +- .../stdlib/js-ir/src/kotlin/math_js-ir.kt | 8 +- .../stdlib/js-ir/src/kotlin/numbers_js-ir.kt | 16 ++- 4 files changed, 62 insertions(+), 71 deletions(-) diff --git a/libraries/stdlib/js-ir/runtime/bitUtils.kt b/libraries/stdlib/js-ir/runtime/bitUtils.kt index 5450eba5c7b..c37bf6cf68a 100644 --- a/libraries/stdlib/js-ir/runtime/bitUtils.kt +++ b/libraries/stdlib/js-ir/runtime/bitUtils.kt @@ -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() - private val bufFloat32 = Float32Array(buf).unsafeCast() - private val bufInt32 = Int32Array(buf).unsafeCast() - 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() +private val bufFloat32 = Float32Array(buf).unsafeCast() +private val bufInt32 = Int32Array(buf).unsafeCast() - 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() === 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() === obj) { + return obj.toInt() + } + + bufFloat64[0] = obj + return bufInt32[highIndex] * 31 + bufInt32[lowIndex] } diff --git a/libraries/stdlib/js-ir/runtime/coreRuntime.kt b/libraries/stdlib/js-ir/runtime/coreRuntime.kt index 3d178f3de4e..e2ee9735641 100644 --- a/libraries/stdlib/js-ir/runtime/coreRuntime.kt +++ b/libraries/stdlib/js-ir/runtime/coreRuntime.kt @@ -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()) 1 else 0 else -> getStringHashCode(js("String(obj)")) } diff --git a/libraries/stdlib/js-ir/src/kotlin/math_js-ir.kt b/libraries/stdlib/js-ir/src/kotlin/math_js-ir.kt index 50c4009fa25..69456f6b51d 100644 --- a/libraries/stdlib/js-ir/src/kotlin/math_js-ir.kt +++ b/libraries/stdlib/js-ir/src/kotlin/math_js-ir.kt @@ -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 } \ No newline at end of file diff --git a/libraries/stdlib/js-ir/src/kotlin/numbers_js-ir.kt b/libraries/stdlib/js-ir/src/kotlin/numbers_js-ir.kt index 80f7082e2e0..771a4b9585f 100644 --- a/libraries/stdlib/js-ir/src/kotlin/numbers_js-ir.kt +++ b/libraries/stdlib/js-ir/src/kotlin/numbers_js-ir.kt @@ -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) \ No newline at end of file + floatFromBits(bits) \ No newline at end of file