From 81c727679fcf2011c75d10a77c4ca3208a383377 Mon Sep 17 00:00:00 2001 From: Ynse Hoornenborg Date: Mon, 9 Dec 2024 20:19:13 +0100 Subject: [PATCH] Add shift operators to Point struct --- src/ImageSharp/Primitives/Point.cs | 36 +++++++++++++++++++ .../ImageSharp.Tests/Primitives/PointTests.cs | 23 +++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/Primitives/Point.cs b/src/ImageSharp/Primitives/Point.cs index 8ace7ffacf..8f817fe3c1 100644 --- a/src/ImageSharp/Primitives/Point.cs +++ b/src/ImageSharp/Primitives/Point.cs @@ -144,6 +144,24 @@ public struct Point : IEquatable public static Point operator /(Point left, int right) => new(left.X / right, left.Y / right); + /// + /// Shift to the right by a amount producing . + /// + /// Shifted value of type . + /// Shifted amount of type . + /// Result of type . + public static Point operator >>(Point left, int right) + => new(left.X >> right, left.Y >> right); + + /// + /// Shift to the left by a amount producing . + /// + /// Shifted value of type . + /// Shifted amount of type . + /// Result of type . + public static Point operator <<(Point left, int right) + => new(left.X << right, left.Y << right); + /// /// Compares two objects for equality. /// @@ -267,6 +285,24 @@ public struct Point : IEquatable [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Offset(Point point) => this.Offset(point.X, point.Y); + /// + /// Shifts the coordinate value of this to the right with the specified amount. + /// + /// The point to shift. + /// The number of bits to shift to the right. + /// The . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Point ShiftRight(Point point, int bitCount) => new(unchecked(point.X >> bitCount), unchecked(point.Y >> bitCount)); + + /// + /// Shifts the coordinate value of this to the left with the specified amount. + /// + /// The point to shift. + /// The number of bits to shift to the left. + /// The . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Point ShiftLeft(Point point, int bitCount) => new(unchecked(point.X << bitCount), unchecked(point.Y << bitCount)); + /// public override int GetHashCode() => HashCode.Combine(this.X, this.Y); diff --git a/tests/ImageSharp.Tests/Primitives/PointTests.cs b/tests/ImageSharp.Tests/Primitives/PointTests.cs index f5c64abf52..2921b6fb8b 100644 --- a/tests/ImageSharp.Tests/Primitives/PointTests.cs +++ b/tests/ImageSharp.Tests/Primitives/PointTests.cs @@ -4,7 +4,7 @@ using System.Globalization; using System.Numerics; -namespace SixLabors.ImageSharp.Tests; +namespace SixLabors.ImageSharp.Tests.Primitives; public class PointTests { @@ -112,6 +112,27 @@ public class PointTests Assert.Equal(subExpected, Point.Subtract(p, s)); } + [Theory] + [InlineData(int.MaxValue, int.MaxValue, 5)] + [InlineData(int.MinValue, int.MinValue, 4)] + [InlineData(int.MaxValue, int.MaxValue, 2)] + [InlineData(0, 0, 3)] + public void ShiftTest(int x, int y, int s) + { + Point rightExpected, leftExpected, p = new Point(x, y); + + unchecked + { + rightExpected = new Point(x >> s, y >> s); + leftExpected = new Point(x << s, y << s); + } + + Assert.Equal(rightExpected, p >> s); + Assert.Equal(leftExpected, p << s); + Assert.Equal(rightExpected, Point.ShiftRight(p, s)); + Assert.Equal(leftExpected, Point.ShiftLeft(p, s)); + } + [Theory] [InlineData(float.MaxValue, float.MinValue)] [InlineData(float.MinValue, float.MinValue)]