Skip to content

Commit ae36b09

Browse files
committed
Disabled Function Pointers feature by default. Enable by defining MATHX_FUNCTION_POINTERS
1 parent 1369d48 commit ae36b09

9 files changed

+71
-35
lines changed

Runtime/_Experimental/Jobify/FunctionPointers.Experimental.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// ** Repository : https://github.com/LTMX/Unity.mathx
55
#endregion
66

7+
#if MATHX_FUNCTION_POINTERS
8+
79
using System;
810
using Unity.Burst;
911

@@ -222,8 +224,8 @@ public class FunctionPointers_Experimental
222224
// myMethodwithParams.invoke
223225
// }
224226

225-
226-
227227
#endregion
228228
}
229-
}
229+
}
230+
231+
#endif

Runtime/_Experimental/Jobify/FunctionPointers.MethodSignatures.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// ** Repository : https://github.com/LTMX/Unity.mathx
55
#endregion
66

7+
#if MATHX_FUNCTION_POINTERS
8+
79
using System;
810
using System.Runtime.InteropServices;
911
using static Unity.Burst.BurstCompiler;
@@ -191,4 +193,6 @@ public class Signature
191193
// [UFP(C)] public delegate double4 d4x4_d4(double4 f, double4 f1, double4 f2, double4 f3);
192194
}
193195
}
194-
}
196+
}
197+
198+
#endif

Runtime/_Experimental/Jobify/FunctionPointers.Operations.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// ** Repository : https://github.com/LTMX/Unity.mathx
55
#endregion
66

7+
#if MATHX_FUNCTION_POINTERS
8+
79
using System.Runtime.CompilerServices;
810
using static Unity.Mathematics.FunctionPointers.Signature;
911
using static Unity.Mathematics.mathx;
@@ -60,4 +62,6 @@ public static partial class FunctionPointers
6062

6163

6264
}
63-
}
65+
}
66+
67+
#endif

Runtime/_Experimental/Jobify/FunctionPointers.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
// ** Repository : https://github.com/LTMX/Unity.mathx
55
#endregion
66

7-
using Unity.Burst;
7+
#if MATHX_FUNCTION_POINTERS
8+
89
using static Unity.Mathematics.FunctionPointers.Signature;
910

1011
namespace Unity.Mathematics
1112
{
1213
public static partial class FunctionPointers
1314
{
1415
// ** Very important to cache the function pointer for performance reasons
15-
// public static readonly f1x3_f1 p_smax_exp = compile<f1x3_f1>(mathx.smax_exp);
16+
public static readonly f1x3_f1 p_smax_exp = compile<f1x3_f1>(mathx.smax_exp);
1617
}
17-
}
18+
}
19+
#endif

Runtime/_Experimental/Jobify/Jobify.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// ** Repository : https://github.com/LTMX/Unity.mathx
55
#endregion
66

7+
#if MATHX_FUNCTION_POINTERS
8+
79
using System;
810
using System.Runtime.InteropServices;
911
using Unity.Burst;
@@ -58,5 +60,6 @@ public ActionJob(FunctionPointer<Action> action) {
5860
}
5961

6062
}
61-
62-
}
63+
}
64+
65+
#endif

Runtime/_Experimental/Jobify/JobifyExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// ** Repository : https://github.com/LTMX/Unity.mathx
55
#endregion
66

7+
#if MATHX_FUNCTION_POINTERS
8+
79
using Unity.Burst;
810
using Unity.Jobs;
911
using static Unity.Mathematics.FunctionPointers.Signature;
@@ -21,4 +23,6 @@ public static void ExecuteAndComplete(this Jobified j)
2123
j.Schedule().Complete();
2224
}
2325
}
24-
}
26+
}
27+
28+
#endif

Runtime/_Experimental/WIP.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
using System.Collections.Generic;
99
using System.Linq;
1010
using System.Runtime.CompilerServices;
11+
12+
#if MATHX_FUNCTION_POINTERS
13+
1114
using System.Runtime.InteropServices;
1215
using Unity.Burst;
1316
using static Unity.Mathematics.FunctionPointers;
1417
using static Unity.Mathematics.FunctionPointers.Signature;
1518

