Interop, backend: add bitsTo{Float,Double} intrinsics
This commit is contained in:
committed by
SvyatoslavScherbina
parent
9afd9360b3
commit
01dd86d3b5
@@ -2,3 +2,6 @@ package kotlinx.cinterop
|
|||||||
|
|
||||||
internal fun decodeFromUtf8(bytes: ByteArray) = String(bytes)
|
internal fun decodeFromUtf8(bytes: ByteArray) = String(bytes)
|
||||||
internal fun encodeToUtf8(str: String) = str.toByteArray()
|
internal fun encodeToUtf8(str: String) = str.toByteArray()
|
||||||
|
|
||||||
|
fun bitsToFloat(bits: Int): Float = java.lang.Float.intBitsToFloat(bits)
|
||||||
|
fun bitsToDouble(bits: Long): Double = java.lang.Double.longBitsToDouble(bits)
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package kotlinx.cinterop
|
package kotlinx.cinterop
|
||||||
|
|
||||||
|
import konan.internal.Intrinsic
|
||||||
|
|
||||||
internal fun decodeFromUtf8(bytes: ByteArray): String = kotlin.text.fromUtf8Array(bytes, 0, bytes.size)
|
internal fun decodeFromUtf8(bytes: ByteArray): String = kotlin.text.fromUtf8Array(bytes, 0, bytes.size)
|
||||||
|
|
||||||
fun encodeToUtf8(str: String): ByteArray {
|
fun encodeToUtf8(str: String): ByteArray {
|
||||||
@@ -14,3 +16,9 @@ fun encodeToUtf8(str: String): ByteArray {
|
|||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Intrinsic
|
||||||
|
external fun bitsToFloat(bits: Int): Float
|
||||||
|
|
||||||
|
@Intrinsic
|
||||||
|
external fun bitsToDouble(bits: Long): Double
|
||||||
+4
@@ -93,6 +93,10 @@ internal class InteropBuiltIns(builtIns: KonanBuiltIns) {
|
|||||||
|
|
||||||
val nativePtrPlusLong = nativePtr.unsubstitutedMemberScope.getContributedFunctions("plus").single()
|
val nativePtrPlusLong = nativePtr.unsubstitutedMemberScope.getContributedFunctions("plus").single()
|
||||||
|
|
||||||
|
val bitsToFloat = packageScope.getContributedFunctions("bitsToFloat").single()
|
||||||
|
|
||||||
|
val bitsToDouble = packageScope.getContributedFunctions("bitsToDouble").single()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun MemberScope.getContributedVariables(name: String) =
|
private fun MemberScope.getContributedVariables(name: String) =
|
||||||
|
|||||||
+27
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.ir.builders.irCall
|
|||||||
import org.jetbrains.kotlin.ir.builders.irGet
|
import org.jetbrains.kotlin.ir.builders.irGet
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||||
import org.jetbrains.kotlin.ir.expressions.*
|
import org.jetbrains.kotlin.ir.expressions.*
|
||||||
|
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrGetObjectValueImpl
|
import org.jetbrains.kotlin.ir.expressions.impl.IrGetObjectValueImpl
|
||||||
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
|
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
@@ -186,6 +187,26 @@ private class InteropTransformer(val context: Context) : IrBuildingTransformer(c
|
|||||||
builder.typeOf(type) ?: expression
|
builder.typeOf(type) ?: expression
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interop.bitsToFloat -> {
|
||||||
|
val argument = expression.getValueArgument(0)
|
||||||
|
if (argument is IrConst<*> && argument.kind == IrConstKind.Int) {
|
||||||
|
val floatValue = kotlinx.cinterop.bitsToFloat(argument.value as Int)
|
||||||
|
builder.irFloat(floatValue)
|
||||||
|
} else {
|
||||||
|
expression
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interop.bitsToDouble -> {
|
||||||
|
val argument = expression.getValueArgument(0)
|
||||||
|
if (argument is IrConst<*> && argument.kind == IrConstKind.Long) {
|
||||||
|
val doubleValue = kotlinx.cinterop.bitsToDouble(argument.value as Long)
|
||||||
|
builder.irDouble(doubleValue)
|
||||||
|
} else {
|
||||||
|
expression
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
else -> expression
|
else -> expression
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -195,3 +216,9 @@ private class InteropTransformer(val context: Context) : IrBuildingTransformer(c
|
|||||||
return getTypeArgument(typeParameter)!!
|
return getTypeArgument(typeParameter)!!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun IrBuilder.irFloat(value: Float) =
|
||||||
|
IrConstImpl.float(startOffset, endOffset, context.builtIns.floatType, value)
|
||||||
|
|
||||||
|
private fun IrBuilder.irDouble(value: Double) =
|
||||||
|
IrConstImpl.double(startOffset, endOffset, context.builtIns.doubleType, value)
|
||||||
Reference in New Issue
Block a user