Skip to content

Commit

Permalink
Hopefully fix vectorization on Mac
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSandersonMS committed Mar 7, 2024
1 parent ab2bacd commit b6eb570
Showing 1 changed file with 52 additions and 6 deletions.
58 changes: 52 additions & 6 deletions src/SmartComponents.LocalEmbeddings/VectorCompat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ public static unsafe Vector256<byte> Vector256Xor(Vector256<byte> lhs, Vector256
#if NET8_0_OR_GREATER
return Vector256.Xor(lhs, rhs);
#else
return Vector.Xor(lhs.AsVector(), rhs.AsVector()).AsVector256();
// Assume the target platform at least supports 128-bit vectors
return Vector256.Create(
Vector128Xor(lhs.GetLower(), rhs.GetLower()),
Vector128Xor(lhs.GetUpper(), rhs.GetUpper()));
#endif
}

Expand All @@ -68,7 +71,21 @@ public static unsafe Vector256<T> Vector256Multiply<T>(Vector256<T> vector, T va
#if NET8_0_OR_GREATER
return vector * value;
#else
return Vector.Multiply(vector.AsVector(), value).AsVector256();
// Assume the target platform at least supports 128-bit vectors
var lower = Vector.Multiply(vector.GetLower().AsVector(), value).AsVector128();
var upper = Vector.Multiply(vector.GetUpper().AsVector(), value).AsVector128();
return Vector256.Create(lower.AsByte(), upper.AsByte()).As<byte, T>();
#endif
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Vector128<int> Vector128Add(Vector128<int> lhs, Vector128<int> rhs)
{
#if NET8_0_OR_GREATER
return lhs + rhs;
#else
// Assume the target platform at least supports 128-bit vectors
return Vector.Add(lhs.AsVector(), rhs.AsVector()).AsVector128();
#endif
}

Expand All @@ -78,7 +95,20 @@ public static unsafe Vector256<int> Vector256Add(Vector256<int> lhs, Vector256<i
#if NET8_0_OR_GREATER
return lhs + rhs;
#else
return Vector.Add(lhs.AsVector(), rhs.AsVector()).AsVector256();
return Vector256.Create(
Vector128Add(lhs.GetLower(), rhs.GetLower()),
Vector128Add(lhs.GetUpper(), rhs.GetUpper()));
#endif
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Vector128<T> Vector128Multiply<T>(Vector128<T> lhs, Vector128<T> rhs) where T : unmanaged
{
#if NET8_0_OR_GREATER
return lhs * rhs;
#else
// Assume the target platform at least supports 128-bit vectors
return Vector.Multiply(lhs.AsVector(), rhs.AsVector()).AsVector128();
#endif
}

Expand All @@ -88,20 +118,36 @@ public static unsafe Vector256<T> Vector256Multiply<T>(Vector256<T> lhs, Vector2
#if NET8_0_OR_GREATER
return lhs * rhs;
#else
return Vector.Multiply(lhs.AsVector(), rhs.AsVector()).AsVector256();
return Vector256.Create(
Vector128Multiply(lhs.GetLower(), rhs.GetLower()).AsByte(),
Vector128Multiply(lhs.GetUpper(), rhs.GetUpper()).AsByte()).As<byte, T>();
#endif
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe T Vector256Sum<T>(Vector256<T> vector) where T : unmanaged
public static unsafe T Vector128Sum<T>(Vector128<T> vector) where T : unmanaged
{
#if NET8_0_OR_GREATER
return Vector256.Sum(vector);
return Vector128.Sum(vector);
#else
// Assume the target platform at least supports 128-bit vectors
return Vector.Sum(vector.AsVector());
#endif
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe T Vector256Sum<T>(Vector256<T> vector) where T: unmanaged
{
#if NET8_0_OR_GREATER
return Vector256.Sum(vector);
#else
// Assume the target platform at least supports 128-bit vectors
return Vector.Sum(Vector.Add(
vector.GetLower().AsVector(),
vector.GetUpper().AsVector()));
#endif
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Vector256<int> Vector256ConvertToInt32(Vector256<float> vector)
{
Expand Down

0 comments on commit b6eb570

Please sign in to comment.