1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-23 05:37:41 +00:00

LibWeb: Use floats instead of doubles for CSS numbers

Using doubles isn't necessary, and they make things slightly bigger and
slower, so let's use floats instead.
This commit is contained in:
Sam Atkins 2022-03-21 17:22:05 +00:00 committed by Andreas Kling
parent 1f5b5d3f99
commit 0795b9f7bb
6 changed files with 33 additions and 33 deletions

View file

@ -27,7 +27,7 @@ String MediaFeatureValue::to_string() const
[](Length const& length) { return length.to_string(); },
[](Ratio const& ratio) { return ratio.to_string(); },
[](Resolution const& resolution) { return resolution.to_string(); },
[](double number) { return String::number(number); });
[](float number) { return String::number(number); });
}
bool MediaFeatureValue::is_same_type(MediaFeatureValue const& other) const
@ -37,7 +37,7 @@ bool MediaFeatureValue::is_same_type(MediaFeatureValue const& other) const
[&](Length const&) { return other.is_length(); },
[&](Ratio const&) { return other.is_ratio(); },
[&](Resolution const&) { return other.is_resolution(); },
[&](double) { return other.is_number(); });
[&](float) { return other.is_number(); });
}
String MediaFeature::to_string() const

View file

@ -42,7 +42,7 @@ public:
{
}
explicit MediaFeatureValue(double number)
explicit MediaFeatureValue(float number)
: m_value(number)
{
}
@ -51,7 +51,7 @@ public:
bool is_ident() const { return m_value.has<ValueID>(); }
bool is_length() const { return m_value.has<Length>(); }
bool is_number() const { return m_value.has<double>(); }
bool is_number() const { return m_value.has<float>(); }
bool is_ratio() const { return m_value.has<Ratio>(); }
bool is_resolution() const { return m_value.has<Resolution>(); }
bool is_same_type(MediaFeatureValue const& other) const;
@ -80,14 +80,14 @@ public:
return m_value.get<Resolution>();
}
double number() const
float number() const
{
VERIFY(is_number());
return m_value.get<double>();
return m_value.get<float>();
}
private:
Variant<ValueID, Length, Ratio, Resolution, double> m_value;
Variant<ValueID, Length, Ratio, Resolution, float> m_value;
};
// https://www.w3.org/TR/mediaqueries-4/#mq-features

View file

@ -2476,9 +2476,9 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
&& g_val.is(Token::Type::Percentage)
&& b_val.is(Token::Type::Percentage)) {
u8 r = clamp(lroundf(r_val.percentage() * 2.55), 0, 255);
u8 g = clamp(lroundf(g_val.percentage() * 2.55), 0, 255);
u8 b = clamp(lroundf(b_val.percentage() * 2.55), 0, 255);
u8 r = clamp(lroundf(r_val.percentage() * 2.55f), 0, 255);
u8 g = clamp(lroundf(g_val.percentage() * 2.55f), 0, 255);
u8 b = clamp(lroundf(b_val.percentage() * 2.55f), 0, 255);
return Color(r, g, b);
}
} else if (function.name().equals_ignoring_case("rgba")) {
@ -2498,7 +2498,7 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
auto r = r_val.to_integer();
auto g = g_val.to_integer();
auto b = b_val.to_integer();
auto a = clamp(lroundf(a_val.number_value() * 255.0), 0, 255);
auto a = clamp(lroundf(a_val.number_value() * 255.0f), 0, 255);
if (AK::is_within_range<u8>(r) && AK::is_within_range<u8>(g) && AK::is_within_range<u8>(b))
return Color(r, g, b, a);
@ -2512,10 +2512,10 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
auto b = b_val.percentage();
auto a = a_val.number_value();
u8 r_255 = clamp(lroundf(r * 2.55), 0, 255);
u8 g_255 = clamp(lroundf(g * 2.55), 0, 255);
u8 b_255 = clamp(lroundf(b * 2.55), 0, 255);
u8 a_255 = clamp(lroundf(a * 255.0), 0, 255);
u8 r_255 = clamp(lroundf(r * 2.55f), 0, 255);
u8 g_255 = clamp(lroundf(g * 2.55f), 0, 255);
u8 b_255 = clamp(lroundf(b * 2.55f), 0, 255);
u8 a_255 = clamp(lroundf(a * 255.0f), 0, 255);
return Color(r_255, g_255, b_255, a_255);
}
} else if (function.name().equals_ignoring_case("hsl")) {
@ -2530,9 +2530,9 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
&& s_val.is(Token::Type::Percentage)
&& l_val.is(Token::Type::Percentage)) {
auto h = static_cast<float>(h_val.number_value());
auto s = static_cast<float>(s_val.percentage() / 100.0f);
auto l = static_cast<float>(l_val.percentage() / 100.0f);
auto h = h_val.number_value();
auto s = s_val.percentage() / 100.0f;
auto l = l_val.percentage() / 100.0f;
return Color::from_hsl(h, s, l);
}
} else if (function.name().equals_ignoring_case("hsla")) {
@ -2549,10 +2549,10 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
&& l_val.is(Token::Type::Percentage)
&& a_val.is(Token::Type::Number)) {
auto h = static_cast<float>(h_val.number_value());
auto s = static_cast<float>(s_val.percentage() / 100.0f);
auto l = static_cast<float>(l_val.percentage() / 100.0f);
auto a = static_cast<float>(a_val.number_value());
auto h = h_val.number_value();
auto s = s_val.percentage() / 100.0f;
auto l = l_val.percentage() / 100.0f;
auto a = a_val.number_value();
return Color::from_hsla(h, s, l, a);
}
}

