Skip to content

Yet another simplification of Dragonbox - #2984

Merged
vitaut merged 3 commits into
fmtlib:masterfrom
jk-jeon:master
Jul 14, 2022
Merged

vitaut merged 3 commits into
fmtlib:masterfrom
jk-jeon:master

Conversation

@jk-jeon

@jk-jeon jk-jeon commented Jul 12, 2022

Copy link
Copy Markdown
Contributor

jk-jeon/dragonbox#31 pointed out that I was doing something silly. I corrected that, which simplifies the source code a little bit (and the generated assembly as well), while the flow of the logic is not affected. Also, I replaced several logical operators into bitwise operators which seems to improve the code generation a little bit.

@vitaut vitaut left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice simplification!

Comment thread include/fmt/format-inl.h
if (r < deltai) {
// Exclude the right endpoint if necessary.
if (r == 0 && z_mul.is_integer && !include_right_endpoint) {
if (r == 0 && (z_mul.is_integer & !include_right_endpoint)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why switch to bitwise AND? Better codegen?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. With logical AND, it generates three test instructions and three conditional branches. With bitwise AND, it generates two test instructions and two conditional branches. I thought compilers are aware of how to optimize this when all the things are just booleans variables (thus no short-circuit is really involved), but apparently they don't bother doing so. I am not sure why...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, compilers are not sure if all boolean variables involved are really just 0 or 1. Like, if one is 2 and another is 1, they are both true so && should give true, but & will give 0. It is wrong from the first place to use bool for things with more than two states, but maybe the current standard allows bool variables to hold nonsensical values without UB.

Comment thread include/fmt/format.h
Comment on lines -1279 to -1280
static const int divisibility_check_by_5_threshold = 39;
static const int case_fc_pm_half_lower_threshold = -1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be removed from float_info<double> as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha. Another careless mistake. Sorry about that, it's fixed now.

@vitaut
vitaut merged commit 688a627 into fmtlib:master Jul 14, 2022
@vitaut

vitaut commented Jul 14, 2022

Copy link
Copy Markdown
Contributor

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants