From 6622ddf91991d4c21d6c869fcdab3a2a99b23bd0 Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Thu, 6 Apr 2017 16:44:23 +0300 Subject: [PATCH] Naive attempt to reduce branching --- dotnet/WhiteRabbit/PhraseSet.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/dotnet/WhiteRabbit/PhraseSet.cs b/dotnet/WhiteRabbit/PhraseSet.cs index 6bed70a..b952a8a 100644 --- a/dotnet/WhiteRabbit/PhraseSet.cs +++ b/dotnet/WhiteRabbit/PhraseSet.cs @@ -15,22 +15,23 @@ { var permutation = permutations[offset + i]; var startPointer = bufferPointer + i * 8; - byte[] currentWord = words[permutation[0]]; + byte[] currentWord; var j = 0; var wordIndex = 0; var currentPointer = (byte*)startPointer; byte* lastPointer = currentPointer + length; + for (; currentPointer < lastPointer; currentPointer++) { - if (j >= currentWord.Length) - { - j = 0; - wordIndex++; - currentWord = words[permutation[wordIndex]]; - } - + currentWord = words[permutation[wordIndex]]; *currentPointer = currentWord[j]; + j++; + + // 0xffffffff if greater than or equal, 0 if less than + var comparisonResult = unchecked((int)((((uint)j - (uint)currentWord.Length) >> 31) - 1)); + j = (comparisonResult & 0) | (~comparisonResult & j); + wordIndex = (comparisonResult & (wordIndex + 1)) | (~comparisonResult & wordIndex); } *currentPointer = 128;