Skip to content

Commit 0795b9f

Browse files
AtkinsSJawesomekling
authored andcommitted
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.
1 parent 1f5b5d3 commit 0795b9f

File tree

6 files changed

+33
-33
lines changed

6 files changed

+33
-33
lines changed

Userland/Libraries/LibWeb/CSS/MediaQuery.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ String MediaFeatureValue::to_string() const
2727
[](Length const& length) { return length.to_string(); },
2828
[](Ratio const& ratio) { return ratio.to_string(); },
2929
[](Resolution const& resolution) { return resolution.to_string(); },
30-
[](double number) { return String::number(number); });
30+
[](float number) { return String::number(number); });
3131
}
3232

3333
bool MediaFeatureValue::is_same_type(MediaFeatureValue const& other) const
@@ -37,7 +37,7 @@ bool MediaFeatureValue::is_same_type(MediaFeatureValue const& other) const
3737
[&](Length const&) { return other.is_length(); },
3838
[&](Ratio const&) { return other.is_ratio(); },
3939
[&](Resolution const&) { return other.is_resolution(); },
40-
[&](double) { return other.is_number(); });
40+
[&](float) { return other.is_number(); });
4141
}
4242

4343
String MediaFeature::to_string() const

Userland/Libraries/LibWeb/CSS/MediaQuery.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class MediaFeatureValue {
4242
{
4343
}
4444

45-
explicit MediaFeatureValue(double number)
45+
explicit MediaFeatureValue(float number)
4646
: m_value(number)
4747
{
4848
}
@@ -51,7 +51,7 @@ class MediaFeatureValue {
5151

5252
bool is_ident() const { return m_value.has<ValueID>(); }
5353
bool is_length() const { return m_value.has<Length>(); }
54-
bool is_number() const { return m_value.has<double>(); }
54+
bool is_number() const { return m_value.has<float>(); }
5555
bool is_ratio() const { return m_value.has<Ratio>(); }
5656
bool is_resolution() const { return m_value.has<Resolution>(); }
5757
bool is_same_type(MediaFeatureValue const& other) const;
@@ -80,14 +80,14 @@ class MediaFeatureValue {
8080
return m_value.get<Resolution>();
8181
}
8282

83-
double number() const
83+
float number() const
8484
{
8585
VERIFY(is_number());
86-
return m_value.get<double>();
86+
return m_value.get<float>();
8787
}
8888

8989
private:
90-
Variant<ValueID, Length, Ratio, Resolution, double> m_value;
90+
Variant<ValueID, Length, Ratio, Resolution, float> m_value;
9191
};
9292

9393
// https://www.w3.org/TR/mediaqueries-4/#mq-features

Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2476,9 +2476,9 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
24762476
&& g_val.is(Token::Type::Percentage)
24772477
&& b_val.is(Token::Type::Percentage)) {
24782478

2479-
u8 r = clamp(lroundf(r_val.percentage() * 2.55), 0, 255);
2480-
u8 g = clamp(lroundf(g_val.percentage() * 2.55), 0, 255);
2481-
u8 b = clamp(lroundf(b_val.percentage() * 2.55), 0, 255);
2479+
u8 r = clamp(lroundf(r_val.percentage() * 2.55f), 0, 255);
2480+
u8 g = clamp(lroundf(g_val.percentage() * 2.55f), 0, 255);
2481+
u8 b = clamp(lroundf(b_val.percentage() * 2.55f), 0, 255);
24822482
return Color(r, g, b);
24832483
}
24842484
} else if (function.name().equals_ignoring_case("rgba")) {
@@ -2498,7 +2498,7 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
24982498
auto r = r_val.to_integer();
24992499
auto g = g_val.to_integer();
25002500
auto b = b_val.to_integer();
2501-
auto a = clamp(lroundf(a_val.number_value() * 255.0), 0, 255);
2501+
auto a = clamp(lroundf(a_val.number_value() * 255.0f), 0, 255);
25022502
if (AK::is_within_range<u8>(r) && AK::is_within_range<u8>(g) && AK::is_within_range<u8>(b))
25032503
return Color(r, g, b, a);
25042504

@@ -2512,10 +2512,10 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
25122512
auto b = b_val.percentage();
25132513
auto a = a_val.number_value();
25142514

2515-
u8 r_255 = clamp(lroundf(r * 2.55), 0, 255);
2516-
u8 g_255 = clamp(lroundf(g * 2.55), 0, 255);
2517-
u8 b_255 = clamp(lroundf(b * 2.55), 0, 255);
2518-
u8 a_255 = clamp(lroundf(a * 255.0), 0, 255);
2515+
u8 r_255 = clamp(lroundf(r * 2.55f), 0, 255);
2516+
u8 g_255 = clamp(lroundf(g * 2.55f), 0, 255);
2517+
u8 b_255 = clamp(lroundf(b * 2.55f), 0, 255);
2518+
u8 a_255 = clamp(lroundf(a * 255.0f), 0, 255);
25192519
return Color(r_255, g_255, b_255, a_255);
25202520
}
25212521
} else if (function.name().equals_ignoring_case("hsl")) {
@@ -2530,9 +2530,9 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
25302530
&& s_val.is(Token::Type::Percentage)
25312531
&& l_val.is(Token::Type::Percentage)) {
25322532

2533-
auto h = static_cast<float>(h_val.number_value());
2534-
auto s = static_cast<float>(s_val.percentage() / 100.0f);
2535-
auto l = static_cast<float>(l_val.percentage() / 100.0f);
2533+
auto h = h_val.number_value();
2534+
auto s = s_val.percentage() / 100.0f;
2535+
auto l = l_val.percentage() / 100.0f;
25362536
return Color::from_hsl(h, s, l);
25372537
}
25382538
} else if (function.name().equals_ignoring_case("hsla")) {
@@ -2549,10 +2549,10 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
25492549
&& l_val.is(Token::Type::Percentage)
25502550
&& a_val.is(Token::Type::Number)) {
25512551

2552-
auto h = static_cast<float>(h_val.number_value());
2553-
auto s = static_cast<float>(s_val.percentage() / 100.0f);
2554-
auto l = static_cast<float>(l_val.percentage() / 100.0f);
2555-
auto a = static_cast<float>(a_val.number_value());
2552+
auto h = h_val.number_value();
2553+
auto s = s_val.percentage() / 100.0f;
2554+
auto l = l_val.percentage() / 100.0f;
2555+
auto a = a_val.number_value();
25562556
return Color::from_hsla(h, s, l, a);
25572557
}
25582558
}

