Skip to content

P0533R9 constexpr For <cmath> And <cstdlib> - #6413

Merged
Stephan T. Lavavej (StephanTLavavej) merged 85 commits into
microsoft:mainfrom
StephanTLavavej:constexpr-cmath-23
Aug 24, 2026
Merged

P0533R9 constexpr For <cmath> And <cstdlib>#6413
Stephan T. Lavavej (StephanTLavavej) merged 85 commits into
microsoft:mainfrom
StephanTLavavej:constexpr-cmath-23

Conversation

@StephanTLavavej

Copy link
Copy Markdown
Member

Fixes #2530 (C++23's Final Boss) and works towards #3789 (C++26's Miniboss) for MSVC. I've filed #6412 to track implementing this for Clang and EDG.

Mirrors internal MSVC-PR-767459. Thanks to Cody Miller (@Codiferous) for figuring out how to do this without UCRT changes, implementing this in the compiler, and writing the initial library changes.

The trick here is that a new compiler option (/experimental:mathlib in the current MSVC Build Tools Preview, /Zc:cmath very soon) enables what Cody calls "header duplexing", where inclusions of <math.h> and <stdlib.h> are replaced with inclusions of the new <__msvc_math.hpp> and <__msvc_stdlib.hpp> here, allowing us to light them up for C++23/26 without requiring UCRT header changes which are a big headache (and would be WinSDK-dependent). We may be able to eliminate this trickery in the future, but it would be good to completely finalize what we need for C++23/26 and MSVC/Clang/EDG before attempting to push changes into the UCRT.

Cody's new builtins are powered by LLVM libc, which provides highly accurate implementations. This avoids baking the notorious accuracy issues present in the UCRT, into the compiler's new constexpr builtins, and also avoids divergence between compiletime and runtime behavior. LLVM libc provides everything we need for C++23, but some functions needed for C++26 are not yet implemented by LLVM libc, so we have workarounds to continue using the UCRT there.

The test I'm adding exercises everything at compiletime and runtime where possible, but does not attempt to be a careful precision test.

For the first time, we are adding exports to a satellite DLL, specifically adding Boost.Math's implementations of lgamma and tgamma to the Special Math satellite, because LLVM libc currently lacks them. We have refrained from doing so in the past, but this is permitted by our binary compatibility requirements (which were somewhat recently exercised by the constexpr mutex constructor change).

…aded classification/comparison functions need this.
Copilot AI balanced review requested due to automatic review settings August 22, 2026 21:48
@StephanTLavavej Stephan T. Lavavej (StephanTLavavej) added cxx23 C++23 feature cxx26 C++26 feature labels Aug 22, 2026
@github-project-automation github-project-automation Bot moved this to Initial Review in STL Code Reviews Aug 22, 2026
@azure-pipelines

This comment was marked as resolved.

This comment was marked as resolved.

Comment thread stl/inc/__msvc_math.hpp
Copilot AI review requested due to automatic review settings August 23, 2026 20:49

This comment was marked as resolved.

Comment thread stl/inc/__msvc_math.hpp
@StephanTLavavej Stephan T. Lavavej (StephanTLavavej) moved this from Final Review to Merging in STL Code Reviews Aug 24, 2026
@StephanTLavavej
Stephan T. Lavavej (StephanTLavavej) merged commit fbb868b into microsoft:main Aug 24, 2026
48 checks passed
@github-project-automation github-project-automation Bot moved this from Merging to Done in STL Code Reviews Aug 24, 2026
@StephanTLavavej
Stephan T. Lavavej (StephanTLavavej) deleted the constexpr-cmath-23 branch August 24, 2026 18:12

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great to see this in. I'm leaving partial comments as I was mid-review as it got merged. NOTE: I only made it as far as this commit: 177ed2a .

Comment thread stl/src/special_math.cpp
Comment thread stl/inc/__msvc_stdlib.hpp
Comment on lines +11 to +12
// * Changed code to support the MSVC frontend intercepting inclusions of <stdlib.h>.
// * Changed code for C++23's P0533R9 "constexpr For <cmath> And <cstdlib>".

@davidmrdavid David Justo (davidmrdavid) Aug 24, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

non blocking nit: these 2 bullet points are summarizing what could be really large changes. It would be neat if we could summarize in a bit more details what these changes are, in case someone needs to reconstruct them in the future.

This is different from the preceding bullet points, which are a lot more mechanical and easier to understand: formatting, code is removed under macros, etc.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is really "diff the two files and this describes what you're seeing". A more detailed description would just repeat the code, and would be harder to keep in sync.

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

Labels

cxx23 C++23 feature cxx26 C++26 feature

6 participants