1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 16:47:34 +00:00

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.
This commit is contained in:
Hendiadyoin1 2021-07-17 18:29:28 +02:00 committed by Ali Mohammad Pur
parent c5f6ba6e71
commit ed46d52252
40 changed files with 116 additions and 156 deletions

View file

@ -22,7 +22,6 @@
#include <LibGUI/Window.h>
#include <LibProtocol/RequestClient.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <math.h>
namespace Browser {

View file

@ -6,7 +6,7 @@
#include "Calculator.h"
#include <AK/Assertions.h>
#include <math.h>
#include <AK/Math.h>
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:

View file

@ -8,11 +8,11 @@
#include "RollWidget.h"
#include "TrackManager.h"
#include <AK/Math.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Scrollbar.h>
#include <LibGfx/Font.h>
#include <LibGfx/FontDatabase.h>
#include <math.h>
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<int>(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<double>(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<int>(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;

View file

@ -7,6 +7,7 @@
*/
#include "Track.h"
#include <AK/Math.h>
#include <AK/NumericLimits.h>
#include <LibAudio/Loader.h>
#include <math.h>
@ -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;
}

View file

@ -11,7 +11,6 @@
#include <LibGUI/Menu.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Rect.h>
#include <math.h>
namespace PixelPaint {

View file

@ -7,24 +7,24 @@
#include "LineTool.h"
#include "ImageEditor.h"
#include "Layer.h"
#include <AK/Math.h>
#include <LibGUI/Action.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Painter.h>
#include <math.h>
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<float>(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<float>(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()

View file

@ -11,7 +11,6 @@
#include <LibGUI/Menu.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Rect.h>
#include <math.h>
namespace PixelPaint {

View file

@ -7,6 +7,7 @@
#include "SprayTool.h"
#include "ImageEditor.h"
#include "Layer.h"
#include <AK/Math.h>
#include <AK/Queue.h>
#include <LibGUI/Action.h>
#include <LibGUI/BoxLayout.h>
@ -15,7 +16,6 @@
#include <LibGUI/Painter.h>
#include <LibGUI/Slider.h>
#include <LibGfx/Bitmap.h>
#include <math.h>
#include <stdio.h>
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())

View file

@ -6,7 +6,7 @@
#include "AudioAlgorithms.h"
#include <AK/Complex.h>
#include <math.h>
#include <AK/Math.h>
// 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<Complex<double>>& sample_data, bool invert)
}
for (int len = 2; len <= n; len <<= 1) {
double ang = 2 * M_PI / len * (invert ? -1 : 1);
Complex<double> wlen(cos(ang), sin(ang));
double ang = 2 * AK::Pi<double> / len * (invert ? -1 : 1);
Complex<double> wlen(AK::cos(ang), AK::sin(ang));
for (int i = 0; i < n; i += len) {
Complex<double> w = { 1., 0. };
for (int j = 0; j < len / 2; j++) {

View file

@ -7,11 +7,11 @@
#include "BarsVisualizationWidget.h"
#include "AudioAlgorithms.h"
#include <AK/Complex.h>
#include <AK/Math.h>
#include <LibGUI/Event.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Window.h>
#include <math.h>
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>, (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<Audio::Buffer> 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();

View file

@ -5,9 +5,9 @@
*/
#include "SampleWidget.h"
#include <AK/Math.h>
#include <LibAudio/Buffer.h>
#include <LibGUI/Painter.h>
#include <math.h>
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;