0003-math-make-no-atomics-a-soft-failure-in-bernoulli_details_hpp.patch 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. From 7d482f6ebc356e6ec455ccb5f51a23971bf6ce5b Mon Sep 17 00:00:00 2001
  2. From: jzmaddock <john@johnmaddock.co.uk>
  3. Date: Wed, 1 Sep 2021 20:31:53 +0100
  4. Subject: [PATCH] Make no atomics a soft failure in bernoulli_details.hpp.
  5. Include an "escape macro" so thread safety can be disabled if certain
  6. bernoulli features are to be used in a no-atomics environment. Fixes
  7. https://github.com/boostorg/math/issues/673.
  8. [buildroot@heine.tech:
  9. - backport from boostorg/math 7d482f6ebc356e6ec455ccb5f51a23971bf6ce5b
  10. - alter path to match boost release
  11. ]
  12. Signed-off-by: Michael Nosthoff <buildroot@heine.tech>
  13. ---
  14. .../detail/bernoulli_details.hpp | 10 +++++++---
  15. libs/math/test/Jamfile.v2 | 3 +++
  16. test/compile_test/bernoulli_no_atomic_d.cpp | 14 ++++++++++++++
  17. test/compile_test/bernoulli_no_atomic_fail.cpp | 15 +++++++++++++++
  18. test/compile_test/bernoulli_no_atomic_mp.cpp | 16 ++++++++++++++++
  19. 5 files changed, 55 insertions(+), 3 deletions(-)
  20. create mode 100644 test/compile_test/bernoulli_no_atomic_d.cpp
  21. create mode 100644 test/compile_test/bernoulli_no_atomic_fail.cpp
  22. create mode 100644 test/compile_test/bernoulli_no_atomic_mp.cpp
  23. diff --git a/boost/math/special_functions/detail/bernoulli_details.hpp b/boost/math/special_functions/detail/bernoulli_details.hpp
  24. index cf35545264..8519b7c89c 100644
  25. --- a/boost/math/special_functions/detail/bernoulli_details.hpp
  26. +++ b/boost/math/special_functions/detail/bernoulli_details.hpp
  27. @@ -360,7 +360,7 @@ class bernoulli_numbers_cache
  28. return out;
  29. }
  30. - #ifndef BOOST_HAS_THREADS
  31. + #if !defined(BOOST_HAS_THREADS) || defined(BOOST_MATH_BERNOULLI_UNTHREADED)
  32. //
  33. // Single threaded code, very simple:
  34. //
  35. @@ -382,6 +382,8 @@ class bernoulli_numbers_cache
  36. *out = (i >= m_overflow_limit) ? policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, T(i), pol) : bn[i];
  37. ++out;
  38. }
  39. + #elif defined(BOOST_MATH_NO_ATOMIC_INT)
  40. + static_assert(sizeof(T) == 1, "Unsupported configuration: your platform appears to have no atomic integers. If you are happy with thread-unsafe code, then you may define BOOST_MATH_BERNOULLI_UNTHREADED to suppress this error.");
  41. #else
  42. //
  43. // Double-checked locking pattern, lets us access cached already cached values
  44. @@ -464,7 +466,7 @@ class bernoulli_numbers_cache
  45. return out;
  46. }
  47. - #ifndef BOOST_HAS_THREADS
  48. + #if !defined(BOOST_HAS_THREADS) || defined(BOOST_MATH_BERNOULLI_UNTHREADED)
  49. //
  50. // Single threaded code, very simple:
  51. //
  52. @@ -494,6 +496,8 @@ class bernoulli_numbers_cache
  53. }
  54. ++out;
  55. }
  56. + #elif defined(BOOST_MATH_NO_ATOMIC_INT)
  57. + static_assert(sizeof(T) == 1, "Unsupported configuration: your platform appears to have no atomic integers. If you are happy with thread-unsafe code, then you may define BOOST_MATH_BERNOULLI_UNTHREADED to suppress this error.");
  58. #else
  59. //
  60. // Double-checked locking pattern, lets us access cached already cached values
  61. @@ -555,7 +559,7 @@ class bernoulli_numbers_cache
  62. // The value at which we know overflow has already occurred for the Bn:
  63. std::size_t m_overflow_limit;
  64. - #ifdef BOOST_HAS_THREADS
  65. + #if defined(BOOST_HAS_THREADS) && !defined(BOOST_MATH_NO_ATOMIC_INT)
  66. std::mutex m_mutex;
  67. atomic_counter_type m_counter, m_current_precision;
  68. #else
  69. diff --git a/libs/math/test/Jamfile.v2 b/libs/math/test/Jamfile.v2
  70. index 52fb87f5e5..3ac63f9279 100644
  71. --- a/libs/math/test/Jamfile.v2
  72. +++ b/libs/math/test/Jamfile.v2
  73. @@ -1137,6 +1137,9 @@ test-suite misc :
  74. # [ run __temporary_test.cpp test_instances//test_instances : : : <test-info>always_show_run_output <pch>off ]
  75. [ compile test_no_long_double_policy.cpp ]
  76. + [ compile compile_test/bernoulli_no_atomic_d.cpp ]
  77. + [ compile compile_test/bernoulli_no_atomic_mp.cpp ]
  78. + [ compile-fail compile_test/bernoulli_no_atomic_fail.cpp ]
  79. ;
  80. test-suite interpolators :
  81. diff --git a/test/compile_test/bernoulli_no_atomic_d.cpp b/test/compile_test/bernoulli_no_atomic_d.cpp
  82. new file mode 100644
  83. index 0000000000..61926f7e1f
  84. --- /dev/null
  85. +++ b/test/compile_test/bernoulli_no_atomic_d.cpp
  86. @@ -0,0 +1,14 @@
  87. +// (C) Copyright John Maddock 2021.
  88. +// Use, modification and distribution are subject to the
  89. +// Boost Software License, Version 1.0. (See accompanying file
  90. +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  91. +
  92. +#define BOOST_MATH_NO_ATOMIC_INT
  93. +
  94. +#include <boost/math/special_functions/bernoulli.hpp>
  95. +#include "test_compile_result.hpp"
  96. +
  97. +void compile_and_link_test()
  98. +{
  99. + check_result<double>(boost::math::bernoulli_b2n<double>(4));
  100. +}
  101. diff --git a/test/compile_test/bernoulli_no_atomic_fail.cpp b/test/compile_test/bernoulli_no_atomic_fail.cpp
  102. new file mode 100644
  103. index 0000000000..bbd7152412
  104. --- /dev/null
  105. +++ b/test/compile_test/bernoulli_no_atomic_fail.cpp
  106. @@ -0,0 +1,15 @@
  107. +// (C) Copyright John Maddock 2021.
  108. +// Use, modification and distribution are subject to the
  109. +// Boost Software License, Version 1.0. (See accompanying file
  110. +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  111. +
  112. +#define BOOST_MATH_NO_ATOMIC_INT
  113. +
  114. +#include <boost/math/special_functions/bernoulli.hpp>
  115. +#include <boost/multiprecision/cpp_bin_float.hpp>
  116. +#include "test_compile_result.hpp"
  117. +
  118. +void compile_and_link_test()
  119. +{
  120. + check_result<boost::multiprecision::cpp_bin_float_50>(boost::math::bernoulli_b2n<boost::multiprecision::cpp_bin_float_50>(4));
  121. +}
  122. diff --git a/test/compile_test/bernoulli_no_atomic_mp.cpp b/test/compile_test/bernoulli_no_atomic_mp.cpp
  123. new file mode 100644
  124. index 0000000000..8d5a6e78e6
  125. --- /dev/null
  126. +++ b/test/compile_test/bernoulli_no_atomic_mp.cpp
  127. @@ -0,0 +1,16 @@
  128. +// (C) Copyright John Maddock 2021.
  129. +// Use, modification and distribution are subject to the
  130. +// Boost Software License, Version 1.0. (See accompanying file
  131. +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  132. +
  133. +#define BOOST_MATH_NO_ATOMIC_INT
  134. +#define BOOST_MATH_BERNOULLI_UNTHREADED
  135. +
  136. +#include <boost/math/special_functions/bernoulli.hpp>
  137. +#include <boost/multiprecision/cpp_bin_float.hpp>
  138. +#include "test_compile_result.hpp"
  139. +
  140. +void compile_and_link_test()
  141. +{
  142. + check_result<boost::multiprecision::cpp_bin_float_50>(boost::math::bernoulli_b2n<boost::multiprecision::cpp_bin_float_50>(4));
  143. +}