diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt index f4431b9d34c..af0d8089d79 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt @@ -54,23 +54,6 @@ external fun CPointer<*>.getRawValue(): NativePtr @ExperimentalForeignApi internal fun CPointer<*>.cPointerToString() = "CPointer(raw=$rawValue)" -@ExperimentalForeignApi -@Suppress("FINAL_UPPER_BOUND") -public class Vector128VarOf(rawPtr: NativePtr) : CVariable(rawPtr) { - @Deprecated("Use sizeOf() or alignOf() instead.") - @Suppress("DEPRECATION") - companion object : Type(size = 16, align = 16) -} - -@ExperimentalForeignApi -public typealias Vector128Var = Vector128VarOf - -@ExperimentalForeignApi -@Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST") -public var Vector128VarOf.value: T - get() = nativeMemUtils.getVector(this) as T - set(value) = nativeMemUtils.putVector(this, value) - /** * Returns a pointer to C function which calls given Kotlin *static* function. * diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/Vector128.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/Vector128.kt new file mode 100644 index 00000000000..1410bb5b130 --- /dev/null +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/Vector128.kt @@ -0,0 +1,85 @@ +/* + * 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. + */ +package kotlinx.cinterop + +import kotlin.native.internal.GCUnsafeCall +import kotlin.native.internal.TypedIntrinsic +import kotlin.native.internal.IntrinsicType +import kotlinx.cinterop.ExperimentalForeignApi + + +@SinceKotlin("1.9") +@ExperimentalForeignApi +public final class Vector128 private constructor() { + @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) + external fun getByteAt(index: Int): Byte + + @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) + external fun getIntAt(index: Int): Int + + @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) + external fun getLongAt(index: Int): Long + + @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) + external fun getFloatAt(index: Int): Float + + @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) + external fun getDoubleAt(index: Int): Double + + @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) + external fun getUByteAt(index: Int): UByte + + @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) + external fun getUIntAt(index: Int): UInt + + @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) + external fun getULongAt(index: Int): ULong + + public override fun toString() = + "(0x${getUIntAt(0).toString(16)}, 0x${getUIntAt(1).toString(16)}, 0x${getUIntAt(2).toString(16)}, 0x${getUIntAt(3).toString(16)})" + + // Not as good for floating types + public override fun equals(other: Any?): Boolean = + other is Vector128 && getLongAt(0) == other.getLongAt(0) && getLongAt(1) == other.getLongAt(1) + + override fun hashCode(): Int { + val x0 = getLongAt(0) + val x1 = getLongAt(1) + return 31 * (x0 xor (x0 shr 32)).toInt() + (x1 xor (x1 shr 32)).toInt() + } +} + + +@SinceKotlin("1.9") +@ExperimentalForeignApi +@GCUnsafeCall("Kotlin_Interop_Vector4f_of") +public external fun vectorOf(f0: Float, f1: Float, f2: Float, f3: Float): Vector128 + +@SinceKotlin("1.9") +@ExperimentalForeignApi +@GCUnsafeCall("Kotlin_Interop_Vector4i32_of") +public external fun vectorOf(f0: Int, f1: Int, f2: Int, f3: Int): Vector128 + + +@SinceKotlin("1.9") +@ExperimentalForeignApi +@Suppress("FINAL_UPPER_BOUND") +public class Vector128VarOf(rawPtr: NativePtr) : CVariable(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") + companion object : Type(size = 16, align = 16) +} + +@SinceKotlin("1.9") +@ExperimentalForeignApi +public typealias Vector128Var = Vector128VarOf + +@SinceKotlin("1.9") +@ExperimentalForeignApi +@Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST") +public var Vector128VarOf.value: T + get() = nativeMemUtils.getVector(this) as T + set(value) = nativeMemUtils.putVector(this, value) + diff --git a/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/KotlinCodeModel.kt b/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/KotlinCodeModel.kt index 450c38bb2f8..29d01f287c5 100644 --- a/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/KotlinCodeModel.kt +++ b/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/KotlinCodeModel.kt @@ -189,7 +189,7 @@ object KotlinTypes { val map by CollectionClassifier val nativePtr by InteropType - val vector128 by KotlinNativeType + val vector128 by InteropType val cOpaque by InteropType val cOpaquePointer by InteropType diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanFqNames.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanFqNames.kt index b3b79b525cc..cfe7df21490 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanFqNames.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanFqNames.kt @@ -10,7 +10,6 @@ import org.jetbrains.kotlin.name.Name internal const val NATIVE_PTR_NAME = "NativePtr" internal const val NON_NULL_NATIVE_PTR_NAME = "NonNullNativePtr" -internal const val VECTOR128 = "Vector128" internal const val IMMUTABLE_BLOB_OF = "immutableBlobOf" object KonanFqNames { @@ -20,7 +19,7 @@ object KonanFqNames { val internalPackageName = FqName("kotlin.native.internal") val nativePtr = internalPackageName.child(Name.identifier(NATIVE_PTR_NAME)).toUnsafe() val nonNullNativePtr = internalPackageName.child(Name.identifier(NON_NULL_NATIVE_PTR_NAME)).toUnsafe() - val Vector128 = packageName.child(Name.identifier(VECTOR128)) + val Vector128 = FqName("kotlinx.cinterop.Vector128") val throws = FqName("kotlin.Throws") val cancellationException = FqName("kotlin.coroutines.cancellation.CancellationException") val threadLocal = FqName("kotlin.native.concurrent.ThreadLocal") diff --git a/kotlin-native/backend.native/tests/interop/basics/vectors.kt b/kotlin-native/backend.native/tests/interop/basics/vectors.kt index afd227fd9bb..09ab8732cf5 100644 --- a/kotlin-native/backend.native/tests/interop/basics/vectors.kt +++ b/kotlin-native/backend.native/tests/interop/basics/vectors.kt @@ -1,6 +1,7 @@ @file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class, kotlin.experimental.ExperimentalNativeApi::class) import kotlinx.cinterop.* +import kotlinx.cinterop.vectorOf import kotlin.native.* import kotlin.test.* import cvectors.* diff --git a/kotlin-native/backend.native/tests/runtime/basic/simd.kt b/kotlin-native/backend.native/tests/runtime/basic/simd.kt index d1310dc0298..fa1bc5f79eb 100644 --- a/kotlin-native/backend.native/tests/runtime/basic/simd.kt +++ b/kotlin-native/backend.native/tests/runtime/basic/simd.kt @@ -7,6 +7,8 @@ package runtime.basic.simd import kotlin.test.* +import kotlinx.cinterop.Vector128 +import kotlinx.cinterop.vectorOf @Test fun runTest() { diff --git a/kotlin-native/runtime/src/main/cpp/Types.cpp b/kotlin-native/runtime/src/main/cpp/Types.cpp index e0a5e60db17..fa70f7c9aed 100644 --- a/kotlin-native/runtime/src/main/cpp/Types.cpp +++ b/kotlin-native/runtime/src/main/cpp/Types.cpp @@ -93,7 +93,7 @@ bool IsSubInterface(const TypeInfo* thiz, const TypeInfo* other) { return false; } -KVector4f Kotlin_Vector4f_of(KFloat f0, KFloat f1, KFloat f2, KFloat f3) { +KVector4f Kotlin_Interop_Vector4f_of(KFloat f0, KFloat f1, KFloat f2, KFloat f3) { return {f0, f1, f2, f3}; } @@ -103,7 +103,7 @@ KVector4f Kotlin_Vector4f_of(KFloat f0, KFloat f1, KFloat f2, KFloat f3) { * To avoid illegal bitcast from/to function types the following function * return type MUST be <4 x float> and explicit type cast is done on the variable type. */ -KVector4f Kotlin_Vector4i32_of(KInt f0, KInt f1, KInt f2, KInt f3) { +KVector4f Kotlin_Interop_Vector4i32_of(KInt f0, KInt f1, KInt f2, KInt f3) { KInt __attribute__ ((__vector_size__(16))) v4i = {f0, f1, f2, f3}; return (KVector4f)v4i; } diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Intrinsics.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Intrinsics.kt index ab5dfac0703..2630ef8c184 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Intrinsics.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Intrinsics.kt @@ -6,10 +6,7 @@ package kotlin.native.internal -import kotlinx.cinterop.CPointer -import kotlinx.cinterop.NativePointed -import kotlinx.cinterop.NativePtr -import kotlinx.cinterop.ExperimentalForeignApi +import kotlinx.cinterop.* import kotlin.native.internal.TypedIntrinsic import kotlin.native.internal.IntrinsicType diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/simd.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/simd.kt index d2ca6a3d3f5..d9897132388 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/simd.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/simd.kt @@ -10,54 +10,21 @@ import kotlin.native.internal.IntrinsicType import kotlinx.cinterop.ExperimentalForeignApi +@Deprecated("Use kotlinx.cinterop.Vector128 instead.", ReplaceWith("kotlinx.cinterop.Vector128")) +@DeprecatedSinceKotlin(warningSince = "1.9") @ExperimentalForeignApi -public final class Vector128 private constructor() { - @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) - external fun getByteAt(index: Int): Byte - - @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) - external fun getIntAt(index: Int): Int - - @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) - external fun getLongAt(index: Int): Long - - @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) - external fun getFloatAt(index: Int): Float - - @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) - external fun getDoubleAt(index: Int): Double - - @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) - external fun getUByteAt(index: Int): UByte - - @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) - external fun getUIntAt(index: Int): UInt - - @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) - external fun getULongAt(index: Int): ULong - - public override fun toString() = - "(0x${getUIntAt(0).toString(16)}, 0x${getUIntAt(1).toString(16)}, 0x${getUIntAt(2).toString(16)}, 0x${getUIntAt(3).toString(16)})" - - @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) - public fun equals(other: Vector128): Boolean = - getLongAt(0) == other.getLongAt(0) && getLongAt(1) == other.getLongAt(1) - - // Not as good for floating types - public override fun equals(other: Any?): Boolean = - other is Vector128 && getLongAt(0) == other.getLongAt(0) && getLongAt(1) == other.getLongAt(1) - - override fun hashCode(): Int { - val x0 = getLongAt(0) - val x1 = getLongAt(1) - return 31 * (x0 xor (x0 shr 32)).toInt() + (x1 xor (x1 shr 32)).toInt() - } -} +public typealias Vector128 = kotlinx.cinterop.Vector128 +@Suppress("DEPRECATION") +@Deprecated("Use kotlinx.cinterop.vectorOf instead.", ReplaceWith("kotlinx.cinterop.vectorOf(f0, f1, f2, f3)")) +@DeprecatedSinceKotlin(warningSince = "1.9") @ExperimentalForeignApi -@GCUnsafeCall("Kotlin_Vector4f_of") +@GCUnsafeCall("Kotlin_Interop_Vector4f_of") external fun vectorOf(f0: Float, f1: Float, f2: Float, f3: Float): Vector128 +@Suppress("DEPRECATION") +@Deprecated("Use kotlinx.cinterop.vectorOf instead.", ReplaceWith("kotlinx.cinterop.vectorOf(f0, f1, f2, f3)")) +@DeprecatedSinceKotlin(warningSince = "1.9") @ExperimentalForeignApi -@GCUnsafeCall("Kotlin_Vector4i32_of") +@GCUnsafeCall("Kotlin_Interop_Vector4i32_of") external fun vectorOf(f0: Int, f1: Int, f2: Int, f3: Int): Vector128