Skip to content

Commit 6eb53e9

Browse files
Move FastClamp to Numerics
1 parent f177b91 commit 6eb53e9

28 files changed

Lines changed: 56 additions & 55 deletions

src/ImageSharp/ColorSpaces/Cmyk.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public Cmyk(float c, float m, float y, float k)
5959
[MethodImpl(InliningOptions.ShortMethod)]
6060
public Cmyk(Vector4 vector)
6161
{
62-
vector = Vector4Utilities.FastClamp(vector, Min, Max);
62+
vector = Numerics.Clamp(vector, Min, Max);
6363
this.C = vector.X;
6464
this.M = vector.Y;
6565
this.Y = vector.Z;

src/ImageSharp/Common/Helpers/Numerics.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,19 @@ public static double Clamp(double value, double min, double max)
253253
return value;
254254
}
255255

256+
/// <summary>
257+
/// Returns the value clamped to the inclusive range of min and max.
258+
/// 5x Faster than <see cref="Vector4.Clamp(Vector4, Vector4, Vector4)"/>
259+
/// on platforms &lt; NET 5.
260+
/// </summary>
261+
/// <param name="value">The value to clamp.</param>
262+
/// <param name="min">The minimum inclusive value.</param>
263+
/// <param name="max">The maximum inclusive value.</param>
264+
/// <returns>The clamped <see cref="Vector4"/>.</returns>
265+
[MethodImpl(InliningOptions.ShortMethod)]
266+
public static Vector4 Clamp(Vector4 value, Vector4 min, Vector4 max)
267+
=> Vector4.Min(Vector4.Max(value, min), max);
268+
256269
/// <summary>
257270
/// Clamps the span values to the inclusive range of min and max.
258271
/// </summary>

src/ImageSharp/Common/Helpers/SimdUtils.FallbackIntrinsics128.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ internal static void NormalizedFloatToByteSaturate(
125125
Vector4 s = Unsafe.Add(ref sBase, i);
126126
s *= maxBytes;
127127
s += half;
128-
s = Vector4Utilities.FastClamp(s, Vector4.Zero, maxBytes);
128+
s = Numerics.Clamp(s, Vector4.Zero, maxBytes);
129129

130130
ref ByteVector4 d = ref Unsafe.Add(ref dBase, i);
131131
d.X = (byte)s.X;

src/ImageSharp/Common/Helpers/SimdUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static bool HasAvx2
5151
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5252
internal static Vector4 PseudoRound(this Vector4 v)
5353
{
54-
Vector4 sign = Vector4Utilities.FastClamp(v, new Vector4(-1), new Vector4(1));
54+
Vector4 sign = Numerics.Clamp(v, new Vector4(-1), new Vector4(1));
5555

5656
return v + (sign * 0.5f);
5757
}

src/ImageSharp/Common/Helpers/Vector4Utilities.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,6 @@ internal static class Vector4Utilities
2020
private const int BlendAlphaControl = 0b_10_00_10_00;
2121
private const int ShuffleAlphaControl = 0b_11_11_11_11;
2222

23-
/// <summary>
24-
/// Restricts a vector between a minimum and a maximum value.
25-
/// 5x Faster then <see cref="Vector4.Clamp(Vector4, Vector4, Vector4)"/>.
26-
/// </summary>
27-
/// <param name="x">The vector to restrict.</param>
28-
/// <param name="min">The minimum value.</param>
29-
/// <param name="max">The maximum value.</param>
30-
/// <returns>The <see cref="Vector4"/>.</returns>
31-
[MethodImpl(InliningOptions.ShortMethod)]
32-
public static Vector4 FastClamp(Vector4 x, Vector4 min, Vector4 max)
33-
=> Vector4.Min(Vector4.Max(x, min), max);
34-
3523
/// <summary>
3624
/// Pre-multiplies the "x", "y", "z" components of a vector by its "w" component leaving the "w" component intact.
3725
/// </summary>

src/ImageSharp/Formats/Jpeg/Components/Block8x8F.Generated.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ public void NormalizeColorsInPlace(float maximum)
1919
var CMax4 = new Vector4(maximum);
2020
var COff4 = new Vector4(MathF.Ceiling(maximum / 2));
2121

22-
this.V0L = Vector4Utilities.FastClamp(this.V0L + COff4, CMin4, CMax4);
23-
this.V0R = Vector4Utilities.FastClamp(this.V0R + COff4, CMin4, CMax4);
24-
this.V1L = Vector4Utilities.FastClamp(this.V1L + COff4, CMin4, CMax4);
25-
this.V1R = Vector4Utilities.FastClamp(this.V1R + COff4, CMin4, CMax4);
26-
this.V2L = Vector4Utilities.FastClamp(this.V2L + COff4, CMin4, CMax4);
27-
this.V2R = Vector4Utilities.FastClamp(this.V2R + COff4, CMin4, CMax4);
28-
this.V3L = Vector4Utilities.FastClamp(this.V3L + COff4, CMin4, CMax4);
29-
this.V3R = Vector4Utilities.FastClamp(this.V3R + COff4, CMin4, CMax4);
30-
this.V4L = Vector4Utilities.FastClamp(this.V4L + COff4, CMin4, CMax4);
31-
this.V4R = Vector4Utilities.FastClamp(this.V4R + COff4, CMin4, CMax4);
32-
this.V5L = Vector4Utilities.FastClamp(this.V5L + COff4, CMin4, CMax4);
33-
this.V5R = Vector4Utilities.FastClamp(this.V5R + COff4, CMin4, CMax4);
34-
this.V6L = Vector4Utilities.FastClamp(this.V6L + COff4, CMin4, CMax4);
35-
this.V6R = Vector4Utilities.FastClamp(this.V6R + COff4, CMin4, CMax4);
36-
this.V7L = Vector4Utilities.FastClamp(this.V7L + COff4, CMin4, CMax4);
37-
this.V7R = Vector4Utilities.FastClamp(this.V7R + COff4, CMin4, CMax4);
22+
this.V0L = Numerics.Clamp(this.V0L + COff4, CMin4, CMax4);
23+
this.V0R = Numerics.Clamp(this.V0R + COff4, CMin4, CMax4);
24+
this.V1L = Numerics.Clamp(this.V1L + COff4, CMin4, CMax4);
25+
this.V1R = Numerics.Clamp(this.V1R + COff4, CMin4, CMax4);
26+
this.V2L = Numerics.Clamp(this.V2L + COff4, CMin4, CMax4);
27+
this.V2R = Numerics.Clamp(this.V2R + COff4, CMin4, CMax4);
28+
this.V3L = Numerics.Clamp(this.V3L + COff4, CMin4, CMax4);
29+
this.V3R = Numerics.Clamp(this.V3R + COff4, CMin4, CMax4);
30+
this.V4L = Numerics.Clamp(this.V4L + COff4, CMin4, CMax4);
31+
this.V4R = Numerics.Clamp(this.V4R + COff4, CMin4, CMax4);
32+
this.V5L = Numerics.Clamp(this.V5L + COff4, CMin4, CMax4);
33+
this.V5R = Numerics.Clamp(this.V5R + COff4, CMin4, CMax4);
34+
this.V6L = Numerics.Clamp(this.V6L + COff4, CMin4, CMax4);
35+
this.V6R = Numerics.Clamp(this.V6R + COff4, CMin4, CMax4);
36+
this.V7L = Numerics.Clamp(this.V7L + COff4, CMin4, CMax4);
37+
this.V7R = Numerics.Clamp(this.V7R + COff4, CMin4, CMax4);
3838
}
3939

