Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new Vector-specific extension methods. #1037

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Src/ILGPU/Util/Vectors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2023 ILGPU Project
// www.ilgpu.net
//
// File: Vectors.cs
//
// This file is part of ILGPU and is distributed under the University of Illinois Open
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace ILGPU.Util
{
/// <summary>
/// Represents performance-optimized vector helpers operating on spans.
/// </summary>
public static class Vectors
{
/// <summary>
/// Loads a vector (unsafe) from the given span while assuming proper alignment.
/// </summary>
/// <typeparam name="T">The vector element type.</typeparam>
/// <param name="source">The source span to load from.</param>
/// <returns>The loaded vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Vector<T> LoadAlignedVectorUnsafe<T>(
this ReadOnlySpan<T> source)
where T : struct
{
void* sourcePtr = Unsafe.AsPointer(ref MemoryMarshal.GetReference(source));
return Unsafe.Read<Vector<T>>(sourcePtr);
}

/// <summary>
/// Loads a vector (unsafe) from the given span while assuming proper alignment.
/// </summary>
/// <typeparam name="T">The vector element type.</typeparam>
/// <param name="source">The source span to load from.</param>
/// <returns>The loaded vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector<T> LoadAlignedVectorUnsafe<T>(this Span<T> source)
where T : struct =>
((ReadOnlySpan<T>)source).LoadAlignedVectorUnsafe();

/// <summary>
/// Stores the current vector into the given span while assuming proper alignment.
/// </summary>
/// <typeparam name="T">The element type.</typeparam>
/// <param name="value">The vector to store.</param>
/// <param name="target">The target span to store to.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void StoreAlignedVectorUnsafe<T>(
this Vector<T> value,
Span<T> target)
where T : struct
{
void* targetPtr = Unsafe.AsPointer(ref MemoryMarshal.GetReference(target));
Unsafe.Write(targetPtr, value);
}
}
}
Loading