mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 04:09:13 +00:00
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.
This commit is contained in:
parent
62f84e94c8
commit
c6fafd3e90
Notes:
sideshowbarker
2024-07-18 10:06:28 +09:00
Author: https://github.com/BertalanD
Commit: c6fafd3e90
Pull-request: https://github.com/SerenityOS/serenity/pull/8470
Reviewed-by: https://github.com/Dexesttp
Reviewed-by: https://github.com/Hendiadyoin1
Reviewed-by: https://github.com/alimpfard
Reviewed-by: https://github.com/gunnarbeutner ✅
5 changed files with 29 additions and 9 deletions
|
@ -482,7 +482,7 @@ void FlexFormattingContext::run(Box& box, LayoutMode)
|
|||
|
||||
if (sum_of_unfrozen_flex_items_flex_factors < 1) {
|
||||
auto intermediate_free_space = initial_free_space * sum_of_unfrozen_flex_items_flex_factors;
|
||||
if (abs(intermediate_free_space) < abs(remaining_free_space))
|
||||
if (AK::abs(intermediate_free_space) < AK::abs(remaining_free_space))
|
||||
remaining_free_space = intermediate_free_space;
|
||||
}
|
||||
|
||||
|
@ -503,7 +503,7 @@ void FlexFormattingContext::run(Box& box, LayoutMode)
|
|||
|
||||
for_each_unfrozen_item([&](FlexItem* flex_item) {
|
||||
float ratio = flex_item->scaled_flex_shrink_factor / sum_of_scaled_flex_shrink_factor_of_unfrozen_items;
|
||||
flex_item->target_main_size = flex_item->flex_base_size - (abs(remaining_free_space) * ratio);
|
||||
flex_item->target_main_size = flex_item->flex_base_size - (AK::abs(remaining_free_space) * ratio);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue