Skip to content

Implement and test consumption of Boost.Math as a module#1422

Open
mborland wants to merge 24 commits into
developfrom
module
Open

Implement and test consumption of Boost.Math as a module#1422
mborland wants to merge 24 commits into
developfrom
module

Conversation

@mborland

Copy link
Copy Markdown
Member

This implements module support for a large subset of the library as well as testing with associated infrastructure. Since we don't control our dependencies, consumption of the module only supports standalone mode. A modules.qbk files was added that explicitly lays out limitations that we can't circumvent such as policy configuration macros will not work.

Please let me know what you think @jzmaddock, @NAThompson, @ckormanyos, @ChuanqiXu9

mborland added 16 commits July 15, 2026 11:36
Adds gated C++20 named module support (module boost.math), inert unless
BOOST_MATH_BUILD_MODULE is defined:

* BOOST_MATH_EXPORT / BOOST_MATH_TEST_EXPORT macro block in tools/config.hpp
* module/math.cxx interface unit covering constants and policies
* module/CMakeLists.txt test harness requiring import std
* module/quick_test.cpp smoke test and module/Jamfile for b2
* test/CMakeLists.txt branch for -DBOOST_MATH_BUILD_MODULE=ON
* cmake-module-test CI job (clang-20 + libc++ + CMake 4.4.0)
* Export annotations for constants, policies, error handling, and the
  tools support headers they pull in
* std includes in those headers guarded for module builds
* Namespace scope static constexpr variables converted to
  BOOST_MATH_INLINE_CONSTEXPR (float_constants and friends,
  max_string_digits) for module linkage
Mechanical sweep wrapping every standard library include in
#ifndef BOOST_MATH_BUILD_MODULE so the module interface unit can provide
them through the global module fragment or import std. Also converts the
static constexpr color map data tables to BOOST_MATH_INLINE_CONSTEXPR for
module linkage. No change in any existing configuration; validated against
the full compile_test suite and the module build.
* BOOST_MATH_EXPORT on the public declarations in math_fwd.hpp,
  round_fwd.hpp, unchecked_factorial.hpp, unchecked_bernoulli.hpp and
  every special function header (declaration and definition sites)
* Adds special_functions.hpp to the module interface unit
* rsqrt.hpp, jacobi.hpp and cardinal_b_spline.hpp now include
  tools/config.hpp so the export macro is always defined
* math_unit_test.hpp resolves math entities through the import in
  module mode
* Module test harness gains test_digamma_simple, test_expm1_simple,
  test_log1p_simple, git_issue_1194 and git_issue_1255
* BOOST_MATH_EXPORT on the 38 distribution class templates (fwd.hpp and
  definition sites), every non-member accessor overload, the convenience
  typedefs, the complement machinery and find_location/find_scale
* distributions/fwd.hpp now includes tools/config.hpp so the export
  macro is always defined
* Adds distributions.hpp to the module interface unit
* Module test harness gains git_issue_1294, git_issue_800,
  git_issue_1120 and scipy_issue_17146
* BOOST_MATH_EXPORT on the constexpr cmath function set
* Adds ccmath/ccmath.hpp to the module interface unit
* Module test harness gains ccmath_isinf_test and ccmath_isnan_test
* BOOST_MATH_EXPORT on the root finders, minima, polynomial and its
  operators, rational/polynomial evaluators, series and continued
  fraction machinery, recurrences, norms, condition numbers, color maps,
  expansions, agm and toms748
* Adds the curated tools headers to the module interface unit
* Module test harness gains git_issue_1139, cubic_roots_test and
  quartic_roots_test
* BOOST_MATH_EXPORT on the integrator class templates, trapezoidal and
  the wavelet transform, plus the interpolator class templates and their
  stream operators
* Adds the quadrature and interpolator headers to the module interface
  unit (cardinal_trigonometric and the deprecated boost::math namespace
  shims stay textual only)
* Module test harness gains git_issue_898,
  cardinal_quadratic_b_spline_test and whittaker_shannon_test
* BOOST_MATH_EXPORT on the statistics free functions (univariate,
  bivariate, tests, regression, signal) and the optimization
  algorithms, parameter structs and constraint/convergence policies
* BOOST_MATH_TEST_EXPORT on the optimization detail helpers the
  differential evolution test exercises directly
* Adds the statistics and optimization headers to the module interface
  unit; cma_es is excluded because it requires Eigen
* test_functions_for_optimization.hpp resolves constants through the
  import in module mode
