2
1

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. ---
  9. .../detail/bernoulli_details.hpp | 10 +++++++---
  10. libs/math/test/Jamfile.v2 | 3 +++
  11. test/compile_test/bernoulli_no_atomic_d.cpp | 14 ++++++++++++++
  12. test/compile_test/bernoulli_no_atomic_fail.cpp | 15 +++++++++++++++
  13. test/compile_test/bernoulli_no_atomic_mp.cpp | 16 ++++++++++++++++
  14. 5 files changed, 55 insertions(+), 3 deletions(-)
  15. create mode 100644 test/compile_test/bernoulli_no_atomic_d.cpp
  16. create mode 100644 test/compile_test/bernoulli_no_atomic_fail.cpp
  17. create mode 100644 test/compile_test/bernoulli_no_atomic_mp.cpp
  18. diff --git a/boost/math/special_functions/detail/bernoulli_details.hpp b/boost/math/special_functions/detail/bernoulli_details.hpp
  19. index cf35545264..8519b7c89c 100644
  20. --- a/boost/math/special_functions/detail/bernoulli_details.hpp
  21. +++ b/boost/math/special_functions/detail/bernoulli_details.hpp
  22. @@ -360,7 +360,7 @@ class bernoulli_numbers_cache
  23. return out;
  24. }
  25. - #ifndef BOOST_HAS_THREADS
  26. + #if !defined(BOOST_HAS_THREADS) || defined(BOOST_MATH_BERNOULLI_UNTHREADED)
  27. //
  28. // Single threaded code, very simple:
  29. //
  30. @@ -382,6 +382,8 @@ class bernoulli_numbers_cache
  31. *out = (i >= m_overflow_limit) ? policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, T(i), pol) : bn[i];
  32. ++out;
  33. }
  34. + #elif defined(BOOST_MATH_NO_ATOMIC_INT)
  35. + 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.");
  36. #else
  37. //
  38. // Double-checked locking pattern, lets us access cached already cached values
  39. @@ -464,7 +466,7 @@ class bernoulli_numbers_cache
  40. return out;
  41. }
  42. - #ifndef BOOST_HAS_THREADS
  43. + #if !defined(BOOST_HAS_THREADS) || defined(BOOST_MATH_BERNOULLI_UNTHREADED)
  44. //
  45. // Single threaded code, very simple:
  46. //
  47. @@ -494,6 +496,8 @@ class bernoulli_numbers_cache
  48. }
  49. ++out;
  50. }
  51. + #elif defined(BOOST_MATH_NO_ATOMIC_INT)
  52. + 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.");
  53. #else
  54. //
  55. // Double-checked locking pattern, lets us access cached already cached values
  56. @@ -555,7 +559,7 @@ class bernoulli_numbers_cache
  57. // The value at which we know overflow has already occurred for the Bn:
  58. std::size_t m_overflow_limit;
  59. - #ifdef BOOST_HAS_THREADS
  60. + #if defined(BOOST_HAS_THREADS) && !defined(BOOST_MATH_NO_ATOMIC_INT)
  61. std::mutex m_mutex;
  62. atomic_counter_type m_counter, m_current_precision;
  63. #else
  64. diff --git a/libs/math/test/Jamfile.v2 b/libs/math/test/Jamfile.v2
  65. index 52fb87f5e5..3ac63f9279 100644
  66. --- a/libs/math/test/Jamfile.v2
  67. +++ b/libs/math/test/Jamfile.v2
  68. @@ -1137,6 +1137,9 @@ test-suite misc :
  69. # [ run __temporary_test.cpp test_instances//test_instances : : : <test-info>always_show_run_output <pch>off ]
  70. [ compile test_no_long_double_policy.cpp ]
  71. + [ compile compile_test/bernoulli_no_atomic_d.cpp ]
  72. + [ compile compile_test/bernoulli_no_atomic_mp.cpp ]
  73. + [ compile-fail compile_test/bernoulli_no_atomic_fail.cpp ]
  74. ;
  75. test-suite interpolators :
  76. diff --git a/test/compile_test/bernoulli_no_atomic_d.cpp b/test/compile_test/bernoulli_no_atomic_d.cpp
  77. new file mode 100644
  78. index 0000000000..61926f7e1f
  79. --- /dev/null
  80. +++ b/test/compile_test/bernoulli_no_atomic_d.cpp
  81. @@ -0,0 +1,14 @@
  82. +// (C) Copyright John Maddock 2021.
  83. +// Use, modification and distribution are subject to the
  84. +// Boost Software License, Version 1.0. (See accompanying file
  85. +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  86. +
  87. +#define BOOST_MATH_NO_ATOMIC_INT
  88. +
  89. +#include <boost/math/special_functions/bernoulli.hpp>
  90. +#include "test_compile_result.hpp"
  91. +
  92. +void compile_and_link_test()
  93. +{
  94. + check_result<double>(boost::math::bernoulli_b2n<double>(4));
  95. +}
  96. diff --git a/test/compile_test/bernoulli_no_atomic_fail.cpp b/test/compile_test/bernoulli_no_atomic_fail.cpp
  97. new file mode 100644
  98. index 0000000000..bbd7152412
  99. --- /dev/null
  100. +++ b/test/compile_test/bernoulli_no_atomic_fail.cpp
  101. @@ -0,0 +1,15 @@
  102. +// (C) Copyright John Maddock 2021.
  103. +// Use, modification and distribution are subject to the
  104. +// Boost Software License, Version 1.0. (See accompanying file
  105. +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  106. +
  107. +#define BOOST_MATH_NO_ATOMIC_INT
  108. +
  109. +#include <boost/math/special_functions/bernoulli.hpp>
  110. +#include <boost/multiprecision/cpp_bin_float.hpp>
  111. +#include "test_compile_result.hpp"
  112. +
  113. +void compile_and_link_test()
  114. +{
  115. + check_result<boost::multiprecision::cpp_bin_float_50>(boost::math::bernoulli_b2n<boost::multiprecision::cpp_bin_float_50>(4));
  116. +}
  117. diff --git a/test/compile_test/bernoulli_no_atomic_mp.cpp b/test/compile_test/bernoulli_no_atomic_mp.cpp
  118. new file mode 100644
  119. index 0000000000..8d5a6e78e6
  120. --- /dev/null
  121. +++ b/test/compile_test/bernoulli_no_atomic_mp.cpp
  122. @@ -0,0 +1,16 @@
  123. +// (C) Copyright John Maddock 2021.
  124. +// Use, modification and distribution are subject to the
  125. +// Boost Software License, Version 1.0. (See accompanying file
  126. +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  127. +
  128. +#define BOOST_MATH_NO_ATOMIC_INT
  129. +#define BOOST_MATH_BERNOULLI_UNTHREADED
  130. +
  131. +#include <boost/math/special_functions/bernoulli.hpp>
  132. +#include <boost/multiprecision/cpp_bin_float.hpp>
  133. +#include "test_compile_result.hpp"
  134. +
  135. +void compile_and_link_test()
  136. +{
  137. + check_result<boost::multiprecision::cpp_bin_float_50>(boost::math::bernoulli_b2n<boost::multiprecision::cpp_bin_float_50>(4));
  138. +}