Skip to content

Commit

Permalink
Added branchless Math.Min calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Turnerj committed Jun 19, 2020
1 parent 662f80f commit a8d7fdf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 34 deletions.
16 changes: 16 additions & 0 deletions src/Quickenshtein/Internal/AdvOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;

namespace Quickenshtein.Internal
{
internal static class AdvOperations
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int MathMin(int x, int y)
{
return y + ((x - y) & ((x - y) >> (sizeof(int) * 8 - 1)));
}
}
}
68 changes: 34 additions & 34 deletions src/Quickenshtein/Levenshtein.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ internal static unsafe void CalculateRow(int* previousRowPtr, char* targetPtr, i
lastDeletionCost = previousRowPtr[0];
if (sourcePrevChar != targetPtr[0])
{
localCost = Math.Min(lastInsertionCost, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(lastInsertionCost, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
lastInsertionCost = localCost;
Expand All @@ -49,8 +49,8 @@ internal static unsafe void CalculateRow(int* previousRowPtr, char* targetPtr, i
lastDeletionCost = previousRowPtr[0];
if (sourcePrevChar != targetPtr[0])
{
localCost = Math.Min(lastInsertionCost, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(lastInsertionCost, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
lastInsertionCost = localCost;
Expand All @@ -63,8 +63,8 @@ internal static unsafe void CalculateRow(int* previousRowPtr, char* targetPtr, i
lastDeletionCost = previousRowPtr[0];
if (sourcePrevChar != targetPtr[0])
{
localCost = Math.Min(lastInsertionCost, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(lastInsertionCost, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
lastInsertionCost = localCost;
Expand All @@ -77,8 +77,8 @@ internal static unsafe void CalculateRow(int* previousRowPtr, char* targetPtr, i
lastDeletionCost = previousRowPtr[0];
if (sourcePrevChar != targetPtr[0])
{
localCost = Math.Min(lastInsertionCost, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(lastInsertionCost, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
lastInsertionCost = localCost;
Expand All @@ -91,8 +91,8 @@ internal static unsafe void CalculateRow(int* previousRowPtr, char* targetPtr, i
lastDeletionCost = previousRowPtr[0];
if (sourcePrevChar != targetPtr[0])
{
localCost = Math.Min(lastInsertionCost, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(lastInsertionCost, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
lastInsertionCost = localCost;
Expand All @@ -105,8 +105,8 @@ internal static unsafe void CalculateRow(int* previousRowPtr, char* targetPtr, i
lastDeletionCost = previousRowPtr[0];
if (sourcePrevChar != targetPtr[0])
{
localCost = Math.Min(lastInsertionCost, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(lastInsertionCost, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
lastInsertionCost = localCost;
Expand All @@ -119,8 +119,8 @@ internal static unsafe void CalculateRow(int* previousRowPtr, char* targetPtr, i
lastDeletionCost = previousRowPtr[0];
if (sourcePrevChar != targetPtr[0])
{
localCost = Math.Min(lastInsertionCost, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(lastInsertionCost, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
lastInsertionCost = localCost;
Expand All @@ -133,8 +133,8 @@ internal static unsafe void CalculateRow(int* previousRowPtr, char* targetPtr, i
lastDeletionCost = previousRowPtr[0];
if (sourcePrevChar != targetPtr[0])
{
localCost = Math.Min(lastInsertionCost, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(lastInsertionCost, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
lastInsertionCost = localCost;
Expand All @@ -152,8 +152,8 @@ internal static unsafe void CalculateRow(int* previousRowPtr, char* targetPtr, i
lastDeletionCost = previousRowPtr[0];
if (sourcePrevChar != targetPtr[0])
{
localCost = Math.Min(lastInsertionCost, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(lastInsertionCost, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
lastInsertionCost = localCost;
Expand All @@ -166,8 +166,8 @@ internal static unsafe void CalculateRow(int* previousRowPtr, char* targetPtr, i
lastDeletionCost = previousRowPtr[0];
if (sourcePrevChar != targetPtr[0])
{
localCost = Math.Min(lastInsertionCost, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(lastInsertionCost, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
lastInsertionCost = localCost;
Expand All @@ -180,8 +180,8 @@ internal static unsafe void CalculateRow(int* previousRowPtr, char* targetPtr, i
lastDeletionCost = previousRowPtr[0];
if (sourcePrevChar != targetPtr[0])
{
localCost = Math.Min(lastInsertionCost, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(lastInsertionCost, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
lastInsertionCost = localCost;
Expand All @@ -194,8 +194,8 @@ internal static unsafe void CalculateRow(int* previousRowPtr, char* targetPtr, i
lastDeletionCost = previousRowPtr[0];
if (sourcePrevChar != targetPtr[0])
{
localCost = Math.Min(lastInsertionCost, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(lastInsertionCost, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
lastInsertionCost = localCost;
Expand All @@ -213,8 +213,8 @@ internal static unsafe void CalculateRow(int* previousRowPtr, char* targetPtr, i
lastDeletionCost = previousRowPtr[0];
if (sourcePrevChar != targetPtr[0])
{
localCost = Math.Min(lastInsertionCost, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(lastInsertionCost, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
lastInsertionCost = localCost;
Expand Down Expand Up @@ -307,35 +307,35 @@ private static unsafe void CalculateColumn_4Rows(ref char* targetPtr, ref int* p
var localCost = row1Costs;
if (sourceChar1 != targetChar)
{
localCost = Math.Min(row2Costs, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(row2Costs, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
row1Costs = lastDeletionCost;
lastDeletionCost = localCost;
localCost = row2Costs;
if (sourceChar2 != targetChar)
{
localCost = Math.Min(row3Costs, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(row3Costs, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
row2Costs = lastDeletionCost;
lastDeletionCost = localCost;
localCost = row3Costs;
if (sourceChar3 != targetChar)
{
localCost = Math.Min(row4Costs, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(row4Costs, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
row3Costs = lastDeletionCost;
lastDeletionCost = localCost;
localCost = row4Costs;
if (sourceChar4 != targetChar)
{
localCost = Math.Min(row5Costs, localCost);
localCost = Math.Min(lastDeletionCost, localCost);
localCost = AdvOperations.MathMin(row5Costs, localCost);
localCost = AdvOperations.MathMin(lastDeletionCost, localCost);
localCost++;
}
row4Costs = lastDeletionCost;
Expand Down

0 comments on commit a8d7fdf

Please sign in to comment.