using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace ARMeilleure.State
{
    /// 
    /// Represents a 128-bit vector.
    /// 
    [StructLayout(LayoutKind.Sequential, Size = 16)]
    public struct V128 : IEquatable
    {
        // _e0 & _e1 could be marked as readonly, however they are not readonly because we modify them through the Unsafe
        // APIs. This also means that one should be careful when changing the layout of this struct.
        private ulong _e0;
        private ulong _e1;
        /// 
        /// Gets a new  with all bits set to zero.
        /// 
        public static V128 Zero => new V128(0, 0);
        /// 
        /// Initializes a new instance of the  struct with the specified  value
        /// as a scalar.
        /// 
        /// Scalar value
        public V128(double value) : this(value, 0) { }
        /// 
        /// Initializes a new instance of the  struct with the specified  elements.
        /// 
        /// Element 0
        /// Element 1
        public V128(double e0, double e1)
        {
            _e0 = (ulong)BitConverter.DoubleToInt64Bits(e0);
            _e1 = (ulong)BitConverter.DoubleToInt64Bits(e1);
        }
        /// 
        /// Initializes a new instance of the  struct with the specified  value as a
        /// scalar.
        /// 
        /// Scalar value
        public V128(float value) : this(value, 0, 0, 0) { }
        /// 
        /// Initializes a new instance of the  struct with the specified  elements.
        /// 
        /// Element 0
        /// Element 1
        /// Element 2
        /// Element 3
        public V128(float e0, float e1, float e2, float e3)
        {
            _e0  = (ulong)(uint)BitConverter.SingleToInt32Bits(e0) << 0;
            _e0 |= (ulong)(uint)BitConverter.SingleToInt32Bits(e1) << 32;
            _e1  = (ulong)(uint)BitConverter.SingleToInt32Bits(e2) << 0;
            _e1 |= (ulong)(uint)BitConverter.SingleToInt32Bits(e3) << 32;
        }
        /// 
        /// Initializes a new instance of the  struct with the specified 
        /// elements.
        /// 
        /// Element 0
        /// Element 1
        public V128(long e0, long e1) : this((ulong)e0, (ulong)e1) { }
        /// 
        /// Initializes a new instance of the  struct with the specified  elements.
        /// 
        /// Element 0
        /// Element 1
        public V128(ulong e0, ulong e1)
        {
            _e0 = e0;
            _e1 = e1;
        }
        /// 
        /// Initializes a new instance of the  struct with the specified  elements.
        /// 
        /// Element 0
        /// Element 1
        /// Element 2
        /// Element 3
        public V128(int e0, int e1, int e2, int e3) : this((uint)e0, (uint)e1, (uint)e2, (uint)e3) { }
        /// 
        /// Initializes a new instance of the  struct with the specified  elements.
        /// 
        /// Element 0
        /// Element 1
        /// Element 2
        /// Element 3
        public V128(uint e0, uint e1, uint e2, uint e3)
        {
            _e0  = (ulong)e0 << 0;
            _e0 |= (ulong)e1 << 32;
            _e1  = (ulong)e2 << 0;
            _e1 |= (ulong)e3 << 32;
        }
        /// 
        /// Initializes a new instance of the  struct from the specified  array.
        /// 
        ///  array to use
        public V128(byte[] data)
        {
            _e0 = (ulong)BitConverter.ToInt64(data, 0);
            _e1 = (ulong)BitConverter.ToInt64(data, 8);
        }
        /// 
        /// Returns the value of the  as a  scalar.
        /// 
        /// Type of scalar
        /// Value of the  as a  scalar
        /// Size of  is larger than 16 bytes
        public T As() where T : unmanaged
        {
            return Extract(0);
        }
        /// 
        /// Extracts the element at the specified index as a  from the .
        /// 
        /// Element type
        /// Index of element
        /// Element at the specified index as a  from the 
        /// 
        ///  is out of bound or the size of  is larger than 16 bytes
        /// 
        public T Extract(int index) where T : unmanaged
        {
            if ((uint)index >= GetElementCount())
                ThrowIndexOutOfRange();
            // Performs:
            //  return *((*T)this + index);
            return Unsafe.Add(ref Unsafe.As(ref this), index);
        }
        /// 
        /// Inserts the specified value into the element at the specified index in the .
        /// 
        /// Element type
        /// Index of element
        /// Value to insert
        /// 
        ///  is out of bound or the size of  is larger than 16 bytes
        /// 
        public void Insert(int index, T value) where T : unmanaged
        {
            if ((uint)index >= GetElementCount())
                ThrowIndexOutOfRange();
            // Performs:
            //  *((*T)this + index) = value;
            Unsafe.Add(ref Unsafe.As(ref this), index) = value;
        }
        /// 
        /// Returns a new  array which represents the .
        /// 
        /// A new  array which represents the 
        public byte[] ToArray()
        {
            byte[]     data = new byte[16];
            Span span = data;
            BitConverter.TryWriteBytes(span, _e0);
            BitConverter.TryWriteBytes(span.Slice(8), _e1);
            return data;
        }
        /// 
        /// Performs a bitwise logical left shift on the specified  by the specified shift count.
        /// 
        ///  instance
        /// Number of shifts
        /// Result of left shift
        /// 
        /// This supports shift counts up to 63; anything above may result in unexpected behaviour.
        /// 
        public static V128 operator <<(V128 x, int shift)
        {
            ulong shiftOut = x._e0 >> (64 - shift);
            return new V128(x._e0 << shift, (x._e1 << shift) | shiftOut);
        }
        /// 
        /// Performs a bitwise logical right shift on the specified  by the specified shift count.
        /// 
        ///  instance
        /// Number of shifts
        /// Result of right shift
        /// 
        /// This supports shift counts up to 63; anything above may result in unexpected behaviour.
        /// 
        public static V128 operator >>(V128 x, int shift)
        {
            ulong shiftOut = x._e1 & ((1UL << shift) - 1);
            return new V128((x._e0 >> shift) | (shiftOut << (64 - shift)), x._e1 >> shift);
        }
        /// 
        /// Performs a bitwise not on the specified .
        /// 
        /// Target 
        /// Result of not operation
        public static V128 operator ~(V128 x) => new V128(~x._e0, ~x._e1);
        /// 
        /// Performs a bitwise and on the specified  instances.
        /// 
        /// First instance
        /// Second instance
        /// Result of and operation
        public static V128 operator &(V128 x, V128 y) => new V128(x._e0 & y._e0, x._e1 & y._e1);
        /// 
        /// Performs a bitwise or on the specified  instances.
        /// 
        /// First instance
        /// Second instance
        /// Result of or operation
        public static V128 operator |(V128 x, V128 y) => new V128(x._e0 | y._e0, x._e1 | y._e1);
        /// 
        /// Performs a bitwise exlusive or on the specified  instances.
        /// 
        /// First instance
        /// Second instance
        /// Result of exclusive or operation
        public static V128 operator ^(V128 x, V128 y) => new V128(x._e0 ^ y._e0, x._e1 ^ y._e1);
        /// 
        /// Determines if the specified  instances are equal.
        /// 
        /// First instance
        /// Second instance
        /// true if equal; otherwise false
        public static bool operator ==(V128 x, V128 y) => x.Equals(y);
        /// 
        /// Determines if the specified  instances are not equal.
        /// 
        /// First instance
        /// Second instance
        /// true if not equal; otherwise false
        public static bool operator !=(V128 x, V128 y) => !x.Equals(y);
        /// 
        /// Determines if the specified  is equal to this  instance.
        /// 
        /// Other  instance
        /// true if equal; otherwise false
        public bool Equals(V128 other)
        {
            return other._e0 == _e0 && other._e1 == _e1;
        }
        /// 
        /// Determines if the specified  is equal to this  instance.
        /// 
        /// Other  instance
        /// true if equal; otherwise false
        public override bool Equals(object obj)
        {
            return obj is V128 vector && Equals(vector);
        }
        /// 
        public override int GetHashCode()
        {
            return HashCode.Combine(_e0, _e1);
        }
        /// 
        public override string ToString()
        {
            return $"0x{_e1:X16}{_e0:X16}";
        }
        private uint GetElementCount() where T : unmanaged
        {
            return (uint)(Unsafe.SizeOf() / Unsafe.SizeOf());
        }
        private static void ThrowIndexOutOfRange()
        {
            throw new ArgumentOutOfRangeException("index");
        }
    }
}