From 453683bf44f2112719802eaac2b332d49eebd640 Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 19 Aug 2024 15:03:56 -0700 Subject: Tuple swizzling, concat, comparison and `countof`. (#4856) * Tuple swizzling and element access. * Update proposal status. * Cleanup. * Fix merrge error. * Address review. --- source/slang/core.meta.slang | 79 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 2 deletions(-) (limited to 'source/slang/core.meta.slang') diff --git a/source/slang/core.meta.slang b/source/slang/core.meta.slang index 6c51ccef0..84e1b8168 100644 --- a/source/slang/core.meta.slang +++ b/source/slang/core.meta.slang @@ -874,10 +874,85 @@ __generic __magic_type(TupleType) struct Tuple { - __intrinsic_op($(0)) + __intrinsic_op($(kIROp_MakeTuple)) __init(expand each T); } +__intrinsic_op($(kIROp_MakeTuple)) +Tuple makeTuple(T v); + +Tuple concat(Tuple t, Tuple u) +{ + return makeTuple(expand each t, expand each u); +} + + +[__unsafeForceInlineEarly] +bool __assign(inout bool v, bool newVal) +{ + v = newVal; + return newVal; +} + +[__unsafeForceInlineEarly] +void __tupleLessKernel(inout bool result, inout bool exit, T a, T b) +{ + if (!exit) + { + if (a.lessThan(b)) + { + result = true; + exit = true; + } + else if (!a.equals(b)) + { + exit = true; + } + } +} + +[__unsafeForceInlineEarly] +void __tupleGreaterKernel(inout bool result, inout bool exit, T a, T b) +{ + if (!exit) + { + if (!a.lessThanOrEquals(b)) + { + result = true; + exit = true; + } + else if (!a.equals(b)) + { + exit = true; + } + } +} + +__generic +extension Tuple : IComparable +{ + bool lessThan(Tuple other) + { + bool result = false; + bool exit = false; + expand __tupleLessKernel(result, exit, each this, each other); + return result; + } + bool lessThanOrEquals(Tuple other) + { + bool result = false; + bool exit = false; + expand __tupleGreaterKernel(result, exit, each this, each other); + return !result; + } + bool equals(Tuple other) + { + bool result = true; + expand result && __assign(result, result && (each this).equals(each other)); + return result; + } +} + __generic __magic_type(NativeRefType) __intrinsic_type($(kIROp_NativePtrType)) @@ -2181,7 +2256,7 @@ __generic [OverloadRank(-10)] bool operator >=(T v0, T v1) { - return v1.lessThan(v1); + return v1.lessThanOrEquals(v0); } __generic [__unsafeForceInlineEarly] -- cgit v1.2.3