using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

/*
    Copyright (c) 2011, flixmonk. (http://DVDSwarm.codeplex.com)
    All Rights Reserved.
        
    Redistribution and use in source and binary forms, with or without modification,
    are permitted provided that the following conditions are met:
        
    * Redistributions of source code must retain the above copyright notice, this list
      of conditions and the following disclaimer.
        
    * Redistributions in binary form must reproduce the above copyright notice, this
    list of conditions and the following disclaimer in the documentation and/or other
    materials provided with the distribution. 
        
    * Neither the name of The Flixmonk nor the names of its contributors may be used to
    endorse or promote products derived from this software without specific prior written
    permission.
        
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
    SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
    OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
    TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace DVDSwarm
{
    /// <summary>
    /// Returns information about SIMD features available in the processor. Requires the
    /// external C++ DLL named "Simd.dll".
    /// </summary>
    sealed public class Simd
    {
        #region Fields

        Boolean _dllExists = true;
        Boolean _has3DNow = false;
        Boolean _has3DNowPlus = false;
        Boolean _hasMmx = false;
        Boolean _hasMmxPlus = false;
        Boolean _hasSse = false;
        Boolean _hasSse2 = false;
        Boolean _hasSse3 = false;
        Boolean _hasSse41 = false;
        Boolean _hasSse42 = false;
        Boolean _hasSse4a = false;
        Boolean _hasSse5 = false;
        Boolean _hasSsse3 = false;
        
        #endregion
        
        #region Properties

        /// <summary>
        /// Gets a value indicating whether the required DLL file exists. If false,
        /// the processor features cannot be determined.
        /// </summary>
        public Boolean DllExists
        {
            get
            {
                return _dllExists;
            }
        }
        /// <summary>
        /// Gets a value indicating whether the processor feature is available.
        /// </summary>
        public Boolean Has3DNow
        {
            get
            {
                return _has3DNow;
            }
        }
        /// <summary>
        /// Gets a value indicating whether the processor feature is available.
        /// </summary>
        public Boolean Has3DNowPlus
        {
            get
            {
                return _has3DNowPlus;
            }
        }
        /// <summary>
        /// Gets a value indicating whether the processor feature is available.
        /// </summary>
        public Boolean HasMmx
        {
            get
            {
                return _hasMmx;
            }
        }
        /// <summary>
        /// Gets a value indicating whether the processor feature is available.
        /// </summary>
        public Boolean HasMmxPlus
        {
            get
            {
                return _hasMmxPlus;
            }
        }
        /// <summary>
        /// Gets a value indicating whether the processor feature is available.
        /// </summary>
        public Boolean HasSse
        {
            get
            {
                return _hasSse;
            }
        }
        /// <summary>
        /// Gets a value indicating whether the processor feature is available.
        /// </summary>
        public Boolean HasSse2
        {
            get
            {
                return _hasSse2;
            }
        }
        /// <summary>
        /// Gets a value indicating whether the processor feature is available.
        /// </summary>
        public Boolean HasSse3
        {
            get
            {
                return _hasSse3;
            }
        }
        /// <summary>
        /// Gets a value indicating whether the processor feature is available.
        /// </summary>
        public Boolean HasSse41
        {
            get
            {
                return _hasSse41;
            }
        }
        /// <summary>
        /// Gets a value indicating whether the processor feature is available.
        /// </summary>
        public Boolean HasSse42
        {
            get
            {
                return _hasSse42;
            }
        }
        /// <summary>
        /// Gets a value indicating whether the processor feature is available.
        /// </summary>
        public Boolean HasSse4a
        {
            get
            {
                return _hasSse4a;
            }
        }
        /// <summary>
        /// Gets a value indicating whether the processor feature is available.
        /// </summary>
        public Boolean HasSse5
        {
            get
            {
                return _hasSse5;
            }
        }
        /// <summary>
        /// Gets a value indicating whether the processor feature is available.
        /// </summary>
        public Boolean HasSsse3
        {
            get
            {
                return _hasSsse3;
            }
        }
        
        #endregion

        #region Methods
        
        /// <summary>
        /// Creates a new instance of the Simd class.
        /// </summary>
        public Simd()
        {
            if (!System.IO.File.Exists(Application.StartupPath + "\\Simd.dll"))
            {
                _dllExists = false;
                return;
            }

            //
		    // Features are mapped to bits in an int. If the bit is set, the feature is available.
		    //
		    // Bit  Feature
		    // ---  ----------------
		    // 00   3DNow!
		    // 01   3DNow!+
		    // 02   MMX
		    // 03   MMX+
		    // 04   SSE
		    // 05   SSE2
		    // 06   SSE3
		    // 07   SSSE3
		    // 08   SSE4.1
		    // 09   SSE4.2
		    // 10   SSE4a
		    // 11   SSE5
        
            Int32 value = UnsafeNativeDll.DetectFeatures();

            if ((value & 1) == 1)
            {
                _has3DNow = true;
            }
            if ((value & 2) == 2)
            {
                _has3DNowPlus = true;
            }
            if ((value & 4) == 4)
            {
                _hasMmx = true;
            }
            if ((value & 8) == 8)
            {
                _hasMmxPlus = true;
            }
            if ((value & 0x10) == 0x10)
            {
                _hasSse = true;
            }
            if ((value & 0x20) == 0x20)
            {
                _hasSse2 = true;
            }
            if ((value & 0x40) == 0x40)
            {
                _hasSse3 = true;
            }
            if ((value & 0x80) == 0x80)
            {
                _hasSsse3 = true;
            }
            if ((value & 0x100) == 0x100)
            {
                _hasSse41 = true;
            }
            if ((value & 0x200) == 0x200)
            {
                _hasSse42 = true;
            }
            if ((value & 0x400) == 0x400)
            {
                _hasSse4a = true;
            }
            if ((value & 0x800) == 0x800)
            {
                _hasSse5 = true;
            }
        }

        /// <summary>
        /// Provides access to the unmanaged code dll.
        /// </summary>
        private static class UnsafeNativeDll
        {
            [DllImport("\\Simd.dll")]
            public static extern Int32 DetectFeatures();
        }

        #endregion
    }
}
