summaryrefslogtreecommitdiff
path: root/tests/src/JIT/HardwareIntrinsics/X86/Shared/BooleanUnOpTest_DataTable.cs
blob: a94532413d2d1fc200d461ba6d24f55cb55ad3dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

namespace JIT.HardwareIntrinsics.X86
{
    public unsafe struct BooleanUnaryOpTest__DataTable<TOp1> : IDisposable
        where TOp1 : struct
    {
        private byte[] inArray;

        private GCHandle inHandle;

        private ulong alignment;

        public BooleanUnaryOpTest__DataTable(TOp1[] inArray, int alignment)
        {
            int sizeOfinArray = inArray.Length * Unsafe.SizeOf<TOp1>();
            if ((alignment != 32 && alignment != 16) || (alignment * 2) < sizeOfinArray)
            {
                throw new ArgumentException("Invalid value of alignment");
            }
            this.inArray = new byte[alignment * 2];

            this.inHandle = GCHandle.Alloc(this.inArray, GCHandleType.Pinned);

            this.alignment = (ulong)alignment;

            Unsafe.CopyBlockUnaligned(ref Unsafe.AsRef<byte>(inArrayPtr), ref Unsafe.As<TOp1, byte>(ref inArray[0]), (uint)sizeOfinArray);
        }

        public void* inArrayPtr => Align((byte*)(inHandle.AddrOfPinnedObject().ToPointer()), alignment);

        public void Dispose()
        {
            inHandle.Free();
        }

        private static unsafe void* Align(byte* buffer, ulong expectedAlignment)
        {
            return (void*)(((ulong)buffer + expectedAlignment - 1) & ~(expectedAlignment - 1));
        }
    }
}