Implement unsigned/signed conversions
This commit is contained in:
@@ -44,6 +44,9 @@ enum class UnsignedType {
|
|||||||
|
|
||||||
val capitalized: String get() = name.substring(0, 2) + name.substring(2).toLowerCase()
|
val capitalized: String get() = name.substring(0, 2) + name.substring(2).toLowerCase()
|
||||||
val asSigned: PrimitiveType = PrimitiveType.valueOf(name.substring(1))
|
val asSigned: PrimitiveType = PrimitiveType.valueOf(name.substring(1))
|
||||||
|
|
||||||
|
val byteSize = (1 shl ordinal)
|
||||||
|
val mask = "0x${List(byteSize) { "FF" }.chunked(2).joinToString("_") { it.joinToString("") }}"
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class ProgressionKind {
|
enum class ProgressionKind {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.generators.builtins.unsigned
|
|||||||
|
|
||||||
|
|
||||||
import org.jetbrains.kotlin.generators.builtins.UnsignedType
|
import org.jetbrains.kotlin.generators.builtins.UnsignedType
|
||||||
|
import org.jetbrains.kotlin.generators.builtins.convert
|
||||||
import org.jetbrains.kotlin.generators.builtins.generateBuiltIns.BuiltInsSourceGenerator
|
import org.jetbrains.kotlin.generators.builtins.generateBuiltIns.BuiltInsSourceGenerator
|
||||||
import org.jetbrains.kotlin.generators.builtins.ranges.GeneratePrimitives
|
import org.jetbrains.kotlin.generators.builtins.ranges.GeneratePrimitives
|
||||||
import java.io.File
|
import java.io.File
|
||||||
@@ -160,13 +161,19 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns
|
|||||||
private fun generateMemberConversions() {
|
private fun generateMemberConversions() {
|
||||||
for (otherType in UnsignedType.values()) {
|
for (otherType in UnsignedType.values()) {
|
||||||
val signed = otherType.asSigned.capitalized
|
val signed = otherType.asSigned.capitalized
|
||||||
out.println(" public fun to$signed(): $signed = TODO()")
|
out.print(" public fun to$signed(): $signed = ")
|
||||||
|
out.println(when {
|
||||||
|
otherType < type -> "data.to$signed()"
|
||||||
|
otherType == type -> "data"
|
||||||
|
else -> "data.to$signed() and ${type.mask}"
|
||||||
|
})
|
||||||
}
|
}
|
||||||
out.println()
|
out.println()
|
||||||
|
|
||||||
for (otherType in UnsignedType.values()) {
|
for (otherType in UnsignedType.values()) {
|
||||||
val name = otherType.capitalized
|
val name = otherType.capitalized
|
||||||
out.println(" public fun to$name(): $name = TODO()")
|
out.print(" public fun to$name(): $name = ")
|
||||||
|
out.println(if (type == otherType) "this" else "data.to${otherType.capitalized}()")
|
||||||
}
|
}
|
||||||
out.println()
|
out.println()
|
||||||
}
|
}
|
||||||
@@ -175,7 +182,12 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns
|
|||||||
for (otherType in UnsignedType.values()) {
|
for (otherType in UnsignedType.values()) {
|
||||||
val otherSigned = otherType.asSigned.capitalized
|
val otherSigned = otherType.asSigned.capitalized
|
||||||
val thisSigned = type.asSigned.capitalized
|
val thisSigned = type.asSigned.capitalized
|
||||||
out.println("public fun $otherSigned.to$className(): $className = $className(this.to$thisSigned())")
|
out.print("public fun $otherSigned.to$className(): $className = ")
|
||||||
|
out.println(when {
|
||||||
|
otherType < type -> "$className(this.to$thisSigned() and ${otherType.mask})"
|
||||||
|
otherType == type -> "$className(this)"
|
||||||
|
else -> "$className(this.to$thisSigned())"
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -113,19 +113,19 @@ public inline class UByte internal constructor(private val data: Byte) : Compara
|
|||||||
/** Inverts the bits in this value. */
|
/** Inverts the bits in this value. */
|
||||||
public fun inv(): UByte = UByte(data.inv())
|
public fun inv(): UByte = UByte(data.inv())
|
||||||
|
|
||||||
public fun toByte(): Byte = TODO()
|
public fun toByte(): Byte = data
|
||||||
public fun toShort(): Short = TODO()
|
public fun toShort(): Short = data.toShort() and 0xFF
|
||||||
public fun toInt(): Int = TODO()
|
public fun toInt(): Int = data.toInt() and 0xFF
|
||||||
public fun toLong(): Long = TODO()
|
public fun toLong(): Long = data.toLong() and 0xFF
|
||||||
|
|
||||||
public fun toUByte(): UByte = TODO()
|
public fun toUByte(): UByte = this
|
||||||
public fun toUShort(): UShort = TODO()
|
public fun toUShort(): UShort = data.toUShort()
|
||||||
public fun toUInt(): UInt = TODO()
|
public fun toUInt(): UInt = data.toUInt()
|
||||||
public fun toULong(): ULong = TODO()
|
public fun toULong(): ULong = data.toULong()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Byte.toUByte(): UByte = UByte(this.toByte())
|
public fun Byte.toUByte(): UByte = UByte(this)
|
||||||
public fun Short.toUByte(): UByte = UByte(this.toByte())
|
public fun Short.toUByte(): UByte = UByte(this.toByte())
|
||||||
public fun Int.toUByte(): UByte = UByte(this.toByte())
|
public fun Int.toUByte(): UByte = UByte(this.toByte())
|
||||||
public fun Long.toUByte(): UByte = UByte(this.toByte())
|
public fun Long.toUByte(): UByte = UByte(this.toByte())
|
||||||
|
|||||||
@@ -117,19 +117,19 @@ public inline class UInt internal constructor(private val data: Int) : Comparabl
|
|||||||
/** Inverts the bits in this value. */
|
/** Inverts the bits in this value. */
|
||||||
public fun inv(): UInt = UInt(data.inv())
|
public fun inv(): UInt = UInt(data.inv())
|
||||||
|
|
||||||
public fun toByte(): Byte = TODO()
|
public fun toByte(): Byte = data.toByte()
|
||||||
public fun toShort(): Short = TODO()
|
public fun toShort(): Short = data.toShort()
|
||||||
public fun toInt(): Int = TODO()
|
public fun toInt(): Int = data
|
||||||
public fun toLong(): Long = TODO()
|
public fun toLong(): Long = data.toLong() and 0xFFFF_FFFF
|
||||||
|
|
||||||
public fun toUByte(): UByte = TODO()
|
public fun toUByte(): UByte = data.toUByte()
|
||||||
public fun toUShort(): UShort = TODO()
|
public fun toUShort(): UShort = data.toUShort()
|
||||||
public fun toUInt(): UInt = TODO()
|
public fun toUInt(): UInt = this
|
||||||
public fun toULong(): ULong = TODO()
|
public fun toULong(): ULong = data.toULong()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Byte.toUInt(): UInt = UInt(this.toInt())
|
public fun Byte.toUInt(): UInt = UInt(this.toInt() and 0xFF)
|
||||||
public fun Short.toUInt(): UInt = UInt(this.toInt())
|
public fun Short.toUInt(): UInt = UInt(this.toInt() and 0xFFFF)
|
||||||
public fun Int.toUInt(): UInt = UInt(this.toInt())
|
public fun Int.toUInt(): UInt = UInt(this)
|
||||||
public fun Long.toUInt(): UInt = UInt(this.toInt())
|
public fun Long.toUInt(): UInt = UInt(this.toInt())
|
||||||
|
|||||||
@@ -117,19 +117,19 @@ public inline class ULong internal constructor(private val data: Long) : Compara
|
|||||||
/** Inverts the bits in this value. */
|
/** Inverts the bits in this value. */
|
||||||
public fun inv(): ULong = ULong(data.inv())
|
public fun inv(): ULong = ULong(data.inv())
|
||||||
|
|
||||||
public fun toByte(): Byte = TODO()
|
public fun toByte(): Byte = data.toByte()
|
||||||
public fun toShort(): Short = TODO()
|
public fun toShort(): Short = data.toShort()
|
||||||
public fun toInt(): Int = TODO()
|
public fun toInt(): Int = data.toInt()
|
||||||
public fun toLong(): Long = TODO()
|
public fun toLong(): Long = data
|
||||||
|
|
||||||
public fun toUByte(): UByte = TODO()
|
public fun toUByte(): UByte = data.toUByte()
|
||||||
public fun toUShort(): UShort = TODO()
|
public fun toUShort(): UShort = data.toUShort()
|
||||||
public fun toUInt(): UInt = TODO()
|
public fun toUInt(): UInt = data.toUInt()
|
||||||
public fun toULong(): ULong = TODO()
|
public fun toULong(): ULong = this
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Byte.toULong(): ULong = ULong(this.toLong())
|
public fun Byte.toULong(): ULong = ULong(this.toLong() and 0xFF)
|
||||||
public fun Short.toULong(): ULong = ULong(this.toLong())
|
public fun Short.toULong(): ULong = ULong(this.toLong() and 0xFFFF)
|
||||||
public fun Int.toULong(): ULong = ULong(this.toLong())
|
public fun Int.toULong(): ULong = ULong(this.toLong() and 0xFFFF_FFFF)
|
||||||
public fun Long.toULong(): ULong = ULong(this.toLong())
|
public fun Long.toULong(): ULong = ULong(this)
|
||||||
|
|||||||
@@ -113,19 +113,19 @@ public inline class UShort internal constructor(private val data: Short) : Compa
|
|||||||
/** Inverts the bits in this value. */
|
/** Inverts the bits in this value. */
|
||||||
public fun inv(): UShort = UShort(data.inv())
|
public fun inv(): UShort = UShort(data.inv())
|
||||||
|
|
||||||
public fun toByte(): Byte = TODO()
|
public fun toByte(): Byte = data.toByte()
|
||||||
public fun toShort(): Short = TODO()
|
public fun toShort(): Short = data
|
||||||
public fun toInt(): Int = TODO()
|
public fun toInt(): Int = data.toInt() and 0xFFFF
|
||||||
public fun toLong(): Long = TODO()
|
public fun toLong(): Long = data.toLong() and 0xFFFF
|
||||||
|
|
||||||
public fun toUByte(): UByte = TODO()
|
public fun toUByte(): UByte = data.toUByte()
|
||||||
public fun toUShort(): UShort = TODO()
|
public fun toUShort(): UShort = this
|
||||||
public fun toUInt(): UInt = TODO()
|
public fun toUInt(): UInt = data.toUInt()
|
||||||
public fun toULong(): ULong = TODO()
|
public fun toULong(): ULong = data.toULong()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Byte.toUShort(): UShort = UShort(this.toShort())
|
public fun Byte.toUShort(): UShort = UShort(this.toShort() and 0xFF)
|
||||||
public fun Short.toUShort(): UShort = UShort(this.toShort())
|
public fun Short.toUShort(): UShort = UShort(this)
|
||||||
public fun Int.toUShort(): UShort = UShort(this.toShort())
|
public fun Int.toUShort(): UShort = UShort(this.toShort())
|
||||||
public fun Long.toUShort(): UShort = UShort(this.toShort())
|
public fun Long.toUShort(): UShort = UShort(this.toShort())
|
||||||
|
|||||||
Reference in New Issue
Block a user