[K/Wasm] Generate wasm-specific unsigned implementations ^KT-58039 Fixed

This commit is contained in:
Artem Kobzar
2024-01-23 18:49:06 +01:00
committed by Space Team
parent b59993d88a
commit 8c69ffe8c9
24 changed files with 666 additions and 541 deletions
@@ -5,7 +5,6 @@
package kotlin.text
import kotlin.math.abs
import kotlin.wasm.internal.wasm_f32_demote_f64
/**
@@ -103,7 +102,7 @@ public actual fun String.toDoubleOrNull(): Double? {
* @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion.
*/
@SinceKotlin("1.2")
public actual fun Byte.toString(radix: Int): String = this.toLong().toString(radix)
public actual fun Byte.toString(radix: Int): String = this.toInt().toString(radix)
/**
* Returns a string representation of this [Short] value in the specified [radix].
@@ -111,7 +110,7 @@ public actual fun Byte.toString(radix: Int): String = this.toLong().toString(rad
* @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion.
*/
@SinceKotlin("1.2")
public actual fun Short.toString(radix: Int): String = this.toLong().toString(radix)
public actual fun Short.toString(radix: Int): String = this.toInt().toString(radix)
/**
* Returns a string representation of this [Int] value in the specified [radix].
@@ -119,7 +118,13 @@ public actual fun Short.toString(radix: Int): String = this.toLong().toString(ra
* @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion.
*/
@SinceKotlin("1.2")
public actual fun Int.toString(radix: Int): String = toLong().toString(radix)
public actual fun Int.toString(radix: Int): String {
val isNegative = this < 0
val absValue = if (isNegative) -this else this
val absValueString = uintToString(absValue, checkRadix(radix))
return if (isNegative) "-$absValueString" else absValueString
}
/**
* Returns a string representation of this [Long] value in the specified [radix].
@@ -128,28 +133,9 @@ public actual fun Int.toString(radix: Int): String = toLong().toString(radix)
*/
@SinceKotlin("1.2")
public actual fun Long.toString(radix: Int): String {
checkRadix(radix)
fun Long.getChar() = toInt().let { if (it < 10) '0' + it else 'a' + (it - 10) }
if (radix == 10) return toString()
if (this in 0 until radix) return getChar().toString()
val isNegative = this < 0
val buffer = CharArray(Long.SIZE_BITS + 1)
val absValue = if (isNegative) -this else this
val absValueString = ulongToString(absValue, checkRadix(radix))
var currentBufferIndex = buffer.lastIndex
var current: Long = this
while(current != 0L) {
buffer[currentBufferIndex] = abs(current % radix).getChar()
current /= radix
currentBufferIndex--
}
if (isNegative) {
buffer[currentBufferIndex] = '-'
currentBufferIndex--
}
return buffer.concatToString(currentBufferIndex + 1)
}
return if (isNegative) "-$absValueString" else absValueString
}
@@ -1,105 +1,134 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:kotlin.jvm.JvmName("UnsignedKt")
package kotlin
@PublishedApi
internal fun uintCompare(v1: Int, v2: Int): Int = (v1 xor Int.MIN_VALUE).compareTo(v2 xor Int.MIN_VALUE)
@PublishedApi
internal fun ulongCompare(v1: Long, v2: Long): Int = (v1 xor Long.MIN_VALUE).compareTo(v2 xor Long.MIN_VALUE)
import kotlin.internal.InlineOnly
import kotlin.math.abs
import kotlin.wasm.internal.*
import kotlin.wasm.internal.WasmOp
import kotlin.wasm.internal.implementedAsIntrinsic
import kotlin.wasm.internal.wasm_u32_compareTo
@PublishedApi
internal fun uintDivide(v1: UInt, v2: UInt): UInt = (v1.toLong() / v2.toLong()).toUInt()
@PublishedApi
internal fun uintRemainder(v1: UInt, v2: UInt): UInt = (v1.toLong() % v2.toLong()).toUInt()
// Division and remainder are based on Guava's UnsignedLongs implementation
// Copyright 2011 The Guava Authors
@WasmOp(WasmOp.I32_REM_U)
internal actual fun uintRemainder(v1: UInt, v2: UInt): UInt = implementedAsIntrinsic
@PublishedApi
internal fun ulongDivide(v1: ULong, v2: ULong): ULong {
val dividend = v1.toLong()
val divisor = v2.toLong()
if (divisor < 0) { // i.e., divisor >= 2^63:
return if (v1 < v2) ULong(0) else ULong(1)
@WasmOp(WasmOp.I32_DIV_U)
internal actual fun uintDivide(v1: UInt, v2: UInt): UInt = implementedAsIntrinsic
@PublishedApi
@WasmOp(WasmOp.I64_REM_U)
internal actual fun ulongRemainder(v1: ULong, v2: ULong): ULong = implementedAsIntrinsic
@PublishedApi
@WasmOp(WasmOp.I64_DIV_U)
internal actual fun ulongDivide(v1: ULong, v2: ULong): ULong = implementedAsIntrinsic
@PublishedApi
@InlineOnly
internal actual inline fun uintCompare(v1: Int, v2: Int): Int = wasm_u32_compareTo(v1, v2)
@PublishedApi
@InlineOnly
internal actual inline fun ulongCompare(v1: Long, v2: Long): Int = wasm_u64_compareTo(v1, v2)
@PublishedApi
@WasmOp(WasmOp.I64_EXTEND_I32_U)
internal actual fun uintToULong(value: Int): ULong = implementedAsIntrinsic
@PublishedApi
@WasmOp(WasmOp.I64_EXTEND_I32_U)
internal actual fun uintToLong(value: Int): Long = implementedAsIntrinsic
@PublishedApi
@WasmOp(WasmOp.F32_CONVERT_I32_U)
internal actual fun uintToFloat(value: Int): Float = implementedAsIntrinsic
@PublishedApi
@WasmOp(WasmOp.I32_TRUNC_SAT_F32_U)
internal actual fun floatToUInt(value: Float): UInt = implementedAsIntrinsic
@PublishedApi
@WasmOp(WasmOp.F64_CONVERT_I32_U)
internal actual fun uintToDouble(value: Int): Double = implementedAsIntrinsic
@PublishedApi
@WasmOp(WasmOp.I32_TRUNC_SAT_F64_U)
internal actual fun doubleToUInt(value: Double): UInt = implementedAsIntrinsic
@PublishedApi
@WasmOp(WasmOp.F32_CONVERT_I64_U)
internal actual fun ulongToFloat(value: Long): Float = implementedAsIntrinsic
@PublishedApi
@WasmOp(WasmOp.I64_TRUNC_SAT_F32_U)
internal actual fun floatToULong(value: Float): ULong = implementedAsIntrinsic
@PublishedApi
@WasmOp(WasmOp.F64_CONVERT_I64_U)
internal actual fun ulongToDouble(value: Long): Double = implementedAsIntrinsic
@PublishedApi
@WasmOp(WasmOp.I64_TRUNC_SAT_F64_U)
internal actual fun doubleToULong(value: Double): ULong = implementedAsIntrinsic
@InlineOnly
internal actual inline fun uintToString(value: Int): String = utoa32(value.toUInt())
internal actual fun uintToString(value: Int, base: Int): String {
var unsignedValue = value.toUInt()
if (base == 10) return unsignedValue.toString()
if (value in 0 until base) return value.getChar().toString()
val buffer = WasmCharArray(UInt.SIZE_BITS)
val ulongRadix = base.toUInt()
var currentBufferIndex = UInt.SIZE_BITS - 1
while (unsignedValue != 0U) {
buffer.set(currentBufferIndex, (unsignedValue % ulongRadix).toInt().getChar())
unsignedValue /= ulongRadix
currentBufferIndex--
}
// Optimization - use signed division if both dividend and divisor < 2^63
if (dividend >= 0) {
return ULong(dividend / divisor)
return buffer.createStringStartingFrom(currentBufferIndex + 1)
}
@InlineOnly
internal actual inline fun ulongToString(value: Long): String = utoa64(value.toULong())
internal actual fun ulongToString(value: Long, base: Int): String {
var unsignedValue = value.toULong()
if (base == 10) return unsignedValue.toString()
if (value in 0 until base) return value.toInt().getChar().toString()
val buffer = WasmCharArray(ULong.SIZE_BITS)
val ulongRadix = base.toULong()
var currentBufferIndex = ULong.SIZE_BITS - 1
while (unsignedValue != 0UL) {
buffer.set(currentBufferIndex, (unsignedValue % ulongRadix).toInt().getChar())
unsignedValue /= ulongRadix
currentBufferIndex--
}
// Otherwise, approximate the quotient, check, and correct if necessary.
val quotient = ((dividend ushr 1) / divisor) shl 1
val rem = dividend - quotient * divisor
return ULong(quotient + if (ULong(rem) >= ULong(divisor)) 1 else 0)
return buffer.createStringStartingFrom(currentBufferIndex + 1)
}
@PublishedApi
internal fun ulongRemainder(v1: ULong, v2: ULong): ULong {
val dividend = v1.toLong()
val divisor = v2.toLong()
if (divisor < 0) { // i.e., divisor >= 2^63:
return if (v1 < v2) {
v1 // dividend < divisor
} else {
v1 - v2 // dividend >= divisor
}
}
// Optimization - use signed modulus if both dividend and divisor < 2^63
if (dividend >= 0) {
return ULong(dividend % divisor)
}
// Otherwise, approximate the quotient, check, and correct if necessary.
val quotient = ((dividend ushr 1) / divisor) shl 1
val rem = dividend - quotient * divisor
return ULong(rem - if (ULong(rem) >= ULong(divisor)) divisor else 0)
}
@PublishedApi
internal fun doubleToUInt(v: Double): UInt = when {
v.isNaN() -> 0u
v <= UInt.MIN_VALUE.toDouble() -> UInt.MIN_VALUE
v >= UInt.MAX_VALUE.toDouble() -> UInt.MAX_VALUE
v <= Int.MAX_VALUE -> v.toInt().toUInt()
else -> (v - Int.MAX_VALUE).toInt().toUInt() + Int.MAX_VALUE.toUInt() // Int.MAX_VALUE < v < UInt.MAX_VALUE
}
@PublishedApi
internal fun doubleToULong(v: Double): ULong = when {
v.isNaN() -> 0u
v <= ULong.MIN_VALUE.toDouble() -> ULong.MIN_VALUE
v >= ULong.MAX_VALUE.toDouble() -> ULong.MAX_VALUE
v < Long.MAX_VALUE -> v.toLong().toULong()
// Real values from Long.MAX_VALUE to (Long.MAX_VALUE + 1) are not representable in Double, so don't handle them.
else -> (v - 9223372036854775808.0).toLong().toULong() + 9223372036854775808uL // Long.MAX_VALUE + 1 < v < ULong.MAX_VALUE
}
@PublishedApi
internal fun uintToDouble(v: Int): Double = (v and Int.MAX_VALUE).toDouble() + (v ushr 31 shl 30).toDouble() * 2
@PublishedApi
internal fun ulongToDouble(v: Long): Double = (v ushr 11).toDouble() * 2048 + (v and 2047)
internal fun ulongToString(v: Long): String = ulongToString(v, 10)
internal fun ulongToString(v: Long, base: Int): String {
if (v >= 0) return v.toString(base)
var quotient = ((v ushr 1) / base) shl 1
var rem = v - quotient * base
if (rem >= base) {
rem -= base
quotient += 1
}
return quotient.toString(base) + rem.toString(base)
internal fun WasmCharArray.createStringStartingFrom(index: Int): String {
if (index == 0) return createString()
val newLength = this.len() - index
if (newLength == 0) return ""
val newChars = WasmCharArray(newLength)
copyWasmArray(this, newChars, index, 0, newLength)
return newChars.createString()
}
private fun Int.getChar() = if (this < 10) '0' + this else 'a' + (this - 10)