19+
#endif
20+
1621
namespace Unity.Mathematics
1722
{
1823
public static partial class mathx
@@ -21,7 +26,10 @@ public static partial class mathx
2126
[MethodImpl(IL)] public static float3 projectplane(this float3 f, float3 planeNormal) => f - project(f, planeNormal);
2227
// [MethodImpl(IL)] public static f3 MultiplyPoint(this float4x4 m, f3 v) => v.mul(m) / m[3].xyz;
2328

29+
#if MATHX_FUNCTION_POINTERS
2430
public static IEnumerable<T> apply<T>(this IEnumerable<T> t, Func<T, T> f) => t.Select(x => GetFunctionPointerDelegate(f).Invoke(x));
31+
#endif
32+
2533

2634
/// <summary>Apply a function to a value a number of times </summary>
2735
/// <param name="input">input object</param>
@@ -62,9 +70,11 @@ public static T apply<T>(this T input, Func<T, T> function, int cycles)
6270
// return input;
6371
// }
6472

65-
public static FunctionPointer<T> GetFunctionPointerDelegate<T>(T functionPointer) where T : Delegate => new(Marshal.GetFunctionPointerForDelegate(functionPointer));
73+
#if MATHX_FUNCTION_POINTERS
74+
public static FunctionPointer<T> GetFunctionPointerDelegate<T>(T functionPointer) where T : Delegate => new(Marshal.GetFunctionPointerForDelegate(functionPointer));
6675
// public static ActionJob ToActionJob(this Action action) => new(GetFunctionPointerDelegate(action));
67-
76+
77+
#endif
6878
}
6979

7080

@@ -87,6 +97,7 @@ public static class RandomUtils
8797

8898
private static float rand(uint seed) => xxhash32(seed) / (float)uint.MaxValue;
8999

100+
#if MATHX_FUNCTION_POINTERS
90101
public static float4 make(int4 f, i1_f1 func) => new(func.Invoke(f.x), func.Invoke(f.y), func.Invoke(f.z), func.Invoke(f.w));
91102
public static float3 make(int3 f, i1_f1 func) => new(func.Invoke(f.x), func.Invoke(f.y), func.Invoke(f.z));
92103
public static float2 make(int2 f, i1_f1 func) => new(func.Invoke(f.x), func.Invoke(f.y));
@@ -106,6 +117,7 @@ public static class RandomUtils
106117
public static float3 make(uint3 f, u1_f1 func) => new(func.Invoke(f.x), func.Invoke(f.y), func.Invoke(f.z));
107118
public static float2 make(uint2 f, u1_f1 func) => new(func.Invoke(f.x), func.Invoke(f.y));
108119
public static float make(uint f, u1_f1 func) => func.Invoke(f);
120+
#endif
109121

