Browse Source

Expand Clamp benchmark [skip ci]

pull/97/head
James Jackson-South 9 years ago
parent
commit
660eecf0f2
  1. 63
      tests/ImageSharp.Benchmarks/General/Clamp.cs

63
tests/ImageSharp.Benchmarks/General/Clamp.cs

@ -6,6 +6,7 @@
namespace ImageSharp.Benchmarks.General namespace ImageSharp.Benchmarks.General
{ {
using System; using System;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Attributes;
@ -17,24 +18,24 @@ namespace ImageSharp.Benchmarks.General
[Benchmark(Baseline = true, Description = "Maths Clamp")] [Benchmark(Baseline = true, Description = "Maths Clamp")]
public byte ClampMaths() public byte ClampMaths()
{ {
int value = this.Value; int value = this.Value;
return (byte)Math.Min(Math.Max(0, value), 255); return (byte)Math.Min(Math.Max(byte.MinValue, value), byte.MaxValue);
} }
[Benchmark(Description = "No Maths Clamp")] [Benchmark(Description = "No Maths Clamp")]
public byte ClampNoMaths() public byte ClampNoMaths()
{ {
int value = this.Value; int value = this.Value;
value = value >= 255 ? 255 : value; value = value >= byte.MaxValue ? byte.MaxValue : value;
return (byte)(value <= 0 ? 0 : value); return (byte)(value <= byte.MinValue ? byte.MinValue : value);
} }
[Benchmark(Description = "No Maths No Equals Clamp")] [Benchmark(Description = "No Maths No Equals Clamp")]
public byte ClampNoMathsNoEquals() public byte ClampNoMathsNoEquals()
{ {
int value = this.Value; int value = this.Value;
value = value > 255 ? 255 : value; value = value > byte.MaxValue ? byte.MaxValue : value;
return (byte)(value < 0 ? 0 : value); return (byte)(value < byte.MinValue ? byte.MinValue : value);
} }
[Benchmark(Description = "No Maths Clamp No Ternary")] [Benchmark(Description = "No Maths Clamp No Ternary")]
@ -42,14 +43,14 @@ namespace ImageSharp.Benchmarks.General
{ {
int value = this.Value; int value = this.Value;
if (value >= 255) if (value >= byte.MaxValue)
{ {
return 255; return byte.MaxValue;
} }
if (value <= 0) if (value <= byte.MinValue)
{ {
return 0; return byte.MinValue;
} }
return (byte)value; return (byte)value;
@ -58,19 +59,37 @@ namespace ImageSharp.Benchmarks.General
[Benchmark(Description = "No Maths No Equals Clamp No Ternary")] [Benchmark(Description = "No Maths No Equals Clamp No Ternary")]
public byte ClampNoMathsEqualsNoTernary() public byte ClampNoMathsEqualsNoTernary()
{ {
int value = this.Value; int value = this.Value;
if (value > 255) if (value > byte.MaxValue)
{ {
return 255; return byte.MaxValue;
} }
if (value < 0) if (value < byte.MinValue)
{ {
return 0; return byte.MinValue;
} }
return (byte)value; return (byte)value;
}
[Benchmark(Description = "Clamp using Bitwise Abs")]
public byte ClampBitwise()
{
int x = this.Value;
int absmax = byte.MaxValue - x;
x = (x + byte.MaxValue - AbsBitwiseVer(ref absmax)) >> 1;
x = (x + byte.MinValue + AbsBitwiseVer(ref x)) >> 1;
return (byte)x;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int AbsBitwiseVer(ref int x)
{
int y = x >> 31;
return (x ^ y) - y;
} }
} }
} }

Loading…
Cancel
Save