summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx
diff options
context:
space:
mode:
authorDaniel Bertalan <dani@danielbertalan.dev>2021-07-05 18:38:17 +0200
committerGunnar Beutner <gunnar@beutner.name>2021-07-08 10:11:00 +0200
commitc6fafd3e90b05e02dd313e93e3d4455f59e46d4d (patch)
treebab16aaf468f0033b38e7e60492a52d8f05264fc /Userland/Libraries/LibGfx
parent62f84e94c8a362a6b3bb4827cb818dee11d8ecf3 (diff)
downloadserenity-c6fafd3e90b05e02dd313e93e3d4455f59e46d4d.zip
AK+Userland: Add generic `AK::abs()` function and use it
Previously, in LibGFX's `Point` class, calculated distances were passed to the integer `abs` function, even if the stored type was a float. This caused the value to unexpectedly be truncated. Luckily, this API was not used with floating point types, but that can change in the future, so why not fix it now :^) Since we are in C++, we can use function overloading to make things easy, and to automatically use the right version. This is even better than the LibC/LibM functions, as using a bit of hackery, they are able to be constant-evaluated. They use compiler intrinsics, so they do not depend on external code and the compiler can emit the most optimized code by default. Since we aren't using the C++ standard library's trick of importing everything into the `AK` namespace, this `abs` function cannot be exported to the global namespace, as the names would clash.
Diffstat (limited to 'Userland/Libraries/LibGfx')
-rw-r--r--Userland/Libraries/LibGfx/Point.h5
1 files changed, 2 insertions, 3 deletions
diff --git a/Userland/Libraries/LibGfx/Point.h b/Userland/Libraries/LibGfx/Point.h
index c5a3c54be4..1d1f8f2df9 100644
--- a/Userland/Libraries/LibGfx/Point.h
+++ b/Userland/Libraries/LibGfx/Point.h
@@ -13,7 +13,6 @@
#include <LibGfx/Orientation.h>
#include <LibIPC/Forward.h>
#include <math.h>
-#include <stdlib.h>
namespace Gfx {
@@ -217,7 +216,7 @@ public:
// Returns pixels moved from other in either direction
[[nodiscard]] T pixels_moved(Point<T> const& other) const
{
- return max(abs(dx_relative_to(other)), abs(dy_relative_to(other)));
+ return max(AK::abs(dx_relative_to(other)), AK::abs(dy_relative_to(other)));
}
[[nodiscard]] float distance_from(Point<T> const& other) const
@@ -229,7 +228,7 @@ public:
[[nodiscard]] Point absolute_relative_distance_to(Point const& other) const
{
- return { abs(dx_relative_to(other)), abs(dy_relative_to(other)) };
+ return { AK::abs(dx_relative_to(other)), AK::abs(dy_relative_to(other)) };
}
template<typename U>