From f3fc1197ae2cda9421bd6a944daf8d43e5109475 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 8 Oct 2019 19:42:42 +0300 Subject: [PATCH] Add SIZE_BITS and SIZE_BYTES constants to Double and Float #KT-29182 --- core/builtins/native/kotlin/Primitives.kt | 24 +++++++++++++++++++ generators/builtins/primitives.kt | 7 +++--- libraries/stdlib/js-ir/builtins/Primitives.kt | 24 +++++++++++++++++++ .../js/runtime/primitiveCompanionObjects.kt | 12 ++++++++++ .../jvm/internal/PrimitiveCompanionObjects.kt | 4 ++++ .../kotlin-stdlib-runtime-merged.txt | 4 ++++ 6 files changed, 72 insertions(+), 3 deletions(-) diff --git a/core/builtins/native/kotlin/Primitives.kt b/core/builtins/native/kotlin/Primitives.kt index f1c1963815b..9bf85e13b27 100644 --- a/core/builtins/native/kotlin/Primitives.kt +++ b/core/builtins/native/kotlin/Primitives.kt @@ -919,6 +919,18 @@ public class Float private constructor() : Number(), Comparable { * A constant holding the "not a number" value of Float. */ public const val NaN: Float = -(0.0F/0.0F) + + /** + * The number of bytes used to represent an instance of Float in a binary form. + */ + @SinceKotlin("1.4") + public const val SIZE_BYTES: Int = 4 + + /** + * The number of bits used to represent an instance of Float in a binary form. + */ + @SinceKotlin("1.4") + public const val SIZE_BITS: Int = 32 } /** @@ -1120,6 +1132,18 @@ public class Double private constructor() : Number(), Comparable { * A constant holding the "not a number" value of Double. */ public const val NaN: Double = -(0.0/0.0) + + /** + * The number of bytes used to represent an instance of Double in a binary form. + */ + @SinceKotlin("1.4") + public const val SIZE_BYTES: Int = 8 + + /** + * The number of bits used to represent an instance of Double in a binary form. + */ + @SinceKotlin("1.4") + public const val SIZE_BITS: Int = 64 } /** diff --git a/generators/builtins/primitives.kt b/generators/builtins/primitives.kt index a0a3b3f4eef..67d0fe29bd8 100644 --- a/generators/builtins/primitives.kt +++ b/generators/builtins/primitives.kt @@ -100,18 +100,19 @@ class GeneratePrimitives(out: PrintWriter) : BuiltInsSourceGenerator(out) { */ public const val MAX_VALUE: $className = $maxValue""") } - if (kind.isIntegral) { + if (kind.isIntegral || kind.isFloatingPoint) { + val sizeSince = if (kind.isFloatingPoint) "1.4" else "1.3" out.println(""" /** * The number of bytes used to represent an instance of $className in a binary form. */ - @SinceKotlin("1.3") + @SinceKotlin("$sizeSince") public const val SIZE_BYTES: Int = ${kind.byteSize} /** * The number of bits used to represent an instance of $className in a binary form. */ - @SinceKotlin("1.3") + @SinceKotlin("$sizeSince") public const val SIZE_BITS: Int = ${kind.bitSize}""") } out.println(""" }""") diff --git a/libraries/stdlib/js-ir/builtins/Primitives.kt b/libraries/stdlib/js-ir/builtins/Primitives.kt index d21afaf6fe4..6a9ae8b7642 100644 --- a/libraries/stdlib/js-ir/builtins/Primitives.kt +++ b/libraries/stdlib/js-ir/builtins/Primitives.kt @@ -701,6 +701,18 @@ public class Float private constructor() : Number(), Comparable { * A constant holding the "not a number" value of Float. */ public const val NaN: Float = -(0.0F/0.0F) + + /** + * The number of bytes used to represent an instance of Float in a binary form. + */ + @SinceKotlin("1.4") + public const val SIZE_BYTES: Int = 4 + + /** + * The number of bits used to represent an instance of Float in a binary form. + */ + @SinceKotlin("1.4") + public const val SIZE_BITS: Int = 32 } /** @@ -906,6 +918,18 @@ public class Double private constructor() : Number(), Comparable { * A constant holding the "not a number" value of Double. */ public const val NaN: Double = -(0.0/0.0) + + /** + * The number of bytes used to represent an instance of Double in a binary form. + */ + @SinceKotlin("1.4") + public const val SIZE_BYTES: Int = 8 + + /** + * The number of bits used to represent an instance of Double in a binary form. + */ + @SinceKotlin("1.4") + public const val SIZE_BITS: Int = 64 } /** diff --git a/libraries/stdlib/js/runtime/primitiveCompanionObjects.kt b/libraries/stdlib/js/runtime/primitiveCompanionObjects.kt index 664fd4dd8c1..06365bbfd78 100644 --- a/libraries/stdlib/js/runtime/primitiveCompanionObjects.kt +++ b/libraries/stdlib/js/runtime/primitiveCompanionObjects.kt @@ -21,6 +21,12 @@ internal object DoubleCompanionObject { @JsName("NaN") const val NaN: Double = -(0.0/0.0) + + @JsName("SIZE_BYTES") + const val SIZE_BYTES = 8 + + @JsName("SIZE_BITS") + const val SIZE_BITS = 64 } @JsName("FloatCompanionObject") @@ -39,6 +45,12 @@ internal object FloatCompanionObject { @JsName("NaN") const val NaN: Float = -(0.0F/0.0F) + + @JsName("SIZE_BYTES") + const val SIZE_BYTES = 4 + + @JsName("SIZE_BITS") + const val SIZE_BITS = 32 } @JsName("IntCompanionObject") diff --git a/libraries/stdlib/jvm/runtime/kotlin/jvm/internal/PrimitiveCompanionObjects.kt b/libraries/stdlib/jvm/runtime/kotlin/jvm/internal/PrimitiveCompanionObjects.kt index ea355251d4b..7c48793276c 100644 --- a/libraries/stdlib/jvm/runtime/kotlin/jvm/internal/PrimitiveCompanionObjects.kt +++ b/libraries/stdlib/jvm/runtime/kotlin/jvm/internal/PrimitiveCompanionObjects.kt @@ -11,6 +11,8 @@ internal object DoubleCompanionObject { const val POSITIVE_INFINITY: Double = java.lang.Double.POSITIVE_INFINITY const val NEGATIVE_INFINITY: Double = java.lang.Double.NEGATIVE_INFINITY const val NaN: Double = java.lang.Double.NaN + const val SIZE_BYTES: Int = 8 + const val SIZE_BITS: Int = SIZE_BYTES * 8 // for binary compatibility with pre 1.4 fun getMIN_VALUE(): Double = java.lang.Double.MIN_VALUE @@ -26,6 +28,8 @@ internal object FloatCompanionObject { const val POSITIVE_INFINITY: Float = java.lang.Float.POSITIVE_INFINITY const val NEGATIVE_INFINITY: Float = java.lang.Float.NEGATIVE_INFINITY const val NaN: Float = java.lang.Float.NaN + const val SIZE_BYTES: Int = 4 + const val SIZE_BITS: Int = SIZE_BYTES * 8 // for binary compatibility with pre 1.4 fun getMIN_VALUE(): Float = java.lang.Float.MIN_VALUE diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt index bede34fd228..28ad08ebb12 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt @@ -3318,6 +3318,8 @@ public final class kotlin/jvm/internal/DoubleCompanionObject { public static final field NEGATIVE_INFINITY D public static final field NaN D public static final field POSITIVE_INFINITY D + public static final field SIZE_BITS I + public static final field SIZE_BYTES I public final fun getMAX_VALUE ()D public final fun getMIN_VALUE ()D public final fun getNEGATIVE_INFINITY ()D @@ -3343,6 +3345,8 @@ public final class kotlin/jvm/internal/FloatCompanionObject { public static final field NEGATIVE_INFINITY F public static final field NaN F public static final field POSITIVE_INFINITY F + public static final field SIZE_BITS I + public static final field SIZE_BYTES I public final fun getMAX_VALUE ()F public final fun getMIN_VALUE ()F public final fun getNEGATIVE_INFINITY ()F