Esempio n. 1
0
        public BitGroupCoding(int numBase, Alphabet abc, char paddingChar)
            : base(numBase, abc)
        {
            if (!BitTwiddling.IsPowerOfTwo(numBase))
            {
                throw new ArgumentOutOfRangeException("numBase", "The '" + typeof(BitGroupCoding) + "' can only be used with a numerical base that is a power of 2.");
            }
            int v;

            if (abc.TryDecode(_paddingChar, out v))
            {
                throw new ArgumentOutOfRangeException("The padding char cannot be a member of the alphabet.");
            }

            // Hmm...
            var bitCount = (int)Math.Log(numBase, 2);

            _bitCount = bitCount;
            _bitMask  = (1u << bitCount) - 1;

            var bitGroupSize      = BitTwiddling.LeastCommonMultiple(bitCount, 8);
            var bitGroupByteCount = bitGroupSize / 8;
            var bitGroupCharCount = bitGroupSize / bitCount;

            // Encode
            var charCountFromByteCount = new int[1 + bitGroupByteCount];

            for (int i = 0; i <= bitGroupByteCount; i++)
            {
                charCountFromByteCount[i] = (8 * i + (bitCount - 1)) / bitCount;
            }

            // Decode
            var byteCountFromCharCount = new int[1 + bitGroupCharCount];

            for (int i = 0; i < byteCountFromCharCount.Length; i++)
            {
                byteCountFromCharCount[i] = -1;
            }
            for (int i = 1; i <= bitGroupByteCount; i++)
            {
                byteCountFromCharCount[charCountFromByteCount[i]] = i;
            }

            // Save
            _bitGroupByteCount      = bitGroupByteCount;
            _bitGroupCharCount      = bitGroupCharCount;
            _charCountFromByteCount = charCountFromByteCount;
            _byteCountFromCharCount = byteCountFromCharCount;
            _paddingChar            = paddingChar;
        }
Esempio n. 2
0
        private static long CheckLong(int loopCount)
        {
            long cur = 0;
            long i1  = loopCount;

            for (long i = 0; i < i1; i++)
            {
                cur ^= BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i)
                       ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i)
                       ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i);
                cur++;
            }
            return(cur);
        }
Esempio n. 3
0
        private static int CheckInt(int loopCount)
        {
            int cur = 0;
            int i1  = loopCount;

            for (int i = 0; i < i1; i++)
            {
                cur ^= BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i)
                       ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i)
                       ^ BitTwiddling.QMax(cur, i) ^ BitTwiddling.QMax(cur, i);
                cur++;
            }
            return(cur);
        }
        public static long Next(this IRandomGenerator <ulong> randomGenerator, long lessThan)
        {
            if (lessThan <= 0L)
            {
                return(0);
            }
            ulong num1 = BitTwiddling.PowerOf2Mask((ulong)lessThan);
            long  num2;

            do
            {
                num2 = (long)randomGenerator.Next() & (long)num1;
            }while (num2 >= lessThan);
            return(num2);
        }
        public static int Next(this IRandomGenerator <uint> randomGenerator, int lessThan)
        {
            if (lessThan <= 0)
            {
                return(0);
            }
            uint num1 = BitTwiddling.PowerOf2Mask((uint)lessThan);
            int  num2;

            do
            {
                num2 = (int)randomGenerator.Next() & (int)num1;
            }while (num2 >= lessThan);
            return(num2);
        }
Esempio n. 6
0
        public static SimpleInstruction StrengthReductionDivision(InstructionNode node)
        {
            if (!(node.Instruction == IRInstruction.DivUnsigned32 ||
                  node.Instruction == IRInstruction.DivUnsigned64))
            {
                return(null);
            }

            var result = node.Result;
            var op1    = node.Operand1;
            var op2    = node.Operand2;

            if (!op2.IsResolvedConstant)
            {
                return(null);
            }

            if (op2.IsConstantZero || op2.IsVirtualRegister)
            {
                return(null);
            }

            if ((node.Instruction == IRInstruction.DivUnsigned32 || node.Instruction == IRInstruction.DivUnsigned64) && BitTwiddling.IsPowerOfTwo(op2.ConstantUnsignedLongInteger))
            {
                int shift = BitTwiddling.GetPowerOfTwo(op2.ConstantUnsignedLongInteger);

                if (shift < 32 || (shift < 64 && result.Is64BitInteger))
                {
                    return(new SimpleInstruction()
                    {
                        Instruction = Select(result.Is64BitInteger, IRInstruction.ShiftRight32, IRInstruction.ShiftRight64),
                        Result = result,
                        Operand1 = op1,
                        Operand2 = ConstantOperand.Create(result.Type, (uint)shift)
                    });
                }
            }

            return(null);
        }
