Browse Source

Faster FastAbs

af/merge-core
James Jackson-South 9 years ago
parent
commit
9ae92244f1
  1. 3
      src/ImageSharp/Common/Helpers/ImageMaths.cs
  2. 8
      tests/ImageSharp.Benchmarks/General/Abs.cs

3
src/ImageSharp/Common/Helpers/ImageMaths.cs

@ -25,7 +25,8 @@ namespace ImageSharp
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int FastAbs(int x) public static int FastAbs(int x)
{ {
return (x ^ (x >> 31)) - (x >> 31); int y = x >> 31;
return (x ^ y) - y;
} }
/// <summary> /// <summary>

8
tests/ImageSharp.Benchmarks/General/Abs.cs

@ -29,5 +29,13 @@
int x = this.X; int x = this.X;
return (x ^ (x >> 31)) - (x >> 31); return (x ^ (x >> 31)) - (x >> 31);
} }
[Benchmark(Description = "Bitwise Abs With Variable")]
public int AbsBitwiseVer()
{
int x = this.X;
int y = x >> 31;
return (x ^ y) - y;
}
} }
} }

Loading…
Cancel
Save