110122
private static uint xxhash32(uint seed) {
111123
var hash = seed + PRIME5;

Runtime/mathx.common.float.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@
44
// ** Repository : https://github.com/LTMX/Unity.mathx
55
#endregion
66

7-
using System;
87
using System.Runtime.CompilerServices;
9-
using System.Threading.Tasks;
10-
using AOT;
11-
using Unity.Burst;
12-
using static Unity.Mathematics.FunctionPointers.Signature;
13-
148
namespace Unity.Mathematics
159
{
1610
public static partial class mathx

Runtime/mathx.selection.float.cs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
// ** Repository : https://github.com/LTMX/Unity.mathx
55
#endregion
66

7-
using System;
7+
88
using System.Runtime.CompilerServices;
9+
10+
#if MATHX_FUNCTION_POINTERS
11+
12+
using System;
913
using AOT;
1014
using Unity.Burst;
1115
using Unity.Collections.LowLevel.Unsafe;
12-
using static Unity.Mathematics.FunctionPointers;
13-
using static Unity.Mathematics.FunctionPointers.Signature;
16+
using static Unity.Mathematics.FunctionPointers
17+
18+
#endif
19+
1420

1521
namespace Unity.Mathematics
1622
{
@@ -19,45 +25,47 @@ public static partial class mathx
1925
// Component-wise comparison --------------------------------------------------------------
2026

2127
/// <inheritdoc cref="math.cmax(float4)"/>
22-
[MethodImpl(IL)] public static float cmax(this float4 f) => f.xy.fmax(f.zw).cmax();
28+
[MethodImpl(IL)] public static float cmax(this float4 f) => math.cmax(f);
2329

2430
/// <inheritdoc cref="math.cmax(float4)"/>
25-
[MethodImpl(IL)] public static float cmax(this float3 f) => f.x.max(f.y).max(f.z);
31+
[MethodImpl(IL)] public static float cmax(this float3 f) => math.cmax(f);
2632

2733
/// <inheritdoc cref="math.cmax(float4)"/>
28-
[MethodImpl(IL)] public static float cmax(this float2 f) => f.x.max(f.y);
34+
[MethodImpl(IL)] public static float cmax(this float2 f) => math.cmax(f);
2935

3036
/// <inheritdoc cref="math.cmin(float4)"/>
31-
[MethodImpl(IL)] public static float cmin(this float4 f) => fcmin(f);
37+
[MethodImpl(IL)] public static float cmin(this float4 f) => math.cmin(f);
3238
/// <inheritdoc cref="math.cmin(float4)"/>
33-
[MethodImpl(IL)] public static float cmin(this float3 f) => f.x.min(f.y).min(f.z);
39+
[MethodImpl(IL)] public static float cmin(this float3 f) => math.cmin(f);
3440
/// <inheritdoc cref="math.cmin(float4)"/>
35-
[MethodImpl(IL)] public static float cmin(this float2 f) => fcmin(f);
41+
[MethodImpl(IL)] public static float cmin(this float2 f) => math.cmin(f);
3642

3743
/// returns the greatest absolute value of the components
38-
[MethodImpl(IL)] public static float acmax(this float4 f) => f.abs().fcmin();
44+
[MethodImpl(IL)] public static float acmax(this float4 f) => f.abs().cmin();
3945
/// <inheritdoc cref="acmax(float4)"/>
40-
[MethodImpl(IL)] public static float acmax(this float3 f) => f.abs().fcmin();
46+
[MethodImpl(IL)] public static float acmax(this float3 f) => f.abs().cmin();
4147
/// <inheritdoc cref="acmax(float4)"/>
42-
[MethodImpl(IL)] public static float acmax(this float2 f) => f.abs().fcmin();
48+
[MethodImpl(IL)] public static float acmax(this float2 f) => f.abs().cmin();
4349

4450
/// returns the smallest absolute value of the components
45-
[MethodImpl(IL)] public static float acmin(this float4 f) => f.abs().fcmin();
51+
[MethodImpl(IL)] public static float acmin(this float4 f) => f.abs().cmin();
4652
/// <inheritdoc cref="acmin(float4)"/>
47-
[MethodImpl(IL)] public static float acmin(this float3 f) => f.abs().fcmin();
53+
[MethodImpl(IL)] public static float acmin(this float3 f) => f.abs().cmin();
4854
/// <inheritdoc cref="acmin(float4)"/>
49-
[MethodImpl(IL)] public static float acmin(this float2 f) => f.abs().fcmin();
55+
[MethodImpl(IL)] public static float acmin(this float2 f) => f.abs().cmin();
5056

5157

5258
// [BurstCompile]
5359

54-
60+
#if MATHX_FUNCTION_POINTERS
5561
public static readonly f1x2_f1 p_fmax = compile<f1x2_f1>(fmax); // We want to generate this line
5662

5763
[MethodImpl(IL)]
5864
public static int fmax(int x, int y) => x ^ ((x ^ y) & -(x < y ? 1 : 0));
5965

66+
6067
[BurstCompile, MonoPInvokeCallback(typeof(f1x2_f1))] // and also generate this attribute for the method we added the attribute to
68+
6169
[MethodImpl(IL)] public static float fmax(this float x, float y) => x <= y ? x : y;
6270

6371
[MethodImpl(IL)] public static float2 fmax(this float2 x, float y) => p_fmax.RunPerAxis(x, y);
@@ -69,6 +77,7 @@ public static partial class mathx
6977
[MethodImpl(IL)] public static float4 fmax(this float4 x, float4 y) => p_fmax.RunPerAxis(x, y);
7078

7179
[MethodImpl(IL)] public static float fcmax(this float2 x) => fmax(x.x, x.y);
80+
7281
[MethodImpl(IL)] public static float fcmax(this float3 x) => p_fmax.RunNested(x);
7382
[MethodImpl(IL)] public static float fcmax(this float4 x) => p_fmax.RunNested(x);
7483

@@ -79,6 +88,8 @@ public static partial class mathx
7988
[MethodImpl(IL)] public static float fcmin(this float3 x) => x.x.fmin(x.y).fmin(x.z);
8089
[MethodImpl(IL)] public static float fcmin(this float4 x) => fmin(x.x.fmin(x.y), x.z.fmin(x.w));
8190

91+
#endif
92+
8293

8394
// /// <summary>
8495
// /// Returns the sign of x

0 commit comments

Comments
 (0)