Fixing the `LoadAlignedVector128` HWIntrinsic test to ensure that we always read...
authorTanner Gooding <tagoo@outlook.com>
Wed, 17 Jan 2018 15:51:43 +0000 (07:51 -0800)
committerTanner Gooding <tagoo@outlook.com>
Thu, 18 Jan 2018 03:56:19 +0000 (19:56 -0800)
tests/src/JIT/HardwareIntrinsics/X86/Sse/LoadAlignedVector128.cs

index fdaf40a..eaf6909 100644 (file)
@@ -22,7 +22,8 @@ namespace IntelHardwareIntrinsicTest
 
             if (Sse.IsSupported)
             {
-                float* inArray = stackalloc float[4];
+                byte* inBuffer = stackalloc byte[32];
+                float* inArray = Align(inBuffer, 16);
                 float* outArray = stackalloc float[4];
 
                 var vf = Sse.LoadAlignedVector128(inArray);
@@ -47,5 +48,15 @@ namespace IntelHardwareIntrinsicTest
 
             return testResult;
         }
+
+        static unsafe float* Align(byte* buffer, byte expectedAlignment)
+        {
+            // Compute how bad the misalignment is, which is at most (expectedAlignment - 1).
+            // Then subtract that from the expectedAlignment and add it to the original address
+            // to compute the aligned address.
+
+            var misalignment = expectedAlignment - ((ulong)(buffer) % expectedAlignment);
+            return (float*)(buffer + misalignment);
+        }
     }
 }