Fix interop support of struct containing simd (#3602)

This commit is contained in:
Vladimir Ivanov
2019-11-22 14:38:05 +03:00
committed by GitHub
parent be6722d44a
commit f1b4a9f150
6 changed files with 60 additions and 3 deletions
@@ -66,6 +66,7 @@ private fun tryRenderVar(type: Type, name: String): String? = when (type) {
CharType, is BoolType -> "char $name" CharType, is BoolType -> "char $name"
is IntegerType -> "${type.spelling} $name" is IntegerType -> "${type.spelling} $name"
is FloatingType -> "${type.spelling} $name" is FloatingType -> "${type.spelling} $name"
is VectorType -> "${type.spelling} $name"
is RecordType -> "${tryRenderStructOrUnion(type.decl.def!!)} $name" is RecordType -> "${tryRenderStructOrUnion(type.decl.def!!)} $name"
is EnumType -> tryRenderVar(type.def.baseType, name) is EnumType -> tryRenderVar(type.def.baseType, name)
is PointerType -> "void* $name" is PointerType -> "void* $name"
@@ -70,3 +70,15 @@ enum NonStrictEnum2 {
NonStrictEnum2A, NonStrictEnum2A,
NonStrictEnum2B __attribute__((unused)) = 1 NonStrictEnum2B __attribute__((unused)) = 1
}; };
typedef float __attribute__ ((__vector_size__ (16))) KVector4f;
typedef int __attribute__ ((__vector_size__ (16))) KVector4i32;
static float sendV4F(KVector4f v) {
return v[0] + 2 * v[1] + 4 * v[2] + 8 * v[3];
}
static int sendV4I(KVector4i32 v) {
return v[0] + 2 * v[1] + 4 * v[2] + 8 * v[3];
}
@@ -21,4 +21,8 @@ fun main() {
assertEquals(1u, StrictEnum2.StrictEnum2B.value) assertEquals(1u, StrictEnum2.StrictEnum2B.value)
assertEquals(0u, NonStrictEnum1A) assertEquals(0u, NonStrictEnum1A)
assertEquals(1u, NonStrictEnum2B) assertEquals(1u, NonStrictEnum2B)
assertEquals(49, sendV4I(vectorOf(1, 2, 3, 4)))
assertEquals(49, (sendV4F(vectorOf(1f, 2f, 3f, 4f)) + 0.00001).toInt())
} }
@@ -13,6 +13,7 @@ private fun primitives() {
assertEquals(3.14f, PrimitiveTestSubject.floatFn()) assertEquals(3.14f, PrimitiveTestSubject.floatFn())
assertEquals(3.14, PrimitiveTestSubject.doubleFn()) assertEquals(3.14, PrimitiveTestSubject.doubleFn())
assertEquals(42, PrimitiveTestSubject.intFn()) assertEquals(42, PrimitiveTestSubject.intFn())
assertEquals(vectorOf(2f, 4f, 5f, 8f), PrimitiveTestSubject.simdFn())
} }
private fun aggregates() { private fun aggregates() {
@@ -44,4 +45,14 @@ private fun aggregates() {
assertEquals(3.0f, f3) assertEquals(3.0f, f3)
assertEquals(4.0f, f4) assertEquals(4.0f, f4)
} }
AggregateTestSubject.simd_quatfFn().useContents {
assertEquals(vectorOf(1f, 4f, 9f, 25f), vector)
}
AggregateTestSubject.geterogeneousSmallFn().useContents {
assertEquals(1, s1)
assertEquals(vectorOf(1f, 4f, 9f, 25f), v2)
assertEquals(3f, f3)
assertEquals(4, i4)
}
} }
@@ -1,10 +1,12 @@
#import <objc/NSObject.h> #import <objc/NSObject.h>
#include <simd/simd.h>
@interface PrimitiveTestSubject : NSObject @interface PrimitiveTestSubject : NSObject
+ (int)intFn; + (int)intFn;
+ (float)floatFn; + (float)floatFn;
+ (double)doubleFn; + (double)doubleFn;
+ (simd_float4)simdFn;
@end; @end;
@@ -45,6 +47,13 @@ typedef struct {
// TODO: Add more cases later: SIMD, bitfields. // TODO: Add more cases later: SIMD, bitfields.
typedef struct {
short s1;
simd_float4 v2;
float f3;
int i4;
} GeterogeneousSmall;
@interface AggregateTestSubject : NSObject @interface AggregateTestSubject : NSObject
+ (SingleFloat)singleFloatFn; + (SingleFloat)singleFloatFn;
@@ -52,5 +61,8 @@ typedef struct {
+ (EvenSmallerPacked)evenSmallerPackedFn; + (EvenSmallerPacked)evenSmallerPackedFn;
+ (HomogeneousSmall)homogeneousSmallFn; + (HomogeneousSmall)homogeneousSmallFn;
+ (HomogeneousBig)homogeneousBigFn; + (HomogeneousBig)homogeneousBigFn;
+ (simd_quatf)simd_quatfFn;
+ (GeterogeneousSmall)geterogeneousSmallFn;
@end; @end;
@@ -14,6 +14,15 @@
return 3.14; return 3.14;
} }
+ (simd_float4)simdFn {
simd_float4 v;
v.x = 2;
v.y = 4;
v.z = 5;
v.w = 8;
return v;
}
@end; @end;
@implementation AggregateTestSubject @implementation AggregateTestSubject
@@ -61,4 +70,12 @@
return s; return s;
} }
+ (GeterogeneousSmall)geterogeneousSmallFn {
return (GeterogeneousSmall){1, {1, 4, 9, 25}, 3, 4};
}
+ (simd_quatf)simd_quatfFn {
return (simd_quatf){ {1, 4, 9, 25} };
}
@end; @end;