// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics; namespace System.Numerics { /// <summary>Provides a collection of static methods for creating, manipulating, and otherwise operating on generic vectors.</summary> [Intrinsic] public static partial class Vector { internal static int Alignment => Vector<byte>.Count; /// <summary>Gets a value that indicates whether vector operations are subject to hardware acceleration through JIT intrinsic support.</summary> /// <value><see langword="true" /> if vector operations are subject to hardware acceleration; otherwise, <see langword="false" />.</value> /// <remarks>Vector operations are subject to hardware acceleration on systems that support single instruction, multiple data (SIMD) instructions and when the RyuJIT just-in-time compiler is used to compile managed code.</remarks> public static bool IsHardwareAccelerated { [Intrinsic] get => IsHardwareAccelerated; } /// <typeparam name="T">The type of the elements in the vector.</typeparam> extension<T>(Vector<T>) where T : IFloatingPointConstants<T> { /// <inheritdoc cref="Vector128.get_E{T}" /> public static Vector<T> E { [Intrinsic] get => Create(T.E); } /// <inheritdoc cref="Vector128.get_Pi{T}" /> public static Vector<T> Pi { [Intrinsic] get => Create(T.Pi); } /// <inheritdoc cref="Vector128.get_Tau{T}" /> public static Vector<T> Tau { [Intrinsic] get => Create(T.Tau); } } /// <typeparam name="T">The type of the elements in the vector.</typeparam> extension<T>(Vector<T>) where T : IFloatingPointIeee754<T> { /// <inheritdoc cref="Vector128.get_Epsilon{T}" /> public static Vector<T> Epsilon { [Intrinsic] get => Create(T.Epsilon); } /// <inheritdoc cref="Vector128.get_NaN{T}" /> public static Vector<T> NaN { [Intrinsic] get => Create(T.NaN); } /// <inheritdoc cref="Vector128.get_NegativeInfinity{T}" /> public static Vector<T> NegativeInfinity { [Intrinsic] get => Create(T.NegativeInfinity); } /// <inheritdoc cref="Vector128.get_NegativeZero{T}" /> public static Vector<T> NegativeZero { [Intrinsic] get => Create(T.NegativeZero); } /// <inheritdoc cref="Vector128.get_PositiveInfinity{T}" /> public static Vector<T> PositiveInfinity { [Intrinsic] get => Create(T.PositiveInfinity); } } /// <typeparam name="T">The type of the elements in the vector.</typeparam> extension<T>(Vector<T>) where T : ISignedNumber<T> { /// <inheritdoc cref="Vector128.get_NegativeOne{T}" /> public static Vector<T> NegativeOne { [Intrinsic] get => Create(T.NegativeOne); } /// <inheritdoc cref="Vector128.get_SignSequence{T}" /> public static Vector<T> SignSequence { [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] get => CreateAlternatingSequence(T.One, T.NegativeOne); } } /// <summary>Computes the absolute value of each element in a vector.</summary> /// <param name="value">The vector that will have its absolute value computed.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>A vector whose elements are the absolute value of the elements in <paramref name="value" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> Abs<T>(Vector<T> value) { if (Scalar<T>.IsUnsigned) { return value; } else { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T element = Scalar<T>.Abs(value.GetElementUnsafe(index)); result.SetElementUnsafe(index, element); } return result; } } /// <inheritdoc cref="Vector128.Add{T}(Vector128{T}, Vector128{T})" /> [Intrinsic] public static Vector<T> Add<T>(Vector<T> left, Vector<T> right) => left + right; /// <inheritdoc cref="Vector128.AddSaturate{T}(Vector128{T}, Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> AddSaturate<T>(Vector<T> left, Vector<T> right) { if ((typeof(T) == typeof(float)) || (typeof(T) == typeof(double))) { return left + right; } else { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.AddSaturate(left.GetElementUnsafe(index), right.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } } /// <inheritdoc cref="Vector128.All{T}(Vector128{T}, T)" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool All<T>(Vector<T> vector, T value) => vector == Create(value); /// <inheritdoc cref="Vector128.AllWhereAllBitsSet{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool AllWhereAllBitsSet<T>(Vector<T> vector) { if (typeof(T) == typeof(float)) { return All(vector.As<T, int>(), -1); } else if (typeof(T) == typeof(double)) { return All(vector.As<T, long>(), -1); } else { return All(vector, Scalar<T>.AllBitsSet); } } /// <summary>Computes the bitwise-and of a given vector and the ones complement of another vector.</summary> /// <param name="left">The vector to bitwise-and with <paramref name="right" />.</param> /// <param name="right">The vector to that is ones-complemented before being bitwise-and with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>The bitwise-and of <paramref name="left" /> and the ones-complement of <paramref name="right" />.</returns> [Intrinsic] public static Vector<T> AndNot<T>(Vector<T> left, Vector<T> right) => left & ~right; /// <inheritdoc cref="Vector128.Any{T}(Vector128{T}, T)" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool Any<T>(Vector<T> vector, T value) => EqualsAny(vector, Create(value)); /// <inheritdoc cref="Vector128.AnyWhereAllBitsSet{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool AnyWhereAllBitsSet<T>(Vector<T> vector) { if (typeof(T) == typeof(float)) { return Any(vector.As<T, int>(), -1); } else if (typeof(T) == typeof(double)) { return Any(vector.As<T, long>(), -1); } else { return Any(vector, Scalar<T>.AllBitsSet); } } /// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see cref="Vector{U}" />.</summary> /// <typeparam name="TFrom">The type of the input vector.</typeparam> /// <typeparam name="TTo">The type of the vector <paramref name="vector" /> should be reinterpreted as.</typeparam> /// <param name="vector">The vector to reinterpret.</param> /// <returns><paramref name="vector" /> reinterpreted as a new <see cref="Vector{U}" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="vector" /> (<typeparamref name="TFrom" />) or the type of the target (<typeparamref name="TTo" />) is not supported.</exception> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<TTo> As<TFrom, TTo>(this Vector<TFrom> vector) { ThrowHelper.ThrowForUnsupportedNumericsVectorBaseType<TFrom>(); ThrowHelper.ThrowForUnsupportedNumericsVectorBaseType<TTo>(); return Unsafe.BitCast<Vector<TFrom>, Vector<TTo>>(vector); } /// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see langword="Vector<Byte>" />.</summary> /// <typeparam name="T">The type of the input vector.</typeparam> /// <param name="value">The vector to reinterpret.</param> /// <returns><paramref name="value" /> reinterpreted as a new <see langword="Vector<Byte>" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] public static Vector<byte> AsVectorByte<T>(Vector<T> value) => value.As<T, byte>(); /// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see langword="Vector<Double>" />.</summary> /// <typeparam name="T">The type of the input vector.</typeparam> /// <param name="value">The vector to reinterpret.</param> /// <returns><paramref name="value" /> reinterpreted as a new <see langword="Vector<Double>" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] public static Vector<double> AsVectorDouble<T>(Vector<T> value) => value.As<T, double>(); /// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see langword="Vector<Int16>" />.</summary> /// <typeparam name="T">The type of the input vector.</typeparam> /// <param name="value">The vector to reinterpret.</param> /// <returns><paramref name="value" /> reinterpreted as a new <see langword="Vector<Int16>" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] public static Vector<short> AsVectorInt16<T>(Vector<T> value) => value.As<T, short>(); /// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see langword="Vector<Int32>" />.</summary> /// <typeparam name="T">The type of the input vector.</typeparam> /// <param name="value">The vector to reinterpret.</param> /// <returns><paramref name="value" /> reinterpreted as a new <see langword="Vector<Int32>" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] public static Vector<int> AsVectorInt32<T>(Vector<T> value) => value.As<T, int>(); /// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see langword="Vector<Int64>" />.</summary> /// <typeparam name="T">The type of the input vector.</typeparam> /// <param name="value">The vector to reinterpret.</param> /// <returns><paramref name="value" /> reinterpreted as a new <see langword="Vector<Int64>" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] public static Vector<long> AsVectorInt64<T>(Vector<T> value) => value.As<T, long>(); /// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see langword="Vector<IntPtr>" />.</summary> /// <typeparam name="T">The type of the input vector.</typeparam> /// <param name="value">The vector to reinterpret.</param> /// <returns><paramref name="value" /> reinterpreted as a new <see langword="Vector<IntPtr>" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] public static Vector<nint> AsVectorNInt<T>(Vector<T> value) => value.As<T, nint>(); /// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see langword="Vector<UIntPtr>" />.</summary> /// <typeparam name="T">The type of the input vector.</typeparam> /// <param name="value">The vector to reinterpret.</param> /// <returns><paramref name="value" /> reinterpreted as a new <see langword="Vector<UIntPtr>" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [CLSCompliant(false)] public static Vector<nuint> AsVectorNUInt<T>(Vector<T> value) => value.As<T, nuint>(); /// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see langword="Vector<SByte>" />.</summary> /// <typeparam name="T">The type of the input vector.</typeparam> /// <param name="value">The vector to reinterpret.</param> /// <returns><paramref name="value" /> reinterpreted as a new <see langword="Vector<SByte>" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [CLSCompliant(false)] public static Vector<sbyte> AsVectorSByte<T>(Vector<T> value) => value.As<T, sbyte>(); /// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see langword="Vector<Single>" />.</summary> /// <typeparam name="T">The type of the input vector.</typeparam> /// <param name="value">The vector to reinterpret.</param> /// <returns><paramref name="value" /> reinterpreted as a new <see langword="Vector<Single>" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] public static Vector<float> AsVectorSingle<T>(Vector<T> value) => value.As<T, float>(); /// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see langword="Vector<UInt16>" />.</summary> /// <typeparam name="T">The type of the input vector.</typeparam> /// <param name="value">The vector to reinterpret.</param> /// <returns><paramref name="value" /> reinterpreted as a new <see langword="Vector<UInt16>" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [CLSCompliant(false)] public static Vector<ushort> AsVectorUInt16<T>(Vector<T> value) => value.As<T, ushort>(); /// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see langword="Vector<UInt32>" />.</summary> /// <typeparam name="T">The type of the input vector.</typeparam> /// <param name="value">The vector to reinterpret.</param> /// <returns><paramref name="value" /> reinterpreted as a new <see langword="Vector<UInt32>" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [CLSCompliant(false)] public static Vector<uint> AsVectorUInt32<T>(Vector<T> value) => value.As<T, uint>(); /// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see langword="Vector<UInt64>" />.</summary> /// <typeparam name="T">The type of the input vector.</typeparam> /// <param name="value">The vector to reinterpret.</param> /// <returns><paramref name="value" /> reinterpreted as a new <see langword="Vector<UInt64>" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [CLSCompliant(false)] public static Vector<ulong> AsVectorUInt64<T>(Vector<T> value) => value.As<T, ulong>(); /// <summary>Computes the bitwise-and of two vectors.</summary> /// <param name="left">The vector to bitwise-and with <paramref name="right" />.</param> /// <param name="right">The vector to bitwise-and with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>The bitwise-and of <paramref name="left" /> and <paramref name="right" />.</returns> [Intrinsic] public static Vector<T> BitwiseAnd<T>(Vector<T> left, Vector<T> right) => left & right; /// <summary>Computes the bitwise-or of two vectors.</summary> /// <param name="left">The vector to bitwise-or with <paramref name="right" />.</param> /// <param name="right">The vector to bitwise-or with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>The bitwise-or of <paramref name="left" /> and <paramref name="right" />.</returns> [Intrinsic] public static Vector<T> BitwiseOr<T>(Vector<T> left, Vector<T> right) => left | right; /// <summary>Computes the ceiling of each element in a vector.</summary> /// <param name="vector">The vector that will have its ceiling computed.</param> /// <returns>A vector whose elements are the ceiling of the elements in <paramref name="vector" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static Vector<T> Ceiling<T>(Vector<T> vector) { if (Scalar<T>.IsFloatingPoint) { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.Ceiling(vector.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } else { return vector; } } /// <summary>Computes the ceiling of each element in a vector.</summary> /// <param name="value">The vector that will have its ceiling computed.</param> /// <returns>A vector whose elements are the ceiling of the elements in <paramref name="value" />.</returns> /// <seealso cref="double.Ceiling(double)" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> Ceiling(Vector<double> value) { Unsafe.SkipInit(out Vector<double> result); for (int index = 0; index < Vector<double>.Count; index++) { double element = Scalar<double>.Ceiling(value.GetElementUnsafe(index)); result.SetElementUnsafe(index, element); } return result; } /// <summary>Computes the ceiling of each element in a vector.</summary> /// <param name="value">The vector that will have its ceiling computed.</param> /// <returns>A vector whose elements are the ceiling of the elements in <paramref name="value" />.</returns> /// <seealso cref="float.Ceiling(float)" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<float> Ceiling(Vector<float> value) { Unsafe.SkipInit(out Vector<float> result); for (int index = 0; index < Vector<float>.Count; index++) { float element = Scalar<float>.Ceiling(value.GetElementUnsafe(index)); result.SetElementUnsafe(index, element); } return result; } /// <inheritdoc cref="Vector128.Clamp{T}(Vector128{T}, Vector128{T}, Vector128{T})" /> [Intrinsic] public static Vector<T> Clamp<T>(Vector<T> value, Vector<T> min, Vector<T> max) { // We must follow HLSL behavior in the case user specified min value is bigger than max value. return Min(Max(value, min), max); } /// <inheritdoc cref="Vector128.ClampNative{T}(Vector128{T}, Vector128{T}, Vector128{T})" /> [Intrinsic] public static Vector<T> ClampNative<T>(Vector<T> value, Vector<T> min, Vector<T> max) { // We must follow HLSL behavior in the case user specified min value is bigger than max value. return MinNative(MaxNative(value, min), max); } /// <summary>Conditionally selects a value from two vectors on a bitwise basis.</summary> /// <param name="condition">The mask that is used to select a value from <paramref name="left" /> or <paramref name="right" />.</param> /// <param name="left">The vector that is selected when the corresponding bit in <paramref name="condition" /> is one.</param> /// <param name="right">The vector that is selected when the corresponding bit in <paramref name="condition" /> is zero.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>A vector whose bits come from <paramref name="left" /> or <paramref name="right" /> based on the value of <paramref name="condition" />.</returns> /// <remarks>The returned vector is equivalent to <paramref name="condition" /> <c>?</c> <paramref name="left" /> <c>:</c> <paramref name="right" /> on a per-bit basis.</remarks> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> ConditionalSelect<T>(Vector<T> condition, Vector<T> left, Vector<T> right) => (left & condition) | AndNot(right, condition); /// <summary>Conditionally selects a value from two vectors on a bitwise basis.</summary> /// <param name="condition">The mask that is used to select a value from <paramref name="left" /> or <paramref name="right" />.</param> /// <param name="left">The vector that is selected when the corresponding bit in <paramref name="condition" /> is one.</param> /// <param name="right">The vector that is selected when the corresponding bit in <paramref name="condition" /> is zero.</param> /// <returns>A vector whose bits come from <paramref name="left" /> or <paramref name="right" /> based on the value of <paramref name="condition" />.</returns> /// <remarks>The returned vector is equivalent to <paramref name="condition" /> <c>?</c> <paramref name="left" /> <c>:</c> <paramref name="right" /> on a per-bit basis.</remarks> [Intrinsic] public static Vector<float> ConditionalSelect(Vector<int> condition, Vector<float> left, Vector<float> right) => ConditionalSelect(condition.As<int, float>(), left, right); /// <summary>Conditionally selects a value from two vectors on a bitwise basis.</summary> /// <param name="condition">The mask that is used to select a value from <paramref name="left" /> or <paramref name="right" />.</param> /// <param name="left">The vector that is selected when the corresponding bit in <paramref name="condition" /> is one.</param> /// <param name="right">The vector that is selected when the corresponding bit in <paramref name="condition" /> is zero.</param> /// <returns>A vector whose bits come from <paramref name="left" /> or <paramref name="right" /> based on the value of <paramref name="condition" />.</returns> /// <remarks>The returned vector is equivalent to <paramref name="condition" /> <c>?</c> <paramref name="left" /> <c>:</c> <paramref name="right" /> on a per-bit basis.</remarks> [Intrinsic] public static Vector<double> ConditionalSelect(Vector<long> condition, Vector<double> left, Vector<double> right) => ConditionalSelect(condition.As<long, double>(), left, right); /// <summary>Converts a <see langword="Vector<Int64>" /> to a <see langword="Vector<Double>" />.</summary> /// <param name="value">The vector to convert.</param> /// <returns>The converted vector.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> ConvertToDouble(Vector<long> value) { if (Vector<double>.Count == Vector512<double>.Count) { return Vector512.ConvertToDouble(value.AsVector512()).AsVector(); } else if (Vector<double>.Count == Vector256<double>.Count) { return Vector256.ConvertToDouble(value.AsVector256()).AsVector(); } else { Debug.Assert(Vector<double>.Count == Vector128<double>.Count); return Vector128.ConvertToDouble(value.AsVector128()).AsVector(); } } /// <summary>Converts a <see langword="Vector<UInt64>" /> to a <see langword="Vector<Double>" />.</summary> /// <param name="value">The vector to convert.</param> /// <returns>The converted vector.</returns> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> ConvertToDouble(Vector<ulong> value) { if (Vector<double>.Count == Vector512<double>.Count) { return Vector512.ConvertToDouble(value.AsVector512()).AsVector(); } else if (Vector<double>.Count == Vector256<double>.Count) { return Vector256.ConvertToDouble(value.AsVector256()).AsVector(); } else { Debug.Assert(Vector<double>.Count == Vector128<double>.Count); return Vector128.ConvertToDouble(value.AsVector128()).AsVector(); } } /// <summary>Converts a <see langword="Vector<Single>" /> to a <see langword="Vector<Int32>" /> using saturation on overflow.</summary> /// <param name="value">The vector to convert.</param> /// <returns>The converted vector.</returns> [Intrinsic] public static Vector<int> ConvertToInt32(Vector<float> value) { Unsafe.SkipInit(out Vector<int> result); for (int i = 0; i < Vector<int>.Count; i++) { int element = float.ConvertToInteger<int>(value.GetElementUnsafe(i)); result.SetElementUnsafe(i, element); } return result; } /// <summary>Converts a <see langword="Vector<Single>" /> to a <see langword="Vector<Int32>" /> using platform specific behavior on overflow.</summary> /// <param name="value">The vector to convert.</param> /// <returns>The converted vector.</returns> [Intrinsic] public static Vector<int> ConvertToInt32Native(Vector<float> value) { Unsafe.SkipInit(out Vector<int> result); for (int i = 0; i < Vector<int>.Count; i++) { int element = float.ConvertToIntegerNative<int>(value.GetElementUnsafe(i)); result.SetElementUnsafe(i, element); } return result; } /// <summary>Converts a <see langword="Vector<Double>" /> to a <see langword="Vector<Int64>" /> using saturation on overflow.</summary> /// <param name="value">The vector to convert.</param> /// <returns>The converted vector.</returns> [Intrinsic] public static Vector<long> ConvertToInt64(Vector<double> value) { Unsafe.SkipInit(out Vector<long> result); for (int i = 0; i < Vector<long>.Count; i++) { long element = double.ConvertToInteger<long>(value.GetElementUnsafe(i)); result.SetElementUnsafe(i, element); } return result; } /// <summary>Converts a <see langword="Vector<Double>" /> to a <see langword="Vector<Int64>" /> using platform specific behavior on overflow.</summary> /// <param name="value">The vector to convert.</param> /// <returns>The converted vector.</returns> [Intrinsic] public static Vector<long> ConvertToInt64Native(Vector<double> value) { Unsafe.SkipInit(out Vector<long> result); for (int i = 0; i < Vector<long>.Count; i++) { long element = double.ConvertToIntegerNative<long>(value.GetElementUnsafe(i)); result.SetElementUnsafe(i, element); } return result; } /// <summary>Converts a <see langword="Vector<Int32>" /> to a <see langword="Vector<Single>" />.</summary> /// <param name="value">The vector to convert.</param> /// <returns>The converted vector.</returns> [Intrinsic] public static Vector<float> ConvertToSingle(Vector<int> value) { Unsafe.SkipInit(out Vector<float> result); for (int i = 0; i < Vector<float>.Count; i++) { float element = value.GetElementUnsafe(i); result.SetElementUnsafe(i, element); } return result; } /// <summary>Converts a <see langword="Vector<UInt32>" /> to a <see langword="Vector<Single>" />.</summary> /// <param name="value">The vector to convert.</param> /// <returns>The converted vector.</returns> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<float> ConvertToSingle(Vector<uint> value) { if (Vector<float>.Count == Vector512<float>.Count) { return Vector512.ConvertToSingle(value.AsVector512()).AsVector(); } else if (Vector<float>.Count == Vector256<float>.Count) { return Vector256.ConvertToSingle(value.AsVector256()).AsVector(); } else { Debug.Assert(Vector<float>.Count == Vector128<float>.Count); return Vector128.ConvertToSingle(value.AsVector128()).AsVector(); } } /// <summary>Converts a <see langword="Vector<Single>" /> to a <see langword="Vector<UInt32>" /> using saturation on overflow.</summary> /// <param name="value">The vector to convert.</param> /// <returns>The converted vector.</returns> [Intrinsic] [CLSCompliant(false)] public static Vector<uint> ConvertToUInt32(Vector<float> value) { Unsafe.SkipInit(out Vector<uint> result); for (int i = 0; i < Vector<uint>.Count; i++) { uint element = float.ConvertToInteger<uint>(value.GetElementUnsafe(i)); result.SetElementUnsafe(i, element); } return result; } /// <summary>Converts a <see langword="Vector<Single>" /> to a <see langword="Vector<UInt32>" /> using platform specific behavior on overflow.</summary> /// <param name="value">The vector to convert.</param> /// <returns>The converted vector.</returns> [Intrinsic] [CLSCompliant(false)] public static Vector<uint> ConvertToUInt32Native(Vector<float> value) { Unsafe.SkipInit(out Vector<uint> result); for (int i = 0; i < Vector<uint>.Count; i++) { uint element = float.ConvertToIntegerNative<uint>(value.GetElementUnsafe(i)); result.SetElementUnsafe(i, element); } return result; } /// <summary>Converts a <see langword="Vector<Double>" /> to a <see langword="Vector<UInt64>" /> using saturation on overflow.</summary> /// <param name="value">The vector to convert.</param> /// <returns>The converted vector.</returns> [Intrinsic] [CLSCompliant(false)] public static Vector<ulong> ConvertToUInt64(Vector<double> value) { Unsafe.SkipInit(out Vector<ulong> result); for (int i = 0; i < Vector<ulong>.Count; i++) { ulong element = double.ConvertToInteger<ulong>(value.GetElementUnsafe(i)); result.SetElementUnsafe(i, element); } return result; } /// <summary>Converts a <see langword="Vector<Double>" /> to a <see langword="Vector<UInt64>" /> using platform specific behavior on overflow.</summary> /// <param name="value">The vector to convert.</param> /// <returns>The converted vector.</returns> [Intrinsic] [CLSCompliant(false)] public static Vector<ulong> ConvertToUInt64Native(Vector<double> value) { Unsafe.SkipInit(out Vector<ulong> result); for (int i = 0; i < Vector<ulong>.Count; i++) { ulong element = double.ConvertToIntegerNative<ulong>(value.GetElementUnsafe(i)); result.SetElementUnsafe(i, element); } return result; } internal static Vector<T> Cos<T>(Vector<T> vector) where T : ITrigonometricFunctions<T> { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = T.Cos(vector.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } /// <inheritdoc cref="Vector128.Cos(Vector128{double})" /> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> Cos(Vector<double> vector) { if (IsHardwareAccelerated) { return VectorMath.CosDouble<Vector<double>, Vector<long>>(vector); } else { return Cos<double>(vector); } } /// <inheritdoc cref="Vector128.Cos(Vector128{float})" /> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<float> Cos(Vector<float> vector) { if (IsHardwareAccelerated) { return VectorMath.CosSingle<Vector<float>, Vector<int>, Vector<double>, Vector<long>>(vector); } else { return Cos<float>(vector); } } /// <inheritdoc cref="Vector128.CopySign{T}(Vector128{T}, Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> CopySign<T>(Vector<T> value, Vector<T> sign) { if (Scalar<T>.IsUnsigned) { return value; } else if (IsHardwareAccelerated) { return VectorMath.CopySign<Vector<T>, T>(value, sign); } else { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T element = Scalar<T>.CopySign(value.GetElementUnsafe(index), sign.GetElementUnsafe(index)); result.SetElementUnsafe(index, element); } return result; } } /// <inheritdoc cref="Vector128.Count{T}(Vector128{T}, T)" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Count<T>(Vector<T> vector, T value) { if (Vector<T>.Count == Vector512<T>.Count) { return Vector512.Count(vector.AsVector512(), value); } else if (Vector<T>.Count == Vector256<T>.Count) { return Vector256.Count(vector.AsVector256(), value); } else { Debug.Assert(Vector<T>.Count == Vector128<T>.Count); return Vector128.Count(vector.AsVector128(), value); } } /// <inheritdoc cref="Vector128.CountWhereAllBitsSet{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int CountWhereAllBitsSet<T>(Vector<T> vector) { if (typeof(T) == typeof(float)) { return Count(vector.As<T, int>(), -1); } else if (typeof(T) == typeof(double)) { return Count(vector.As<T, long>(), -1); } else { return Count(vector, Scalar<T>.AllBitsSet); } } /// <summary>Creates a new <see cref="Vector{T}" /> instance with all elements initialized to the specified value.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="value">The value that all elements will be initialized to.</param> /// <returns>A new <see cref="Vector{T}" /> with all elements initialized to <paramref name="value" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] public static Vector<T> Create<T>(T value) { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { result.SetElementUnsafe(index, value); } return result; } /// <summary>Creates a new <see cref="Vector{T}" /> from a given readonly span.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="values">The readonly span from which the vector is created.</param> /// <returns>A new <see cref="Vector{T}" /> with its elements set to the first <see cref="Vector{T}.Count" /> elements from <paramref name="values" />.</returns> /// <exception cref="ArgumentOutOfRangeException">The length of <paramref name="values" /> is less than <see cref="Vector128{T}.Count" />.</exception> /// <exception cref="NotSupportedException">The type of <paramref name="values" /> (<typeparamref name="T" />) is not supported.</exception> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> Create<T>(ReadOnlySpan<T> values) { if (values.Length < Vector<T>.Count) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.values); } return Unsafe.ReadUnaligned<Vector<T>>(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values))); } /// <summary>Creates a new <see cref="Vector{T}" /> instance with the first element initialized to the specified value and the remaining elements initialized to zero.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="value">The value that element 0 will be initialized to.</param> /// <returns>A new <see cref="Vector{T}" /> instance with the first element initialized to <paramref name="value" /> and the remaining elements initialized to zero.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] public static Vector<T> CreateScalar<T>(T value) { Vector<T> result = Vector<T>.Zero; result.SetElementUnsafe(0, value); return result; } /// <summary>Creates a new <see cref="Vector{T}" /> instance with the first element initialized to the specified value and the remaining elements left uninitialized.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="value">The value that element 0 will be initialized to.</param> /// <returns>A new <see cref="Vector{T}" /> instance with the first element initialized to <paramref name="value" /> and the remaining elements left uninitialized.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> CreateScalarUnsafe<T>(T value) { // This relies on us stripping the "init" flag from the ".locals" // declaration to let the upper bits be uninitialized. ThrowHelper.ThrowForUnsupportedNumericsVectorBaseType<T>(); Unsafe.SkipInit(out Vector<T> result); result.SetElementUnsafe(0, value); return result; } /// <summary>Creates a new <see cref="Vector{T}" /> instance where the elements begin at a specified value and which are spaced apart according to another specified value.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="start">The value that element 0 will be initialized to.</param> /// <param name="step">The value that indicates how far apart each element should be from the previous.</param> /// <returns>A new <see cref="Vector{T}" /> instance with the first element initialized to <paramref name="start" /> and each subsequent element initialized to the value of the previous element plus <paramref name="step" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> CreateSequence<T>(T start, T step) => (Vector<T>.Indices * step) + Create(start); /// <summary>Creates a new <see cref="Vector{T}" /> instance where the elements begin at a specified value and are multiplied by another specified value.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="initial">The value that element 0 will be initialized to.</param> /// <param name="multiplier">The value that indicates how each element should be scaled from the previous.</param> /// <returns>A new <see cref="Vector{T}" /> instance with each element initialized to <paramref name="initial" /> multiplied by <paramref name="multiplier" /> raised to the element index.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="initial"/> and <paramref name="multiplier"/> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> CreateGeometricSequence<T>(T initial, [ConstantExpected] T multiplier) { if (Vector<T>.Count == Vector512<T>.Count) { return Vector512.CreateGeometricSequence(initial, multiplier).AsVector(); } else if (Vector<T>.Count == Vector256<T>.Count) { return Vector256.CreateGeometricSequence(initial, multiplier).AsVector(); } else { Debug.Assert(Vector<T>.Count == Vector128<T>.Count); return Vector128.CreateGeometricSequence(initial, multiplier).AsVector(); } } /// <summary>Creates a new <see cref="Vector{T}" /> instance whose elements alternate between two specified values.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="even">The value assigned to even-indexed elements.</param> /// <param name="odd">The value assigned to odd-indexed elements.</param> /// <returns>A new <see cref="Vector{T}" /> instance whose even-indexed elements are initialized to <paramref name="even" /> and odd-indexed elements are initialized to <paramref name="odd" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="even"/> and <paramref name="odd"/> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> CreateAlternatingSequence<T>(T even, T odd) { Unsafe.SkipInit(out Vector<T> result); for (int index = 1; index < Vector<T>.Count; index += 2) { result.SetElementUnsafe(index - 1, even); result.SetElementUnsafe(index, odd); } return result; } /// <summary>Creates a new <see cref="Vector{T}" /> instance whose elements are the reciprocal of an arithmetic sequence.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="start">The value that element 0 of the arithmetic sequence will be initialized to.</param> /// <param name="step">The value that indicates how far apart each element of the arithmetic sequence should be from the previous.</param> /// <returns>A new <see cref="Vector{T}" /> instance whose elements are initialized to one divided by the corresponding element of the arithmetic sequence.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="start"/> and <paramref name="step"/> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> CreateHarmonicSequence<T>(T start, T step) => Vector<T>.One / CreateSequence(start, step); /// <summary>Creates a new vector by concatenating the lower halves of two vectors.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="left">The vector that provides the lower half of the result.</param> /// <param name="right">The vector that provides the upper half of the result.</param> /// <returns>A new vector whose lower half comes from the lower half of <paramref name="left" /> and whose upper half comes from the lower half of <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> ConcatLowerLower<T>(Vector<T> left, Vector<T> right) { if (Vector<T>.Count == Vector512<T>.Count) { return Vector512.ConcatLowerLower(left.AsVector512(), right.AsVector512()).AsVector(); } else if (Vector<T>.Count == Vector256<T>.Count) { return Vector256.ConcatLowerLower(left.AsVector256(), right.AsVector256()).AsVector(); } else { Debug.Assert(Vector<T>.Count == Vector128<T>.Count); return Vector128.ConcatLowerLower(left.AsVector128(), right.AsVector128()).AsVector(); } } /// <summary>Creates a new vector by concatenating the upper half of one vector and the lower half of another vector.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="left">The vector that provides the lower half of the result.</param> /// <param name="right">The vector that provides the upper half of the result.</param> /// <returns>A new vector whose lower half comes from the upper half of <paramref name="left" /> and whose upper half comes from the lower half of <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> ConcatUpperLower<T>(Vector<T> left, Vector<T> right) { if (Vector<T>.Count == Vector512<T>.Count) { return Vector512.ConcatUpperLower(left.AsVector512(), right.AsVector512()).AsVector(); } else if (Vector<T>.Count == Vector256<T>.Count) { return Vector256.ConcatUpperLower(left.AsVector256(), right.AsVector256()).AsVector(); } else { Debug.Assert(Vector<T>.Count == Vector128<T>.Count); return Vector128.ConcatUpperLower(left.AsVector128(), right.AsVector128()).AsVector(); } } /// <summary>Creates a new vector by concatenating the upper halves of two vectors.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="left">The vector that provides the lower half of the result.</param> /// <param name="right">The vector that provides the upper half of the result.</param> /// <returns>A new vector whose lower half comes from the upper half of <paramref name="left" /> and whose upper half comes from the upper half of <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> ConcatUpperUpper<T>(Vector<T> left, Vector<T> right) { if (Vector<T>.Count == Vector512<T>.Count) { return Vector512.ConcatUpperUpper(left.AsVector512(), right.AsVector512()).AsVector(); } else if (Vector<T>.Count == Vector256<T>.Count) { return Vector256.ConcatUpperUpper(left.AsVector256(), right.AsVector256()).AsVector(); } else { Debug.Assert(Vector<T>.Count == Vector128<T>.Count); return Vector128.ConcatUpperUpper(left.AsVector128(), right.AsVector128()).AsVector(); } } /// <summary>Creates a new vector by concatenating the lower half of one vector and the upper half of another vector.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="left">The vector that provides the lower half of the result.</param> /// <param name="right">The vector that provides the upper half of the result.</param> /// <returns>A new vector whose lower half comes from the lower half of <paramref name="left" /> and whose upper half comes from the upper half of <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> ConcatLowerUpper<T>(Vector<T> left, Vector<T> right) { if (Vector<T>.Count == Vector512<T>.Count) { return Vector512.ConcatLowerUpper(left.AsVector512(), right.AsVector512()).AsVector(); } else if (Vector<T>.Count == Vector256<T>.Count) { return Vector256.ConcatLowerUpper(left.AsVector256(), right.AsVector256()).AsVector(); } else { Debug.Assert(Vector<T>.Count == Vector128<T>.Count); return Vector128.ConcatLowerUpper(left.AsVector128(), right.AsVector128()).AsVector(); } } /// <summary>Interleaves the lower halves of two vectors.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="left">The vector that provides the even-indexed elements.</param> /// <param name="right">The vector that provides the odd-indexed elements.</param> /// <returns>A new vector containing interleaved elements from the lower halves of <paramref name="left" /> and <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> ZipLower<T>(Vector<T> left, Vector<T> right) { if (Vector<T>.Count == Vector512<T>.Count) { return Vector512.ZipLower(left.AsVector512(), right.AsVector512()).AsVector(); } else if (Vector<T>.Count == Vector256<T>.Count) { return Vector256.ZipLower(left.AsVector256(), right.AsVector256()).AsVector(); } else { Debug.Assert(Vector<T>.Count == Vector128<T>.Count); return Vector128.ZipLower(left.AsVector128(), right.AsVector128()).AsVector(); } } /// <summary>Interleaves the upper halves of two vectors.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="left">The vector that provides the even-indexed elements.</param> /// <param name="right">The vector that provides the odd-indexed elements.</param> /// <returns>A new vector containing interleaved elements from the upper halves of <paramref name="left" /> and <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> ZipUpper<T>(Vector<T> left, Vector<T> right) { if (Vector<T>.Count == Vector512<T>.Count) { return Vector512.ZipUpper(left.AsVector512(), right.AsVector512()).AsVector(); } else if (Vector<T>.Count == Vector256<T>.Count) { return Vector256.ZipUpper(left.AsVector256(), right.AsVector256()).AsVector(); } else { Debug.Assert(Vector<T>.Count == Vector128<T>.Count); return Vector128.ZipUpper(left.AsVector128(), right.AsVector128()).AsVector(); } } /// <summary>Interleaves two vectors into their lower and upper halves.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="left">The vector that provides the even-indexed elements.</param> /// <param name="right">The vector that provides the odd-indexed elements.</param> /// <returns>A pair of vectors containing interleaved elements from the lower and upper halves of <paramref name="left" /> and <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static (Vector<T> Lower, Vector<T> Upper) Zip<T>(Vector<T> left, Vector<T> right) => (ZipLower(left, right), ZipUpper(left, right)); /// <summary>De-interleaves the even-indexed elements from two vectors.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="left">The vector that provides the lower half of the result.</param> /// <param name="right">The vector that provides the upper half of the result.</param> /// <returns>A new vector containing the even-indexed elements from <paramref name="left" /> followed by the even-indexed elements from <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> UnzipEven<T>(Vector<T> left, Vector<T> right) { if (Vector<T>.Count == Vector512<T>.Count) { return Vector512.UnzipEven(left.AsVector512(), right.AsVector512()).AsVector(); } else if (Vector<T>.Count == Vector256<T>.Count) { return Vector256.UnzipEven(left.AsVector256(), right.AsVector256()).AsVector(); } else { Debug.Assert(Vector<T>.Count == Vector128<T>.Count); return Vector128.UnzipEven(left.AsVector128(), right.AsVector128()).AsVector(); } } /// <summary>De-interleaves the odd-indexed elements from two vectors.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="left">The vector that provides the lower half of the result.</param> /// <param name="right">The vector that provides the upper half of the result.</param> /// <returns>A new vector containing the odd-indexed elements from <paramref name="left" /> followed by the odd-indexed elements from <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> UnzipOdd<T>(Vector<T> left, Vector<T> right) { if (Vector<T>.Count == Vector512<T>.Count) { return Vector512.UnzipOdd(left.AsVector512(), right.AsVector512()).AsVector(); } else if (Vector<T>.Count == Vector256<T>.Count) { return Vector256.UnzipOdd(left.AsVector256(), right.AsVector256()).AsVector(); } else { Debug.Assert(Vector<T>.Count == Vector128<T>.Count); return Vector128.UnzipOdd(left.AsVector128(), right.AsVector128()).AsVector(); } } /// <summary>De-interleaves two vectors into their even-indexed and odd-indexed elements.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="left">The vector that provides the lower half of each result.</param> /// <param name="right">The vector that provides the upper half of each result.</param> /// <returns>A pair of vectors containing the even-indexed and odd-indexed elements from <paramref name="left" /> and <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static (Vector<T> Even, Vector<T> Odd) Unzip<T>(Vector<T> left, Vector<T> right) => (UnzipEven(left, right), UnzipOdd(left, right)); /// <summary>Creates a new vector with the elements of a specified vector in reverse order.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="vector">The vector whose elements will be reversed.</param> /// <returns>A new vector containing the elements of <paramref name="vector" /> in reverse order.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> Reverse<T>(Vector<T> vector) { if (Vector<T>.Count == Vector512<T>.Count) { return Vector512.Reverse(vector.AsVector512()).AsVector(); } else if (Vector<T>.Count == Vector256<T>.Count) { return Vector256.Reverse(vector.AsVector256()).AsVector(); } else { Debug.Assert(Vector<T>.Count == Vector128<T>.Count); return Vector128.Reverse(vector.AsVector128()).AsVector(); } } internal static Vector<T> DegreesToRadians<T>(Vector<T> degrees) where T : ITrigonometricFunctions<T> { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = T.DegreesToRadians(degrees.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } /// <inheritdoc cref="Vector128.DegreesToRadians(Vector128{double})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> DegreesToRadians(Vector<double> degrees) { if (IsHardwareAccelerated) { return VectorMath.DegreesToRadians<Vector<double>, double>(degrees); } else { return DegreesToRadians<double>(degrees); } } /// <inheritdoc cref="Vector128.DegreesToRadians(Vector128{float})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<float> DegreesToRadians(Vector<float> degrees) { if (IsHardwareAccelerated) { return VectorMath.DegreesToRadians<Vector<float>, float>(degrees); } else { return DegreesToRadians<float>(degrees); } } /// <summary>Divides two vectors to compute their quotient.</summary> /// <param name="left">The vector that will be divided by <paramref name="right" />.</param> /// <param name="right">The vector that will divide <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>The quotient of <paramref name="left" /> divided by <paramref name="right" />.</returns> [Intrinsic] public static Vector<T> Divide<T>(Vector<T> left, Vector<T> right) => left / right; /// <summary>Divides a vector by a scalar to compute the per-element quotient.</summary> /// <param name="left">The vector that will be divided by <paramref name="right" />.</param> /// <param name="right">The scalar that will divide <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>The quotient of <paramref name="left" /> divided by <paramref name="right" />.</returns> [Intrinsic] public static Vector<T> Divide<T>(Vector<T> left, T right) => left / right; /// <summary>Computes the dot product of two vectors.</summary> /// <param name="left">The vector that will be dotted with <paramref name="right" />.</param> /// <param name="right">The vector that will be dotted with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>The dot product of <paramref name="left" /> and <paramref name="right" />.</returns> [Intrinsic] public static T Dot<T>(Vector<T> left, Vector<T> right) => Sum(left * right); /// <summary>Compares two vectors to determine if they are equal on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="right" />.</param> /// <param name="right">The vector to compare with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>A vector whose elements are all-bits-set or zero, depending on if the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were equal.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> Equals<T>(Vector<T> left, Vector<T> right) { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.Equals(left.GetElementUnsafe(index), right.GetElementUnsafe(index)) ? Scalar<T>.AllBitsSet : default!; result.SetElementUnsafe(index, value); } return result; } /// <summary>Compares two vectors to determine if they are equal on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="right" />.</param> /// <param name="right">The vector to compare with <paramref name="left" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were equal.</returns> [Intrinsic] public static Vector<long> Equals(Vector<double> left, Vector<double> right) => Equals<double>(left, right).As<double, long>(); /// <summary>Compares two vectors to determine if they are equal on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="right" />.</param> /// <param name="right">The vector to compare with <paramref name="left" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were equal.</returns> [Intrinsic] public static Vector<int> Equals(Vector<int> left, Vector<int> right) => Equals<int>(left, right); /// <summary>Compares two vectors to determine if they are equal on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="right" />.</param> /// <param name="right">The vector to compare with <paramref name="left" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were equal.</returns> [Intrinsic] public static Vector<long> Equals(Vector<long> left, Vector<long> right) => Equals<long>(left, right); /// <summary>Compares two vectors to determine if they are equal on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="right" />.</param> /// <param name="right">The vector to compare with <paramref name="left" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were equal.</returns> [Intrinsic] public static Vector<int> Equals(Vector<float> left, Vector<float> right) => Equals<float>(left, right).As<float, int>(); /// <summary>Compares two vectors to determine if all elements are equal.</summary> /// <param name="left">The vector to compare with <paramref name="right" />.</param> /// <param name="right">The vector to compare with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns><c>true</c> if all elements in <paramref name="left" /> were equal to the corresponding element in <paramref name="right" />.</returns> [Intrinsic] public static bool EqualsAll<T>(Vector<T> left, Vector<T> right) => left == right; /// <summary>Compares two vectors to determine if any elements are equal.</summary> /// <param name="left">The vector to compare with <paramref name="right" />.</param> /// <param name="right">The vector to compare with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns><c>true</c> if any elements in <paramref name="left" /> was equal to the corresponding element in <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool EqualsAny<T>(Vector<T> left, Vector<T> right) { for (int index = 0; index < Vector<T>.Count; index++) { if (Scalar<T>.Equals(left.GetElementUnsafe(index), right.GetElementUnsafe(index))) { return true; } } return false; } internal static Vector<T> Exp<T>(Vector<T> vector) where T : IExponentialFunctions<T> { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = T.Exp(vector.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } /// <inheritdoc cref="Vector128.Exp(Vector128{double})" /> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> Exp(Vector<double> vector) { if (IsHardwareAccelerated) { return VectorMath.ExpDouble<Vector<double>, Vector<ulong>>(vector); } else { return Exp<double>(vector); } } /// <inheritdoc cref="Vector128.Exp(Vector128{float})" /> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<float> Exp(Vector<float> vector) { if (IsHardwareAccelerated) { return VectorMath.ExpSingle<Vector<float>, Vector<uint>, Vector<double>, Vector<ulong>>(vector); } else { return Exp<float>(vector); } } /// <summary>Computes the floor of each element in a vector.</summary> /// <param name="vector">The vector that will have its floor computed.</param> /// <returns>A vector whose elements are the floor of the elements in <paramref name="vector" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static Vector<T> Floor<T>(Vector<T> vector) { if (Scalar<T>.IsFloatingPoint) { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.Floor(vector.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } else { return vector; } } /// <summary>Computes the floor of each element in a vector.</summary> /// <param name="value">The vector that will have its floor computed.</param> /// <returns>A vector whose elements are the floor of the elements in <paramref name="value" />.</returns> /// <seealso cref="double.Floor(double)" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> Floor(Vector<double> value) { Unsafe.SkipInit(out Vector<double> result); for (int index = 0; index < Vector<double>.Count; index++) { double element = Scalar<double>.Floor(value.GetElementUnsafe(index)); result.SetElementUnsafe(index, element); } return result; } /// <summary>Computes the floor of each element in a vector.</summary> /// <param name="value">The vector that will have its floor computed.</param> /// <returns>A vector whose elements are the floor of the elements in <paramref name="value" />.</returns> /// <seealso cref="float.Floor(float)" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<float> Floor(Vector<float> value) { Unsafe.SkipInit(out Vector<float> result); for (int index = 0; index < Vector<float>.Count; index++) { float element = Scalar<float>.Floor(value.GetElementUnsafe(index)); result.SetElementUnsafe(index, element); } return result; } /// <summary>Computes (<paramref name="left" /> * <paramref name="right" />) + <paramref name="addend" />, rounded as one ternary operation.</summary> /// <param name="left">The vector to be multiplied with <paramref name="right" />.</param> /// <param name="right">The vector to be multiplied with <paramref name="left" />.</param> /// <param name="addend">The vector to be added to the result of <paramref name="left" /> multiplied by <paramref name="right" />.</param> /// <returns>(<paramref name="left" /> * <paramref name="right" />) + <paramref name="addend" />, rounded as one ternary operation.</returns> /// <remarks> /// <para>This computes (<paramref name="left" /> * <paramref name="right" />) as if to infinite precision, adds <paramref name="addend" /> to that result as if to infinite precision, and finally rounds to the nearest representable value.</para> /// <para>This differs from the non-fused sequence which would compute (<paramref name="left" /> * <paramref name="right" />) as if to infinite precision, round the result to the nearest representable value, add <paramref name="addend" /> to the rounded result as if to infinite precision, and finally round to the nearest representable value.</para> /// </remarks> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> FusedMultiplyAdd(Vector<double> left, Vector<double> right, Vector<double> addend) { Unsafe.SkipInit(out Vector<double> result); for (int index = 0; index < Vector<double>.Count; index++) { double value = double.FusedMultiplyAdd(left.GetElementUnsafe(index), right.GetElementUnsafe(index), addend.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } /// <summary>Computes (<paramref name="left" /> * <paramref name="right" />) + <paramref name="addend" />, rounded as one ternary operation.</summary> /// <param name="left">The vector to be multiplied with <paramref name="right" />.</param> /// <param name="right">The vector to be multiplied with <paramref name="left" />.</param> /// <param name="addend">The vector to be added to the result of <paramref name="left" /> multiplied by <paramref name="right" />.</param> /// <returns>(<paramref name="left" /> * <paramref name="right" />) + <paramref name="addend" />, rounded as one ternary operation.</returns> /// <remarks> /// <para>This computes (<paramref name="left" /> * <paramref name="right" />) as if to infinite precision, adds <paramref name="addend" /> to that result as if to infinite precision, and finally rounds to the nearest representable value.</para> /// <para>This differs from the non-fused sequence which would compute (<paramref name="left" /> * <paramref name="right" />) as if to infinite precision, round the result to the nearest representable value, add <paramref name="addend" /> to the rounded result as if to infinite precision, and finally round to the nearest representable value.</para> /// </remarks> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<float> FusedMultiplyAdd(Vector<float> left, Vector<float> right, Vector<float> addend) { Unsafe.SkipInit(out Vector<float> result); for (int index = 0; index < Vector<float>.Count; index++) { float value = float.FusedMultiplyAdd(left.GetElementUnsafe(index), right.GetElementUnsafe(index), addend.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } /// <summary>Gets the element at the specified index.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="vector">The vector to get the element from.</param> /// <param name="index">The index of the element to get.</param> /// <returns>The value of the element at <paramref name="index" />.</returns> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index" /> was less than zero or greater than the number of elements.</exception> /// <exception cref="NotSupportedException">The type of <paramref name="vector" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T GetElement<T>(this Vector<T> vector, int index) { if ((uint)(index) >= (uint)(Vector<T>.Count)) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } return vector.GetElementUnsafe(index); } /// <summary>Compares two vectors to determine which is greater on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were greater.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> GreaterThan<T>(Vector<T> left, Vector<T> right) { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.GreaterThan(left.GetElementUnsafe(index), right.GetElementUnsafe(index)) ? Scalar<T>.AllBitsSet : default!; result.SetElementUnsafe(index, value); } return result; } /// <summary>Compares two vectors to determine which is greater on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were greater.</returns> [Intrinsic] public static Vector<long> GreaterThan(Vector<double> left, Vector<double> right) => GreaterThan<double>(left, right).As<double, long>(); /// <summary>Compares two vectors to determine which is greater on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were greater.</returns> [Intrinsic] public static Vector<int> GreaterThan(Vector<int> left, Vector<int> right) => GreaterThan<int>(left, right); /// <summary>Compares two vectors to determine which is greater on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were greater.</returns> [Intrinsic] public static Vector<long> GreaterThan(Vector<long> left, Vector<long> right) => GreaterThan<long>(left, right); /// <summary>Compares two vectors to determine which is greater on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were greater.</returns> [Intrinsic] public static Vector<int> GreaterThan(Vector<float> left, Vector<float> right) => GreaterThan<float>(left, right).As<float, int>(); /// <summary>Compares two vectors to determine if all elements are greater.</summary> /// <param name="left">The vector to compare with <paramref name="right" />.</param> /// <param name="right">The vector to compare with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns><c>true</c> if all elements in <paramref name="left" /> were greater than the corresponding element in <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool GreaterThanAll<T>(Vector<T> left, Vector<T> right) { for (int index = 0; index < Vector<T>.Count; index++) { if (!Scalar<T>.GreaterThan(left.GetElementUnsafe(index), right.GetElementUnsafe(index))) { return false; } } return true; } /// <summary>Compares two vectors to determine if any elements are greater.</summary> /// <param name="left">The vector to compare with <paramref name="right" />.</param> /// <param name="right">The vector to compare with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns><c>true</c> if any elements in <paramref name="left" /> was greater than the corresponding element in <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool GreaterThanAny<T>(Vector<T> left, Vector<T> right) { for (int index = 0; index < Vector<T>.Count; index++) { if (Scalar<T>.GreaterThan(left.GetElementUnsafe(index), right.GetElementUnsafe(index))) { return true; } } return false; } /// <summary>Compares two vectors to determine which is greater or equal on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were greater or equal.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> GreaterThanOrEqual<T>(Vector<T> left, Vector<T> right) { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.GreaterThanOrEqual(left.GetElementUnsafe(index), right.GetElementUnsafe(index)) ? Scalar<T>.AllBitsSet : default!; result.SetElementUnsafe(index, value); } return result; } /// <summary>Compares two vectors to determine which is greater or equal on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were greater or equal.</returns> [Intrinsic] public static Vector<long> GreaterThanOrEqual(Vector<double> left, Vector<double> right) => GreaterThanOrEqual<double>(left, right).As<double, long>(); /// <summary>Compares two vectors to determine which is greater or equal on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were greater or equal.</returns> [Intrinsic] public static Vector<int> GreaterThanOrEqual(Vector<int> left, Vector<int> right) => GreaterThanOrEqual<int>(left, right); /// <summary>Compares two vectors to determine which is greater or equal on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were greater or equal.</returns> [Intrinsic] public static Vector<long> GreaterThanOrEqual(Vector<long> left, Vector<long> right) => GreaterThanOrEqual<long>(left, right); /// <summary>Compares two vectors to determine which is greater or equal on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were greater or equal.</returns> [Intrinsic] public static Vector<int> GreaterThanOrEqual(Vector<float> left, Vector<float> right) => GreaterThanOrEqual<float>(left, right).As<float, int>(); /// <summary>Compares two vectors to determine if all elements are greater or equal.</summary> /// <param name="left">The vector to compare with <paramref name="right" />.</param> /// <param name="right">The vector to compare with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns><c>true</c> if all elements in <paramref name="left" /> were greater than or equal to the corresponding element in <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool GreaterThanOrEqualAll<T>(Vector<T> left, Vector<T> right) { for (int index = 0; index < Vector<T>.Count; index++) { if (!Scalar<T>.GreaterThanOrEqual(left.GetElementUnsafe(index), right.GetElementUnsafe(index))) { return false; } } return true; } /// <summary>Compares two vectors to determine if any elements are greater or equal.</summary> /// <param name="left">The vector to compare with <paramref name="right" />.</param> /// <param name="right">The vector to compare with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns><c>true</c> if any elements in <paramref name="left" /> was greater than or equal to the corresponding element in <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool GreaterThanOrEqualAny<T>(Vector<T> left, Vector<T> right) { for (int index = 0; index < Vector<T>.Count; index++) { if (Scalar<T>.GreaterThanOrEqual(left.GetElementUnsafe(index), right.GetElementUnsafe(index))) { return true; } } return false; } internal static Vector<T> Hypot<T>(Vector<T> x, Vector<T> y) where T : IRootFunctions<T> { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = T.Hypot(x.GetElementUnsafe(index), y.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } /// <inheritdoc cref="Vector128.Hypot(Vector128{double}, Vector128{double})" /> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> Hypot(Vector<double> x, Vector<double> y) { if (IsHardwareAccelerated) { return VectorMath.HypotDouble<Vector<double>, Vector<ulong>>(x, y); } else { return Hypot<double>(x, y); } } /// <inheritdoc cref="Vector128.Hypot(Vector128{float}, Vector128{float})" /> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<float> Hypot(Vector<float> x, Vector<float> y) { if (IsHardwareAccelerated) { return VectorMath.HypotSingle<Vector<float>, Vector<double>>(x, y); } else { return Hypot<float>(x, y); } } /// <inheritdoc cref="Vector128.IndexOf{T}(Vector128{T}, T)" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int IndexOf<T>(Vector<T> vector, T value) { if (Vector<T>.Count == Vector512<T>.Count) { return Vector512.IndexOf(vector.AsVector512(), value); } else if (Vector<T>.Count == Vector256<T>.Count) { return Vector256.IndexOf(vector.AsVector256(), value); } else { Debug.Assert(Vector<T>.Count == Vector128<T>.Count); return Vector128.IndexOf(vector.AsVector128(), value); } } /// <inheritdoc cref="Vector128.IndexOfWhereAllBitsSet{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int IndexOfWhereAllBitsSet<T>(Vector<T> vector) { if (typeof(T) == typeof(float)) { return IndexOf(vector.As<T, int>(), -1); } else if (typeof(T) == typeof(double)) { return IndexOf(vector.As<T, long>(), -1); } else { return IndexOf(vector, Scalar<T>.AllBitsSet); } } /// <inheritdoc cref="Vector128.IsEvenInteger{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> IsEvenInteger<T>(Vector<T> vector) { if (typeof(T) == typeof(float)) { return VectorMath.IsEvenIntegerSingle<Vector<float>, Vector<uint>>(vector.As<T, float>()).As<float, T>(); } else if (typeof(T) == typeof(double)) { return VectorMath.IsEvenIntegerDouble<Vector<double>, Vector<ulong>>(vector.As<T, double>()).As<double, T>(); } return IsZero(vector & Vector<T>.One); } /// <inheritdoc cref="Vector128.IsFinite{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> IsFinite<T>(Vector<T> vector) { if (typeof(T) == typeof(float)) { return ~IsZero(AndNot(Vector<float>.PositiveInfinity.As<float, uint>(), vector.As<T, uint>())).As<uint, T>(); } else if (typeof(T) == typeof(double)) { return ~IsZero(AndNot(Vector<double>.PositiveInfinity.As<double, ulong>(), vector.As<T, ulong>())).As<ulong, T>(); } return Vector<T>.AllBitsSet; } /// <inheritdoc cref="Vector128.IsInfinity{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> IsInfinity<T>(Vector<T> vector) { if ((typeof(T) == typeof(float)) || (typeof(T) == typeof(double))) { return IsPositiveInfinity(Abs(vector)); } return Vector<T>.Zero; } /// <inheritdoc cref="Vector128.IsInteger{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> IsInteger<T>(Vector<T> vector) { if ((typeof(T) == typeof(float)) || (typeof(T) == typeof(double))) { return IsFinite(vector) & Equals(vector, Truncate(vector)); } return Vector<T>.AllBitsSet; } /// <inheritdoc cref="Vector128.IsNaN{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> IsNaN<T>(Vector<T> vector) { if ((typeof(T) == typeof(float)) || (typeof(T) == typeof(double))) { return ~Equals(vector, vector); } return Vector<T>.Zero; } /// <inheritdoc cref="Vector128.IsNegative{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> IsNegative<T>(Vector<T> vector) { if (Scalar<T>.IsUnsigned) { return Vector<T>.Zero; } else if (typeof(T) == typeof(float)) { return LessThan(vector.As<T, int>(), Vector<int>.Zero).As<int, T>(); } else if (typeof(T) == typeof(double)) { return LessThan(vector.As<T, long>(), Vector<long>.Zero).As<long, T>(); } else { return LessThan(vector, Vector<T>.Zero); } } /// <inheritdoc cref="Vector128.IsNegativeInfinity{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> IsNegativeInfinity<T>(Vector<T> vector) { if (typeof(T) == typeof(float)) { return Equals(vector, Vector<float>.NegativeInfinity.As<float, T>()); } else if (typeof(T) == typeof(double)) { return Equals(vector, Vector<double>.NegativeInfinity.As<double, T>()); } return Vector<T>.Zero; } /// <inheritdoc cref="Vector128.IsNormal{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> IsNormal<T>(Vector<T> vector) { if (typeof(T) == typeof(float)) { return LessThan(Abs(vector).As<T, uint>() - Create<uint>(float.SmallestNormalBits), Create<uint>(float.PositiveInfinityBits - float.SmallestNormalBits)).As<uint, T>(); } else if (typeof(T) == typeof(double)) { return LessThan(Abs(vector).As<T, ulong>() - Create<ulong>(double.SmallestNormalBits), Create<ulong>(double.PositiveInfinityBits - double.SmallestNormalBits)).As<ulong, T>(); } return ~IsZero(vector); } /// <inheritdoc cref="Vector128.IsOddInteger{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> IsOddInteger<T>(Vector<T> vector) { if (typeof(T) == typeof(float)) { return VectorMath.IsOddIntegerSingle<Vector<float>, Vector<uint>>(vector.As<T, float>()).As<float, T>(); } else if (typeof(T) == typeof(double)) { return VectorMath.IsOddIntegerDouble<Vector<double>, Vector<ulong>>(vector.As<T, double>()).As<double, T>(); } return ~IsZero(vector & Vector<T>.One); } /// <inheritdoc cref="Vector128.IsPositive{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> IsPositive<T>(Vector<T> vector) { if (Scalar<T>.IsUnsigned) { return Vector<T>.AllBitsSet; } else if (typeof(T) == typeof(float)) { return GreaterThanOrEqual(vector.As<T, int>(), Vector<int>.Zero).As<int, T>(); } else if (typeof(T) == typeof(double)) { return GreaterThanOrEqual(vector.As<T, long>(), Vector<long>.Zero).As<long, T>(); } else { return GreaterThanOrEqual(vector, Vector<T>.Zero); } } /// <inheritdoc cref="Vector128.IsPositiveInfinity{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> IsPositiveInfinity<T>(Vector<T> vector) { if (typeof(T) == typeof(float)) { return Equals(vector, Vector<float>.PositiveInfinity.As<float, T>()); } else if (typeof(T) == typeof(double)) { return Equals(vector, Vector<double>.PositiveInfinity.As<double, T>()); } return Vector<T>.Zero; } /// <inheritdoc cref="Vector128.IsSubnormal{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> IsSubnormal<T>(Vector<T> vector) { if (typeof(T) == typeof(float)) { return LessThan(Abs(vector).As<T, uint>() - Vector<uint>.One, Create<uint>(float.MaxTrailingSignificand)).As<uint, T>(); } else if (typeof(T) == typeof(double)) { return LessThan(Abs(vector).As<T, ulong>() - Vector<ulong>.One, Create<ulong>(double.MaxTrailingSignificand)).As<ulong, T>(); } return Vector<T>.Zero; } /// <inheritdoc cref="Vector128.IsZero{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> IsZero<T>(Vector<T> vector) => Equals(vector, Vector<T>.Zero); /// <inheritdoc cref="Vector128.LastIndexOf{T}(Vector128{T}, T)" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LastIndexOf<T>(Vector<T> vector, T value) { if (Vector<T>.Count == Vector512<T>.Count) { return Vector512.LastIndexOf(vector.AsVector512(), value); } else if (Vector<T>.Count == Vector256<T>.Count) { return Vector256.LastIndexOf(vector.AsVector256(), value); } else { Debug.Assert(Vector<T>.Count == Vector128<T>.Count); return Vector128.LastIndexOf(vector.AsVector128(), value); } } /// <inheritdoc cref="Vector128.LastIndexOfWhereAllBitsSet{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LastIndexOfWhereAllBitsSet<T>(Vector<T> vector) { if (typeof(T) == typeof(float)) { return LastIndexOf(vector.As<T, int>(), -1); } else if (typeof(T) == typeof(double)) { return LastIndexOf(vector.As<T, long>(), -1); } else { return LastIndexOf(vector, Scalar<T>.AllBitsSet); } } internal static Vector<T> Lerp<T>(Vector<T> x, Vector<T> y, Vector<T> amount) where T : IFloatingPointIeee754<T> { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = T.Lerp(x.GetElementUnsafe(index), y.GetElementUnsafe(index), amount.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } /// <summary>Performs a linear interpolation between two vectors based on the given weighting.</summary> /// <param name="x">The first vector.</param> /// <param name="y">The second vector.</param> /// <param name="amount">A value between 0 and 1 that indicates the weight of <paramref name="y" />.</param> /// <returns>The interpolated vector.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> Lerp(Vector<double> x, Vector<double> y, Vector<double> amount) { if (IsHardwareAccelerated) { return VectorMath.Lerp<Vector<double>, double>(x, y, amount); } else { return Lerp<double>(x, y, amount); } } /// <summary>Performs a linear interpolation between two vectors based on the given weighting.</summary> /// <param name="x">The first vector.</param> /// <param name="y">The second vector.</param> /// <param name="amount">A value between 0 and 1 that indicates the weight of <paramref name="y" />.</param> /// <returns>The interpolated vector.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<float> Lerp(Vector<float> x, Vector<float> y, Vector<float> amount) { if (IsHardwareAccelerated) { return VectorMath.Lerp<Vector<float>, float>(x, y, amount); } else { return Lerp<float>(x, y, amount); } } /// <summary>Compares two vectors to determine which is less on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were less.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> LessThan<T>(Vector<T> left, Vector<T> right) { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.LessThan(left.GetElementUnsafe(index), right.GetElementUnsafe(index)) ? Scalar<T>.AllBitsSet : default!; result.SetElementUnsafe(index, value); } return result; } /// <summary>Compares two vectors to determine which is less on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were less.</returns> [Intrinsic] public static Vector<long> LessThan(Vector<double> left, Vector<double> right) => LessThan<double>(left, right).As<double, long>(); /// <summary>Compares two vectors to determine which is less on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were less.</returns> [Intrinsic] public static Vector<int> LessThan(Vector<int> left, Vector<int> right) => LessThan<int>(left, right); /// <summary>Compares two vectors to determine which is less on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were less.</returns> [Intrinsic] public static Vector<long> LessThan(Vector<long> left, Vector<long> right) => LessThan<long>(left, right); /// <summary>Compares two vectors to determine which is less on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were less.</returns> [Intrinsic] public static Vector<int> LessThan(Vector<float> left, Vector<float> right) => LessThan<float>(left, right).As<float, int>(); /// <summary>Compares two vectors to determine if all elements are less.</summary> /// <param name="left">The vector to compare with <paramref name="right" />.</param> /// <param name="right">The vector to compare with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns><c>true</c> if all elements in <paramref name="left" /> were less than the corresponding element in <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool LessThanAll<T>(Vector<T> left, Vector<T> right) { for (int index = 0; index < Vector<T>.Count; index++) { if (!Scalar<T>.LessThan(left.GetElementUnsafe(index), right.GetElementUnsafe(index))) { return false; } } return true; } /// <summary>Compares two vectors to determine if any elements are less.</summary> /// <param name="left">The vector to compare with <paramref name="right" />.</param> /// <param name="right">The vector to compare with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns><c>true</c> if any elements in <paramref name="left" /> was less than the corresponding element in <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool LessThanAny<T>(Vector<T> left, Vector<T> right) { for (int index = 0; index < Vector<T>.Count; index++) { if (Scalar<T>.LessThan(left.GetElementUnsafe(index), right.GetElementUnsafe(index))) { return true; } } return false; } /// <summary>Compares two vectors to determine which is less or equal on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were less or equal.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> LessThanOrEqual<T>(Vector<T> left, Vector<T> right) { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.LessThanOrEqual(left.GetElementUnsafe(index), right.GetElementUnsafe(index)) ? Scalar<T>.AllBitsSet : default!; result.SetElementUnsafe(index, value); } return result; } /// <summary>Compares two vectors to determine which is less or equal on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were less or equal.</returns> [Intrinsic] public static Vector<long> LessThanOrEqual(Vector<double> left, Vector<double> right) => LessThanOrEqual<double>(left, right).As<double, long>(); /// <summary>Compares two vectors to determine which is less or equal on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were less or equal.</returns> [Intrinsic] public static Vector<int> LessThanOrEqual(Vector<int> left, Vector<int> right) => LessThanOrEqual<int>(left, right); /// <summary>Compares two vectors to determine which is less or equal on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were less or equal.</returns> [Intrinsic] public static Vector<long> LessThanOrEqual(Vector<long> left, Vector<long> right) => LessThanOrEqual<long>(left, right); /// <summary>Compares two vectors to determine which is less or equal on a per-element basis.</summary> /// <param name="left">The vector to compare with <paramref name="left" />.</param> /// <param name="right">The vector to compare with <paramref name="right" />.</param> /// <returns>A vector whose elements are all-bits-set or zero, depending on if which of the corresponding elements in <paramref name="left" /> and <paramref name="right" /> were less or equal.</returns> [Intrinsic] public static Vector<int> LessThanOrEqual(Vector<float> left, Vector<float> right) => LessThanOrEqual<float>(left, right).As<float, int>(); /// <summary>Compares two vectors to determine if all elements are less or equal.</summary> /// <param name="left">The vector to compare with <paramref name="right" />.</param> /// <param name="right">The vector to compare with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns><c>true</c> if all elements in <paramref name="left" /> were less than or equal to the corresponding element in <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool LessThanOrEqualAll<T>(Vector<T> left, Vector<T> right) { for (int index = 0; index < Vector<T>.Count; index++) { if (!Scalar<T>.LessThanOrEqual(left.GetElementUnsafe(index), right.GetElementUnsafe(index))) { return false; } } return true; } /// <summary>Compares two vectors to determine if any elements are less or equal.</summary> /// <param name="left">The vector to compare with <paramref name="right" />.</param> /// <param name="right">The vector to compare with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns><c>true</c> if any elements in <paramref name="left" /> was less than or equal to the corresponding element in <paramref name="right" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool LessThanOrEqualAny<T>(Vector<T> left, Vector<T> right) { for (int index = 0; index < Vector<T>.Count; index++) { if (Scalar<T>.LessThanOrEqual(left.GetElementUnsafe(index), right.GetElementUnsafe(index))) { return true; } } return false; } /// <summary>Loads a vector from the given source.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="source">The source from which the vector will be loaded.</param> /// <returns>The vector loaded from <paramref name="source" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="source" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [CLSCompliant(false)] public static unsafe Vector<T> Load<T>(T* source) => LoadUnsafe(ref *source); /// <summary>Loads a vector from the given aligned source.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="source">The aligned source from which the vector will be loaded.</param> /// <returns>The vector loaded from <paramref name="source" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="source" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe Vector<T> LoadAligned<T>(T* source) { ThrowHelper.ThrowForUnsupportedNumericsVectorBaseType<T>(); if (((nuint)(source) % (uint)(Alignment)) != 0) { ThrowHelper.ThrowAccessViolationException(); } return *(Vector<T>*)source; } /// <summary>Loads a vector from the given aligned source.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="source">The aligned source from which the vector will be loaded.</param> /// <returns>The vector loaded from <paramref name="source" />.</returns> /// <remarks>This method may bypass the cache on certain platforms.</remarks> /// <exception cref="NotSupportedException">The type of <paramref name="source" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [CLSCompliant(false)] public static unsafe Vector<T> LoadAlignedNonTemporal<T>(T* source) => LoadAligned(source); /// <summary>Loads a vector from the given source.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="source">The source from which the vector will be loaded.</param> /// <returns>The vector loaded from <paramref name="source" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="source" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> LoadUnsafe<T>(ref readonly T source) { ThrowHelper.ThrowForUnsupportedNumericsVectorBaseType<T>(); ref readonly byte address = ref Unsafe.As<T, byte>(ref Unsafe.AsRef(in source)); return Unsafe.ReadUnaligned<Vector<T>>(in address); } /// <summary>Loads a vector from the given source and element offset.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="source">The source to which <paramref name="elementOffset" /> will be added before loading the vector.</param> /// <param name="elementOffset">The element offset from <paramref name="source" /> from which the vector will be loaded.</param> /// <returns>The vector loaded from <paramref name="source" /> plus <paramref name="elementOffset" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="source" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> LoadUnsafe<T>(ref readonly T source, nuint elementOffset) { ThrowHelper.ThrowForUnsupportedNumericsVectorBaseType<T>(); ref readonly byte address = ref Unsafe.As<T, byte>(ref Unsafe.Add(ref Unsafe.AsRef(in source), (nint)elementOffset)); return Unsafe.ReadUnaligned<Vector<T>>(in address); } internal static Vector<T> Log<T>(Vector<T> vector) where T : ILogarithmicFunctions<T> { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = T.Log(vector.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } /// <inheritdoc cref="Vector128.Log(Vector128{double})" /> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> Log(Vector<double> vector) { if (IsHardwareAccelerated) { return VectorMath.LogDouble<Vector<double>, Vector<long>, Vector<ulong>>(vector); } else { return Log<double>(vector); } } /// <inheritdoc cref="Vector128.Log(Vector128{float})" /> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<float> Log(Vector<float> vector) { if (IsHardwareAccelerated) { return VectorMath.LogSingle<Vector<float>, Vector<int>, Vector<uint>>(vector); } else { return Log<float>(vector); } } internal static Vector<T> Log2<T>(Vector<T> vector) where T : ILogarithmicFunctions<T> { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = T.Log2(vector.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } /// <inheritdoc cref="Vector128.Log2(Vector128{double})" /> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> Log2(Vector<double> vector) { if (IsHardwareAccelerated) { return VectorMath.Log2Double<Vector<double>, Vector<long>, Vector<ulong>>(vector); } else { return Log2<double>(vector); } } /// <inheritdoc cref="Vector128.Log2(Vector128{float})" /> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<float> Log2(Vector<float> vector) { if (IsHardwareAccelerated) { return VectorMath.Log2Single<Vector<float>, Vector<int>, Vector<uint>>(vector); } else { return Log2<float>(vector); } } /// <inheritdoc cref="Vector128.Max{T}(Vector128{T}, Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> Max<T>(Vector<T> left, Vector<T> right) { if (IsHardwareAccelerated) { return VectorMath.Max<Vector<T>, T>(left, right); } else { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.Max(left.GetElementUnsafe(index), right.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } } /// <inheritdoc cref="Vector128.MaxMagnitude{T}(Vector128{T}, Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> MaxMagnitude<T>(Vector<T> left, Vector<T> right) { if (IsHardwareAccelerated) { return VectorMath.MaxMagnitude<Vector<T>, T>(left, right); } else { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.MaxMagnitude(left.GetElementUnsafe(index), right.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } } /// <inheritdoc cref="Vector128.MaxMagnitudeNumber{T}(Vector128{T}, Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> MaxMagnitudeNumber<T>(Vector<T> left, Vector<T> right) { if (IsHardwareAccelerated) { return VectorMath.MaxMagnitudeNumber<Vector<T>, T>(left, right); } else { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.MaxMagnitudeNumber(left.GetElementUnsafe(index), right.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } } /// <inheritdoc cref="Vector128.MaxNative{T}(Vector128{T}, Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> MaxNative<T>(Vector<T> left, Vector<T> right) { if (IsHardwareAccelerated) { return ConditionalSelect(GreaterThan(left, right), left, right); } else { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.GreaterThan(left.GetElementUnsafe(index), right.GetElementUnsafe(index)) ? left.GetElementUnsafe(index) : right.GetElementUnsafe(index); result.SetElementUnsafe(index, value); } return result; } } /// <inheritdoc cref="Vector128.MaxNumber{T}(Vector128{T}, Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> MaxNumber<T>(Vector<T> left, Vector<T> right) { if (IsHardwareAccelerated) { return VectorMath.MaxNumber<Vector<T>, T>(left, right); } else { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.MaxNumber(left.GetElementUnsafe(index), right.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } } /// <inheritdoc cref="Vector128.Min{T}(Vector128{T}, Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> Min<T>(Vector<T> left, Vector<T> right) { if (IsHardwareAccelerated) { return VectorMath.Min<Vector<T>, T>(left, right); } else { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.Min(left.GetElementUnsafe(index), right.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } } /// <inheritdoc cref="Vector128.MinMagnitude{T}(Vector128{T}, Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> MinMagnitude<T>(Vector<T> left, Vector<T> right) { if (IsHardwareAccelerated) { return VectorMath.MinMagnitude<Vector<T>, T>(left, right); } else { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.MinMagnitude(left.GetElementUnsafe(index), right.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } } /// <inheritdoc cref="Vector128.MinMagnitudeNumber{T}(Vector128{T}, Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> MinMagnitudeNumber<T>(Vector<T> left, Vector<T> right) { if (IsHardwareAccelerated) { return VectorMath.MinMagnitudeNumber<Vector<T>, T>(left, right); } else { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.MinMagnitudeNumber(left.GetElementUnsafe(index), right.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } } /// <inheritdoc cref="Vector128.MinNative{T}(Vector128{T}, Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> MinNative<T>(Vector<T> left, Vector<T> right) { if (IsHardwareAccelerated) { return ConditionalSelect(LessThan(left, right), left, right); } else { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.LessThan(left.GetElementUnsafe(index), right.GetElementUnsafe(index)) ? left.GetElementUnsafe(index) : right.GetElementUnsafe(index); result.SetElementUnsafe(index, value); } return result; } } /// <inheritdoc cref="Vector128.MinNumber{T}(Vector128{T}, Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> MinNumber<T>(Vector<T> left, Vector<T> right) { if (IsHardwareAccelerated) { return VectorMath.MinNumber<Vector<T>, T>(left, right); } else { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.MinNumber(left.GetElementUnsafe(index), right.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } } /// <summary>Multiplies two vectors to compute their element-wise product.</summary> /// <param name="left">The vector to multiply with <paramref name="right" />.</param> /// <param name="right">The vector to multiply with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>The element-wise product of <paramref name="left" /> and <paramref name="right" />.</returns> [Intrinsic] public static Vector<T> Multiply<T>(Vector<T> left, Vector<T> right) => left * right; /// <summary>Multiplies a vector by a scalar to compute their product.</summary> /// <param name="left">The vector to multiply with <paramref name="right" />.</param> /// <param name="right">The scalar to multiply with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>The product of <paramref name="left" /> and <paramref name="right" />.</returns> [Intrinsic] public static Vector<T> Multiply<T>(Vector<T> left, T right) => left * right; /// <summary>Multiplies a vector by a scalar to compute their product.</summary> /// <param name="left">The scalar to multiply with <paramref name="right" />.</param> /// <param name="right">The vector to multiply with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>The product of <paramref name="left" /> and <paramref name="right" />.</returns> [Intrinsic] public static Vector<T> Multiply<T>(T left, Vector<T> right) => right * left; [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static Vector<T> MultiplyAddEstimate<T>(Vector<T> left, Vector<T> right, Vector<T> addend) { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.MultiplyAddEstimate(left.GetElementUnsafe(index), right.GetElementUnsafe(index), addend.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } /// <inheritdoc cref="Vector128.MultiplyAddEstimate(Vector128{double}, Vector128{double}, Vector128{double})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> MultiplyAddEstimate(Vector<double> left, Vector<double> right, Vector<double> addend) { Unsafe.SkipInit(out Vector<double> result); for (int index = 0; index < Vector<double>.Count; index++) { double element = double.MultiplyAddEstimate(left.GetElementUnsafe(index), right.GetElementUnsafe(index), addend.GetElementUnsafe(index)); result.SetElementUnsafe(index, element); } return result; } /// <inheritdoc cref="Vector128.MultiplyAddEstimate(Vector128{float}, Vector128{float}, Vector128{float})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<float> MultiplyAddEstimate(Vector<float> left, Vector<float> right, Vector<float> addend) { Unsafe.SkipInit(out Vector<float> result); for (int index = 0; index < Vector<float>.Count; index++) { float element = float.MultiplyAddEstimate(left.GetElementUnsafe(index), right.GetElementUnsafe(index), addend.GetElementUnsafe(index)); result.SetElementUnsafe(index, element); } return result; } [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static Vector<TResult> Narrow<TSource, TResult>(Vector<TSource> low, Vector<TSource> high) where TSource : INumber<TSource> where TResult : INumber<TResult> { Unsafe.SkipInit(out Vector<TResult> result); for (int i = 0; i < Vector<TSource>.Count; i++) { TResult value = TResult.CreateTruncating(low.GetElementUnsafe(i)); result.SetElementUnsafe(i, value); } for (int i = Vector<TSource>.Count; i < Vector<TResult>.Count; i++) { TResult value = TResult.CreateTruncating(high.GetElementUnsafe(i - Vector<TSource>.Count)); result.SetElementUnsafe(i, value); } return result; } /// <inheritdoc cref="Vector128.Narrow(Vector128{double}, Vector128{double})"/> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<float> Narrow(Vector<double> low, Vector<double> high) => Narrow<double, float>(low, high); /// <inheritdoc cref="Vector128.Narrow(Vector128{short}, Vector128{short})"/> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<sbyte> Narrow(Vector<short> low, Vector<short> high) => Narrow<short, sbyte>(low, high); /// <inheritdoc cref="Vector128.Narrow(Vector128{int}, Vector128{int})"/> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<short> Narrow(Vector<int> low, Vector<int> high) => Narrow<int, short>(low, high); /// <inheritdoc cref="Vector128.Narrow(Vector128{long}, Vector128{long})"/> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<int> Narrow(Vector<long> low, Vector<long> high) => Narrow<long, int>(low, high); /// <inheritdoc cref="Vector128.Narrow(Vector128{ushort}, Vector128{ushort})"/> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<byte> Narrow(Vector<ushort> low, Vector<ushort> high) => Narrow<ushort, byte>(low, high); /// <inheritdoc cref="Vector128.Narrow(Vector128{uint}, Vector128{uint})"/> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<ushort> Narrow(Vector<uint> low, Vector<uint> high) => Narrow<uint, ushort>(low, high); /// <inheritdoc cref="Vector128.Narrow(Vector128{ulong}, Vector128{ulong})"/> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<uint> Narrow(Vector<ulong> low, Vector<ulong> high) => Narrow<ulong, uint>(low, high); [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static Vector<TResult> NarrowWithSaturation<TSource, TResult>(Vector<TSource> low, Vector<TSource> high) where TSource : INumber<TSource> where TResult : INumber<TResult> { Unsafe.SkipInit(out Vector<TResult> result); for (int i = 0; i < Vector<TSource>.Count; i++) { TResult value = TResult.CreateSaturating(low.GetElementUnsafe(i)); result.SetElementUnsafe(i, value); } for (int i = Vector<TSource>.Count; i < Vector<TResult>.Count; i++) { TResult value = TResult.CreateSaturating(high.GetElementUnsafe(i - Vector<TSource>.Count)); result.SetElementUnsafe(i, value); } return result; } /// <inheritdoc cref="Vector128.NarrowWithSaturation(Vector128{double}, Vector128{double})"/> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<float> NarrowWithSaturation(Vector<double> low, Vector<double> high) => NarrowWithSaturation<double, float>(low, high); /// <inheritdoc cref="Vector128.NarrowWithSaturation(Vector128{short}, Vector128{short})"/> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<sbyte> NarrowWithSaturation(Vector<short> low, Vector<short> high) => NarrowWithSaturation<short, sbyte>(low, high); /// <inheritdoc cref="Vector128.NarrowWithSaturation(Vector128{int}, Vector128{int})"/> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<short> NarrowWithSaturation(Vector<int> low, Vector<int> high) => NarrowWithSaturation<int, short>(low, high); /// <inheritdoc cref="Vector128.NarrowWithSaturation(Vector128{long}, Vector128{long})"/> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<int> NarrowWithSaturation(Vector<long> low, Vector<long> high) => NarrowWithSaturation<long, int>(low, high); /// <inheritdoc cref="Vector128.NarrowWithSaturation(Vector128{ushort}, Vector128{ushort})"/> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<byte> NarrowWithSaturation(Vector<ushort> low, Vector<ushort> high) => NarrowWithSaturation<ushort, byte>(low, high); /// <inheritdoc cref="Vector128.NarrowWithSaturation(Vector128{uint}, Vector128{uint})"/> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<ushort> NarrowWithSaturation(Vector<uint> low, Vector<uint> high) => NarrowWithSaturation<uint, ushort>(low, high); /// <inheritdoc cref="Vector128.NarrowWithSaturation(Vector128{ulong}, Vector128{ulong})"/> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<uint> NarrowWithSaturation(Vector<ulong> low, Vector<ulong> high) => NarrowWithSaturation<ulong, uint>(low, high); /// <summary>Computes the unary negation of a vector.</summary> /// <param name="value">The vector to negate.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>A vector whose elements are the unary negation of the corresponding elements in <paramref name="value" />.</returns> [Intrinsic] public static Vector<T> Negate<T>(Vector<T> value) => -value; /// <inheritdoc cref="Vector128.None{T}(Vector128{T}, T)" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool None<T>(Vector<T> vector, T value) => !EqualsAny(vector, Create(value)); /// <inheritdoc cref="Vector128.NoneWhereAllBitsSet{T}(Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool NoneWhereAllBitsSet<T>(Vector<T> vector) { if (typeof(T) == typeof(float)) { return None(vector.As<T, int>(), -1); } else if (typeof(T) == typeof(double)) { return None(vector.As<T, long>(), -1); } else { return None(vector, Scalar<T>.AllBitsSet); } } /// <summary>Computes the ones-complement of a vector.</summary> /// <param name="value">The vector whose ones-complement is to be computed.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>A vector whose elements are the ones-complement of the corresponding elements in <paramref name="value" />.</returns> [Intrinsic] public static Vector<T> OnesComplement<T>(Vector<T> value) => ~value; internal static Vector<T> RadiansToDegrees<T>(Vector<T> radians) where T : ITrigonometricFunctions<T> { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = T.RadiansToDegrees(radians.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } /// <inheritdoc cref="Vector128.RadiansToDegrees(Vector128{double})" /> [Intrinsic] public static Vector<double> RadiansToDegrees(Vector<double> radians) { if (IsHardwareAccelerated) { return VectorMath.RadiansToDegrees<Vector<double>, double>(radians); } else { return RadiansToDegrees<double>(radians); } } /// <inheritdoc cref="Vector128.RadiansToDegrees(Vector128{float})" /> [Intrinsic] public static Vector<float> RadiansToDegrees(Vector<float> radians) { if (IsHardwareAccelerated) { return VectorMath.RadiansToDegrees<Vector<float>, float>(radians); } else { return RadiansToDegrees<float>(radians); } } [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static Vector<T> Round<T>(Vector<T> vector) { if (Scalar<T>.IsFloatingPoint) { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.Round(vector.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } else { return vector; } } /// <inheritdoc cref="Vector128.Round(Vector128{double})" /> [Intrinsic] public static Vector<double> Round(Vector<double> vector) => Round<double>(vector); /// <inheritdoc cref="Vector128.Round(Vector128{float})" /> [Intrinsic] public static Vector<float> Round(Vector<float> vector) => Round<float>(vector); /// <inheritdoc cref="Vector128.Round(Vector128{double}, MidpointRounding)" /> [Intrinsic] public static Vector<double> Round(Vector<double> vector, MidpointRounding mode) => VectorMath.RoundDouble(vector, mode); /// <inheritdoc cref="Vector128.Round(Vector128{float}, MidpointRounding)" /> [Intrinsic] public static Vector<float> Round(Vector<float> vector, MidpointRounding mode) => VectorMath.RoundSingle(vector, mode); /// <summary>Shifts each element of a vector left by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted left by <paramref name="shiftCount" />.</returns> [Intrinsic] public static Vector<byte> ShiftLeft(Vector<byte> value, int shiftCount) => value << shiftCount; /// <summary>Shifts each element of a vector left by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted left by <paramref name="shiftCount" />.</returns> [Intrinsic] public static Vector<short> ShiftLeft(Vector<short> value, int shiftCount) => value << shiftCount; /// <summary>Shifts each element of a vector left by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted left by <paramref name="shiftCount" />.</returns> [Intrinsic] public static Vector<int> ShiftLeft(Vector<int> value, int shiftCount) => value << shiftCount; /// <summary>Shifts each element of a vector left by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted left by <paramref name="shiftCount" />.</returns> [Intrinsic] public static Vector<long> ShiftLeft(Vector<long> value, int shiftCount) => value << shiftCount; /// <summary>Shifts each element of a vector left by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted left by <paramref name="shiftCount" />.</returns> [Intrinsic] public static Vector<nint> ShiftLeft(Vector<nint> value, int shiftCount) => value << shiftCount; /// <summary>Shifts each element of a vector left by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted left by <paramref name="shiftCount" />.</returns> [Intrinsic] [CLSCompliant(false)] public static Vector<nuint> ShiftLeft(Vector<nuint> value, int shiftCount) => value << shiftCount; /// <summary>Shifts each element of a vector left by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted left by <paramref name="shiftCount" />.</returns> [Intrinsic] [CLSCompliant(false)] public static Vector<sbyte> ShiftLeft(Vector<sbyte> value, int shiftCount) => value << shiftCount; /// <summary>Shifts each element of a vector left by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted left by <paramref name="shiftCount" />.</returns> [Intrinsic] [CLSCompliant(false)] public static Vector<ushort> ShiftLeft(Vector<ushort> value, int shiftCount) => value << shiftCount; /// <summary>Shifts each element of a vector left by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted left by <paramref name="shiftCount" />.</returns> [Intrinsic] [CLSCompliant(false)] public static Vector<uint> ShiftLeft(Vector<uint> value, int shiftCount) => value << shiftCount; [Intrinsic] internal static Vector<uint> ShiftLeft(Vector<uint> vector, Vector<uint> shiftCount) { if (Vector<uint>.Count == Vector512<uint>.Count) { return Vector512.ShiftLeft(vector.AsVector512(), shiftCount.AsVector512()).AsVector(); } else if (Vector<uint>.Count == Vector256<uint>.Count) { return Vector256.ShiftLeft(vector.AsVector256(), shiftCount.AsVector256()).AsVector(); } else { Debug.Assert(Vector<uint>.Count == Vector128<uint>.Count); return Vector128.ShiftLeft(vector.AsVector128(), shiftCount.AsVector128()).AsVector(); } } /// <summary>Shifts each element of a vector left by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted left by <paramref name="shiftCount" />.</returns> [Intrinsic] [CLSCompliant(false)] public static Vector<ulong> ShiftLeft(Vector<ulong> value, int shiftCount) => value << shiftCount; [Intrinsic] internal static Vector<ulong> ShiftLeft(Vector<ulong> vector, Vector<ulong> shiftCount) { if (Vector<ulong>.Count == Vector512<ulong>.Count) { return Vector512.ShiftLeft(vector.AsVector512(), shiftCount.AsVector512()).AsVector(); } else if (Vector<ulong>.Count == Vector256<ulong>.Count) { return Vector256.ShiftLeft(vector.AsVector256(), shiftCount.AsVector256()).AsVector(); } else { Debug.Assert(Vector<ulong>.Count == Vector128<ulong>.Count); return Vector128.ShiftLeft(vector.AsVector128(), shiftCount.AsVector128()).AsVector(); } } /// <summary>Shifts (signed) each element of a vector right by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted right by <paramref name="shiftCount" />.</returns> [Intrinsic] public static Vector<short> ShiftRightArithmetic(Vector<short> value, int shiftCount) => value >> shiftCount; /// <summary>Shifts (signed) each element of a vector right by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted right by <paramref name="shiftCount" />.</returns> [Intrinsic] public static Vector<int> ShiftRightArithmetic(Vector<int> value, int shiftCount) => value >> shiftCount; /// <summary>Shifts (signed) each element of a vector right by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted right by <paramref name="shiftCount" />.</returns> [Intrinsic] public static Vector<long> ShiftRightArithmetic(Vector<long> value, int shiftCount) => value >> shiftCount; /// <summary>Shifts (signed) each element of a vector right by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted right by <paramref name="shiftCount" />.</returns> [Intrinsic] public static Vector<nint> ShiftRightArithmetic(Vector<nint> value, int shiftCount) => value >> shiftCount; /// <summary>Shifts (signed) each element of a vector right by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted right by <paramref name="shiftCount" />.</returns> [Intrinsic] [CLSCompliant(false)] public static Vector<sbyte> ShiftRightArithmetic(Vector<sbyte> value, int shiftCount) => value >> shiftCount; /// <summary>Shifts (unsigned) each element of a vector right by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted right by <paramref name="shiftCount" />.</returns> [Intrinsic] public static Vector<byte> ShiftRightLogical(Vector<byte> value, int shiftCount) => value >>> shiftCount; /// <summary>Shifts (unsigned) each element of a vector right by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted right by <paramref name="shiftCount" />.</returns> [Intrinsic] public static Vector<short> ShiftRightLogical(Vector<short> value, int shiftCount) => value >>> shiftCount; /// <summary>Shifts (unsigned) each element of a vector right by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted right by <paramref name="shiftCount" />.</returns> [Intrinsic] public static Vector<int> ShiftRightLogical(Vector<int> value, int shiftCount) => value >>> shiftCount; /// <summary>Shifts (unsigned) each element of a vector right by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted right by <paramref name="shiftCount" />.</returns> [Intrinsic] public static Vector<long> ShiftRightLogical(Vector<long> value, int shiftCount) => value >>> shiftCount; /// <summary>Shifts (unsigned) each element of a vector right by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted right by <paramref name="shiftCount" />.</returns> [Intrinsic] public static Vector<nint> ShiftRightLogical(Vector<nint> value, int shiftCount) => value >>> shiftCount; /// <summary>Shifts (unsigned) each element of a vector right by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted right by <paramref name="shiftCount" />.</returns> [Intrinsic] [CLSCompliant(false)] public static Vector<nuint> ShiftRightLogical(Vector<nuint> value, int shiftCount) => value >>> shiftCount; /// <summary>Shifts (unsigned) each element of a vector right by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted right by <paramref name="shiftCount" />.</returns> [Intrinsic] [CLSCompliant(false)] public static Vector<sbyte> ShiftRightLogical(Vector<sbyte> value, int shiftCount) => value >>> shiftCount; /// <summary>Shifts (unsigned) each element of a vector right by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted right by <paramref name="shiftCount" />.</returns> [Intrinsic] [CLSCompliant(false)] public static Vector<ushort> ShiftRightLogical(Vector<ushort> value, int shiftCount) => value >>> shiftCount; /// <summary>Shifts (unsigned) each element of a vector right by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted right by <paramref name="shiftCount" />.</returns> [Intrinsic] [CLSCompliant(false)] public static Vector<uint> ShiftRightLogical(Vector<uint> value, int shiftCount) => value >>> shiftCount; /// <summary>Shifts (unsigned) each element of a vector right by the specified amount.</summary> /// <param name="value">The vector whose elements are to be shifted.</param> /// <param name="shiftCount">The number of bits by which to shift each element.</param> /// <returns>A vector whose elements where shifted right by <paramref name="shiftCount" />.</returns> [Intrinsic] [CLSCompliant(false)] public static Vector<ulong> ShiftRightLogical(Vector<ulong> value, int shiftCount) => value >>> shiftCount; internal static Vector<T> Sin<T>(Vector<T> vector) where T : ITrigonometricFunctions<T> { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = T.Sin(vector.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } /// <inheritdoc cref="Vector128.Sin(Vector128{double})" /> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> Sin(Vector<double> vector) { if (IsHardwareAccelerated) { return VectorMath.SinDouble<Vector<double>, Vector<long>>(vector); } else { return Sin<double>(vector); } } /// <inheritdoc cref="Vector128.Sin(Vector128{float})" /> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<float> Sin(Vector<float> vector) { if (IsHardwareAccelerated) { return VectorMath.SinSingle<Vector<float>, Vector<int>, Vector<double>, Vector<long>>(vector); } else { return Sin<float>(vector); } } internal static (Vector<T> Sin, Vector<T> Cos) SinCos<T>(Vector<T> vector) where T : ITrigonometricFunctions<T> { Unsafe.SkipInit(out Vector<T> sinResult); Unsafe.SkipInit(out Vector<T> cosResult); for (int index = 0; index < Vector<T>.Count; index++) { (T sinValue, T cosValue) = T.SinCos(vector.GetElementUnsafe(index)); sinResult.SetElementUnsafe(index, sinValue); cosResult.SetElementUnsafe(index, cosValue); } return (sinResult, cosResult); } /// <inheritdoc cref="Vector128.SinCos(Vector128{double})" /> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static (Vector<double> Sin, Vector<double> Cos) SinCos(Vector<double> vector) { if (IsHardwareAccelerated) { return VectorMath.SinCosDouble<Vector<double>, Vector<long>>(vector); } else { return SinCos<double>(vector); } } /// <inheritdoc cref="Vector128.SinCos(Vector128{float})" /> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static (Vector<float> Sin, Vector<float> Cos) SinCos(Vector<float> vector) { if (IsHardwareAccelerated) { return VectorMath.SinCosSingle<Vector<float>, Vector<int>, Vector<double>, Vector<long>>(vector); } else { return SinCos<float>(vector); } } /// <summary>Computes the square root of a vector on a per-element basis.</summary> /// <param name="value">The vector whose square root is to be computed.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>A vector whose elements are the square root of the corresponding elements in <paramref name="value" />.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="value"/> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> SquareRoot<T>(Vector<T> value) { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T element = Scalar<T>.Sqrt(value.GetElementUnsafe(index)); result.SetElementUnsafe(index, element); } return result; } /// <summary>Stores a vector at the given destination.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="source">The vector that will be stored.</param> /// <param name="destination">The destination at which <paramref name="source" /> will be stored.</param> /// <exception cref="NotSupportedException">The type of <paramref name="source" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [CLSCompliant(false)] public static unsafe void Store<T>(this Vector<T> source, T* destination) => source.StoreUnsafe(ref *destination); /// <summary>Stores a vector at the given aligned destination.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="source">The vector that will be stored.</param> /// <param name="destination">The aligned destination at which <paramref name="source" /> will be stored.</param> /// <exception cref="NotSupportedException">The type of <paramref name="source" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void StoreAligned<T>(this Vector<T> source, T* destination) { ThrowHelper.ThrowForUnsupportedNumericsVectorBaseType<T>(); if (((nuint)destination % (uint)(Alignment)) != 0) { ThrowHelper.ThrowAccessViolationException(); } *(Vector<T>*)destination = source; } /// <summary>Stores a vector at the given aligned destination.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="source">The vector that will be stored.</param> /// <param name="destination">The aligned destination at which <paramref name="source" /> will be stored.</param> /// <remarks>This method may bypass the cache on certain platforms.</remarks> /// <exception cref="NotSupportedException">The type of <paramref name="source" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [CLSCompliant(false)] public static unsafe void StoreAlignedNonTemporal<T>(this Vector<T> source, T* destination) => source.StoreAligned(destination); /// <summary>Stores a vector at the given destination.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="source">The vector that will be stored.</param> /// <param name="destination">The destination at which <paramref name="source" /> will be stored.</param> /// <exception cref="NotSupportedException">The type of <paramref name="source" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void StoreUnsafe<T>(this Vector<T> source, ref T destination) { ThrowHelper.ThrowForUnsupportedNumericsVectorBaseType<T>(); ref byte address = ref Unsafe.As<T, byte>(ref destination); Unsafe.WriteUnaligned(ref address, source); } /// <summary>Stores a vector at the given destination.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="source">The vector that will be stored.</param> /// <param name="destination">The destination to which <paramref name="elementOffset" /> will be added before the vector will be stored.</param> /// <param name="elementOffset">The element offset from <paramref name="destination" /> from which the vector will be stored.</param> /// <exception cref="NotSupportedException">The type of <paramref name="source" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void StoreUnsafe<T>(this Vector<T> source, ref T destination, nuint elementOffset) { ThrowHelper.ThrowForUnsupportedNumericsVectorBaseType<T>(); destination = ref Unsafe.Add(ref destination, (nint)elementOffset); Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination), source); } /// <inheritdoc cref="Vector128.Subtract{T}(Vector128{T}, Vector128{T})" /> [Intrinsic] public static Vector<T> Subtract<T>(Vector<T> left, Vector<T> right) => left - right; /// <inheritdoc cref="Vector128.SubtractSaturate{T}(Vector128{T}, Vector128{T})" /> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> SubtractSaturate<T>(Vector<T> left, Vector<T> right) { if ((typeof(T) == typeof(float)) || (typeof(T) == typeof(double))) { return left - right; } else { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.SubtractSaturate(left.GetElementUnsafe(index), right.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } } /// <summary> /// Returns the sum of all elements inside the vector. /// </summary> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Sum<T>(Vector<T> value) { T sum = default!; for (int index = 0; index < Vector<T>.Count; index++) { sum = Scalar<T>.Add(sum, value.GetElementUnsafe(index)); } return sum; } /// <summary>Converts the given vector to a scalar containing the value of the first element.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="vector">The vector to get the first element from.</param> /// <returns>A scalar <typeparamref name="T" /> containing the value of the first element.</returns> /// <exception cref="NotSupportedException">The type of <paramref name="vector" /> (<typeparamref name="T" />) is not supported.</exception> [Intrinsic] public static T ToScalar<T>(this Vector<T> vector) { ThrowHelper.ThrowForUnsupportedNumericsVectorBaseType<T>(); return vector.GetElementUnsafe(0); } [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static Vector<T> Truncate<T>(Vector<T> vector) { if (Scalar<T>.IsFloatingPoint) { Unsafe.SkipInit(out Vector<T> result); for (int index = 0; index < Vector<T>.Count; index++) { T value = Scalar<T>.Truncate(vector.GetElementUnsafe(index)); result.SetElementUnsafe(index, value); } return result; } else { return vector; } } /// <inheritdoc cref="Vector128.Truncate(Vector128{double})" /> [Intrinsic] public static Vector<double> Truncate(Vector<double> vector) => Truncate<double>(vector); /// <inheritdoc cref="Vector128.Truncate(Vector128{float})" /> [Intrinsic] public static Vector<float> Truncate(Vector<float> vector) => Truncate<float>(vector); /// <summary>Widens a <see langword="Vector<Byte>" /> into two <see cref="Vector{UInt16} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A pair of vectors that contain the widened lower and upper halves of <paramref name="source" />.</returns> [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static (Vector<ushort> Lower, Vector<ushort> Upper) Widen(Vector<byte> source) => (WidenLower(source), WidenUpper(source)); /// <summary>Widens a <see langword="Vector<Int16>" /> into two <see cref="Vector{Int32} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A pair of vectors that contain the widened lower and upper halves of <paramref name="source" />.</returns> [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static (Vector<int> Lower, Vector<int> Upper) Widen(Vector<short> source) => (WidenLower(source), WidenUpper(source)); /// <summary>Widens a <see langword="Vector<Int32>" /> into two <see cref="Vector{Int64} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A pair of vectors that contain the widened lower and upper halves of <paramref name="source" />.</returns> [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static (Vector<long> Lower, Vector<long> Upper) Widen(Vector<int> source) => (WidenLower(source), WidenUpper(source)); /// <summary>Widens a <see langword="Vector<SByte>" /> into two <see cref="Vector{Int16} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A pair of vectors that contain the widened lower and upper halves of <paramref name="source" />.</returns> [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static (Vector<short> Lower, Vector<short> Upper) Widen(Vector<sbyte> source) => (WidenLower(source), WidenUpper(source)); /// <summary>Widens a <see langword="Vector<Single>" /> into two <see cref="Vector{Double} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A pair of vectors that contain the widened lower and upper halves of <paramref name="source" />.</returns> [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static (Vector<double> Lower, Vector<double> Upper) Widen(Vector<float> source) => (WidenLower(source), WidenUpper(source)); /// <summary>Widens a <see langword="Vector<UInt16>" /> into two <see cref="Vector{UInt32} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A pair of vectors that contain the widened lower and upper halves of <paramref name="source" />.</returns> [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static (Vector<uint> Lower, Vector<uint> Upper) Widen(Vector<ushort> source) => (WidenLower(source), WidenUpper(source)); /// <summary>Widens a <see langword="Vector<UInt32>" /> into two <see cref="Vector{UInt64} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A pair of vectors that contain the widened lower and upper halves of <paramref name="source" />.</returns> [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static (Vector<ulong> Lower, Vector<ulong> Upper) Widen(Vector<uint> source) => (WidenLower(source), WidenUpper(source)); /// <summary>Widens a <see langword="Vector<Byte>" /> into two <see cref="Vector{UInt16} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <param name="low">A vector that will contain the widened result of the lower half of <paramref name="source" />.</param> /// <param name="high">A vector that will contain the widened result of the upper half of <paramref name="source" />.</param> [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Widen(Vector<byte> source, out Vector<ushort> low, out Vector<ushort> high) { low = WidenLower(source); high = WidenUpper(source); } /// <summary>Widens a <see langword="Vector<Int16>" /> into two <see cref="Vector{Int32} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <param name="low">A vector that will contain the widened result of the lower half of <paramref name="source" />.</param> /// <param name="high">A vector that will contain the widened result of the upper half of <paramref name="source" />.</param> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Widen(Vector<short> source, out Vector<int> low, out Vector<int> high) { low = WidenLower(source); high = WidenUpper(source); } /// <summary>Widens a <see langword="Vector<Int32>" /> into two <see cref="Vector{Int64} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <param name="low">A vector that will contain the widened result of the lower half of <paramref name="source" />.</param> /// <param name="high">A vector that will contain the widened result of the upper half of <paramref name="source" />.</param> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Widen(Vector<int> source, out Vector<long> low, out Vector<long> high) { low = WidenLower(source); high = WidenUpper(source); } /// <summary>Widens a <see langword="Vector<SByte>" /> into two <see cref="Vector{Int16} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <param name="low">A vector that will contain the widened result of the lower half of <paramref name="source" />.</param> /// <param name="high">A vector that will contain the widened result of the upper half of <paramref name="source" />.</param> [MethodImpl(MethodImplOptions.AggressiveInlining)] [CLSCompliant(false)] public static void Widen(Vector<sbyte> source, out Vector<short> low, out Vector<short> high) { low = WidenLower(source); high = WidenUpper(source); } /// <summary>Widens a <see langword="Vector<Single>" /> into two <see cref="Vector{Double} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <param name="low">A vector that will contain the widened result of the lower half of <paramref name="source" />.</param> /// <param name="high">A vector that will contain the widened result of the upper half of <paramref name="source" />.</param> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Widen(Vector<float> source, out Vector<double> low, out Vector<double> high) { low = WidenLower(source); high = WidenUpper(source); } /// <summary>Widens a <see langword="Vector<UInt16>" /> into two <see cref="Vector{UInt32} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <param name="low">A vector that will contain the widened result of the lower half of <paramref name="source" />.</param> /// <param name="high">A vector that will contain the widened result of the upper half of <paramref name="source" />.</param> [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Widen(Vector<ushort> source, out Vector<uint> low, out Vector<uint> high) { low = WidenLower(source); high = WidenUpper(source); } /// <summary>Widens a <see langword="Vector<UInt32>" /> into two <see cref="Vector{UInt64} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <param name="low">A vector that will contain the widened result of the lower half of <paramref name="source" />.</param> /// <param name="high">A vector that will contain the widened result of the upper half of <paramref name="source" />.</param> [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Widen(Vector<uint> source, out Vector<ulong> low, out Vector<ulong> high) { low = WidenLower(source); high = WidenUpper(source); } /// <summary>Widens the lower half of a <see langword="Vector<Byte>" /> into a <see cref="Vector{UInt16} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A vector that contain the widened lower half of <paramref name="source" />.</returns> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<ushort> WidenLower(Vector<byte> source) { Unsafe.SkipInit(out Vector<ushort> lower); for (int i = 0; i < Vector<ushort>.Count; i++) { ushort value = source.GetElementUnsafe(i); lower.SetElementUnsafe(i, value); } return lower; } /// <summary>Widens the lower half of a <see langword="Vector<Int16>" /> into a <see cref="Vector{Int32} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A vector that contain the widened lower half of <paramref name="source" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<int> WidenLower(Vector<short> source) { Unsafe.SkipInit(out Vector<int> lower); for (int i = 0; i < Vector<int>.Count; i++) { int value = source.GetElementUnsafe(i); lower.SetElementUnsafe(i, value); } return lower; } /// <summary>Widens the lower half of a <see langword="Vector<Int32>" /> into a <see cref="Vector{Int64} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A vector that contain the widened lower half of <paramref name="source" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<long> WidenLower(Vector<int> source) { Unsafe.SkipInit(out Vector<long> lower); for (int i = 0; i < Vector<long>.Count; i++) { long value = source.GetElementUnsafe(i); lower.SetElementUnsafe(i, value); } return lower; } /// <summary>Widens the lower half of a <see langword="Vector<SByte>" /> into a <see cref="Vector{Int16} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A vector that contain the widened lower half of <paramref name="source" />.</returns> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<short> WidenLower(Vector<sbyte> source) { Unsafe.SkipInit(out Vector<short> lower); for (int i = 0; i < Vector<short>.Count; i++) { short value = source.GetElementUnsafe(i); lower.SetElementUnsafe(i, value); } return lower; } /// <summary>Widens the lower half of a <see langword="Vector<Single>" /> into a <see cref="Vector{Double} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A vector that contain the widened lower half of <paramref name="source" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> WidenLower(Vector<float> source) { Unsafe.SkipInit(out Vector<double> lower); for (int i = 0; i < Vector<double>.Count; i++) { double value = source.GetElementUnsafe(i); lower.SetElementUnsafe(i, value); } return lower; } /// <summary>Widens the lower half of a <see langword="Vector<UInt16>" /> into a <see cref="Vector{UInt32} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A vector that contain the widened lower half of <paramref name="source" />.</returns> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<uint> WidenLower(Vector<ushort> source) { Unsafe.SkipInit(out Vector<uint> lower); for (int i = 0; i < Vector<uint>.Count; i++) { uint value = source.GetElementUnsafe(i); lower.SetElementUnsafe(i, value); } return lower; } /// <summary>Widens the lower half of a <see langword="Vector<UInt32>" /> into a <see cref="Vector{UInt64} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A vector that contain the widened lower half of <paramref name="source" />.</returns> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<ulong> WidenLower(Vector<uint> source) { Unsafe.SkipInit(out Vector<ulong> lower); for (int i = 0; i < Vector<ulong>.Count; i++) { ulong value = source.GetElementUnsafe(i); lower.SetElementUnsafe(i, value); } return lower; } /// <summary>Widens the upper half of a <see langword="Vector<Byte>" /> into a <see cref="Vector{UInt16} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A vector that contain the widened upper half of <paramref name="source" />.</returns> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<ushort> WidenUpper(Vector<byte> source) { Unsafe.SkipInit(out Vector<ushort> upper); for (int i = Vector<ushort>.Count; i < Vector<byte>.Count; i++) { ushort value = source.GetElementUnsafe(i); upper.SetElementUnsafe(i - Vector<ushort>.Count, value); } return upper; } /// <summary>Widens the upper half of a <see langword="Vector<Int16>" /> into a <see cref="Vector{Int32} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A vector that contain the widened upper half of <paramref name="source" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<int> WidenUpper(Vector<short> source) { Unsafe.SkipInit(out Vector<int> upper); for (int i = Vector<int>.Count; i < Vector<short>.Count; i++) { int value = source.GetElementUnsafe(i); upper.SetElementUnsafe(i - Vector<int>.Count, value); } return upper; } /// <summary>Widens the upper half of a <see langword="Vector<Int32>" /> into a <see cref="Vector{Int64} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A vector that contain the widened upper half of <paramref name="source" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<long> WidenUpper(Vector<int> source) { Unsafe.SkipInit(out Vector<long> upper); for (int i = Vector<long>.Count; i < Vector<int>.Count; i++) { long value = source.GetElementUnsafe(i); upper.SetElementUnsafe(i - Vector<long>.Count, value); } return upper; } /// <summary>Widens the upper half of a <see langword="Vector<SByte>" /> into a <see cref="Vector{Int16} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A vector that contain the widened upper half of <paramref name="source" />.</returns> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<short> WidenUpper(Vector<sbyte> source) { Unsafe.SkipInit(out Vector<short> upper); for (int i = Vector<short>.Count; i < Vector<sbyte>.Count; i++) { short value = source.GetElementUnsafe(i); upper.SetElementUnsafe(i - Vector<short>.Count, value); } return upper; } /// <summary>Widens the upper half of a <see langword="Vector<Single>" /> into a <see cref="Vector{Double} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A vector that contain the widened upper half of <paramref name="source" />.</returns> [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<double> WidenUpper(Vector<float> source) { Unsafe.SkipInit(out Vector<double> upper); for (int i = Vector<double>.Count; i < Vector<float>.Count; i++) { double value = source.GetElementUnsafe(i); upper.SetElementUnsafe(i - Vector<double>.Count, value); } return upper; } /// <summary>Widens the upper half of a <see langword="Vector<UInt16>" /> into a <see cref="Vector{UInt32} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A vector that contain the widened upper half of <paramref name="source" />.</returns> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<uint> WidenUpper(Vector<ushort> source) { Unsafe.SkipInit(out Vector<uint> upper); for (int i = Vector<uint>.Count; i < Vector<ushort>.Count; i++) { uint value = source.GetElementUnsafe(i); upper.SetElementUnsafe(i - Vector<uint>.Count, value); } return upper; } /// <summary>Widens the upper half of a <see langword="Vector<UInt32>" /> into a <see cref="Vector{UInt64} " />.</summary> /// <param name="source">The vector whose elements are to be widened.</param> /// <returns>A vector that contain the widened upper half of <paramref name="source" />.</returns> [Intrinsic] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<ulong> WidenUpper(Vector<uint> source) { Unsafe.SkipInit(out Vector<ulong> upper); for (int i = Vector<ulong>.Count; i < Vector<uint>.Count; i++) { ulong value = source.GetElementUnsafe(i); upper.SetElementUnsafe(i - Vector<ulong>.Count, value); } return upper; } /// <summary>Creates a new <see cref="Vector{T}" /> with the element at the specified index set to the specified value and the remaining elements set to the same value as that in the given vector.</summary> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <param name="vector">The vector to get the remaining elements from.</param> /// <param name="index">The index of the element to set.</param> /// <param name="value">The value to set the element to.</param> /// <returns>A <see cref="Vector{T}" /> with the value of the element at <paramref name="index" /> set to <paramref name="value" /> and the remaining elements set to the same value as that in <paramref name="vector" />.</returns> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index" /> was less than zero or greater than the number of elements.</exception> /// <exception cref="NotSupportedException">The type of <paramref name="vector" /> (<typeparamref name="T" />) is not supported.</exception> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector<T> WithElement<T>(this Vector<T> vector, int index, T value) { if ((uint)(index) >= (uint)(Vector<T>.Count)) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } Vector<T> result = vector; result.SetElementUnsafe(index, value); return result; } /// <summary>Computes the exclusive-or of two vectors.</summary> /// <param name="left">The vector to exclusive-or with <paramref name="right" />.</param> /// <param name="right">The vector to exclusive-or with <paramref name="left" />.</param> /// <typeparam name="T">The type of the elements in the vector.</typeparam> /// <returns>The exclusive-or of <paramref name="left" /> and <paramref name="right" />.</returns> [Intrinsic] public static Vector<T> Xor<T>(Vector<T> left, Vector<T> right) => left ^ right; [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static T GetElementUnsafe<T>(in this Vector<T> vector, int index) { Debug.Assert((index >= 0) && (index < Vector<T>.Count)); ref T address = ref Unsafe.As<Vector<T>, T>(ref Unsafe.AsRef(in vector)); return Unsafe.Add(ref address, index); } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void SetElementUnsafe<T>(in this Vector<T> vector, int index, T value) { Debug.Assert((index >= 0) && (index < Vector<T>.Count)); ref T address = ref Unsafe.As<Vector<T>, T>(ref Unsafe.AsRef(in vector)); Unsafe.Add(ref address, index) = value; } } }