Advertisement
Guest User

Untitled

a guest
Nov 27th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. using System.Runtime.Intrinsics;
  6. using System.Runtime.Intrinsics.X86;
  7.  
  8. namespace Intrinsics
  9. {
  10.     [StructLayout(LayoutKind.Sequential)]
  11.     public struct SrVector
  12.     {
  13.         public readonly float X;
  14.         public readonly float Y;
  15.         public readonly float Z;
  16.  
  17.         private Vector128<float> _m128;
  18.  
  19.         public SrVector(float x, float y, float z)
  20.         {
  21.             X = x;
  22.             Y = y;
  23.             Z = z;
  24.             _m128 = Vector128.Create(X, Y, Z, 0f);
  25.         }
  26.  
  27.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  28.         public static float Dot(SrVector a, SrVector b) => Sse41.DotProduct(a._m128, b._m128, 0x7F).ToScalar();
  29.     }
  30.  
  31.     class Program
  32.     {
  33.         static void Main(string[] args)
  34.         {
  35.             // Do something with the results to ensure the compiler does not optimize the computations away in Release build.
  36.             float sum = 0;
  37.            
  38.             Vector3 v4a = new Vector3(1f, 0f, 3.2f);
  39.             Vector3 v4b = new Vector3(2f, 2f, 0f);
  40.             var vdot = Vector3.Dot(v4a, v4b);
  41.             sum += vdot;
  42.  
  43.             SrVector sra = new SrVector(1f, 0f, 3.2f);
  44.             SrVector srb = new SrVector(2f, 2f, 0f);
  45.             var srdot = SrVector.Dot(sra, srb);
  46.             sum += srdot;
  47.  
  48.             Console.WriteLine(sum);
  49.         }
  50.     }
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement