From ed46d52252375be732841d980f9eec02ea7691f5 Mon Sep 17 00:00:00 2001 From: Hendiadyoin1 Date: Sat, 17 Jul 2021 18:29:28 +0200 Subject: [PATCH] Everywhere: Use AK/Math.h if applicable AK's version should see better inlining behaviors, than the LibM one. We avoid mixed usage for now though. Also clean up some stale math includes and improper floatingpoint usage. --- AK/Complex.h | 44 ++++--------------- .../Applications/Browser/DownloadWidget.cpp | 1 - .../Applications/Calculator/Calculator.cpp | 4 +- Userland/Applications/Piano/RollWidget.cpp | 8 ++-- Userland/Applications/Piano/Track.cpp | 5 ++- .../Applications/PixelPaint/EllipseTool.cpp | 1 - Userland/Applications/PixelPaint/LineTool.cpp | 10 ++--- .../Applications/PixelPaint/RectangleTool.cpp | 1 - .../Applications/PixelPaint/SprayTool.cpp | 6 +-- .../SoundPlayer/AudioAlgorithms.cpp | 6 +-- .../SoundPlayer/BarsVisualizationWidget.cpp | 10 ++--- .../Applications/SoundPlayer/SampleWidget.cpp | 4 +- Userland/Demos/CatDog/CatDog.h | 1 - Userland/Demos/Eyes/EyesWidget.cpp | 8 ++-- Userland/Demos/Mouse/main.cpp | 19 ++++---- Userland/Games/2048/GameSizeDialog.cpp | 4 +- Userland/Libraries/LibAudio/FlacLoader.cpp | 8 ++-- .../Libraries/LibGL/SoftwareGLContext.cpp | 1 - Userland/Libraries/LibGUI/TextEditor.cpp | 1 - Userland/Libraries/LibGfx/GIFLoader.cpp | 8 ++-- Userland/Libraries/LibGfx/Gamma.h | 8 ++-- Userland/Libraries/LibGfx/ICOLoader.cpp | 1 - Userland/Libraries/LibGfx/JPGLoader.cpp | 26 +++++------ Userland/Libraries/LibGfx/Line.h | 1 - Userland/Libraries/LibGfx/Matrix4x4.h | 6 +-- Userland/Libraries/LibGfx/PNGLoader.cpp | 1 - Userland/Libraries/LibGfx/Painter.cpp | 14 +++--- Userland/Libraries/LibGfx/Path.cpp | 22 +++++----- Userland/Libraries/LibGfx/Point.h | 4 +- Userland/Libraries/LibGfx/Vector2.h | 4 +- Userland/Libraries/LibGfx/Vector3.h | 4 +- Userland/Libraries/LibGfx/Vector4.h | 4 +- .../LibJS/Runtime/NumberConstructor.cpp | 14 +++--- Userland/Libraries/LibPDF/Parser.cpp | 1 - Userland/Libraries/LibTest/Macros.h | 3 +- Userland/Libraries/LibVT/TerminalWidget.cpp | 1 - Userland/Libraries/LibVideo/MatroskaReader.h | 4 +- Userland/Utilities/du.cpp | 1 - Userland/Utilities/functrace.cpp | 1 - Userland/Utilities/ntpquery.cpp | 2 +- 40 files changed, 116 insertions(+), 156 deletions(-) diff --git a/AK/Complex.h b/AK/Complex.h index aaefaf2e90..b9deb170f9 100644 --- a/AK/Complex.h +++ b/AK/Complex.h @@ -7,10 +7,7 @@ #pragma once #include -#if __has_include() -# define AKCOMPLEX_CAN_USE_MATH_H -# include -#endif +#include #ifdef __cplusplus # if __cplusplus >= 201103L @@ -45,18 +42,9 @@ public: constexpr T magnitude_squared() const COMPLEX_NOEXCEPT { return m_real * m_real + m_imag * m_imag; } -# ifdef AKCOMPLEX_CAN_USE_MATH_H constexpr T magnitude() const COMPLEX_NOEXCEPT { - // for numbers 32 or under bit long we don't need the extra precision of sqrt - // although it may return values with a considerable error if real and imag are too big? - if constexpr (sizeof(T) <= sizeof(float)) { - return sqrtf(m_real * m_real + m_imag * m_imag); - } else if constexpr (sizeof(T) <= sizeof(double)) { - return sqrt(m_real * m_real + m_imag * m_imag); - } else { - return sqrtl(m_real * m_real + m_imag * m_imag); - } + return hypot(m_real, m_imag); } constexpr T phase() const COMPLEX_NOEXCEPT @@ -67,15 +55,8 @@ public: template static constexpr Complex from_polar(U magnitude, V phase) { - if constexpr (sizeof(T) <= sizeof(float)) { - return Complex(magnitude * cosf(phase), magnitude * sinf(phase)); - } else if constexpr (sizeof(T) <= sizeof(double)) { - return Complex(magnitude * cos(phase), magnitude * sin(phase)); - } else { - return Complex(magnitude * cosl(phase), magnitude * sinl(phase)); - } + return Complex(magnitude * cos(phase), magnitude * sin(phase)); } -# endif template constexpr Complex& operator=(const Complex& other) @@ -288,7 +269,6 @@ static constinit Complex complex_real_unit = Complex((T)1, (T)0); template static constinit Complex complex_imag_unit = Complex((T)0, (T)1); -# ifdef AKCOMPLEX_CAN_USE_MATH_H template static constexpr bool approx_eq(const Complex& a, const Complex& b, const double margin = 0.000001) { @@ -300,23 +280,15 @@ static constexpr bool approx_eq(const Complex& a, const Complex& b, const template static constexpr Complex cexp(const Complex& a) { - // FIXME: this can probably be faster and not use so many expensive trigonometric functions - if constexpr (sizeof(T) <= sizeof(float)) { - return expf(a.real()) * Complex(cosf(a.imag()), sinf(a.imag())); - } else if constexpr (sizeof(T) <= sizeof(double)) { - return exp(a.real()) * Complex(cos(a.imag()), sin(a.imag())); - } else { - return expl(a.real()) * Complex(cosl(a.imag()), sinl(a.imag())); - } + // FIXME: this can probably be faster and not use so many "expensive" trigonometric functions + return exp(a.real()) * Complex(cos(a.imag()), sin(a.imag())); } } -# endif +using AK::approx_eq; +using AK::cexp; using AK::Complex; using AK::complex_imag_unit; using AK::complex_real_unit; -# ifdef AKCOMPLEX_CAN_USE_MATH_H -using AK::approx_eq; -using AK::cexp; -# endif + #endif diff --git a/Userland/Applications/Browser/DownloadWidget.cpp b/Userland/Applications/Browser/DownloadWidget.cpp index 594cbbb37f..39b3835677 100644 --- a/Userland/Applications/Browser/DownloadWidget.cpp +++ b/Userland/Applications/Browser/DownloadWidget.cpp @@ -22,7 +22,6 @@ #include #include #include -#include namespace Browser { diff --git a/Userland/Applications/Calculator/Calculator.cpp b/Userland/Applications/Calculator/Calculator.cpp index 4532bb2541..a0972927a5 100644 --- a/Userland/Applications/Calculator/Calculator.cpp +++ b/Userland/Applications/Calculator/Calculator.cpp @@ -6,7 +6,7 @@ #include "Calculator.h" #include -#include +#include Calculator::Calculator() { @@ -37,7 +37,7 @@ double Calculator::begin_operation(Operation operation, double argument) m_has_error = true; return argument; } - res = sqrt(argument); + res = AK::sqrt(argument); clear_operation(); break; case Operation::Inverse: diff --git a/Userland/Applications/Piano/RollWidget.cpp b/Userland/Applications/Piano/RollWidget.cpp index b9dc063203..5c4e68efa6 100644 --- a/Userland/Applications/Piano/RollWidget.cpp +++ b/Userland/Applications/Piano/RollWidget.cpp @@ -8,11 +8,11 @@ #include "RollWidget.h" #include "TrackManager.h" +#include #include #include #include #include -#include constexpr int note_height = 20; constexpr int max_note_width = note_height * 2; @@ -47,7 +47,7 @@ void RollWidget::paint_event(GUI::PaintEvent& event) if (m_num_notes < time_signature_notes) m_num_notes = time_signature_notes; else - m_num_notes = time_signature_notes * pow(2, static_cast(log2(m_num_notes / time_signature_notes))); + m_num_notes = time_signature_notes * AK::exp2(AK::log2(m_num_notes / time_signature_notes)); m_note_width = static_cast(m_roll_width) / m_num_notes; // This calculates the minimum number of rows needed. We account for a @@ -62,9 +62,9 @@ void RollWidget::paint_event(GUI::PaintEvent& event) int key_pattern_index = (notes_per_octave - 1) - (note_offset % notes_per_octave); int x_offset = horizontal_scrollbar().value(); - int horizontal_note_offset_remainder = fmod(x_offset, m_note_width); + int horizontal_note_offset_remainder = static_cast(AK::fmod((double)x_offset, m_note_width)); int horizontal_paint_area = widget_inner_rect().width() + horizontal_note_offset_remainder; - if (fmod(horizontal_paint_area, m_note_width) != 0) + if (AK::fmod((double)horizontal_paint_area, m_note_width) != 0.) horizontal_paint_area += m_note_width; int horizontal_notes_to_paint = horizontal_paint_area / m_note_width; diff --git a/Userland/Applications/Piano/Track.cpp b/Userland/Applications/Piano/Track.cpp index 38d5a3cc65..4f8567fe16 100644 --- a/Userland/Applications/Piano/Track.cpp +++ b/Userland/Applications/Piano/Track.cpp @@ -7,6 +7,7 @@ */ #include "Track.h" +#include #include #include #include @@ -176,7 +177,7 @@ Audio::Frame Track::square(size_t note) { double pos = note_frequencies[note] / sample_rate; double square_step = pos * 2 * M_PI; - double w = sin(m_pos[note]) >= 0 ? 1 : -1; + double w = AK::sin(m_pos[note]) >= 0 ? 1 : -1; m_pos[note] += square_step; return w; } @@ -185,7 +186,7 @@ Audio::Frame Track::triangle(size_t note) { double triangle_step = note_frequencies[note] / sample_rate; double t = m_pos[note]; - double w = fabs(fmod((4 * t) + 1, 4) - 2) - 1; + double w = AK::fabs(AK::fmod((4 * t) + 1, 4.) - 2) - 1.; m_pos[note] += triangle_step; return w; } diff --git a/Userland/Applications/PixelPaint/EllipseTool.cpp b/Userland/Applications/PixelPaint/EllipseTool.cpp index 7d9ccbf336..cf129ed86a 100644 --- a/Userland/Applications/PixelPaint/EllipseTool.cpp +++ b/Userland/Applications/PixelPaint/EllipseTool.cpp @@ -11,7 +11,6 @@ #include #include #include -#include namespace PixelPaint { diff --git a/Userland/Applications/PixelPaint/LineTool.cpp b/Userland/Applications/PixelPaint/LineTool.cpp index 876f5094a5..8514e0bf32 100644 --- a/Userland/Applications/PixelPaint/LineTool.cpp +++ b/Userland/Applications/PixelPaint/LineTool.cpp @@ -7,24 +7,24 @@ #include "LineTool.h" #include "ImageEditor.h" #include "Layer.h" +#include #include #include #include -#include namespace PixelPaint { static Gfx::IntPoint constrain_line_angle(Gfx::IntPoint const& start_pos, Gfx::IntPoint const& end_pos, float angle_increment) { - float current_angle = atan2f(end_pos.y() - start_pos.y(), end_pos.x() - start_pos.x()) + float { M_PI * 2 }; + float current_angle = AK::atan2(end_pos.y() - start_pos.y(), end_pos.x() - start_pos.x()) + float { M_PI * 2 }; float constrained_angle = ((int)((current_angle + angle_increment / 2) / angle_increment)) * angle_increment; auto diff = end_pos - start_pos; - float line_length = sqrt(diff.x() * diff.x() + diff.y() * diff.y()); + float line_length = AK::hypot(diff.x(), diff.y()); - return { start_pos.x() + (int)(cosf(constrained_angle) * line_length), - start_pos.y() + (int)(sinf(constrained_angle) * line_length) }; + return { start_pos.x() + (int)(AK::cos(constrained_angle) * line_length), + start_pos.y() + (int)(AK::sin(constrained_angle) * line_length) }; } LineTool::LineTool() diff --git a/Userland/Applications/PixelPaint/RectangleTool.cpp b/Userland/Applications/PixelPaint/RectangleTool.cpp index d9ec04015d..239d4d82b7 100644 --- a/Userland/Applications/PixelPaint/RectangleTool.cpp +++ b/Userland/Applications/PixelPaint/RectangleTool.cpp @@ -11,7 +11,6 @@ #include #include #include -#include namespace PixelPaint { diff --git a/Userland/Applications/PixelPaint/SprayTool.cpp b/Userland/Applications/PixelPaint/SprayTool.cpp index fcf47ab517..1fdbe84331 100644 --- a/Userland/Applications/PixelPaint/SprayTool.cpp +++ b/Userland/Applications/PixelPaint/SprayTool.cpp @@ -7,6 +7,7 @@ #include "SprayTool.h" #include "ImageEditor.h" #include "Layer.h" +#include #include #include #include @@ -15,7 +16,6 @@ #include #include #include -#include #include namespace PixelPaint { @@ -52,8 +52,8 @@ void SprayTool::paint_it() for (int i = 0; i < M_PI * base_radius * base_radius * (m_density / 100.0); i++) { double radius = base_radius * nrand(); double angle = 2 * M_PI * nrand(); - const int xpos = m_last_pos.x() + radius * cos(angle); - const int ypos = m_last_pos.y() - radius * sin(angle); + const int xpos = m_last_pos.x() + radius * AK::cos(angle); + const int ypos = m_last_pos.y() - radius * AK::sin(angle); if (xpos < 0 || xpos >= bitmap.width()) continue; if (ypos < 0 || ypos >= bitmap.height()) diff --git a/Userland/Applications/SoundPlayer/AudioAlgorithms.cpp b/Userland/Applications/SoundPlayer/AudioAlgorithms.cpp index cef1ad9402..b25de59797 100644 --- a/Userland/Applications/SoundPlayer/AudioAlgorithms.cpp +++ b/Userland/Applications/SoundPlayer/AudioAlgorithms.cpp @@ -6,7 +6,7 @@ #include "AudioAlgorithms.h" #include -#include +#include // This function uses the input vector as output too. therefore, if you wish to // leave it intact, pass a copy to this function @@ -38,8 +38,8 @@ void fft(Vector>& sample_data, bool invert) } for (int len = 2; len <= n; len <<= 1) { - double ang = 2 * M_PI / len * (invert ? -1 : 1); - Complex wlen(cos(ang), sin(ang)); + double ang = 2 * AK::Pi / len * (invert ? -1 : 1); + Complex wlen(AK::cos(ang), AK::sin(ang)); for (int i = 0; i < n; i += len) { Complex w = { 1., 0. }; for (int j = 0; j < len / 2; j++) { diff --git a/Userland/Applications/SoundPlayer/BarsVisualizationWidget.cpp b/Userland/Applications/SoundPlayer/BarsVisualizationWidget.cpp index a52eb5e8de..be4c09ba46 100644 --- a/Userland/Applications/SoundPlayer/BarsVisualizationWidget.cpp +++ b/Userland/Applications/SoundPlayer/BarsVisualizationWidget.cpp @@ -7,11 +7,11 @@ #include "BarsVisualizationWidget.h" #include "AudioAlgorithms.h" #include +#include #include #include #include #include -#include u32 round_previous_power_of_2(u32 x); @@ -27,7 +27,7 @@ void BarsVisualizationWidget::paint_event(GUI::PaintEvent& event) return; fft(m_sample_buffer, false); - double max = sqrt(m_sample_count * 2); + double max = AK::sqrt(m_sample_count * 2.); double freq_bin = m_samplerate / m_sample_count; @@ -45,10 +45,10 @@ void BarsVisualizationWidget::paint_event(GUI::PaintEvent& event) int bins_per_group = ceil_div((m_sample_count - 1) / 2, group_count) * freq_bin; for (int i = 1; i < m_sample_count / 2; i++) { - groups[(i * freq_bin) / bins_per_group] += fabs(m_sample_buffer.data()[i].real()); + groups[(i * freq_bin) / bins_per_group] += AK::fabs(m_sample_buffer.data()[i].real()); } for (int i = 0; i < group_count; i++) - groups[i] /= max * freq_bin / (m_adjust_frequencies ? (clamp(pow(M_E, (double)i / group_count * 3.) - 1.75, 1., 15.)) : 1.); + groups[i] /= max * freq_bin / (m_adjust_frequencies ? (clamp(AK::pow(AK::E, (double)i / group_count * 3.) - 1.75, 1., 15.)) : 1.); const int horizontal_margin = 30; const int top_vertical_margin = 15; @@ -102,7 +102,7 @@ void BarsVisualizationWidget::set_buffer(RefPtr buffer, int sampl m_sample_count = round_previous_power_of_2(samples_to_use); m_sample_buffer.resize(m_sample_count); for (int i = 0; i < m_sample_count; i++) { - m_sample_buffer.data()[i] = (fabs(buffer->samples()[i].left) + fabs(buffer->samples()[i].right)) / 2.; + m_sample_buffer.data()[i] = (AK::fabs(buffer->samples()[i].left) + AK::fabs(buffer->samples()[i].right)) / 2.; } update(); diff --git a/Userland/Applications/SoundPlayer/SampleWidget.cpp b/Userland/Applications/SoundPlayer/SampleWidget.cpp index 1f47778841..ea97ff714e 100644 --- a/Userland/Applications/SoundPlayer/SampleWidget.cpp +++ b/Userland/Applications/SoundPlayer/SampleWidget.cpp @@ -5,9 +5,9 @@ */ #include "SampleWidget.h" +#include #include #include -#include SampleWidget::SampleWidget() { @@ -34,7 +34,7 @@ void SampleWidget::paint_event(GUI::PaintEvent& event) if (m_buffer) { int samples_per_pixel = m_buffer->sample_count() / frame_inner_rect().width(); for (int sample_index = 0; sample_index < m_buffer->sample_count() && (x - x_offset) < frame_inner_rect().width(); ++sample_index) { - float sample = fabsf((float)m_buffer->samples()[sample_index].left); + float sample = AK::fabs((float)m_buffer->samples()[sample_index].left); sample_max = max(sample, sample_max); ++count; diff --git a/Userland/Demos/CatDog/CatDog.h b/Userland/Demos/CatDog/CatDog.h index 6d9ba5ea3a..6c730df0a8 100644 --- a/Userland/Demos/CatDog/CatDog.h +++ b/Userland/Demos/CatDog/CatDog.h @@ -7,7 +7,6 @@ #include #include #include -#include #include #pragma once diff --git a/Userland/Demos/Eyes/EyesWidget.cpp b/Userland/Demos/Eyes/EyesWidget.cpp index 87eb099787..64c1f1e7f1 100644 --- a/Userland/Demos/Eyes/EyesWidget.cpp +++ b/Userland/Demos/Eyes/EyesWidget.cpp @@ -5,12 +5,12 @@ */ #include "EyesWidget.h" +#include #include #include #include #include #include -#include EyesWidget::~EyesWidget() { @@ -79,7 +79,7 @@ Gfx::IntPoint EyesWidget::pupil_center(Gfx::IntRect& eyeball_bounds) const auto mouse_vector = m_mouse_position - eyeball_bounds.center(); double dx = mouse_vector.x(); double dy = mouse_vector.y(); - double mouse_distance = sqrt(dx * dx + dy * dy); + double mouse_distance = AK::hypot(dx, dy); if (mouse_distance == 0.0) return eyeball_bounds.center(); @@ -93,14 +93,14 @@ Gfx::IntPoint EyesWidget::pupil_center(Gfx::IntRect& eyeball_bounds) const if (dx != 0 && AK::abs(dx) >= AK::abs(dy)) { double slope = dy / dx; double slope_squared = slope * slope; - max_distance_along_this_direction = 0.25 * sqrt( + max_distance_along_this_direction = 0.25 * AK::sqrt( (slope_squared + 1) / (1 / width_squared + slope_squared / height_squared) ); } else if (dy != 0 && AK::abs(dy) >= AK::abs(dx)) { double slope = dx / dy; double slope_squared = slope * slope; - max_distance_along_this_direction = 0.25 * sqrt( + max_distance_along_this_direction = 0.25 * AK::sqrt( (slope_squared + 1) / (slope_squared / width_squared + 1 / height_squared) ); diff --git a/Userland/Demos/Mouse/main.cpp b/Userland/Demos/Mouse/main.cpp index ec3a8c8216..7e1cbf30e5 100644 --- a/Userland/Demos/Mouse/main.cpp +++ b/Userland/Demos/Mouse/main.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -18,8 +19,6 @@ #include #include -#include - class MainFrame final : public GUI::Frame { C_OBJECT(MainFrame); @@ -100,17 +99,17 @@ public: Gfx::IntPoint p3; Gfx::IntPoint p4; - p1.set_x(radius * cos(M_PI * m_wheel_delta_acc / 18) + off_x); - p1.set_y(radius * sin(M_PI * m_wheel_delta_acc / 18) + off_y); + p1.set_x(radius * AK::cos(AK::Pi * m_wheel_delta_acc / 18.) + off_x); + p1.set_y(radius * AK::sin(AK::Pi * m_wheel_delta_acc / 18.) + off_y); - p2.set_x(radius * cos(M_PI * (m_wheel_delta_acc + 18) / 18) + off_x); - p2.set_y(radius * sin(M_PI * (m_wheel_delta_acc + 18) / 18) + off_y); + p2.set_x(radius * AK::cos(AK::Pi * (m_wheel_delta_acc + 18) / 18.) + off_x); + p2.set_y(radius * AK::sin(AK::Pi * (m_wheel_delta_acc + 18) / 18.) + off_y); - p3.set_x(radius * cos(M_PI * (m_wheel_delta_acc + 9) / 18) + off_x); - p3.set_y(radius * sin(M_PI * (m_wheel_delta_acc + 9) / 18) + off_y); + p3.set_x(radius * AK::cos(AK::Pi * (m_wheel_delta_acc + 9) / 18.) + off_x); + p3.set_y(radius * AK::sin(AK::Pi * (m_wheel_delta_acc + 9) / 18.) + off_y); - p4.set_x(radius * cos(M_PI * (m_wheel_delta_acc + 27) / 18) + off_x); - p4.set_y(radius * sin(M_PI * (m_wheel_delta_acc + 27) / 18) + off_y); + p4.set_x(radius * AK::cos(AK::Pi * (m_wheel_delta_acc + 27) / 18.) + off_x); + p4.set_y(radius * AK::sin(AK::Pi * (m_wheel_delta_acc + 27) / 18.) + off_y); painter.draw_line(p1, p2, Color::Red, 2); painter.draw_line(p3, p4, Color::Red, 2); diff --git a/Userland/Games/2048/GameSizeDialog.cpp b/Userland/Games/2048/GameSizeDialog.cpp index 4def92cb5e..0e1ca13312 100644 --- a/Userland/Games/2048/GameSizeDialog.cpp +++ b/Userland/Games/2048/GameSizeDialog.cpp @@ -6,17 +6,17 @@ #include "GameSizeDialog.h" #include "Game.h" +#include #include #include #include #include #include -#include GameSizeDialog::GameSizeDialog(GUI::Window* parent, size_t board_size, size_t target, bool evil_ai) : GUI::Dialog(parent) , m_board_size(board_size) - , m_target_tile_power(log2(target)) + , m_target_tile_power(AK::log2(target)) , m_evil_ai(evil_ai) { set_rect({ 0, 0, 250, 150 }); diff --git a/Userland/Libraries/LibAudio/FlacLoader.cpp b/Userland/Libraries/LibAudio/FlacLoader.cpp index 1ac76d90a6..7110d77e2b 100644 --- a/Userland/Libraries/LibAudio/FlacLoader.cpp +++ b/Userland/Libraries/LibAudio/FlacLoader.cpp @@ -10,12 +10,12 @@ #include #include #include +#include #include #include #include #include #include -#include namespace Audio { @@ -417,9 +417,9 @@ u32 FlacLoaderPlugin::convert_sample_count_code(u8 sample_count_code) return FLAC_BLOCKSIZE_AT_END_OF_HEADER_16; } if (sample_count_code >= 2 && sample_count_code <= 5) { - return 576 * pow(2, (sample_count_code - 2)); + return 576 * AK::exp2(sample_count_code - 2); } - return 256 * pow(2, (sample_count_code - 8)); + return 256 * AK::exp2(sample_count_code - 8); } u32 FlacLoaderPlugin::convert_sample_rate_code(u8 sample_rate_code) @@ -797,7 +797,7 @@ u64 read_utf8_char(InputStream& input) while (((start_byte << length) & 0b10000000) == 0b10000000) ++length; u8 bits_from_start_byte = 8 - (length + 1); - u8 start_byte_bitmask = pow(2, bits_from_start_byte) - 1; + u8 start_byte_bitmask = AK::exp2(bits_from_start_byte) - 1; character = start_byte_bitmask & start_byte; for (u8 i = length; i > 0; --i) { input.read(single_byte_buffer); diff --git a/Userland/Libraries/LibGL/SoftwareGLContext.cpp b/Userland/Libraries/LibGL/SoftwareGLContext.cpp index ec5c9dad33..168876e944 100644 --- a/Userland/Libraries/LibGL/SoftwareGLContext.cpp +++ b/Userland/Libraries/LibGL/SoftwareGLContext.cpp @@ -18,7 +18,6 @@ #include #include #include -#include using AK::dbgln; diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index e69b3a3a74..0202ed163f 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -28,7 +28,6 @@ #include #include #include -#include #include #include diff --git a/Userland/Libraries/LibGfx/GIFLoader.cpp b/Userland/Libraries/LibGfx/GIFLoader.cpp index 21f64ec57b..233fbac0ed 100644 --- a/Userland/Libraries/LibGfx/GIFLoader.cpp +++ b/Userland/Libraries/LibGfx/GIFLoader.cpp @@ -9,11 +9,11 @@ #include #include #include +#include #include #include #include #include -#include #include #include @@ -138,7 +138,7 @@ public: : m_lzw_bytes(lzw_bytes) , m_code_size(min_code_size) , m_original_code_size(min_code_size) - , m_table_capacity(pow(2, min_code_size)) + , m_table_capacity(AK::exp2(min_code_size)) { init_code_table(); } @@ -162,7 +162,7 @@ public: m_code_table.clear(); m_code_table.extend(m_original_code_table); m_code_size = m_original_code_size; - m_table_capacity = pow(2, m_code_size); + m_table_capacity = AK::exp2(m_code_size); m_output.clear(); } @@ -563,7 +563,7 @@ static bool load_gif_frame_descriptors(GIFLoadingContext& context) image.interlaced = (packed_fields & 0x40) != 0; if (!image.use_global_color_map) { - size_t local_color_table_size = pow(2, (packed_fields & 7) + 1); + size_t local_color_table_size = AK::exp2((packed_fields & 7) + 1); for (size_t i = 0; i < local_color_table_size; ++i) { u8 r = 0; diff --git a/Userland/Libraries/LibGfx/Gamma.h b/Userland/Libraries/LibGfx/Gamma.h index e796996a8d..9f0bfaacfe 100644 --- a/Userland/Libraries/LibGfx/Gamma.h +++ b/Userland/Libraries/LibGfx/Gamma.h @@ -7,7 +7,7 @@ #pragma once #include "Color.h" -#include +#include #include #include @@ -58,7 +58,7 @@ inline f32x4 linear_to_gamma4(f32x4 x) // Source for approximation: https://mimosa-pudica.net/fast-gamma/ constexpr float a = 0.00279491f; constexpr float b = 1.15907984f; - float c = (b / sqrtf(1.0f + a)) - 1; + float c = (b / AK::sqrt(1.0f + a)) - 1; return ((b * __builtin_ia32_rsqrtps(x + a)) - c) * x; } @@ -85,8 +85,8 @@ inline float linear_to_gamma(float x) // Source for approximation: https://mimosa-pudica.net/fast-gamma/ constexpr float a = 0.00279491; constexpr float b = 1.15907984; - float c = (b / sqrtf(1 + a)) - 1; - return ((b / __builtin_sqrtf(x + a)) - c) * x; + float c = (b / AK::sqrt(1 + a)) - 1; + return ((b / AK::sqrt(x + a)) - c) * x; } // Linearize v1 and v2, lerp them by mix factor, then convert back. diff --git a/Userland/Libraries/LibGfx/ICOLoader.cpp b/Userland/Libraries/LibGfx/ICOLoader.cpp index f234060ba5..dd6b27e5ca 100644 --- a/Userland/Libraries/LibGfx/ICOLoader.cpp +++ b/Userland/Libraries/LibGfx/ICOLoader.cpp @@ -13,7 +13,6 @@ #include #include #include -#include #include #include diff --git a/Userland/Libraries/LibGfx/JPGLoader.cpp b/Userland/Libraries/LibGfx/JPGLoader.cpp index e5591ad43d..c6c2b46508 100644 --- a/Userland/Libraries/LibGfx/JPGLoader.cpp +++ b/Userland/Libraries/LibGfx/JPGLoader.cpp @@ -10,12 +10,12 @@ #include #include #include +#include #include #include #include #include #include -#include #define JPG_INVALID 0X0000 @@ -864,20 +864,20 @@ static void dequantize(JPGLoadingContext& context, Vector& macrobloc static void inverse_dct(const JPGLoadingContext& context, Vector& macroblocks) { - static const float m0 = 2.0 * cos(1.0 / 16.0 * 2.0 * M_PI); - static const float m1 = 2.0 * cos(2.0 / 16.0 * 2.0 * M_PI); - static const float m3 = 2.0 * cos(2.0 / 16.0 * 2.0 * M_PI); - static const float m5 = 2.0 * cos(3.0 / 16.0 * 2.0 * M_PI); + static const float m0 = 2.0 * AK::cos(1.0 / 16.0 * 2.0 * AK::Pi); + static const float m1 = 2.0 * AK::cos(2.0 / 16.0 * 2.0 * AK::Pi); + static const float m3 = 2.0 * AK::cos(2.0 / 16.0 * 2.0 * AK::Pi); + static const float m5 = 2.0 * AK::cos(3.0 / 16.0 * 2.0 * AK::Pi); static const float m2 = m0 - m5; static const float m4 = m0 + m5; - static const float s0 = cos(0.0 / 16.0 * M_PI) / sqrt(8); - static const float s1 = cos(1.0 / 16.0 * M_PI) / 2.0; - static const float s2 = cos(2.0 / 16.0 * M_PI) / 2.0; - static const float s3 = cos(3.0 / 16.0 * M_PI) / 2.0; - static const float s4 = cos(4.0 / 16.0 * M_PI) / 2.0; - static const float s5 = cos(5.0 / 16.0 * M_PI) / 2.0; - static const float s6 = cos(6.0 / 16.0 * M_PI) / 2.0; - static const float s7 = cos(7.0 / 16.0 * M_PI) / 2.0; + static const float s0 = AK::cos(0.0 / 16.0 * AK::Pi) / sqrt(8); + static const float s1 = AK::cos(1.0 / 16.0 * AK::Pi) / 2.0; + static const float s2 = AK::cos(2.0 / 16.0 * AK::Pi) / 2.0; + static const float s3 = AK::cos(3.0 / 16.0 * AK::Pi) / 2.0; + static const float s4 = AK::cos(4.0 / 16.0 * AK::Pi) / 2.0; + static const float s5 = AK::cos(5.0 / 16.0 * AK::Pi) / 2.0; + static const float s6 = AK::cos(6.0 / 16.0 * AK::Pi) / 2.0; + static const float s7 = AK::cos(7.0 / 16.0 * AK::Pi) / 2.0; for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) { for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) { diff --git a/Userland/Libraries/LibGfx/Line.h b/Userland/Libraries/LibGfx/Line.h index f9d8176846..d5f8360b04 100644 --- a/Userland/Libraries/LibGfx/Line.h +++ b/Userland/Libraries/LibGfx/Line.h @@ -11,7 +11,6 @@ #include #include #include -#include #include namespace Gfx { diff --git a/Userland/Libraries/LibGfx/Matrix4x4.h b/Userland/Libraries/LibGfx/Matrix4x4.h index 04a5bad0f7..4637f4e816 100644 --- a/Userland/Libraries/LibGfx/Matrix4x4.h +++ b/Userland/Libraries/LibGfx/Matrix4x4.h @@ -6,10 +6,10 @@ #pragma once +#include #include #include #include -#include namespace Gfx { @@ -70,8 +70,8 @@ constexpr static Matrix4x4 scale_matrix(const Vector3& s) template constexpr static Matrix4x4 rotation_matrix(const Vector3& axis, T angle) { - T c = cos(angle); - T s = sin(angle); + T c = AK::cos(angle); + T s = AK::sin(angle); T t = 1 - c; T x = axis.x(); T y = axis.y(); diff --git a/Userland/Libraries/LibGfx/PNGLoader.cpp b/Userland/Libraries/LibGfx/PNGLoader.cpp index 8dee3b1d4c..7174b6d50d 100644 --- a/Userland/Libraries/LibGfx/PNGLoader.cpp +++ b/Userland/Libraries/LibGfx/PNGLoader.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include diff --git a/Userland/Libraries/LibGfx/Painter.cpp b/Userland/Libraries/LibGfx/Painter.cpp index c2141d4cc9..f9159daa43 100644 --- a/Userland/Libraries/LibGfx/Painter.cpp +++ b/Userland/Libraries/LibGfx/Painter.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -25,7 +26,6 @@ #include #include #include -#include #include #if defined(__GNUC__) && !defined(__clang__) @@ -492,11 +492,11 @@ void Painter::draw_ellipse_intersecting(const IntRect& rect, Color color, int th double increment = M_PI / number_samples; auto ellipse_x = [&](double theta) -> int { - return (cos(theta) * rect.width() / sqrt(2)) + rect.center().x(); + return (AK::cos(theta) * rect.width() / AK::sqrt(2.)) + rect.center().x(); }; auto ellipse_y = [&](double theta) -> int { - return (sin(theta) * rect.height() / sqrt(2)) + rect.center().y(); + return (AK::sin(theta) * rect.height() / AK::sqrt(2.)) + rect.center().y(); }; for (auto theta = 0.0; theta < 2 * M_PI; theta += increment) { @@ -1920,8 +1920,8 @@ void Painter::for_each_line_segment_on_elliptical_arc(const FloatPoint& p1, cons FloatPoint current_point = relative_start; FloatPoint next_point = { 0, 0 }; - auto sin_x_axis = sinf(x_axis_rotation); - auto cos_x_axis = cosf(x_axis_rotation); + auto sin_x_axis = AK::sin(x_axis_rotation); + auto cos_x_axis = AK::cos(x_axis_rotation); auto rotate_point = [sin_x_axis, cos_x_axis](FloatPoint& p) { auto original_x = p.x(); auto original_y = p.y(); @@ -1931,8 +1931,8 @@ void Painter::for_each_line_segment_on_elliptical_arc(const FloatPoint& p1, cons }; for (double theta = theta_1; theta <= ((double)theta_1 + (double)theta_delta); theta += theta_step) { - next_point.set_x(a * cosf(theta)); - next_point.set_y(b * sinf(theta)); + next_point.set_x(a * AK::cos(theta)); + next_point.set_y(b * AK::sin(theta)); rotate_point(next_point); callback(current_point + center, next_point + center); diff --git a/Userland/Libraries/LibGfx/Path.cpp b/Userland/Libraries/LibGfx/Path.cpp index 9824065e99..b629c46ce5 100644 --- a/Userland/Libraries/LibGfx/Path.cpp +++ b/Userland/Libraries/LibGfx/Path.cpp @@ -6,11 +6,11 @@ #include #include +#include #include #include #include #include -#include namespace Gfx { @@ -21,8 +21,8 @@ void Path::elliptical_arc_to(const FloatPoint& point, const FloatPoint& radii, d double rx = radii.x(); double ry = radii.y(); - double x_axis_rotation_c = cos(x_axis_rotation); - double x_axis_rotation_s = sin(x_axis_rotation); + double x_axis_rotation_c = AK::cos(x_axis_rotation); + double x_axis_rotation_s = AK::sin(x_axis_rotation); // Find the last point FloatPoint last_point { 0, 0 }; @@ -61,24 +61,24 @@ void Path::elliptical_arc_to(const FloatPoint& point, const FloatPoint& radii, d auto y1p = -x_axis_rotation_s * x_avg + x_axis_rotation_c * y_avg; // Step 2: Compute (cx', cy') - double x1p_sq = pow(x1p, 2.0); - double y1p_sq = pow(y1p, 2.0); - double rx_sq = pow(rx, 2.0); - double ry_sq = pow(ry, 2.0); + double x1p_sq = x1p * x1p; + double y1p_sq = y1p * y1p; + double rx_sq = rx * rx; + double ry_sq = ry * ry; // Step 3 of out-of-range radii correction double lambda = x1p_sq / rx_sq + y1p_sq / ry_sq; double multiplier; if (lambda > 1.0) { - auto lambda_sqrt = sqrt(lambda); + auto lambda_sqrt = AK::sqrt(lambda); rx *= lambda_sqrt; ry *= lambda_sqrt; multiplier = 0.0; } else { double numerator = rx_sq * ry_sq - rx_sq * y1p_sq - ry_sq * x1p_sq; double denominator = rx_sq * y1p_sq + ry_sq * x1p_sq; - multiplier = sqrt(numerator / denominator); + multiplier = AK::sqrt(numerator / denominator); } if (large_arc == sweep) @@ -93,8 +93,8 @@ void Path::elliptical_arc_to(const FloatPoint& point, const FloatPoint& radii, d double cx = x_axis_rotation_c * cxp - x_axis_rotation_s * cyp + x_avg; double cy = x_axis_rotation_s * cxp + x_axis_rotation_c * cyp + y_avg; - double theta_1 = atan2((y1p - cyp) / ry, (x1p - cxp) / rx); - double theta_2 = atan2((-y1p - cyp) / ry, (-x1p - cxp) / rx); + double theta_1 = AK::atan2((y1p - cyp) / ry, (x1p - cxp) / rx); + double theta_2 = AK::atan2((-y1p - cyp) / ry, (-x1p - cxp) / rx); auto theta_delta = theta_2 - theta_1; diff --git a/Userland/Libraries/LibGfx/Point.h b/Userland/Libraries/LibGfx/Point.h index 1d1f8f2df9..fcb5d5dbb8 100644 --- a/Userland/Libraries/LibGfx/Point.h +++ b/Userland/Libraries/LibGfx/Point.h @@ -7,12 +7,12 @@ #pragma once #include +#include #include #include #include #include #include -#include namespace Gfx { @@ -223,7 +223,7 @@ public: { if (*this == other) return 0; - return sqrtf(powf(m_x - other.m_x, 2.0f) + powf(m_y - other.m_y, 2.0f)); + return AK::hypot(m_x - other.m_x, m_y - other.m_y); } [[nodiscard]] Point absolute_relative_distance_to(Point const& other) const diff --git a/Userland/Libraries/LibGfx/Vector2.h b/Userland/Libraries/LibGfx/Vector2.h index b4a68a06df..92c8043ff4 100644 --- a/Userland/Libraries/LibGfx/Vector2.h +++ b/Userland/Libraries/LibGfx/Vector2.h @@ -6,8 +6,8 @@ #pragma once +#include #include -#include namespace Gfx { template @@ -105,7 +105,7 @@ public: constexpr T length() const { - return sqrt(m_x * m_x + m_y * m_y); + return AK::hypot(m_x, m_y); } String to_string() const diff --git a/Userland/Libraries/LibGfx/Vector3.h b/Userland/Libraries/LibGfx/Vector3.h index 6f6c41d0c7..0e17d2721e 100644 --- a/Userland/Libraries/LibGfx/Vector3.h +++ b/Userland/Libraries/LibGfx/Vector3.h @@ -6,8 +6,8 @@ #pragma once +#include #include -#include namespace Gfx { template @@ -121,7 +121,7 @@ public: constexpr T length() const { - return sqrt(m_x * m_x + m_y * m_y + m_z * m_z); + return AK::sqrt(m_x * m_x + m_y * m_y + m_z * m_z); } String to_string() const diff --git a/Userland/Libraries/LibGfx/Vector4.h b/Userland/Libraries/LibGfx/Vector4.h index c7570ea852..7745af60f4 100644 --- a/Userland/Libraries/LibGfx/Vector4.h +++ b/Userland/Libraries/LibGfx/Vector4.h @@ -6,8 +6,8 @@ #pragma once +#include #include -#include namespace Gfx { template @@ -121,7 +121,7 @@ public: constexpr T length() const { - return sqrt(m_x * m_x + m_y * m_y + m_z * m_z + m_w * m_w); + return AK::sqrt(m_x * m_x + m_y * m_y + m_z * m_z + m_w * m_w); } String to_string() const diff --git a/Userland/Libraries/LibJS/Runtime/NumberConstructor.cpp b/Userland/Libraries/LibJS/Runtime/NumberConstructor.cpp index 9489f232e4..82770468f4 100644 --- a/Userland/Libraries/LibJS/Runtime/NumberConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/NumberConstructor.cpp @@ -4,21 +4,21 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include #include #include -#include #ifdef __clang__ -# define EPSILON_VALUE pow(2, -52) -# define MAX_SAFE_INTEGER_VALUE pow(2, 53) - 1 -# define MIN_SAFE_INTEGER_VALUE -(pow(2, 53) - 1) +# define EPSILON_VALUE AK::exp2(-52.) +# define MAX_SAFE_INTEGER_VALUE AK::exp2(53.) - 1 +# define MIN_SAFE_INTEGER_VALUE -(AK::exp2(53.) - 1) #else -constexpr const double EPSILON_VALUE { __builtin_pow(2, -52) }; -constexpr const double MAX_SAFE_INTEGER_VALUE { __builtin_pow(2, 53) - 1 }; -constexpr const double MIN_SAFE_INTEGER_VALUE { -(__builtin_pow(2, 53) - 1) }; +constexpr const double EPSILON_VALUE { __builtin_exp2(-52) }; +constexpr const double MAX_SAFE_INTEGER_VALUE { __builtin_exp2(53) - 1 }; +constexpr const double MIN_SAFE_INTEGER_VALUE { -(__builtin_exp2(53) - 1) }; #endif namespace JS { diff --git a/Userland/Libraries/LibPDF/Parser.cpp b/Userland/Libraries/LibPDF/Parser.cpp index 263ea0a270..c94e982c80 100644 --- a/Userland/Libraries/LibPDF/Parser.cpp +++ b/Userland/Libraries/LibPDF/Parser.cpp @@ -14,7 +14,6 @@ #include #include #include -#include namespace PDF { diff --git a/Userland/Libraries/LibTest/Macros.h b/Userland/Libraries/LibTest/Macros.h index 6a38eadfcf..9c084e46a3 100644 --- a/Userland/Libraries/LibTest/Macros.h +++ b/Userland/Libraries/LibTest/Macros.h @@ -9,6 +9,7 @@ #include #include +#include #include namespace AK { @@ -89,7 +90,7 @@ void current_test_case_did_fail(); auto expect_close_lhs = a; \ auto expect_close_rhs = b; \ auto expect_close_diff = static_cast(expect_close_lhs) - static_cast(expect_close_rhs); \ - if (fabs(expect_close_diff) > 0.0000005) { \ + if (AK::fabs(expect_close_diff) > 0.0000005) { \ ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_APPROXIMATE({}, {})" \ " failed with lhs={}, rhs={}, (lhs-rhs)={}", \ __FILE__, __LINE__, #a, #b, expect_close_lhs, expect_close_rhs, expect_close_diff); \ diff --git a/Userland/Libraries/LibVT/TerminalWidget.cpp b/Userland/Libraries/LibVT/TerminalWidget.cpp index aadb1e5ebf..780496296e 100644 --- a/Userland/Libraries/LibVT/TerminalWidget.cpp +++ b/Userland/Libraries/LibVT/TerminalWidget.cpp @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include diff --git a/Userland/Libraries/LibVideo/MatroskaReader.h b/Userland/Libraries/LibVideo/MatroskaReader.h index d3e0a6742c..33d23c3dd3 100644 --- a/Userland/Libraries/LibVideo/MatroskaReader.h +++ b/Userland/Libraries/LibVideo/MatroskaReader.h @@ -8,10 +8,10 @@ #include "MatroskaDocument.h" #include +#include #include #include #include -#include namespace Video { @@ -122,7 +122,7 @@ private: u8 next_octet = read_octet(); result = (result << 8u) | next_octet; } - result -= pow(2, length * 7 - 1) - 1; + result -= AK::exp2(length * 7 - 1) - 1; return result; } diff --git a/Userland/Utilities/du.cpp b/Userland/Utilities/du.cpp index 4f218c9a0f..1782e56355 100644 --- a/Userland/Utilities/du.cpp +++ b/Userland/Utilities/du.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #include #include #include diff --git a/Userland/Utilities/functrace.cpp b/Userland/Utilities/functrace.cpp index f94a7e2018..6cdc9a4389 100644 --- a/Userland/Utilities/functrace.cpp +++ b/Userland/Utilities/functrace.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #include #include #include diff --git a/Userland/Utilities/ntpquery.cpp b/Userland/Utilities/ntpquery.cpp index 802d16a054..a4c36ae616 100644 --- a/Userland/Utilities/ntpquery.cpp +++ b/Userland/Utilities/ntpquery.cpp @@ -292,7 +292,7 @@ int main(int argc, char** argv) NtpTimestamp T3 = transmit_timestamp; NtpTimestamp T4 = destination_timestamp; auto timestamp_difference_in_seconds = [](NtpTimestamp from, NtpTimestamp to) { - return static_cast(to - from) / pow(2.0, 32); + return static_cast(to - from) >> 32; }; // The network round-trip time of the request.