View file

@ -118,7 +118,7 @@ public:
VERIFY(m_type == Type::Number);
return m_value.view();
}
double number_value() const
float number_value() const
{
VERIFY(m_type == Type::Number);
return m_number_value;
@ -135,14 +135,14 @@ public:
VERIFY(m_type == Type::Dimension);
return m_unit.view();
}
double dimension_value() const
float dimension_value() const
{
VERIFY(m_type == Type::Dimension);
return m_number_value;
}
i64 dimension_value_int() const { return to_closest_integer(dimension_value()); }
double percentage() const
float percentage() const
{
VERIFY(m_type == Type::Percentage);
return m_number_value;
@ -165,22 +165,22 @@ public:
Position const& end_position() const { return m_end_position; }
private:
static i64 to_closest_integer(double value)
static i64 to_closest_integer(float value)
{
// https://www.w3.org/TR/css-values-4/#numeric-types
// When a value cannot be explicitly supported due to range/precision limitations, it must be converted
// to the closest value supported by the implementation, but how the implementation defines "closest"
// is explicitly undefined as well.
return llround(value);
return llroundf(value);
}
Type m_type { Type::Invalid };
FlyString m_value;
FlyString m_unit;
HashType m_hash_type { HashType::Unrestricted };
float m_number_value { 0 };
NumberType m_number_type { NumberType::Integer };
double m_number_value { 0 };
HashType m_hash_type { HashType::Unrestricted };
Position m_start_position;
Position m_end_position;

View file

@ -557,7 +557,7 @@ CSSNumber Tokenizer::consume_a_number()
}
// https://www.w3.org/TR/css-syntax-3/#convert-string-to-number
double Tokenizer::convert_a_string_to_a_number(StringView string)
float Tokenizer::convert_a_string_to_a_number(StringView string)
{
auto code_point_at = [&](size_t index) -> u32 {
if (index < string.length())
@ -630,7 +630,7 @@ double Tokenizer::convert_a_string_to_a_number(StringView string)
VERIFY(position == string.length());
// Return the number s·(i + f·10^-d)·10^te.
return sign * (integer_part + fractional_part * pow(10, -fractional_digits)) * pow(10, exponent_sign * exponent);
return sign * (integer_part + fractional_part * powf(10, -fractional_digits)) * powf(10, exponent_sign * exponent);
}
// https://www.w3.org/TR/css-syntax-3/#consume-name

View file

@ -60,7 +60,7 @@ public:
class CSSNumber {
public:
String string;
double value { 0 };
float value { 0 };
Token::NumberType type {};
};
@ -90,7 +90,7 @@ private:
[[nodiscard]] Token consume_a_numeric_token();
[[nodiscard]] Token consume_an_ident_like_token();
[[nodiscard]] CSSNumber consume_a_number();
[[nodiscard]] double convert_a_string_to_a_number(StringView);
[[nodiscard]] float convert_a_string_to_a_number(StringView);
[[nodiscard]] String consume_a_name();
[[nodiscard]] u32 consume_escaped_code_point();
[[nodiscard]] Token consume_a_url_token();