From 0745fb50cf62215f88883f04db6b3d8a1e7b344b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 25 Nov 2021 00:25:18 +0100 Subject: [PATCH] Refactor Float/Vector extensions --- .../Extensions}/FloatExtensions.cs | 89 +++++++++---------- .../Extensions}/VectorExtensions.cs | 89 +++++++++---------- .../UnsupportedDoubleOperationsException.cs | 2 +- 3 files changed, 85 insertions(+), 95 deletions(-) rename src/ComputeSharp/{Graphics/Extensions/System.Numerics => Core/Extensions}/FloatExtensions.cs (55%) rename src/ComputeSharp/{Graphics/Extensions/System.Numerics => Core/Extensions}/VectorExtensions.cs (55%) diff --git a/src/ComputeSharp/Graphics/Extensions/System.Numerics/FloatExtensions.cs b/src/ComputeSharp/Core/Extensions/FloatExtensions.cs similarity index 55% rename from src/ComputeSharp/Graphics/Extensions/System.Numerics/FloatExtensions.cs rename to src/ComputeSharp/Core/Extensions/FloatExtensions.cs index ab081614..dc2e6464 100644 --- a/src/ComputeSharp/Graphics/Extensions/System.Numerics/FloatExtensions.cs +++ b/src/ComputeSharp/Core/Extensions/FloatExtensions.cs @@ -1,8 +1,9 @@ -using System.Diagnostics.Contracts; -using ComputeSharp; -using static System.Runtime.InteropServices.MemoryMarshal; +using System; +using System.Diagnostics.Contracts; +using System.Numerics; +using System.Runtime.InteropServices; -namespace System.Numerics; +namespace ComputeSharp; /// /// A that contains extension methods for the , and types. @@ -10,12 +11,15 @@ namespace System.Numerics; public static class FloatExtensions { /// - /// Converts a array into a with items of type . + /// Converts a into a with items of type . /// - /// The input array. - /// A with items of type , mapping the original array. + /// The input . + /// A with items of type , mapping the original . [Pure] - public static Span AsVector2Span(this Float2[] array) => Cast(array); + public static Span AsVector2(this Span span) + { + return MemoryMarshal.Cast(span); + } /// /// Converts a into a with items of type . @@ -23,31 +27,10 @@ public static class FloatExtensions /// The input . /// A with items of type , mapping the original . [Pure] - public static ReadOnlySpan AsVector2Span(this ReadOnlySpan span) => Cast(span); - - /// - /// Converts a into a with items of type . - /// - /// The input . - /// A with items of type , mapping the original . - [Pure] - public static Span AsVector2Span(this Span span) => Cast(span); - - /// - /// Converts a array into a with items of type . - /// - /// The input array. - /// A with items of type , mapping the original array. - [Pure] - public static Span AsVector3Span(this Float3[] array) => Cast(array); - - /// - /// Converts a into a with items of type . - /// - /// The input . - /// A with items of type , mapping the original . - [Pure] - public static ReadOnlySpan AsVector3Span(this ReadOnlySpan span) => Cast(span); + public static ReadOnlySpan AsVector2(this ReadOnlySpan span) + { + return MemoryMarshal.Cast(span); + } /// /// Converts a into a with items of type . @@ -55,23 +38,21 @@ public static class FloatExtensions /// The input . /// A with items of type , mapping the original . [Pure] - public static Span AsVector3Span(this Span span) => Cast(span); + public static Span AsVector3(this Span span) + { + return MemoryMarshal.Cast(span); + } /// - /// Converts a array into a with items of type . + /// Converts a into a with items of type . /// - /// The input array. - /// A with items of type , mapping the original array. + /// The input . + /// A with items of type , mapping the original . [Pure] - public static Span AsVector4Span(this Float4[] array) => Cast(array); - - /// - /// Converts a into a with items of type . - /// - /// The input . - /// A with items of type , mapping the original . - [Pure] - public static ReadOnlySpan AsVector4Span(this ReadOnlySpan span) => Cast(span); + public static ReadOnlySpan AsVector3(this ReadOnlySpan span) + { + return MemoryMarshal.Cast(span); + } /// /// Converts a into a with items of type . @@ -79,5 +60,19 @@ public static class FloatExtensions /// The input . /// A with items of type , mapping the original . [Pure] - public static Span AsVector4Span(this Span span) => Cast(span); + public static Span AsVector4(this Span span) + { + return MemoryMarshal.Cast(span); + } + + /// + /// Converts a into a with items of type . + /// + /// The input . + /// A with items of type , mapping the original . + [Pure] + public static ReadOnlySpan AsVector4(this ReadOnlySpan span) + { + return MemoryMarshal.Cast(span); + } } diff --git a/src/ComputeSharp/Graphics/Extensions/System.Numerics/VectorExtensions.cs b/src/ComputeSharp/Core/Extensions/VectorExtensions.cs similarity index 55% rename from src/ComputeSharp/Graphics/Extensions/System.Numerics/VectorExtensions.cs rename to src/ComputeSharp/Core/Extensions/VectorExtensions.cs index b29132e5..1dd2a4b5 100644 --- a/src/ComputeSharp/Graphics/Extensions/System.Numerics/VectorExtensions.cs +++ b/src/ComputeSharp/Core/Extensions/VectorExtensions.cs @@ -1,8 +1,9 @@ -using System.Diagnostics.Contracts; -using ComputeSharp; -using static System.Runtime.InteropServices.MemoryMarshal; +using System; +using System.Diagnostics.Contracts; +using System.Numerics; +using System.Runtime.InteropServices; -namespace System.Numerics; +namespace ComputeSharp; /// /// A that contains extension methods for the , and types. @@ -10,12 +11,15 @@ namespace System.Numerics; public static class VectorExtensions { /// - /// Converts a array into a with items of type . + /// Converts a into a with items of type . /// - /// The input array. - /// A with items of type , mapping the original array. + /// The input . + /// A with items of type , mapping the original . [Pure] - public static Span AsFloat2Span(this Vector2[] array) => Cast(array); + public static Span AsFloat2(this Span span) + { + return MemoryMarshal.Cast(span); + } /// /// Converts a into a with items of type . @@ -23,31 +27,10 @@ public static class VectorExtensions /// The input . /// A with items of type , mapping the original . [Pure] - public static ReadOnlySpan AsFloat2Span(this ReadOnlySpan span) => Cast(span); - - /// - /// Converts a into a with items of type . - /// - /// The input . - /// A with items of type , mapping the original . - [Pure] - public static Span AsFloat2Span(this Span span) => Cast(span); - - /// - /// Converts a array into a with items of type . - /// - /// The input array. - /// A with items of type , mapping the original array. - [Pure] - public static Span AsFloat3Span(this Vector3[] array) => Cast(array); - - /// - /// Converts a into a with items of type . - /// - /// The input . - /// A with items of type , mapping the original . - [Pure] - public static ReadOnlySpan AsFloat3Span(this ReadOnlySpan span) => Cast(span); + public static ReadOnlySpan AsFloat2(this ReadOnlySpan span) + { + return MemoryMarshal.Cast(span); + } /// /// Converts a into a with items of type . @@ -55,23 +38,21 @@ public static class VectorExtensions /// The input . /// A with items of type , mapping the original . [Pure] - public static Span AsFloat3Span(this Span span) => Cast(span); + public static Span AsFloat3(this Span span) + { + return MemoryMarshal.Cast(span); + } /// - /// Converts a array into a with items of type . + /// Converts a into a with items of type . /// - /// The input array. - /// A with items of type , mapping the original array. + /// The input . + /// A with items of type , mapping the original . [Pure] - public static Span AsFloat4Span(this Vector4[] array) => Cast(array); - - /// - /// Converts a into a with items of type . - /// - /// The input . - /// A with items of type , mapping the original . - [Pure] - public static ReadOnlySpan AsFloat4Span(this ReadOnlySpan span) => Cast(span); + public static ReadOnlySpan AsFloat3(this ReadOnlySpan span) + { + return MemoryMarshal.Cast(span); + } /// /// Converts a into a with items of type . @@ -79,5 +60,19 @@ public static class VectorExtensions /// The input . /// A with items of type , mapping the original . [Pure] - public static Span AsFloat4Span(this Span span) => Cast(span); + public static Span AsFloat4(this Span span) + { + return MemoryMarshal.Cast(span); + } + + /// + /// Converts a into a with items of type . + /// + /// The input . + /// A with items of type , mapping the original . + [Pure] + public static ReadOnlySpan AsFloat4(this ReadOnlySpan span) + { + return MemoryMarshal.Cast(span); + } } diff --git a/src/ComputeSharp/Graphics/Exceptions/UnsupportedDoubleOperationsException.cs b/src/ComputeSharp/Graphics/Exceptions/UnsupportedDoubleOperationsException.cs index fe2018e1..5685d305 100644 --- a/src/ComputeSharp/Graphics/Exceptions/UnsupportedDoubleOperationsException.cs +++ b/src/ComputeSharp/Graphics/Exceptions/UnsupportedDoubleOperationsException.cs @@ -13,7 +13,7 @@ public sealed class UnsupportedDoubleOperationsException : InvalidOperationExcep /// Creates a new instance. /// /// The message that describes the error. - public UnsupportedDoubleOperationsException(string message) + private UnsupportedDoubleOperationsException(string message) : base(message) { }