From 132f2f88c232301b0ff31058c16e2000f07ce01c Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 31 Aug 2017 07:05:35 +0300 Subject: [PATCH] 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 --- js/js.libraries/src/core/numbers.kt | 54 +++++++++++++++++++ js/js.libraries/src/js/misc.js | 38 +++++++++++++ libraries/stdlib/common/src/kotlin/KotlinH.kt | 42 +++++++++++++++ libraries/stdlib/src/kotlin/util/Numbers.kt | 2 +- .../stdlib/test/numbers/NumbersJVMTest.kt | 12 ----- libraries/stdlib/test/numbers/NumbersTest.kt | 49 +++++++++++++++++ 6 files changed, 184 insertions(+), 13 deletions(-) diff --git a/js/js.libraries/src/core/numbers.kt b/js/js.libraries/src/core/numbers.kt index 53ef6fbb069..bd63af2abbf 100644 --- a/js/js.libraries/src/core/numbers.kt +++ b/js/js.libraries/src/core/numbers.kt @@ -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() + +/** + * 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() \ No newline at end of file diff --git a/js/js.libraries/src/js/misc.js b/js/js.libraries/src/js/misc.js index 54feff2cef6..846946783e3 100644 --- a/js/js.libraries/src/js/misc.js +++ b/js/js.libraries/src/js/misc.js @@ -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]; + }; +})(); + + diff --git a/libraries/stdlib/common/src/kotlin/KotlinH.kt b/libraries/stdlib/common/src/kotlin/KotlinH.kt index 502ebe321dd..d85504020f1 100644 --- a/libraries/stdlib/common/src/kotlin/KotlinH.kt +++ b/libraries/stdlib/common/src/kotlin/KotlinH.kt @@ -90,6 +90,48 @@ expect fun Float.isInfinite(): Boolean expect fun Double.isFinite(): Boolean expect fun Float.isFinite(): Boolean +/** + * 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 expect fun Double.toBits(): Long + +/** + * 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 expect fun Double.toRawBits(): Long + +/** + * Returns the [Double] value corresponding to a given bit representation. + */ +@SinceKotlin("1.2") +public expect fun Double.Companion.fromBits(bits: Long): Double + +/** + * Returns a bit representation of the specified floating-point value as [Int] + * according to the IEEE 754 floating-point "single format" bit layout. + */ +@SinceKotlin("1.2") +public expect fun Float.toBits(): Int + +/** + * 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. + */ +@SinceKotlin("1.2") +public expect fun Float.toRawBits(): Int + +/** + * Returns the [Float] value corresponding to a given bit representation. + */ +@SinceKotlin("1.2") +public expect fun Float.Companion.fromBits(bits: Int): Float + // From concurrent.kt diff --git a/libraries/stdlib/src/kotlin/util/Numbers.kt b/libraries/stdlib/src/kotlin/util/Numbers.kt index ebd79090e8c..00ff9569e9f 100644 --- a/libraries/stdlib/src/kotlin/util/Numbers.kt +++ b/libraries/stdlib/src/kotlin/util/Numbers.kt @@ -63,7 +63,7 @@ public inline fun Double.toRawBits(): Long = java.lang.Double.doubleToRawLongBit */ @SinceKotlin("1.2") @kotlin.internal.InlineOnly -public inline fun Double.Companion.fromBits(bits: Long) = java.lang.Double.longBitsToDouble(bits) +public inline fun Double.Companion.fromBits(bits: Long): Double = java.lang.Double.longBitsToDouble(bits) /** * Returns a bit representation of the specified floating-point value as [Int] diff --git a/libraries/stdlib/test/numbers/NumbersJVMTest.kt b/libraries/stdlib/test/numbers/NumbersJVMTest.kt index 6dcfeaa5f4b..bc43ed780b6 100644 --- a/libraries/stdlib/test/numbers/NumbersJVMTest.kt +++ b/libraries/stdlib/test/numbers/NumbersJVMTest.kt @@ -41,18 +41,6 @@ class NumbersJVMTest { assertEquals(java.lang.Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY) } - @Test fun doubleToBits() { - assertEquals(0x400921fb54442d18L, kotlin.math.PI.toBits()) - assertEquals(kotlin.math.PI, Double.fromBits(0x400921fb54442d18L)) - - for (value in listOf(Double.NEGATIVE_INFINITY, -Double.MAX_VALUE, -1.0, -Double.MIN_VALUE, -0.0, 0.0, Double.POSITIVE_INFINITY, Double.MAX_VALUE, 1.0, Double.MIN_VALUE)) { - assertEquals(value, Double.fromBits(value.toBits())) - assertEquals(value, Double.fromBits(value.toRawBits())) - } - assertTrue(Double.NaN.toBits().let(Double.Companion::fromBits).isNaN()) - assertTrue(Double.NaN.toRawBits().let { Double.fromBits(it) }.isNaN()) - } - @Test fun floatToBits() { val PI_F = kotlin.math.PI.toFloat() assertEquals(0x40490fdb, PI_F.toBits()) diff --git a/libraries/stdlib/test/numbers/NumbersTest.kt b/libraries/stdlib/test/numbers/NumbersTest.kt index 47d04b0beda..6051a0f1aaf 100644 --- a/libraries/stdlib/test/numbers/NumbersTest.kt +++ b/libraries/stdlib/test/numbers/NumbersTest.kt @@ -120,4 +120,53 @@ class NumbersTest { assertEquals(!isNaN && !isInfinite, value.isFinite()) } + @Test fun doubleToBits() { + assertEquals(0x400921fb54442d18L, kotlin.math.PI.toBits()) + assertEquals(0x400921fb54442d18L, kotlin.math.PI.toRawBits()) + assertEquals(kotlin.math.PI, Double.fromBits(0x400921fb54442d18L)) + + for (value in listOf(Double.NEGATIVE_INFINITY, -Double.MAX_VALUE, -1.0, -Double.MIN_VALUE, -0.0, 0.0, Double.POSITIVE_INFINITY, Double.MAX_VALUE, 1.0, Double.MIN_VALUE)) { + assertEquals(value, Double.fromBits(value.toBits())) + assertEquals(value, Double.fromBits(value.toRawBits())) + } + assertTrue(Double.NaN.toBits().let(Double.Companion::fromBits).isNaN()) + assertTrue(Double.NaN.toRawBits().let { Double.fromBits(it) }.isNaN()) + + assertEquals(0x7FF00000L shl 32, Double.POSITIVE_INFINITY.toBits()) + assertEquals(0xFFF00000L shl 32, Double.NEGATIVE_INFINITY.toBits()) + + assertEquals(0x7FF80000_00000000L, Double.NaN.toBits()) + assertEquals(0x7FF80000_00000000L, Double.NaN.toRawBits()) + + val bitsNaN = Double.NaN.toBits() + for (bitsDenormNaN in listOf(0xFFF80000L shl 32, bitsNaN or 1)) { + assertTrue(Double.fromBits(bitsDenormNaN).isNaN(), "expected $bitsDenormNaN represent NaN") + assertEquals(bitsNaN, Double.fromBits(bitsDenormNaN).toBits()) + } + } + + @Test fun floatToBits() { + val PI_F = kotlin.math.PI.toFloat() + assertEquals(0x40490fdb, PI_F.toBits()) + assertAlmostEquals(PI_F, Float.fromBits(0x40490fdb)) // PI_F is actually Double in JS + // -Float.MAX_VALUE, Float.MAX_VALUE, -Float.MIN_VALUE, Float.MIN_VALUE: overflow or underflow + for (value in listOf(Float.NEGATIVE_INFINITY, -1.0F, -0.0F, 0.0F, Float.POSITIVE_INFINITY, 1.0F)) { + assertEquals(value, Float.fromBits(value.toBits())) + assertEquals(value, Float.fromBits(value.toRawBits())) + } + + assertTrue(Float.NaN.toBits().let(Float.Companion::fromBits).isNaN()) + assertTrue(Float.NaN.toRawBits().let { Float.fromBits(it) }.isNaN()) + + assertEquals(0x7fc00000, Float.NaN.toBits()) + assertEquals(0x7fc00000, Float.NaN.toRawBits()) + + val bitsNaN = Float.NaN.toBits() + for (bitsDenormNaN in listOf(0xFFFC0000.toInt(), bitsNaN or 1)) { + assertTrue(Float.fromBits(bitsDenormNaN).isNaN(), "expected $bitsDenormNaN represent NaN") + assertEquals(bitsNaN, Float.fromBits(bitsDenormNaN).toBits()) + } + } + + } \ No newline at end of file