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
This commit is contained in:
Ilya Gorbunov
2018-09-18 17:57:18 +03:00
parent af29dced98
commit 1d7ee22bdc
3 changed files with 10 additions and 6 deletions
+6 -2
View File
@@ -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()
@@ -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.
@@ -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.