Skip to content

Truly independent web browser

Everywhere: Use AK/Math.h over the C library's math functions

Shannon Boothf47205a8bed9
diff --git a/Libraries/LibGfx/ColorConversion.cpp b/Libraries/LibGfx/ColorConversion.cpp
index 0634b80c377..267981302ac 100644
--- a/Libraries/LibGfx/ColorConversion.cpp
+++ b/Libraries/LibGfx/ColorConversion.cpp
@@ -41,7 +41,7 @@ ColorComponents srgb_to_linear_srgb(ColorComponents const& srgb)
if (absolute <= 0.04045)
return static_cast<float>(c / 12.92);
- return static_cast<float>(sign * pow((absolute + 0.055) / 1.055, 2.4));
+ return static_cast<float>(sign * AK::pow<double>((absolute + 0.055) / 1.055, 2.4));
};
return { to_linear(srgb[0]), to_linear(srgb[1]), to_linear(srgb[2]), srgb.alpha() };
@@ -56,7 +56,7 @@ ColorComponents linear_srgb_to_srgb(ColorComponents const& linear)
float absolute = abs(c);
if (absolute > 0.0031308f)
- return sign * static_cast<float>(1.055 * pow(absolute, 1.0 / 2.4) - 0.055);
+ return sign * static_cast<float>(1.055 * AK::pow<double>(absolute, 1.0 / 2.4) - 0.055);
return 12.92f * c;
};
@@ -86,7 +86,7 @@ ColorComponents display_p3_to_linear_display_p3(ColorComponents const& p3)
if (absolute <= 0.04045f)
return c / 12.92f;
- return sign * static_cast<float>(pow((absolute + 0.055f) / 1.055f, 2.4));
+ return sign * static_cast<float>(AK::pow<double>((absolute + 0.055f) / 1.055f, 2.4));
};
return { to_linear(p3[0]), to_linear(p3[1]), to_linear(p3[2]), p3.alpha() };
@@ -100,7 +100,7 @@ ColorComponents a98rgb_to_xyz65(ColorComponents const& a98)
float sign = c < 0 ? -1.0f : 1.0f;
float absolute = abs(c);
- return sign * static_cast<float>(pow(absolute, 563.0 / 256.0));
+ return sign * static_cast<float>(AK::pow<double>(absolute, 563.0 / 256.0));
};
auto linear_r = to_linear(a98[0]);
@@ -125,7 +125,7 @@ ColorComponents pro_photo_rgb_to_xyz50(ColorComponents const& prophoto)
if (absolute <= 16.0f / 512.0f)
return c / 16.0f;
- return sign * static_cast<float>(pow(absolute, 1.8));
+ return sign * static_cast<float>(AK::pow<double>(absolute, 1.8));
};
auto linear_r = to_linear(prophoto[0]);
@@ -153,7 +153,7 @@ ColorComponents rec2020_to_xyz65(ColorComponents const& rec2020)
if (absolute < beta * 4.5)
return c / 4.5f;
- return sign * static_cast<float>(pow((absolute + alpha - 1) / alpha, 1 / 0.45));
+ return sign * static_cast<float>(AK::pow<double>((absolute + alpha - 1) / alpha, 1 / 0.45));
};
auto linear_r = to_linear(rec2020[0]);
@@ -209,7 +209,7 @@ ColorComponents lab_to_xyz50(ColorComponents const& lab)
};
float x = compute(f0);
- float y = L > kappa * epsilon ? static_cast<float>(pow((L + 16.0) / 116.0, 3)) : static_cast<float>(L / kappa);
+ float y = L > kappa * epsilon ? static_cast<float>(AK::pow<double>((L + 16.0) / 116.0, 3)) : static_cast<float>(L / kappa);
float z = compute(f2);
// D50
@@ -270,9 +270,9 @@ ColorComponents xyz65_to_oklab(ColorComponents const& components)
float y = components[1];
float z = components[2];
- float long_cone = cbrtf(0.8190224379967030f * x + 0.3619062600528904f * y - 0.1288737815209879f * z);
- float medium_cone = cbrtf(0.0329836539323885f * x + 0.9292868615863434f * y + 0.0361446663506424f * z);
- float short_cone = cbrtf(0.0481771893596242f * x + 0.2642395317527308f * y + 0.6335478284694309f * z);
+ float long_cone = AK::cbrt<float>(0.8190224379967030f * x + 0.3619062600528904f * y - 0.1288737815209879f * z);
+ float medium_cone = AK::cbrt<float>(0.0329836539323885f * x + 0.9292868615863434f * y + 0.0361446663506424f * z);
+ float short_cone = AK::cbrt<float>(0.0481771893596242f * x + 0.2642395317527308f * y + 0.6335478284694309f * z);
return {
0.2104542683093140f * long_cone + 0.7936177747023054f * medium_cone - 0.0040720430116193f * short_cone,
@@ -318,7 +318,7 @@ ColorComponents xyz50_to_lab(ColorComponents const& components)
auto f = [](float value) -> float {
if (value > epsilon)
- return cbrtf(value);
+ return AK::cbrt<float>(value);
return static_cast<float>((kappa * value + 16.0) / 116.0);
};
@@ -446,7 +446,7 @@ ColorComponents linear_a98_rgb_to_a98_rgb(ColorComponents const& components)
{
auto to_gamma = [](float c) {
double sign = c < 0.0f ? -1.0 : 1.0;
- return static_cast<float>(sign * pow(abs(c), 256.0 / 563.0));
+ return static_cast<float>(sign * AK::pow<double>(abs(c), 256.0 / 563.0));
};
return { to_gamma(components[0]), to_gamma(components[1]), to_gamma(components[2]), components.alpha() };
@@ -476,7 +476,7 @@ ColorComponents linear_prophoto_rgb_to_prophoto_rgb(ColorComponents const& compo
float absolute = abs(c);
if (absolute <= 1.0f / 512.0f)
return c * 16.0f;
- return sign * static_cast<float>(pow(absolute, 1.0 / 1.8));
+ return sign * static_cast<float>(AK::pow<double>(absolute, 1.0 / 1.8));
};
return { to_gamma(components[0]), to_gamma(components[1]), to_gamma(components[2]), components.alpha() };
@@ -512,7 +512,7 @@ ColorComponents linear_rec2020_to_rec2020(ColorComponents const& components)
if (absolute < beta)
return 4.5f * c;
- return sign * static_cast<float>(alpha * pow(absolute, 0.45) - (alpha - 1));
+ return sign * static_cast<float>(alpha * AK::pow<double>(absolute, 0.45) - (alpha - 1));
};
return { to_gamma(components[0]), to_gamma(components[1]), to_gamma(components[2]), components.alpha() };
diff --git a/Libraries/LibGfx/ColorConversion.h b/Libraries/LibGfx/ColorConversion.h
index e1eba41f7a3..6a20aebb760 100644
--- a/Libraries/LibGfx/ColorConversion.h
+++ b/Libraries/LibGfx/ColorConversion.h
@@ -207,9 +207,9 @@ constexpr ColorComponents linear_srgb_to_oklab(ColorComponents const& rgb)
float g = rgb[1];
float b = rgb[2];
- float l = cbrtf(0.4122214708f * r + 0.5363325363f * g + 0.0514459929f * b);
- float m = cbrtf(0.2119034982f * r + 0.6806995451f * g + 0.1073969566f * b);
- float s = cbrtf(0.0883024619f * r + 0.2817188376f * g + 0.6299787005f * b);
+ float l = AK::cbrt<float>(0.4122214708f * r + 0.5363325363f * g + 0.0514459929f * b);
+ float m = AK::cbrt<float>(0.2119034982f * r + 0.6806995451f * g + 0.1073969566f * b);
+ float s = AK::cbrt<float>(0.0883024619f * r + 0.2817188376f * g + 0.6299787005f * b);
return {
0.2104542553f * l + 0.7936177850f * m - 0.0040720468f * s,
diff --git a/Libraries/LibWeb/CSS/CSSSkew.cpp b/Libraries/LibWeb/CSS/CSSSkew.cpp
index 4c14e0a74a8..6312ba9916d 100644
--- a/Libraries/LibWeb/CSS/CSSSkew.cpp
+++ b/Libraries/LibWeb/CSS/CSSSkew.cpp
@@ -5,6 +5,7 @@
*/
#include "CSSSkew.h"
+#include <AK/Math.h>
#include <LibGC/Heap.h>
#include <LibWeb/CSS/CSSNumericValue.h>
#include <LibWeb/CSS/CSSUnitValue.h>
@@ -97,8 +98,8 @@ WebIDL::ExceptionOr<GC::Ref<Geometry::DOMMatrix>> CSSSkew::to_matrix() const
// NB: Gfx::Matrix4x4 is row by row, so the element m21 is in the first row and m12 in the second.
auto matrix = Gfx::DoubleMatrix4x4::identity();
- matrix[0, 1] = tanf(ax_rad);
- matrix[1, 0] = tanf(ay_rad);
+ matrix[0, 1] = AK::tan<float>(ax_rad);
+ matrix[1, 0] = AK::tan<float>(ay_rad);
return Geometry::DOMMatrix::create(matrix, is_2d());
}
diff --git a/Libraries/LibWeb/CSS/CSSSkewX.cpp b/Libraries/LibWeb/CSS/CSSSkewX.cpp
index f59bdd308b5..58de40f45ee 100644
--- a/Libraries/LibWeb/CSS/CSSSkewX.cpp
+++ b/Libraries/LibWeb/CSS/CSSSkewX.cpp
@@ -5,6 +5,7 @@
*/
#include "CSSSkewX.h"
+#include <AK/Math.h>
#include <LibGC/Heap.h>
#include <LibWeb/CSS/CSSNumericValue.h>
#include <LibWeb/CSS/CSSUnitValue.h>
@@ -79,7 +80,7 @@ WebIDL::ExceptionOr<GC::Ref<Geometry::DOMMatrix>> CSSSkewX::to_matrix() const
// NB: Gfx::Matrix4x4 is row by row, so the element m21 is in the first row.
auto matrix = Gfx::DoubleMatrix4x4::identity();
- matrix[0, 1] = tanf(ax_rad);
+ matrix[0, 1] = AK::tan<float>(ax_rad);
return Geometry::DOMMatrix::create(matrix, is_2d());
}
diff --git a/Libraries/LibWeb/CSS/CSSSkewY.cpp b/Libraries/LibWeb/CSS/CSSSkewY.cpp
index 9ecc87c8057..537e928b0d0 100644
--- a/Libraries/LibWeb/CSS/CSSSkewY.cpp
+++ b/Libraries/LibWeb/CSS/CSSSkewY.cpp
@@ -5,6 +5,7 @@
*/
#include "CSSSkewY.h"
+#include <AK/Math.h>
#include <LibGC/Heap.h>
#include <LibWeb/CSS/CSSNumericValue.h>
#include <LibWeb/CSS/CSSUnitValue.h>
@@ -79,7 +80,7 @@ WebIDL::ExceptionOr<GC::Ref<Geometry::DOMMatrix>> CSSSkewY::to_matrix() const
// NB: Gfx::Matrix4x4 is row by row, so the element m12 is in the second row.
auto matrix = Gfx::DoubleMatrix4x4::identity();
- matrix[1, 0] = tanf(ay_rad);
+ matrix[1, 0] = AK::tan<float>(ay_rad);
return Geometry::DOMMatrix::create(matrix, is_2d());
}
diff --git a/Libraries/LibWeb/HTML/Numbers.cpp b/Libraries/LibWeb/HTML/Numbers.cpp
index 9fd4b09b602..567ffb0c1bc 100644
--- a/Libraries/LibWeb/HTML/Numbers.cpp
+++ b/Libraries/LibWeb/HTML/Numbers.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Math.h>
#include <AK/StringConversions.h>
#include <AK/Utf16View.h>
#include <LibWeb/HTML/Numbers.h>
@@ -309,7 +310,7 @@ fraction_exit:
// NB: A zero value stays zero for any exponent. Skip the multiplication, as zero times an infinite power of
// ten would be NaN.
if (value != 0)
- value *= pow(10, exponent);
+ value *= AK::pow<double>(10, exponent);
}
conversion:
@@ -327,7 +328,7 @@ conversion:
double rounded_value = value;
// 17. If rounded-value is 2^1024 or −2^1024, return an error.
- if (abs(rounded_value) >= pow(2, 1024)) {
+ if (abs(rounded_value) >= AK::pow<double>(2, 1024)) {
return {};
}
diff --git a/Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp b/Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp
index d6b3550e07a..6546e776b88 100644
--- a/Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Math.h>
#include <LibWeb/SVG/AttributeNames.h>
#include <LibWeb/SVG/AttributeParsing.h>
#include <LibWeb/SVG/SVGComponentTransferFunctionElement.h>
@@ -234,7 +235,7 @@ ReadonlyBytes SVGComponentTransferFunctionElement::color_table()
auto exponent = this->exponent()->base_val();
auto offset = this->offset()->base_val();
for (int i = 0; i < 256; ++i)
- result.data()[i] = to_u8(amplitude * pow(i / 255.f, exponent) + offset);
+ result.data()[i] = to_u8(amplitude * AK::pow<float>(i / 255.f, exponent) + offset);
break;
}
}
diff --git a/Libraries/LibWeb/WebAudio/AnalyserNode.cpp b/Libraries/LibWeb/WebAudio/AnalyserNode.cpp
index adbfc7d4753..6bb382f3d54 100644
--- a/Libraries/LibWeb/WebAudio/AnalyserNode.cpp
+++ b/Libraries/LibWeb/WebAudio/AnalyserNode.cpp
@@ -111,7 +111,7 @@ Vector<f32> AnalyserNode::apply_a_blackman_window(Vector<f32> const& x) const
unsigned long const N = m_fft_size;
auto w = [&](unsigned long n) {
- return a0 - a1 * cos(2 * AK::Pi<f32> * (f32)n / (f32)N) + a2 * cos(4 * AK::Pi<f32> * (f32)n / (f32)N);
+ return a0 - a1 * AK::cos(2 * AK::Pi<f32> * (f32)n / (f32)N) + a2 * AK::cos(4 * AK::Pi<f32> * (f32)n / (f32)N);
};
Vector<f32> x_hat;
diff --git a/Libraries/LibWeb/WebDriver/Actions.cpp b/Libraries/LibWeb/WebDriver/Actions.cpp
index 04840c306c4..d2d52f1f0c7 100644
--- a/Libraries/LibWeb/WebDriver/Actions.cpp
+++ b/Libraries/LibWeb/WebDriver/Actions.cpp
@@ -1160,8 +1160,8 @@ static PointerInputData pointer_input_data(ActionObject::PointerFields const& ac
else if (azimuth > pi && azimuth < 2 * pi)
tilt_y = -90;
} else {
- tilt_x = atan(cos(azimuth) / tan(altitude)) * 180 / pi;
- tilt_y = atan(sin(azimuth) / tan(altitude)) * 180 / pi;
+ tilt_x = AK::atan(AK::cos(azimuth) / AK::tan(altitude)) * 180 / pi;
+ tilt_y = AK::atan(AK::sin(azimuth) / AK::tan(altitude)) * 180 / pi;
}
properties.tilt_x = action.tilt_x.value_or(static_cast<i32>(floor(tilt_x + 0.5)));
properties.tilt_y = action.tilt_y.value_or(static_cast<i32>(floor(tilt_y + 0.5)));
@@ -1176,7 +1176,7 @@ static PointerInputData pointer_input_data(ActionObject::PointerFields const& ac
else if (tilt_y == 0)
properties.azimuth_angle = tilt_x < 0 ? pi : 0;
else if (abs(tilt_x) != 90 && abs(tilt_y) != 90) {
- properties.azimuth_angle = atan2(tan(radians_y), tan(radians_x));
+ properties.azimuth_angle = AK::atan2(AK::tan(radians_y), AK::tan(radians_x));
if (properties.azimuth_angle < 0)
properties.azimuth_angle += 2 * pi;
}
@@ -1187,9 +1187,9 @@ static PointerInputData pointer_input_data(ActionObject::PointerFields const& ac
else if (tilt_y == 0)
properties.altitude_angle = pi / 2 - fabs(radians_x);
else {
- auto tangent_x = tan(radians_x);
- auto tangent_y = tan(radians_y);
- properties.altitude_angle = atan(1 / sqrt(tangent_x * tangent_x + tangent_y * tangent_y));
+ auto tangent_x = AK::tan(radians_x);
+ auto tangent_y = AK::tan(radians_y);
+ properties.altitude_angle = AK::atan(1 / sqrt(tangent_x * tangent_x + tangent_y * tangent_y));
}
}
return PointerInputData { properties };
diff --git a/Services/Compositor/ContextPresenter.cpp b/Services/Compositor/ContextPresenter.cpp
index a146dd5f98f..2110f00a603 100644
--- a/Services/Compositor/ContextPresenter.cpp
+++ b/Services/Compositor/ContextPresenter.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Math.h>
#include <Compositor/CanvasSurfaceRegistry.h>
#include <Compositor/ContextPresenter.h>
#include <Compositor/ContextScene.h>
@@ -178,8 +179,8 @@ bool ContextPresenter::update_composited_raster_transform(Gfx::FloatRect destina
float scale_x = 1;
float scale_y = 1;
if (!has_perspective) {
- scale_x = hypotf(transform[0, 0], transform[1, 0]) * destination_rect.width() / m_viewport_size.width();
- scale_y = hypotf(transform[0, 1], transform[1, 1]) * destination_rect.height() / m_viewport_size.height();
+ scale_x = AK::hypot<float>(transform[0, 0], transform[1, 0]) * destination_rect.width() / m_viewport_size.width();
+ scale_y = AK::hypot<float>(transform[0, 1], transform[1, 1]) * destination_rect.height() / m_viewport_size.height();
}
if (!isfinite(scale_x) || !isfinite(scale_y))
return false;
diff --git a/Services/Compositor/SkiaUtils.cpp b/Services/Compositor/SkiaUtils.cpp
index d7da78a055d..9b244e0618d 100644
--- a/Services/Compositor/SkiaUtils.cpp
+++ b/Services/Compositor/SkiaUtils.cpp
@@ -327,8 +327,8 @@ sk_sp<SkImageFilter> SkiaFilterReader::filter()
auto angle_degrees = value<float>();
auto input = optional_filter();
float radians = AK::to_radians(angle_degrees);
- auto cosA = cos(radians);
- auto sinA = sin(radians);
+ auto cosA = AK::cos(radians);
+ auto sinA = AK::sin(radians);
auto a00 = 0.213f + cosA * 0.787f - sinA * 0.213f;
auto a01 = 0.715f - cosA * 0.715f - sinA * 0.715f;
diff --git a/UI/Qt/Icon.cpp b/UI/Qt/Icon.cpp
index 0225f0c976c..a3dbc624fe5 100644
--- a/UI/Qt/Icon.cpp
+++ b/UI/Qt/Icon.cpp
@@ -5,6 +5,7 @@
*/
#include <AK/HashMap.h>
+#include <AK/Math.h>
#include <LibCore/Resource.h>
#include <UI/Qt/ChromeStyle.h>
#include <UI/Qt/Icon.h>
@@ -151,7 +152,7 @@ static QPainterPath parse_svg_path(StringView data)
auto cy = cyp + (current.y() + end.y()) / 2;
auto angle = [](double ux, double uy, double vx, double vy) {
- return atan2((ux * vy) - (uy * vx), (ux * vx) + (uy * vy));
+ return AK::atan2((ux * vy) - (uy * vx), (ux * vx) + (uy * vy));
};
auto start_angle = angle(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry);
auto sweep_angle = angle((x1p - cxp) / rx, (y1p - cyp) / ry, (-x1p - cxp) / rx, (-y1p - cyp) / ry);