[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,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)
|
||||
Reference in New Issue
Block a user