From 9ae92244f1146c2a0eafaa39199b2242ea641960 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 21 Jan 2017 07:57:13 +1100 Subject: [PATCH] Faster FastAbs --- src/ImageSharp/Common/Helpers/ImageMaths.cs | 3 ++- tests/ImageSharp.Benchmarks/General/Abs.cs | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/Common/Helpers/ImageMaths.cs b/src/ImageSharp/Common/Helpers/ImageMaths.cs index 147a5914b..fc94c689f 100644 --- a/src/ImageSharp/Common/Helpers/ImageMaths.cs +++ b/src/ImageSharp/Common/Helpers/ImageMaths.cs @@ -25,7 +25,8 @@ namespace ImageSharp [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int FastAbs(int x) { - return (x ^ (x >> 31)) - (x >> 31); + int y = x >> 31; + return (x ^ y) - y; } /// diff --git a/tests/ImageSharp.Benchmarks/General/Abs.cs b/tests/ImageSharp.Benchmarks/General/Abs.cs index b0f033b81..b5b449467 100644 --- a/tests/ImageSharp.Benchmarks/General/Abs.cs +++ b/tests/ImageSharp.Benchmarks/General/Abs.cs @@ -29,5 +29,13 @@ int x = this.X; 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; + } } }