[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:
Svyatoslav Kuzmich
2019-05-06 00:25:43 +03:00
parent 362fbc8770
commit bc29a5b15c
8 changed files with 230 additions and 85 deletions
-12
View File
@@ -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.
*/
+1 -55
View File
@@ -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()