Userland/Libraries/LibWeb/CSS/Parser/Token.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class Token {
118118
VERIFY(m_type == Type::Number);
119119
return m_value.view();
120120
}
121-
double number_value() const
121+
float number_value() const
122122
{
123123
VERIFY(m_type == Type::Number);
124124
return m_number_value;
@@ -135,14 +135,14 @@ class Token {
135135
VERIFY(m_type == Type::Dimension);
136136
return m_unit.view();
137137
}
138-
double dimension_value() const
138+
float dimension_value() const
139139
{
140140
VERIFY(m_type == Type::Dimension);
141141
return m_number_value;
142142
}
143143
i64 dimension_value_int() const { return to_closest_integer(dimension_value()); }
144144

145-
double percentage() const
145+
float percentage() const
146146
{
147147
VERIFY(m_type == Type::Percentage);
148148
return m_number_value;
@@ -165,22 +165,22 @@ class Token {
165165
Position const& end_position() const { return m_end_position; }
166166

167167
private:
168-
static i64 to_closest_integer(double value)
168+
static i64 to_closest_integer(float value)
169169
{
170170
// https://www.w3.org/TR/css-values-4/#numeric-types
171171
// When a value cannot be explicitly supported due to range/precision limitations, it must be converted
172172
// to the closest value supported by the implementation, but how the implementation defines "closest"
173173
// is explicitly undefined as well.
174-
return llround(value);
174+
return llroundf(value);
175175
}
176176

177177
Type m_type { Type::Invalid };
178178

179179
FlyString m_value;
180180
FlyString m_unit;
181-
HashType m_hash_type { HashType::Unrestricted };
181+
float m_number_value { 0 };
182182
NumberType m_number_type { NumberType::Integer };
183-
double m_number_value { 0 };
183+
HashType m_hash_type { HashType::Unrestricted };
184184

185185
Position m_start_position;
186186
Position m_end_position;

Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ CSSNumber Tokenizer::consume_a_number()
557557
}
558558

559559
// https://www.w3.org/TR/css-syntax-3/#convert-string-to-number
560-
double Tokenizer::convert_a_string_to_a_number(StringView string)
560+
float Tokenizer::convert_a_string_to_a_number(StringView string)
561561
{
562562
auto code_point_at = [&](size_t index) -> u32 {
563563
if (index < string.length())
@@ -630,7 +630,7 @@ double Tokenizer::convert_a_string_to_a_number(StringView string)
630630
VERIFY(position == string.length());
631631

632632
// Return the number s·(i + f·10^-d)·10^te.
633-
return sign * (integer_part + fractional_part * pow(10, -fractional_digits)) * pow(10, exponent_sign * exponent);
633+
return sign * (integer_part + fractional_part * powf(10, -fractional_digits)) * powf(10, exponent_sign * exponent);
634634
}
635635

636636
// https://www.w3.org/TR/css-syntax-3/#consume-name

Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class U32Triplet {
6060
class CSSNumber {
6161
public:
6262
String string;
63-
double value { 0 };
63+
float value { 0 };
6464
Token::NumberType type {};
6565
};
6666

@@ -90,7 +90,7 @@ class Tokenizer {
9090
[[nodiscard]] Token consume_a_numeric_token();
9191
[[nodiscard]] Token consume_an_ident_like_token();
9292
[[nodiscard]] CSSNumber consume_a_number();
93-
[[nodiscard]] double convert_a_string_to_a_number(StringView);
93+
[[nodiscard]] float convert_a_string_to_a_number(StringView);
9494
[[nodiscard]] String consume_a_name();
9595
[[nodiscard]] u32 consume_escaped_code_point();
9696
[[nodiscard]] Token consume_a_url_token();

0 commit comments

Comments
 (0)