From 1d7ee22bdc2979dc20de7bcc364385ed283dc5d0 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 18 Sep 2018 17:57:18 +0300 Subject: [PATCH] Simplify comparisons for small unsigned types that can fit in Int UByte and UShort can be extended to Int exactly and then compared as ints --- generators/builtins/unsignedTypes.kt | 8 ++++++-- libraries/stdlib/unsigned/src/kotlin/UByte.kt | 4 ++-- libraries/stdlib/unsigned/src/kotlin/UShort.kt | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/generators/builtins/unsignedTypes.kt b/generators/builtins/unsignedTypes.kt index d2184cb904f..e5d6e2359c4 100644 --- a/generators/builtins/unsignedTypes.kt +++ b/generators/builtins/unsignedTypes.kt @@ -109,8 +109,12 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns if (otherType == type && maxByDomainCapacity(type, UnsignedType.UINT) == type) { out.println("${className.toLowerCase()}Compare(this.data, other.data)") } else { - val ctype = maxByDomainCapacity(maxByDomainCapacity(type, otherType), UnsignedType.UINT) - out.println("${convert("this", type, ctype)}.compareTo(${convert("other", otherType, ctype)})") + if (maxOf(type, otherType) < UnsignedType.UINT) { + out.println("this.toInt().compareTo(other.toInt())") + } else { + val ctype = maxByDomainCapacity(type, otherType) + out.println("${convert("this", type, ctype)}.compareTo(${convert("other", otherType, ctype)})") + } } } out.println() diff --git a/libraries/stdlib/unsigned/src/kotlin/UByte.kt b/libraries/stdlib/unsigned/src/kotlin/UByte.kt index 8353e55dfcf..5af9e4a64e6 100644 --- a/libraries/stdlib/unsigned/src/kotlin/UByte.kt +++ b/libraries/stdlib/unsigned/src/kotlin/UByte.kt @@ -43,7 +43,7 @@ public inline class UByte @PublishedApi internal constructor(@PublishedApi inter */ @kotlin.internal.InlineOnly @Suppress("OVERRIDE_BY_INLINE") - public override inline operator fun compareTo(other: UByte): Int = this.toUInt().compareTo(other.toUInt()) + public override inline operator fun compareTo(other: UByte): Int = this.toInt().compareTo(other.toInt()) /** * Compares this value with the specified value for order. @@ -51,7 +51,7 @@ public inline class UByte @PublishedApi internal constructor(@PublishedApi inter * or a positive number if it's greater than other. */ @kotlin.internal.InlineOnly - public inline operator fun compareTo(other: UShort): Int = this.toUInt().compareTo(other.toUInt()) + public inline operator fun compareTo(other: UShort): Int = this.toInt().compareTo(other.toInt()) /** * Compares this value with the specified value for order. diff --git a/libraries/stdlib/unsigned/src/kotlin/UShort.kt b/libraries/stdlib/unsigned/src/kotlin/UShort.kt index 578c069eb7f..4a0fb15096e 100644 --- a/libraries/stdlib/unsigned/src/kotlin/UShort.kt +++ b/libraries/stdlib/unsigned/src/kotlin/UShort.kt @@ -42,7 +42,7 @@ public inline class UShort @PublishedApi internal constructor(@PublishedApi inte * or a positive number if it's greater than other. */ @kotlin.internal.InlineOnly - public inline operator fun compareTo(other: UByte): Int = this.toUInt().compareTo(other.toUInt()) + public inline operator fun compareTo(other: UByte): Int = this.toInt().compareTo(other.toInt()) /** * Compares this value with the specified value for order. @@ -51,7 +51,7 @@ public inline class UShort @PublishedApi internal constructor(@PublishedApi inte */ @kotlin.internal.InlineOnly @Suppress("OVERRIDE_BY_INLINE") - public override inline operator fun compareTo(other: UShort): Int = this.toUInt().compareTo(other.toUInt()) + public override inline operator fun compareTo(other: UShort): Int = this.toInt().compareTo(other.toInt()) /** * Compares this value with the specified value for order.