Provide Double and Float bit conversion functions as extensions in JS and Common
Instance extension: Double/Float.toBits/toRawBits Companion extension: Double/Float.Companion.fromBits #KT-18264 Fixed
This commit is contained in:
@@ -31,3 +31,57 @@ public 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 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 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 fun Double.toRawBits(): Long = definedExternally
|
||||
|
||||
/**
|
||||
* Returns the [Double] value corresponding to a given bit representation.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@kotlin.internal.InlineOnly
|
||||
public 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 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 fun Float.toRawBits(): Int = definedExternally
|
||||
|
||||
/**
|
||||
* Returns the [Float] value corresponding to a given bit representation.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun Float.Companion.fromBits(bits: Int): Float = js("Kotlin").floatFromBits(bits).unsafeCast<Float>()
|
||||
@@ -48,3 +48,41 @@ Kotlin.imulEmulated = imul;
|
||||
function imul(a, b) {
|
||||
return ((a & 0xffff0000) * (b & 0xffff) + (a & 0xffff) * (b | 0)) | 0;
|
||||
}
|
||||
|
||||
(function() {
|
||||
var buf = new ArrayBuffer(8);
|
||||
var bufFloat64 = new Float64Array(buf);
|
||||
var bufFloat32 = new Float32Array(buf);
|
||||
var bufInt32 = new Uint32Array(buf);
|
||||
|
||||
Kotlin.doubleToBits = function(value) {
|
||||
return Kotlin.doubleToRawBits(isNaN(value) ? NaN : value);
|
||||
};
|
||||
|
||||
Kotlin.doubleToRawBits = function(value) {
|
||||
bufFloat64[0] = value;
|
||||
return Kotlin.Long.fromBits(bufInt32[0], bufInt32[1]);
|
||||
};
|
||||
|
||||
Kotlin.doubleFromBits = function(value) {
|
||||
bufInt32[0] = value.low_;
|
||||
bufInt32[1] = value.high_;
|
||||
return bufFloat64[0];
|
||||
};
|
||||
|
||||
Kotlin.floatToBits = function(value) {
|
||||
return Kotlin.floatToRawBits(isNaN(value) ? NaN : value);
|
||||
};
|
||||
|
||||
Kotlin.floatToRawBits = function(value) {
|
||||
bufFloat32[0] = value;
|
||||
return bufInt32[0];
|
||||
};
|
||||
|
||||
Kotlin.floatFromBits = function(value) {
|
||||
bufInt32[0] = value;
|
||||
return bufFloat32[0];
|
||||
};
|
||||
})();
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user