* Module test harness gains test_runs_test, linear_regression_test,
  ljung_box_test, differential_evolution_test and random_search_test
* BOOST_MATH_EXPORT on autodiff (make_fvar, make_ftuple, promote,
  real_cast), finite differences, lanczos smoothing, the complex
  inverse trigonometric functions, quaternion and octonion including
  their macro generated operator sets, and the boost/math_fwd.hpp
  forward declarations
* Fixes a pre-existing ambiguity: autodiff's fully qualified
  differentiation::detail::fvar references are ambiguous whenever
  finite_difference.hpp is visible in the same translation unit, so
  they now name the autodiff_v1 inline namespace explicitly
* Adds differentiation, complex.hpp, quaternion.hpp and octonion.hpp
  to the module interface unit (finite_difference and lanczos_smoothing
  precede autodiff by necessity)
* Module test harness gains quaternion_constexpr_test and
  octonion_test_simple; both tests now include what they use
The standard library maintainers agreed to provide import std in C++20
mode as well as C++23, so the docs no longer claim a C++23 requirement.
The library sources never gated on C++23: the only floor is the C++17
inline variable check in tools/config.hpp.
@codecov

codecov Bot commented Jul 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.40%. Comparing base (8ee12a5) to head (0246361).

Files with missing lines Patch % Lines
include/boost/math/policies/error_handling.hpp 50.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #1422      +/-   ##
===========================================
- Coverage    95.40%   95.40%   -0.01%     
===========================================
  Files          828      828              
  Lines        69072    69071       -1     
===========================================
- Hits         65896    65895       -1     
  Misses        3176     3176              
Files with missing lines Coverage Δ
include/boost/math/complex/acos.hpp 100.00% <ø> (ø)
include/boost/math/complex/acosh.hpp 100.00% <ø> (ø)
include/boost/math/complex/asin.hpp 100.00% <ø> (ø)
include/boost/math/complex/asinh.hpp 100.00% <ø> (ø)
include/boost/math/complex/atan.hpp 71.42% <ø> (ø)
include/boost/math/complex/atanh.hpp 98.46% <ø> (ø)
include/boost/math/complex/details.hpp 100.00% <ø> (ø)
include/boost/math/complex/fabs.hpp 100.00% <ø> (ø)
...clude/boost/math/constants/calculate_constants.hpp 84.81% <ø> (ø)
include/boost/math/constants/constants.hpp 100.00% <ø> (ø)
... and 229 more

... and 30 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8ee12a5...0246361. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mborland

Copy link
Copy Markdown
Member Author

This is green in general and with both GCC and Clang as a module

@ChuanqiXu9 ChuanqiXu9 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Excellent work

Comment thread module/math.cppm
Comment thread doc/overview/modules.qbk
@jzmaddock

Copy link
Copy Markdown
Collaborator

Wow! Looks like good work to me, time will tell what if any consequences there are?

I notice it's not yet tested with msvc which has really rather good module support, has this been tried yet?

Also any word on compile times?

@mborland

Copy link
Copy Markdown
Member Author

Wow! Looks like good work to me, time will tell what if any consequences there are?

I expect so. I think the ecosystem is still feeling it out.

I notice it's not yet tested with msvc which has really rather good module support, has this been tried yet?

I have not tried with MSVC, but I can add that to the CI.

Also any word on compile times?

Clang takes 33 seconds to build the module plus compile and run the 25 tests. I can slice the module build into a separate CI step to measure it directly.

@mborland

Copy link
Copy Markdown
Member Author

The MSVC module is in CI and works. Build times for just the boost.math module:

MSVC - 15 seconds
Clang - 9 seconds
GCC - 6 seconds

The other Boost libraries that support modules all use extern c++ so I have changed our approach to match that.

@ChuanqiXu9

Copy link
Copy Markdown

The MSVC module is in CI and works. Build times for just the boost.math module:

MSVC - 15 seconds Clang - 9 seconds GCC - 6 seconds

The other Boost libraries that support modules all use extern c++ so I have changed our approach to match that.

Generally we prefer to test the time for using/import the modules. As the story modules builds on everything was built once and we can avoid duplications.

e.g., the blog (https://anarthal.github.io/cppblog/modules3) says:

Build performance gains are higher when lots of translation units consume the same library. Building the Boost.Mp11 test suite (which has around 200 translation units) yields the following results:

Headers: 2min 10s.

Modules: 39s (this includes the time required to build the std and Boost modules).

Benchmarks performed on Ubuntu 22.04 with clang-19 and libc++.

This reads very good.

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.

3 participants