4040
/// <summary>

src/ImageSharp/Formats/Jpeg/Components/Block8x8F.Generated.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components
4141
for (int j = 0; j < 2; j++)
4242
{
4343
char side = j == 0 ? 'L' : 'R';
44-
Write($"this.V{i}{side} = Vector4Utilities.FastClamp(this.V{i}{side} + COff4, CMin4, CMax4);\r\n");
44+
Write($"this.V{i}{side} = Numerics.Clamp(this.V{i}{side} + COff4, CMin4, CMax4);\r\n");
4545
}
4646
}
4747
PopIndent();

src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ private static Vector<float> NormalizeAndRound(Vector<float> row, Vector<float>
671671
private static Vector4 DivideRound(Vector4 dividend, Vector4 divisor)
672672
{
673673
// sign(dividend) = max(min(dividend, 1), -1)
674-
Vector4 sign = Vector4Utilities.FastClamp(dividend, NegativeOne, Vector4.One);
674+
Vector4 sign = Numerics.Clamp(dividend, NegativeOne, Vector4.One);
675675

676676
// AlmostRound(dividend/divisor) = dividend/divisor + 0.5*sign(dividend)
677677
return (dividend / divisor) + (sign * Offset);

src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ private void Pack(ref Vector4 vector)
373373
{
374374
vector *= MaxBytes;
375375
vector += Half;
376-
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, MaxBytes);
376+
vector = Numerics.Clamp(vector, Vector4.Zero, MaxBytes);
377377

378378
this.R = (byte)vector.X;
379379
this.G = (byte)vector.Y;

src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ private void Pack(ref Vector4 vector)
296296
{
297297
vector *= MaxBytes;
298298
vector += Half;
299-
vector = Vector4Utilities.FastClamp(vector, Vector4.Zero, MaxBytes);
299+
vector = Numerics.Clamp(vector, Vector4.Zero, MaxBytes);
300300

301301
this.R = (byte)vector.X;
302302
this.G = (byte)vector.Y;

0 commit comments

Comments
 (0)