public unsafe override int GetHashCode() { int length = _value.Length; int hash = length; fixed(byte *ap = _value) { byte *a = ap; while (length >= 4) { hash = (hash + BitOps.RotateLeft(hash, 5)) ^ *(int *)a; a += 4; length -= 4; } if (length >= 2) { hash = (hash + BitOps.RotateLeft(hash, 5)) ^ *(short *)a; a += 2; length -= 2; } if (length > 0) { hash = (hash + BitOps.RotateLeft(hash, 5)) ^ *a; } hash += BitOps.RotateLeft(hash, 7); hash += BitOps.RotateLeft(hash, 15); return(hash); } }
private static void ConvertRowsAndColumns7To8(byte[] Data, byte[] Result, ref int tPos, ref int rPos, bool IsArea) { //Rows int RowAndFlags = BitOps.GetWord(Data, tPos); int RowAndFlags2 = 0; BitOps.SetWord(Result, rPos, RowAndFlags & 0x3FFF); rPos += 2; tPos += 2; if (IsArea) { RowAndFlags2 = BitOps.GetWord(Data, tPos); BitOps.SetWord(Result, rPos, RowAndFlags2 & 0x3FFF); rPos += 2; tPos += 2; } //Columns Result[rPos] = Data[tPos]; rPos++; tPos++; Result[rPos] = (byte)((RowAndFlags >> 8) & (~0x3F)); rPos++; if (IsArea) { Result[rPos] = Data[tPos]; rPos++; tPos++; Result[rPos] = (byte)((RowAndFlags2 >> 8) & (~0x3F)); rPos++; } }
public int ToHashCode() { int hash1 = _hash1 + BitOps.RotateLeft(_hash1, 8); int hash2 = _hash2 + BitOps.RotateLeft(_hash2, 8); return(hash1 ^ hash2); }
private byte[] GetXFData(byte[] Data) { byte[] Result = new byte[20]; Array.Copy(Data, 0, Result, 0, 2); //Font Index int PxlOffset = PxlVersion == TPxlVersion.v10 ? 2 : 0; int Findex = BitOps.GetWord(Data, 2 + PxlOffset); // if (Findex > 230 && Findex < 0xFFF0) Findex -= 230; Index < 230 are internal BitOps.SetWord(Result, 2, Findex);//Format Index. This is not valid Biff8 Format index, but it will be fixed at MergeFromPxlXF Result[4] = 1; Result[9] = 0xFC; byte VAlign = (byte)((((Data[10 + PxlOffset] >> 4) & 0x3) - 1) & 0x3); Result[6] = (byte)((Data[10 + PxlOffset] & 0xF) | (VAlign << 4)); //Text Attributes. Vertical Align is -1 if (Data[14] != 0xFF) { Result[17] = ((int)TFlxPatternStyle.Solid) << 1; Result[18] = (byte)(GetColor(Data[14]) | 0x80); //background color | 1 bit fgcolor } else { Result[18] = 0xC0; } Result[19] = 0x20; //Pattern fore color, not used on pxl. if (PxlVersion != TPxlVersion.v20) //border color { BitOps.SetWord(Result, 14, (0x40 << 7) | (0x40)); BitOps.SetWord(Result, 12, (0x40 << 7) | (0x40)); } else { BitOps.SetWord(Result, 14, (GetColor(Data[18]) << 7) | (GetColor(Data[16]))); BitOps.SetWord(Result, 12, (GetColor(Data[19]) << 7) | (GetColor(Data[17]))); } if ((Data[11 + PxlOffset] & 0x2) != 0) { Result[10] = 1; //Has left border } if ((Data[11 + PxlOffset] & 0x8) != 0) { Result[10] |= (1 << 4); //Has right border } if ((Data[11 + PxlOffset] & 0x1) != 0) { Result[11] |= 1; //Has top border } if ((Data[11 + PxlOffset] & 0x4) != 0) { Result[11] |= (1 << 4); //Has bottom border } return(Result); }
internal bool CheckHeader() { BofData = new byte[5]; if (DataStream.Length - DataStream.Position < BofData.Length) { return(false); } ShRead(DataStream, BofData, 0, BofData.Length); if (BofData[0] != (int)pxl.BOF) { return(false); } int VersionNumber = BitOps.GetWord(BofData, 1); if (VersionNumber != 0x0009 && VersionNumber != 0x010F && VersionNumber != 0x010E) { return(false); } int StreamType = BitOps.GetWord(BofData, 3); if (StreamType != 0x005) { return(false); } return(true); }
private static bool Convert3D8To7(TReferences References, byte Token, byte[] Data, byte[] ResultData, ref int tPos, ref int rPos) { BitOps.SetWord(ResultData, rPos, 0xFFFF); rPos += 2; Array.Clear(ResultData, rPos, 8); //reserved. rPos += 8; int Sheet1, Sheet2; bool HasExternal; string ExternBookName; References.GetSheetsFromExternSheet(BitOps.GetWord(Data, tPos), out Sheet1, out Sheet2, out HasExternal, out ExternBookName); tPos += 2; if (Sheet1 > FlxConsts.Max_PxlSheets || Sheet2 > FlxConsts.Max_PxlSheets) { return(false); } if (HasExternal) { return(false); } BitOps.SetWord(ResultData, rPos, Sheet1); rPos += 2; BitOps.SetWord(ResultData, rPos, Sheet2); rPos += 2; return(true); }
public static unsafe int ComputeASCIINameHashCode(byte *data, int length, out bool isAscii) { int hash1 = 0x6DA3B944; int hash2 = 0; int asciiMask = 0; for (int i = 0; i < length; i += 2) { int b1 = data[i]; asciiMask |= b1; hash1 = (hash1 + BitOps.RotateLeft(hash1, 5)) ^ b1; if ((i + 1) < length) { int b2 = data[i]; asciiMask |= b2; hash2 = (hash2 + BitOps.RotateLeft(hash2, 5)) ^ b2; } } hash1 += BitOps.RotateLeft(hash1, 8); hash2 += BitOps.RotateLeft(hash2, 8); isAscii = (asciiMask & 0x80) == 0; return(hash1 ^ hash2); }
/// <summary> /// CoreCLR <a href="https://github.com/dotnet/coreclr/blob/030e0af89bb897554acef575075c69aaf5176268/src/vm/typehashingalgorithms.h#L15">ComputeNameHashCode</a> /// </summary> /// <param name="name">Name string to hash</param> public static int NameHashCode(string name) { if (string.IsNullOrEmpty(name)) { return(0); } int hash1 = 0x6DA3B944; int hash2 = 0; // DIFFERENT FROM CORERT: We hash UTF-8 bytes here, while CoreRT hashes UTF-16 characters. byte[] src = Encoding.UTF8.GetBytes(name); for (int i = 0; i < src.Length; i += 2) { hash1 = unchecked (hash1 + BitOps.RotateLeft(hash1, 5)) ^ src[i]; if (i + 1 < src.Length) { hash2 = unchecked (hash2 + BitOps.RotateLeft(hash2, 5)) ^ src[i + 1]; } else { break; } } hash1 = unchecked (hash1 + BitOps.RotateLeft(hash1, 8)); hash2 = unchecked (hash2 + BitOps.RotateLeft(hash2, 8)); return(unchecked ((int)(hash1 ^ hash2))); }
public void Append(string src) { if (src.Length == 0) { return; } int startIndex = 0; if ((_numCharactersHashed & 1) == 1) { _hash2 = (_hash2 + BitOps.RotateLeft(_hash2, 5)) ^ src[0]; startIndex = 1; } for (int i = startIndex; i < src.Length; i += 2) { _hash1 = (_hash1 + BitOps.RotateLeft(_hash1, 5)) ^ src[i]; if ((i + 1) < src.Length) { _hash2 = (_hash2 + BitOps.RotateLeft(_hash2, 5)) ^ src[i + 1]; } } _numCharactersHashed += src.Length; }
public void BitsRequiredTest() { for (uint i = 0; i < 1200; i++) { var req = BitOps.RequiredBits(i); Assert.Greater(1 << req, i, "i: " + i); } }
private static bool IsFile(byte[] Data, int Flags, int pos) { if ((Flags & 0x01) == 0x01 && (Flags & 0x100) == 0) { return(BitOps.CompareMem(FILEGUID, Data, pos)); } return(false); }
/// <summary> /// Releases one or more particles. /// </summary> /// <param name="currentTime">The current time.</param> /// <param name="amount">The number of particles to release.</param> void ReleaseParticles(TickCount currentTime, int amount) { // Find how many we can actually release var lastIndex = Math.Min(Budget - 1, _lastAliveIndex + amount); // Ensure our particles array is large enough to fit the new particles. // When we resize the array, we use the "next power of two" sizing concept to reduce the // memory fragmentation (.NET internally does the same with most collections). To speed things up, // we just find the next power of two instead of looping until we have a large enough value. if (_particles.Length - 1 < lastIndex) { var newSize = BitOps.NextPowerOf2(lastIndex + 1); Debug.Assert(BitOps.IsPowerOf2(newSize), "If this assert fails, something is probably wrong with BitOps.NextPowerOf2() or BitOps.IsPowerOf2()."); Debug.Assert(newSize >= lastIndex + 1); Array.Resize(ref _particles, newSize); } // Start releasing the particles var hasReleaseModifiers = ParticleModifiers.HasReleaseModifiers; for (var i = _lastAliveIndex + 1; i <= lastIndex; i++) { var particle = _particles[i]; if (particle == null) { particle = Particle.Create(); _particles[i] = particle; } // Set up the particle particle.Momentum = Vector2.Zero; particle.LifeStart = currentTime; particle.LifeEnd = (TickCount)(currentTime + ParticleLife.GetNext()); particle.Rotation = ReleaseRotation.GetNext(); particle.Scale = ReleaseScale.GetNext(); ReleaseColor.GetNext(ref particle.Color); // Get the offset and force Vector2 offset; Vector2 force; GenerateParticleOffsetAndForce(particle, out offset, out force); // Set the position Vector2.Add(ref _origin, ref offset, out particle.Position); // Set the velocity Vector2.Multiply(ref force, ReleaseSpeed.GetNext(), out particle.Velocity); if (hasReleaseModifiers) { ParticleModifiers.ProcessReleasedParticle(this, particle); } } // Increase the index of the last active particle _lastAliveIndex = lastIndex; }
internal TNoteRecord(int aId, byte[] aData) : base(aId, BitOps.GetWord(aData, 2)) { OptionFlags = BitConverter.ToUInt16(aData, 4); ObjId = BitConverter.ToUInt16(aData, 6); long aSize = 0; StrOps.GetSimpleString(true, aData, 8, false, 0, ref Author, ref aSize); }
/// <summary> /// CoreCLR <a href="https://github.com/dotnet/coreclr/blob/030e0af89bb897554acef575075c69aaf5176268/src/vm/typehashingalgorithms.h#L88">ComputeGenericInstanceHashCode</a> /// </summary> /// <param name="hashcode">Base hash code</param> /// <param name="instantiation">Instantiation to include in the hash</param> private static int GenericInstanceHashCode(int hashcode, Instantiation instantiation) { for (int i = 0; i < instantiation.Length; i++) { int argumentHashCode = TypeHashCode(instantiation[i]); hashcode = unchecked (hashcode + BitOps.RotateLeft(hashcode, 13)) ^ argumentHashCode; } return(unchecked (hashcode + BitOps.RotateLeft(hashcode, 15))); }
private static byte[] ReadStandardSalt(byte[] VerifierBytes) { int SaltSize = (int)BitOps.GetCardinal(VerifierBytes, 0); byte[] Salt = new byte[SaltSize]; Array.Copy(VerifierBytes, 4, Salt, 0, Salt.Length); return(Salt); }
private static const long[] AntiDiagonalMasks8 = new long[] { 0x80L, 0x8040L, 0x804020L, 0x80402010L, 0x8040201008L, 0x804020100804L, 0x80402010080402L, unchecked ((long)0x8040201008040201L), 0x4020100804020100L, 0x2010080402010000L, 0x1008040201000000L, 0x804020100000000L, 0x402010000000000L, 0x201000000000000L, 0x100000000000000L }; //from top right to bottom left private static long HAndVMoves(int s) { //REMINDER: requires OCCUPIED to be up to date long binaryS = 1L << s; long possibilitiesHorizontal = (OCCUPIED - 2 * binaryS) ^ BitOps.Reverse(BitOps.Reverse(OCCUPIED) - 2 * BitOps.Reverse(binaryS)); long possibilitiesVertical = ((OCCUPIED & FileMasks8[s % 8]) - (2 * binaryS)) ^ BitOps.Reverse(BitOps.Reverse(OCCUPIED & FileMasks8[s % 8]) - (2 * BitOps.Reverse(binaryS))); return((possibilitiesHorizontal & RankMasks8[s / 8]) | (possibilitiesVertical & FileMasks8[s % 8])); }
private TScreenTipRecord(int aId, byte[] aData) : this(aId) { FirstRow = BitOps.GetWord(aData, 2); LastRow = BitOps.GetWord(aData, 4); FirstCol = BitOps.GetWord(aData, 6); LastCol = BitOps.GetWord(aData, 8); FText = Encoding.Unicode.GetString(aData, 10, aData.Length - 10 - 2); }
private static byte[] ConvertToBiff8(TExternSheetList ExternSheetList, byte Token, byte[] Data, ref int tPos) { byte[] Result; int rPos = 0; switch (TBaseParsedToken.CalcBaseToken((ptg)Token)) { case ptg.Name: Result = new byte[4]; //Wrong on Excel docs! BitOps.SetWord(Result, 0, BitOps.GetWord(Data, tPos)); tPos += 14; return(Result); case ptg.NameX: Result = new byte[6]; //This is actually 6 BitOps.SetWord(Result, 2, BitOps.GetWord(Data, tPos + 10)); tPos += 24; return(Result); case ptg.Ref: case ptg.RefN: Result = new byte[4]; ConvertRowsAndColumns7To8(Data, Result, ref tPos, ref rPos, false); return(Result); case ptg.Area: case ptg.AreaN: Result = new byte[8]; ConvertRowsAndColumns7To8(Data, Result, ref tPos, ref rPos, true); return(Result); case ptg.RefErr: tPos += 3; return(new byte[4]); case ptg.AreaErr: tPos += 6; return(new byte[8]); case ptg.Ref3d: case ptg.Ref3dErr: Result = new byte[6]; Convert3D7To8(ExternSheetList, Token, Data, Result, ref tPos, ref rPos); ConvertRowsAndColumns7To8(Data, Result, ref tPos, ref rPos, false); return(Result); case ptg.Area3d: case ptg.Area3dErr: Result = new byte[10]; Convert3D7To8(ExternSheetList, Token, Data, Result, ref tPos, ref rPos); ConvertRowsAndColumns7To8(Data, Result, ref tPos, ref rPos, true); return(Result); } XlsMessages.ThrowException(XlsErr.ErrBadToken, Token); return(null); //just to compile. }
internal void WriteHeader(TOle2File OleFile) { OleFile.WriteRaw(PropHeader, PropHeader.Length); int SectCount = (int)BitOps.GetCardinal(PropHeader, 24); for (int i = 0; i < SectCount; i++) { OleFile.WriteRaw(FmtSection[i], FmtSection[i].Length); } }
private static int ViaSpan(uint value) { int log10 = Unsafe.AddByteOffset( ref MemoryMarshal.GetReference(s_Log10Ceiling32), (IntPtr)BitOps.LeadingZeroCount(value)); int diff = (int)((value - s_Pow10Ceiling32[log10]) >> 31); return((int)(log10 - diff)); }
/// <summary> /// Initializes a new instance of the <see cref="ImmutableHashMap<TKey, TValue>.HashBucket"/> class. /// </summary> /// <param name="hashRoll">The hash roll.</param> /// <param name="used">The used.</param> /// <param name="buckets">The buckets.</param> /// <param name="count">The count.</param> private HashBucket(int hashRoll, uint used, Bucket[] buckets, int count) { Debug.Assert(buckets != null); Debug.Assert(buckets.Length == (int)BitOps.PopCount(used)); _hashRoll = hashRoll & 31; _used = used; _buckets = buckets; _count = count; }
private static int[] GetOptChoose(byte[] RPN, int start, int endpos) { int[] Result = new int[BitOps.GetWord(RPN, start) + 1]; for (int i = 0; i < Result.Length; i++) { Result[i] = endpos + BitOps.GetWord(RPN, start + 2 * (i + 1)); } return(Result); }
public override bool Equals(object o) { TExcelString x = o as TExcelString; if (x == null) { return(false); } return((x.OptionFlags == OptionFlags) && (x.Data == Data) && (BitOps.CompareMem(x.RichTextFormats, RichTextFormats)) && (BitOps.CompareMem(x.FarEastData, x.FarEastData))); }
public static void BitOps_RotateRight_ulong() { ulong value = 0b01010101_01010101_01010101_01010101_01010101_01010101_01010101_01010101ul; Assert.Equal(0b10101010_10101010_10101010_10101010_10101010_10101010_10101010_10101010ul, BitOps.RotateRight(value, 1)); Assert.Equal(0b01010101_01010101_01010101_01010101_01010101_01010101_01010101_01010101ul, BitOps.RotateRight(value, 2)); Assert.Equal(0b10101010_10101010_10101010_10101010_10101010_10101010_10101010_10101010ul, BitOps.RotateRight(value, 3)); Assert.Equal(value, BitOps.RotateRight(value, int.MinValue)); // % 64 = 0 Assert.Equal(BitOps.RotateLeft(value, 63), BitOps.RotateRight(value, int.MaxValue)); // % 64 = 63 }
internal TPaneRecord(int aId, byte[] aData) : base() { Id = aId; ColSplit = BitOps.GetWord(aData, 0); RowSplit = BitOps.GetWord(aData, 2); FirstVisibleRow = BitOps.GetWord(aData, 4); FirstVisibleCol = BitOps.GetWord(aData, 6); ActivePane = BitOps.GetWord(aData, 8); }
internal static TPaletteRecord CreateStandard() { byte[] aData = new byte[2 + XlsConsts.HighColorPaletteRange * 4]; BitOps.SetWord(aData, 0, XlsConsts.HighColorPaletteRange); for (int i = XlsConsts.LowColorPaletteRange - 1; i < XlsConsts.HighColorPaletteRange; i++) { BitOps.SetCardinal(aData, 2 + i * 4, StandardPaletteData[i]); } return(new TPaletteRecord((int)xlr.PALETTE, aData)); }
public void BenchBitOps() { BitOps t = new BitOps(); t.adjust = 1; for (int i = 0; i < BitOpsIterations; i++) { t.Run(); } }
public static int ComputeGenericInstanceHashCode <ARG>(int genericDefinitionHashCode, ARG[] genericTypeArguments) { int hashcode = genericDefinitionHashCode; for (int i = 0; i < genericTypeArguments.Length; i++) { int argumentHashCode = genericTypeArguments[i].GetHashCode(); hashcode = (hashcode + BitOps.RotateLeft(hashcode, 13)) ^ argumentHashCode; } return(hashcode + BitOps.RotateLeft(hashcode, 15)); }
internal override byte[] GetFilePassRecord() { byte[] Result = new byte[GetFilePassRecordLen()]; BitOps.SetWord(Result, 0, (UInt16)(xlr.FILEPASS)); BitOps.SetWord(Result, 2, GetFilePassRecordLen() - XlsConsts.SizeOfTRecordHeader); BitOps.SetWord(Result, 4, 0); BitOps.SetWord(Result, 6, FKey); BitOps.SetWord(Result, 8, FHash); return(Result); }
/// <summary> /// Performs a heuristic guess for what might be a good starting size of the atlas. /// </summary> /// <param name="items">The items that will be going into the atlas.</param> /// <param name="maxSize">The maximum size of the atlas in either dimension.</param> /// <returns>The starting size to use for the atlas.</returns> /// <exception cref="ArgumentException"><paramref name="items"/> is null.</exception> static Point GetStartSize(IEnumerable <ITextureAtlasable> items, int maxSize) { if (items == null) { const string errmsg = "items is null."; if (log.IsFatalEnabled) { log.Fatal(errmsg); } throw new ArgumentException(errmsg, "items"); } var maxWidth = 0; var maxHeight = 0; var totalSize = 0; // Add the area of every AtlasItem along with find the largest width foreach (var item in items) { maxWidth = Math.Max(maxWidth, item.SourceRect.Width + Padding * 2); maxHeight = Math.Max(maxHeight, item.SourceRect.Height + Padding * 2); totalSize += item.SourceRect.Width * item.SourceRect.Height + Padding * 2; } // Get the guessed size to use var guessedSize = (int)Math.Sqrt(totalSize); // Check that the maxSize is able to fit the textures if (maxWidth > maxSize || maxHeight > maxSize) { const string errmsg = "One or more ITextureAtlases can not fit into the atlas with the given maximum texture size"; throw new ArgumentException(errmsg, "items"); } // Use the higher of the two values and round to the next power of 2 var width = BitOps.NextPowerOf2(Math.Max(guessedSize, maxWidth)); var height = BitOps.NextPowerOf2(Math.Max(guessedSize, maxHeight)); // If possible, divide one of the sizes in half if (width / 2 > maxWidth) { width /= 2; } else if (height / 2 > maxHeight) { height /= 2; } // Finally, force below the maximum size width = Math.Min(width, maxSize); height = Math.Min(height, maxSize); return(new Point(width, height)); }
public static void BenchBitOps() { Setup(); BitOps t = new BitOps(); t.adjust = 0; foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < BitOpsIterations; i++) { t.Run(); } } } }
public BitOps combine(BitOps y) { return y.bitOpsWith(this); }