diff --git a/libraries/stdlib/js-ir/runtime/bitUtils.kt b/libraries/stdlib/js-ir/runtime/bitUtils.kt new file mode 100644 index 00000000000..5450eba5c7b --- /dev/null +++ b/libraries/stdlib/js-ir/runtime/bitUtils.kt @@ -0,0 +1,65 @@ +/* + * Copyright 2010-2019 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 + +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 + + 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] + } +} diff --git a/libraries/stdlib/js-ir/runtime/core.kt b/libraries/stdlib/js-ir/runtime/core.kt index e8c5e430e6a..5d37c300b8f 100644 --- a/libraries/stdlib/js-ir/runtime/core.kt +++ b/libraries/stdlib/js-ir/runtime/core.kt @@ -5,6 +5,8 @@ package kotlin.js +import kotlin.js.internal.BitUtils + fun equals(obj1: dynamic, obj2: dynamic): Boolean { if (obj1 == null) { return obj2 == null @@ -43,7 +45,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" -> getNumberHashCode(obj) + "number" -> BitUtils.getNumberHashCode(obj) "boolean" -> if(obj.unsafeCast()) 1 else 0 else -> getStringHashCode(js("String(obj)")) } @@ -73,25 +75,8 @@ fun getStringHashCode(str: String): Int { return hash } -private external class ArrayBuffer(size: Int) -private external class Float64Array(buffer: ArrayBuffer) -private external class Int32Array(buffer: ArrayBuffer) - -fun getNumberHashCode(obj: Any?): Int { - if (jsBitwiseOr(obj, 0) === obj) { - return obj - } - - var byteBuffer = ArrayBuffer(8) - var bufFloat64 = Float64Array(byteBuffer).asDynamic() - var bufInt32 = Int32Array(byteBuffer).asDynamic() - bufFloat64[0] = obj - return jsBitwiseOr(bufInt32[1] * 31, 0) + bufInt32[0].unsafeCast() -} - fun identityHashCode(obj: Any?): Int = getObjectHashCode(obj) - @JsName("captureStack") internal fun captureStack(instance: Throwable) { if (js("Error").captureStackTrace) { diff --git a/libraries/stdlib/js-ir/src/kotlin/math.kt b/libraries/stdlib/js-ir/src/kotlin/math.kt new file mode 100644 index 00000000000..50c4009fa25 --- /dev/null +++ b/libraries/stdlib/js-ir/src/kotlin/math.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2010-2018 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. + * + * If [sign] is `NaN` the sign of the result is undefined. + */ +@SinceKotlin("1.2") +public actual fun Double.withSign(sign: Double): Double { + val thisSignBit = BitUtils.doubleSignBit(this) + val newSignBit = BitUtils.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.kt b/libraries/stdlib/js-ir/src/kotlin/numbers.kt new file mode 100644 index 00000000000..80f7082e2e0 --- /dev/null +++ b/libraries/stdlib/js-ir/src/kotlin/numbers.kt @@ -0,0 +1,64 @@ +/* + * Copyright 2010-2018 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) + +/** + * Returns a bit representation of the specified floating-point value as [Long] + * according to the IEEE 754 floating-point "double format" bit layout, + * preserving `NaN` values exact layout. + */ +@SinceKotlin("1.2") +public actual fun Double.toRawBits(): Long = + BitUtils.doubleToRawBits(this) + +/** + * Returns the [Double] value corresponding to a given bit representation. + */ +@SinceKotlin("1.2") +@kotlin.internal.InlineOnly +public actual inline fun Double.Companion.fromBits(bits: Long): Double = + BitUtils.doubleFromBits(bits) + +/** + * Returns a bit representation of the specified floating-point value as [Int] + * according to the IEEE 754 floating-point "single format" bit layout. + * + * Note that in Kotlin/JS [Float] range is wider than "single format" bit layout can represent, + * so some [Float] values may overflow, underflow or loose their accuracy after conversion to bits and back. + */ +@SinceKotlin("1.2") +public actual fun Float.toBits(): Int = + BitUtils.floatToRawBits(if (this.isNaN()) Float.NaN else this) + +/** + * Returns a bit representation of the specified floating-point value as [Int] + * according to the IEEE 754 floating-point "single format" bit layout, + * preserving `NaN` values exact layout. + * + * Note that in Kotlin/JS [Float] range is wider than "single format" bit layout can represent, + * so some [Float] values may overflow, underflow or loose their accuracy after conversion to bits and back. + */ +@SinceKotlin("1.2") +public actual fun Float.toRawBits(): Int = + BitUtils.floatToRawBits(this) + +/** + * Returns the [Float] value corresponding to a given bit representation. + */ +@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 diff --git a/libraries/stdlib/js-v1/src/kotlin/math.kt b/libraries/stdlib/js-v1/src/kotlin/math.kt new file mode 100644 index 00000000000..59a45a1efab --- /dev/null +++ b/libraries/stdlib/js-v1/src/kotlin/math.kt @@ -0,0 +1,17 @@ +/* + * Copyright 2010-2018 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 + +/** + * Returns this value with the sign bit same as of the [sign] value. + * + * If [sign] is `NaN` the sign of the result is undefined. + */ +@SinceKotlin("1.2") +public actual fun Double.withSign(sign: Double): Double { + val thisSignBit = js("Kotlin").doubleSignBit(this).unsafeCast() + val newSignBit = js("Kotlin").doubleSignBit(sign).unsafeCast() + return if (thisSignBit == newSignBit) this else -this +} \ No newline at end of file diff --git a/libraries/stdlib/js-v1/src/kotlin/numbers.kt b/libraries/stdlib/js-v1/src/kotlin/numbers.kt new file mode 100644 index 00000000000..fd83ff56fbc --- /dev/null +++ b/libraries/stdlib/js-v1/src/kotlin/numbers.kt @@ -0,0 +1,61 @@ +/* + * Copyright 2010-2018 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 + + +/** + * 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") +@library("doubleToBits") +public actual fun Double.toBits(): Long = definedExternally + +/** + * Returns a bit representation of the specified floating-point value as [Long] + * according to the IEEE 754 floating-point "double format" bit layout, + * preserving `NaN` values exact layout. + */ +@SinceKotlin("1.2") +@library("doubleToRawBits") +public actual fun Double.toRawBits(): Long = definedExternally + +/** + * Returns the [Double] value corresponding to a given bit representation. + */ +@SinceKotlin("1.2") +@kotlin.internal.InlineOnly +public actual inline fun Double.Companion.fromBits(bits: Long): Double = js("Kotlin").doubleFromBits(bits).unsafeCast() + +/** + * Returns a bit representation of the specified floating-point value as [Int] + * according to the IEEE 754 floating-point "single format" bit layout. + * + * Note that in Kotlin/JS [Float] range is wider than "single format" bit layout can represent, + * so some [Float] values may overflow, underflow or loose their accuracy after conversion to bits and back. + */ +@SinceKotlin("1.2") +@library("floatToBits") +public actual fun Float.toBits(): Int = definedExternally + +/** + * Returns a bit representation of the specified floating-point value as [Int] + * according to the IEEE 754 floating-point "single format" bit layout, + * preserving `NaN` values exact layout. + * + * Note that in Kotlin/JS [Float] range is wider than "single format" bit layout can represent, + * so some [Float] values may overflow, underflow or loose their accuracy after conversion to bits and back. + */ +@SinceKotlin("1.2") +@library("floatToRawBits") +public actual fun Float.toRawBits(): Int = definedExternally + +/** + * Returns the [Float] value corresponding to a given bit representation. + */ +@SinceKotlin("1.2") +@kotlin.internal.InlineOnly +public actual inline fun Float.Companion.fromBits(bits: Int): Float = js("Kotlin").floatFromBits(bits).unsafeCast() \ No newline at end of file diff --git a/libraries/stdlib/js/src/kotlin/math.kt b/libraries/stdlib/js/src/kotlin/math.kt index 8b03ec2cbff..f96e74f447f 100644 --- a/libraries/stdlib/js/src/kotlin/math.kt +++ b/libraries/stdlib/js/src/kotlin/math.kt @@ -432,18 +432,6 @@ public actual inline val Double.absoluteValue: Double get() = nativeMath.abs(thi @InlineOnly public actual inline val Double.sign: Double get() = nativeMath.sign(this) -/** - * Returns this value with the sign bit same as of the [sign] value. - * - * If [sign] is `NaN` the sign of the result is undefined. - */ -@SinceKotlin("1.2") -public actual fun Double.withSign(sign: Double): Double { - val thisSignBit = js("Kotlin").doubleSignBit(this).unsafeCast() - val newSignBit = js("Kotlin").doubleSignBit(sign).unsafeCast() - return if (thisSignBit == newSignBit) this else -this -} - /** * Returns this value with the sign bit same as of the [sign] value. */ diff --git a/libraries/stdlib/js/src/kotlin/numbers.kt b/libraries/stdlib/js/src/kotlin/numbers.kt index ec93991969c..234b86c291a 100644 --- a/libraries/stdlib/js/src/kotlin/numbers.kt +++ b/libraries/stdlib/js/src/kotlin/numbers.kt @@ -35,58 +35,4 @@ public actual fun Double.isFinite(): Boolean = !isInfinite() && !isNaN() /** * Returns `true` if the argument is a finite floating-point value; returns `false` otherwise (for `NaN` and infinity arguments). */ -public actual fun Float.isFinite(): Boolean = !isInfinite() && !isNaN() - -/** - * 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") -@library("doubleToBits") -public actual fun Double.toBits(): Long = definedExternally - -/** - * Returns a bit representation of the specified floating-point value as [Long] - * according to the IEEE 754 floating-point "double format" bit layout, - * preserving `NaN` values exact layout. - */ -@SinceKotlin("1.2") -@library("doubleToRawBits") -public actual fun Double.toRawBits(): Long = definedExternally - -/** - * Returns the [Double] value corresponding to a given bit representation. - */ -@SinceKotlin("1.2") -@kotlin.internal.InlineOnly -public actual inline fun Double.Companion.fromBits(bits: Long): Double = js("Kotlin").doubleFromBits(bits).unsafeCast() - -/** - * Returns a bit representation of the specified floating-point value as [Int] - * according to the IEEE 754 floating-point "single format" bit layout. - * - * Note that in Kotlin/JS [Float] range is wider than "single format" bit layout can represent, - * so some [Float] values may overflow, underflow or loose their accuracy after conversion to bits and back. - */ -@SinceKotlin("1.2") -@library("floatToBits") -public actual fun Float.toBits(): Int = definedExternally - -/** - * Returns a bit representation of the specified floating-point value as [Int] - * according to the IEEE 754 floating-point "single format" bit layout, - * preserving `NaN` values exact layout. - * - * Note that in Kotlin/JS [Float] range is wider than "single format" bit layout can represent, - * so some [Float] values may overflow, underflow or loose their accuracy after conversion to bits and back. - */ -@SinceKotlin("1.2") -@library("floatToRawBits") -public actual fun Float.toRawBits(): Int = definedExternally - -/** - * Returns the [Float] value corresponding to a given bit representation. - */ -@SinceKotlin("1.2") -@kotlin.internal.InlineOnly -public actual inline fun Float.Companion.fromBits(bits: Int): Float = js("Kotlin").floatFromBits(bits).unsafeCast() \ No newline at end of file +public actual fun Float.isFinite(): Boolean = !isInfinite() && !isNaN() \ No newline at end of file