From a154b211a5c7be988cb551c019246be1810fbbd7 Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Wed, 26 Apr 2017 17:05:19 +0300 Subject: [PATCH] PhraseSet filling moved out to separate method --- dotnet/WhiteRabbit/PhraseSet.cs | 75 ++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/dotnet/WhiteRabbit/PhraseSet.cs b/dotnet/WhiteRabbit/PhraseSet.cs index 1bad330..f409ccd 100644 --- a/dotnet/WhiteRabbit/PhraseSet.cs +++ b/dotnet/WhiteRabbit/PhraseSet.cs @@ -15,51 +15,58 @@ fixed (long* bufferPointer = this.Buffer) { - long* longBuffer = (long*)bufferPointer; - int numberOfWords = wordIndexes.Length; - fixed (ulong* permutationsPointer = permutations) { - var currentPermutationPointer = permutationsPointer + permutationOffset; - for (var i = 0; i < Constants.PhrasesPerSet; i++, currentPermutationPointer++) + fixed (int* wordIndexesPointer = wordIndexes) { - var permutation = *currentPermutationPointer; - if (permutation == 0) - { - continue; - } - - var cumulativeWordOffsetX4 = 0; - for (var j = 0; j < numberOfWords; j++) - { - var currentWord = allWords[wordIndexes[permutation & 15]]; - permutation = permutation >> 4; - longBuffer[0] |= currentWord.Buffers[cumulativeWordOffsetX4 + 0]; - longBuffer[1] |= currentWord.Buffers[cumulativeWordOffsetX4 + 1]; - longBuffer[2] ^= currentWord.Buffers[cumulativeWordOffsetX4 + 2]; - longBuffer[3] ^= currentWord.Buffers[cumulativeWordOffsetX4 + 3]; - cumulativeWordOffsetX4 += currentWord.LengthX4; - } - - longBuffer += 4; + FillPhraseSet(bufferPointer, allWords, wordIndexesPointer, permutationsPointer, permutationOffset, numberOfCharacters, wordIndexes.Length); } } + } + } + + private static unsafe void FillPhraseSet(long* bufferPointer, Word[] allWords, int* wordIndexes, ulong* permutationsPointer, int permutationOffset, int numberOfCharacters, int numberOfWords) + { + long* longBuffer = (long*)bufferPointer; - var length = numberOfCharacters + numberOfWords - 1; - byte* byteBuffer = ((byte*)bufferPointer) + length; - for (var i = 0; i < Constants.PhrasesPerSet; i++) + var currentPermutationPointer = permutationsPointer + permutationOffset; + for (var i = 0; i < Constants.PhrasesPerSet; i++, currentPermutationPointer++) + { + var permutation = *currentPermutationPointer; + if (permutation == 0) { - *byteBuffer = 128; - byteBuffer += 32; + continue; } - var lengthInBits = (uint)(length << 3); - uint* uintBuffer = ((uint*)bufferPointer) + 7; - for (var i = 0; i < Constants.PhrasesPerSet; i++) + var cumulativeWordOffsetX4 = 0; + for (var j = 0; j < numberOfWords; j++) { - *uintBuffer = lengthInBits; - uintBuffer += 8; + var currentWord = allWords[wordIndexes[permutation & 15]]; + permutation = permutation >> 4; + longBuffer[0] |= currentWord.Buffers[cumulativeWordOffsetX4 + 0]; + longBuffer[1] |= currentWord.Buffers[cumulativeWordOffsetX4 + 1]; + longBuffer[2] ^= currentWord.Buffers[cumulativeWordOffsetX4 + 2]; + longBuffer[3] ^= currentWord.Buffers[cumulativeWordOffsetX4 + 3]; + cumulativeWordOffsetX4 += currentWord.LengthX4; } + + longBuffer += 4; + } + + var length = numberOfCharacters + numberOfWords - 1; + byte* byteBuffer = ((byte*)bufferPointer) + length; + for (var i = 0; i < Constants.PhrasesPerSet; i++) + { + *byteBuffer = 128; + byteBuffer += 32; + } + + var lengthInBits = (uint)(length << 3); + uint* uintBuffer = ((uint*)bufferPointer) + 7; + for (var i = 0; i < Constants.PhrasesPerSet; i++) + { + *uintBuffer = lengthInBits; + uintBuffer += 8; } }