Esempio n. 7
0
        public static SimpleInstruction StrengthReductionRemUnsignedModulus(InstructionNode node)
        {
            if (!(node.Instruction == IRInstruction.RemUnsigned32 ||
                  node.Instruction == IRInstruction.RemUnsigned64))
            {
                return(null);
            }

            var result = node.Result;
            var op1    = node.Operand1;
            var op2    = node.Operand2;

            if (!op2.IsResolvedConstant)
            {
                return(null);
            }

            if (op2.ConstantUnsignedLongInteger == 0)
            {
                return(null);
            }

            if (!BitTwiddling.IsPowerOfTwo(op2.ConstantUnsignedLongInteger))
            {
                return(null);
            }

            int power = BitTwiddling.GetPowerOfTwo(op2.ConstantUnsignedLongInteger);

            var mask = (1 << power) - 1;

            return(new SimpleInstruction()
            {
                Instruction = Select(result.Is64BitInteger, IRInstruction.LogicalAnd32, IRInstruction.LogicalAnd64),
                Result = result,
                Operand1 = op1,
                Operand2 = ConstantOperand.Create(result.Type, (uint)mask)
            });
        }
Esempio n. 8
0
        ///////////////////////////////////////////////////////////////////////////
        //    Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3    //
        ///////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///     Takes as input a CSV data values and sets the corresponding
        ///     structure's fields or properties from the CSV data.
        /// </summary>
        /// <typeparam name="T">the type of the structure</typeparam>
        /// <param name="kvp">a CSV string</param>
        /// <param name="structure">the structure to set the fields and properties for</param>
        public static T wasCSVToStructure <T>(this T structure, string kvp, Func <string, string> escape)
        {
            CSV.ToKeyValue(kvp).ToDictionary(o => escape.Invoke(o.Key), o => escape.Invoke(o.Value)).AsParallel().ForAll(d =>
            {
                var info = structure.GetFPInfo(d.Key);

                if (info == null)
                {
                    return;
                }

                var data = wasSharpNET.Reflection.wasGetInfoValue(info, structure);

                // OpenMetaverse particular flags.

                // LL permissions.
                if (data is Permissions)
                {
                    wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, Inventory.wasStringToPermissions(d.Value));
                }

                if (data is ParcelFlags)
                {
                    ParcelFlags parcelFlags;
                    switch (!Enum.TryParse(d.Value, out parcelFlags))
                    {
                    case true:
                        var allFlags =
                            typeof(ParcelFlags).GetFields(BindingFlags.Public | BindingFlags.Static)
                            .ToDictionary(o => o.Name, o => (ParcelFlags)o.GetValue(null));
                        CSV.ToEnumerable(d.Value).AsParallel().Where(o => !string.IsNullOrEmpty(o)).ForAll(
                            o =>
                        {
                            ParcelFlags parcelFlag;
                            if (allFlags.TryGetValue(o, out parcelFlag))
                            {
                                BitTwiddling.SetMaskFlag(ref parcelFlags, parcelFlag);
                            }
                        });
                        break;
                    }
                    wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, parcelFlags);
                    return;
                }

                if (data is GroupPowers)
                {
                    GroupPowers groupPowers;
                    switch (!Enum.TryParse(d.Value, out groupPowers))
                    {
                    case true:
                        var allPowers =
                            typeof(GroupPowers).GetFields(BindingFlags.Public | BindingFlags.Static)
                            .ToDictionary(o => o.Name, o => (GroupPowers)o.GetValue(null));
                        CSV.ToEnumerable(d.Value).AsParallel().Where(o => !string.IsNullOrEmpty(o)).ForAll(
                            o =>
                        {
                            GroupPowers groupPower;
                            if (allPowers.TryGetValue(o, out groupPower))
                            {
                                BitTwiddling.SetMaskFlag(ref groupPowers, groupPower);
                            }
                        });
                        break;
                    }
                    wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, groupPowers);
                    return;
                }
                if (data is AttachmentPoint)
                {
                    byte attachmentPoint;
                    switch (!byte.TryParse(d.Value, out attachmentPoint))
                    {
                    case true:
                        var attachmentPointFieldInfo =
                            typeof(AttachmentPoint).GetFields(BindingFlags.Public | BindingFlags.Static)
                            .AsParallel()
                            .FirstOrDefault(p => string.Equals(d.Value, p.Name, StringComparison.Ordinal));
                        if (attachmentPointFieldInfo == null)
                        {
                            break;
                        }
                        attachmentPoint = (byte)attachmentPointFieldInfo.GetValue(null);
                        break;
                    }
                    wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, attachmentPoint);
                    return;
                }
                if (data is Tree)
                {
                    byte tree;
                    switch (!byte.TryParse(d.Value, out tree))
                    {
                    case true:
                        var treeFieldInfo = typeof(Tree).GetFields(BindingFlags.Public |
                                                                   BindingFlags.Static)
                                            .AsParallel()
                                            .FirstOrDefault(p => string.Equals(d.Value, p.Name, StringComparison.Ordinal));
                        if (treeFieldInfo == null)
                        {
                            break;
                        }
                        tree = (byte)treeFieldInfo.GetValue(null);
                        break;
                    }
                    wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, tree);
                    return;
                }
                if (data is Material)
                {
                    byte material;
                    switch (!byte.TryParse(d.Value, out material))
                    {
                    case true:
                        var materialFieldInfo = typeof(Material).GetFields(BindingFlags.Public |
                                                                           BindingFlags.Static)
                                                .AsParallel()
                                                .FirstOrDefault(p => string.Equals(d.Value, p.Name, StringComparison.Ordinal));
                        if (materialFieldInfo == null)
                        {
                            break;
                        }
                        material = (byte)materialFieldInfo.GetValue(null);
                        break;
                    }
                    wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, material);
                    return;
                }
                if (data is PathCurve)
                {
                    byte pathCurve;
                    switch (!byte.TryParse(d.Value, out pathCurve))
                    {
                    case true:
                        var pathCurveFieldInfo = typeof(PathCurve).GetFields(BindingFlags.Public |
                                                                             BindingFlags.Static)
                                                 .AsParallel()
                                                 .FirstOrDefault(p => string.Equals(d.Value, p.Name, StringComparison.Ordinal));
                        if (pathCurveFieldInfo == null)
                        {
                            break;
                        }
                        pathCurve = (byte)pathCurveFieldInfo.GetValue(null);
                        break;
                    }
                    wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, pathCurve);
                    return;
                }
                if (data is PCode)
                {
                    byte pCode;
                    switch (!byte.TryParse(d.Value, out pCode))
                    {
                    case true:
                        var pCodeFieldInfo = typeof(PCode).GetFields(BindingFlags.Public | BindingFlags.Static)
                                             .AsParallel()
                                             .FirstOrDefault(p => string.Equals(d.Value, p.Name, StringComparison.Ordinal));
                        if (pCodeFieldInfo == null)
                        {
                            break;
                        }
                        pCode = (byte)pCodeFieldInfo.GetValue(null);
                        break;
                    }
                    wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, pCode);
                    return;
                }
                if (data is ProfileCurve)
                {
                    byte profileCurve;
                    switch (!byte.TryParse(d.Value, out profileCurve))
                    {
                    case true:
                        var profileCurveFieldInfo =
                            typeof(ProfileCurve).GetFields(BindingFlags.Public | BindingFlags.Static)
                            .AsParallel()
                            .FirstOrDefault(p => string.Equals(d.Value, p.Name, StringComparison.Ordinal));
                        if (profileCurveFieldInfo == null)
                        {
                            break;
                        }
                        profileCurve = (byte)profileCurveFieldInfo.GetValue(null);
                        break;
                    }
                    wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, profileCurve);
                    return;
                }
                if (data is HoleType)
                {
                    byte holeType;
                    switch (!byte.TryParse(d.Value, out holeType))
                    {
                    case true:
                        var holeTypeFieldInfo = typeof(HoleType).GetFields(BindingFlags.Public |
                                                                           BindingFlags.Static)
                                                .AsParallel()
                                                .FirstOrDefault(p => string.Equals(d.Value, p.Name, StringComparison.Ordinal));
                        if (holeTypeFieldInfo == null)
                        {
                            break;
                        }
                        holeType = (byte)holeTypeFieldInfo.GetValue(null);
                        break;
                    }
                    wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, holeType);
                    return;
                }
                if (data is SculptType)
                {
                    byte sculptType;
                    switch (!byte.TryParse(d.Value, out sculptType))
                    {
                    case true:
                        var sculptTypeFieldInfo = typeof(SculptType).GetFields(BindingFlags.Public |
                                                                               BindingFlags.Static)
                                                  .AsParallel()
                                                  .FirstOrDefault(p => string.Equals(d.Value, p.Name, StringComparison.Ordinal));
                        if (sculptTypeFieldInfo == null)
                        {
                            break;
                        }
                        sculptType = (byte)sculptTypeFieldInfo.GetValue(null);
                        break;
                    }
                    wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, sculptType);
                    return;
                }
                // OpenMetaverse Primitive Types
                if (data is UUID)
                {
                    UUID UUIDData;
                    if (!UUID.TryParse(d.Value, out UUIDData))
                    {
                        return;
                    }

                    wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, UUIDData);
                    return;
                }
                if (data is Vector3)
                {
                    Vector3 vector3Data;
                    if (Vector3.TryParse(d.Value, out vector3Data))
                    {
                        wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, vector3Data);
                        return;
                    }
                }
                if (data is Vector2)
                {
                    Vector3 vector2Data;
                    if (Vector2.TryParse(d.Value, out vector2Data))
                    {
                        wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, vector2Data);
                        return;
                    }
                }
                if (data is Vector3d)
                {
                    Vector3d vector3DData;
                    if (Vector3d.TryParse(d.Value, out vector3DData))
                    {
                        wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, vector3DData);
                        return;
                    }
                }
                if (data is Vector4)
                {
                    Vector4 vector4Data;
                    if (Vector4.TryParse(d.Value, out vector4Data))
                    {
                        wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, vector4Data);
                        return;
                    }
                }
                if (data is Quaternion)
                {
                    Quaternion quaternionData;
                    if (Quaternion.TryParse(d.Value, out quaternionData))
                    {
                        wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, quaternionData);
                        return;
                    }
                }
                // Primitive types.
                if (data is bool)
                {
                    bool boolData;
                    if (bool.TryParse(d.Value, out boolData))
                    {
                        wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, boolData);
                        return;
                    }
                }
                if (data is char)
                {
                    char charData;
                    if (char.TryParse(d.Value, out charData))
                    {
                        wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, charData);
                        return;
                    }
                }
                if (data is decimal)
                {
                    decimal decimalData;
                    if (decimal.TryParse(d.Value, out decimalData))
                    {
                        wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, decimalData);
                        return;
                    }
                }
                if (data is byte)
                {
                    byte byteData;
                    if (byte.TryParse(d.Value, out byteData))
                    {
                        wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, byteData);
                        return;
                    }
                }
                if (data is int)
                {
                    int intData;
                    if (int.TryParse(d.Value, NumberStyles.Integer, Utils.EnUsCulture, out intData))
                    {
                        wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, intData);
                        return;
                    }
                }
                if (data is uint)
                {
                    uint uintData;
                    if (uint.TryParse(d.Value, NumberStyles.Integer, Utils.EnUsCulture, out uintData))
                    {
                        wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, uintData);
                        return;
                    }
                }
                if (data is float)
                {
                    float floatData;
                    if (float.TryParse(d.Value, NumberStyles.Float, Utils.EnUsCulture, out floatData))
                    {
                        wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, floatData);
                        return;
                    }
                }
                if (data is long)
                {
                    long longData;
                    if (long.TryParse(d.Value, NumberStyles.Float, Utils.EnUsCulture, out longData))
                    {
                        wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, longData);
                        return;
                    }
                }
                if (data is DateTime)
                {
                    DateTime dateTimeData;
                    if (DateTime.TryParse(d.Value, out dateTimeData))
                    {
                        wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, dateTimeData);
                        return;
                    }
                }
                if (data is string)
                {
                    wasSharpNET.Reflection.wasSetInfoValue(info, ref structure, d.Value);
                }
            });

            return(structure);
        }
Esempio n. 9
0
 public static ulong NextPowerOf2(ulong v)
 {
     v = BitTwiddling.PowerOf2Mask(v);
     ++v;
     return(v);
 }
Esempio n. 10
0
 public static uint NextPowerOf2(uint v)
 {
     v = BitTwiddling.PowerOf2Mask(v);
     ++v;
     return(v);
 }
Esempio n. 11
0
 protected static uint GetPowerOfTwo(ulong value)
 {
     return(BitTwiddling.GetPowerOfTwo(value));
 }
Esempio n. 12
0
 protected static ulong GetLowestSetBit(ulong value)
 {
     return((ulong)BitTwiddling.GetLowestSetBit(value));
 }
Esempio n. 13
0
 protected static bool IsPowerOfTwo64(Operand operand)
 {
     return(BitTwiddling.IsPowerOfTwo(operand.ConstantUnsigned64));
 }