Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random data on formatting integers with Clang with Microsoft CodeGen #519

Closed
Cleroth opened this issue Jun 16, 2017 · 4 comments
Closed

Comments

@Cleroth
Copy link
Contributor

Cleroth commented Jun 16, 2017

Put simply, when I write this:

cout << fmt::format("{}", 100u) << "\n";
cout << fmt::format("{}", 100) << "\n";
cout << fmt::format("{}", "aoeu") << "\n";
cout << fmt::format("{}", 2.5) << "\n";
cout << fmt::format("{} - {} - {}", 2.5, 3, "a") << "\n";

I get:

♀õôw100
0¸+
aoeu
2.5
2.5 - ÍשO/☺P§3 - a

The characters seem to be random, varying every time I run the program.
I have ostreams included, if that matters.

Compiler: Visual Studio 2017 - Clang with Microsoft CodeGen (v141_clang_c2)

@Cleroth
Copy link
Contributor Author

Cleroth commented Jun 16, 2017

The problem occurs because of FMT_BUILTIN_CLZ. The intrinsic version is wrong.
# define FMT_BUILTIN_CLZ(n) __builtin_clz(n)
should be:
# define FMT_BUILTIN_CLZ(n) (31 - __builtin_clz(n))

And similar for the 64-bit version.

@Cleroth
Copy link
Contributor Author

Cleroth commented Jun 16, 2017

Also, I'm concerned about the intrinsic being used by default with some compilers. I'm not sure if all current CPUs actually support it.

@vitaut
Copy link
Contributor

vitaut commented Jun 17, 2017

Thanks for the bug report and the PR. As I commented on the PR, a simple workaround is to conditionally compile FMT_BUILTIN_CLZ.

Also, I'm concerned about the intrinsic being used by default with some compilers. I'm not sure if all current CPUs actually support it.

I would expect compilers provide a fallback implementation in the runtime.

@vitaut
Copy link
Contributor

vitaut commented Jun 17, 2017

Fixed in 569c5bd.

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

No branches or pull requests

2 participants