[JS IR] stdlib: port methods that use bit representation of numbers
- Port part of 'misc.js' from current backend to 'bitUtils.kt' in IR backend - Enable toBits, toRawBits, fromBits extension function on Double and Float - Enable Double.withSign - Refactor getNumberHashCode using new utils
This commit is contained in:
@@ -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<DoubleArray>()
|
||||
private val bufFloat32 = Float32Array(buf).unsafeCast<FloatArray>()
|
||||
private val bufInt32 = Int32Array(buf).unsafeCast<IntArray>()
|
||||
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<Double>() === obj) {
|
||||
return obj.toInt()
|
||||
}
|
||||
|
||||
bufFloat64[0] = obj
|
||||
return bufInt32[highIndex] * 31 + bufInt32[lowIndex]
|
||||
}
|
||||
}
|
||||
@@ -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<Boolean>()) 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<Int>()
|
||||
}
|
||||
|
||||
fun identityHashCode(obj: Any?): Int = getObjectHashCode(obj)
|
||||
|
||||
|
||||
@JsName("captureStack")
|
||||
internal fun captureStack(instance: Throwable) {
|
||||
if (js("Error").captureStackTrace) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
@@ -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<Int>()
|
||||
val newSignBit = js("Kotlin").doubleSignBit(sign).unsafeCast<Int>()
|
||||
return if (thisSignBit == newSignBit) this else -this
|
||||
}
|
||||
@@ -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<Double>()
|
||||
|
||||
/**
|
||||
* 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<Float>()
|
||||
@@ -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<Int>()
|
||||
val newSignBit = js("Kotlin").doubleSignBit(sign).unsafeCast<Int>()
|
||||
return if (thisSignBit == newSignBit) this else -this
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this value with the sign bit same as of the [sign] value.
|
||||
*/
|
||||
|
||||
@@ -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<Double>()
|
||||
|
||||
/**
|
||||
* 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<Float>()
|
||||
public actual fun Float.isFinite(): Boolean = !isInfinite() && !isNaN()
|
||||
Reference in New Issue
Block a user