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.
10 - backport from boostorg/math 7d482f6ebc356e6ec455ccb5f51a23971bf6ce5b
11 - alter path to match boost release
13 Signed-off-by: Michael Nosthoff <buildroot@heine.tech>
15 Upstream-Status: Backport [https://github.com/boostorg/math/pull/684/commits/7d482f6ebc356e6ec455ccb5f51a23971bf6ce5b]
16 .../detail/bernoulli_details.hpp | 10 +++++++---
17 libs/math/test/Jamfile.v2 | 3 +++
18 test/compile_test/bernoulli_no_atomic_d.cpp | 14 ++++++++++++++
19 test/compile_test/bernoulli_no_atomic_fail.cpp | 15 +++++++++++++++
20 test/compile_test/bernoulli_no_atomic_mp.cpp | 16 ++++++++++++++++
21 5 files changed, 55 insertions(+), 3 deletions(-)
22 create mode 100644 test/compile_test/bernoulli_no_atomic_d.cpp
23 create mode 100644 test/compile_test/bernoulli_no_atomic_fail.cpp
24 create mode 100644 test/compile_test/bernoulli_no_atomic_mp.cpp
26 diff --git a/boost/math/special_functions/detail/bernoulli_details.hpp b/boost/math/special_functions/detail/bernoulli_details.hpp
27 index cf35545264..8519b7c89c 100644
28 --- a/boost/math/special_functions/detail/bernoulli_details.hpp
29 +++ b/boost/math/special_functions/detail/bernoulli_details.hpp
30 @@ -360,7 +360,7 @@ class bernoulli_numbers_cache
34 - #ifndef BOOST_HAS_THREADS
35 + #if !defined(BOOST_HAS_THREADS) || defined(BOOST_MATH_BERNOULLI_UNTHREADED)
37 // Single threaded code, very simple:
39 @@ -382,6 +382,8 @@ class bernoulli_numbers_cache
40 *out = (i >= m_overflow_limit) ? policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, T(i), pol) : bn[i];
43 + #elif defined(BOOST_MATH_NO_ATOMIC_INT)
44 + 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.");
47 // Double-checked locking pattern, lets us access cached already cached values
48 @@ -464,7 +466,7 @@ class bernoulli_numbers_cache
52 - #ifndef BOOST_HAS_THREADS
53 + #if !defined(BOOST_HAS_THREADS) || defined(BOOST_MATH_BERNOULLI_UNTHREADED)
55 // Single threaded code, very simple:
57 @@ -494,6 +496,8 @@ class bernoulli_numbers_cache
61 + #elif defined(BOOST_MATH_NO_ATOMIC_INT)
62 + 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.");
65 // Double-checked locking pattern, lets us access cached already cached values
66 @@ -555,7 +559,7 @@ class bernoulli_numbers_cache
67 // The value at which we know overflow has already occurred for the Bn:
68 std::size_t m_overflow_limit;
70 - #ifdef BOOST_HAS_THREADS
71 + #if defined(BOOST_HAS_THREADS) && !defined(BOOST_MATH_NO_ATOMIC_INT)
73 atomic_counter_type m_counter, m_current_precision;
75 diff --git a/libs/math/test/Jamfile.v2 b/libs/math/test/Jamfile.v2
76 index 52fb87f5e5..3ac63f9279 100644
77 --- a/libs/math/test/Jamfile.v2
78 +++ b/libs/math/test/Jamfile.v2
79 @@ -1137,6 +1137,9 @@ test-suite misc :
81 # [ run __temporary_test.cpp test_instances//test_instances : : : <test-info>always_show_run_output <pch>off ]
82 [ compile test_no_long_double_policy.cpp ]
83 + [ compile compile_test/bernoulli_no_atomic_d.cpp ]
84 + [ compile compile_test/bernoulli_no_atomic_mp.cpp ]
85 + [ compile-fail compile_test/bernoulli_no_atomic_fail.cpp ]
88 test-suite interpolators :
89 diff --git a/test/compile_test/bernoulli_no_atomic_d.cpp b/test/compile_test/bernoulli_no_atomic_d.cpp
91 index 0000000000..61926f7e1f
93 +++ b/test/compile_test/bernoulli_no_atomic_d.cpp
95 +// (C) Copyright John Maddock 2021.
96 +// Use, modification and distribution are subject to the
97 +// Boost Software License, Version 1.0. (See accompanying file
98 +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
100 +#define BOOST_MATH_NO_ATOMIC_INT
102 +#include <boost/math/special_functions/bernoulli.hpp>
103 +#include "test_compile_result.hpp"
105 +void compile_and_link_test()
107 + check_result<double>(boost::math::bernoulli_b2n<double>(4));
109 diff --git a/test/compile_test/bernoulli_no_atomic_fail.cpp b/test/compile_test/bernoulli_no_atomic_fail.cpp
111 index 0000000000..bbd7152412
113 +++ b/test/compile_test/bernoulli_no_atomic_fail.cpp
115 +// (C) Copyright John Maddock 2021.
116 +// Use, modification and distribution are subject to the
117 +// Boost Software License, Version 1.0. (See accompanying file
118 +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
120 +#define BOOST_MATH_NO_ATOMIC_INT
122 +#include <boost/math/special_functions/bernoulli.hpp>
123 +#include <boost/multiprecision/cpp_bin_float.hpp>
124 +#include "test_compile_result.hpp"
126 +void compile_and_link_test()
128 + check_result<boost::multiprecision::cpp_bin_float_50>(boost::math::bernoulli_b2n<boost::multiprecision::cpp_bin_float_50>(4));
130 diff --git a/test/compile_test/bernoulli_no_atomic_mp.cpp b/test/compile_test/bernoulli_no_atomic_mp.cpp
132 index 0000000000..8d5a6e78e6
134 +++ b/test/compile_test/bernoulli_no_atomic_mp.cpp
136 +// (C) Copyright John Maddock 2021.
137 +// Use, modification and distribution are subject to the
138 +// Boost Software License, Version 1.0. (See accompanying file
139 +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
141 +#define BOOST_MATH_NO_ATOMIC_INT
142 +#define BOOST_MATH_BERNOULLI_UNTHREADED
144 +#include <boost/math/special_functions/bernoulli.hpp>
145 +#include <boost/multiprecision/cpp_bin_float.hpp>
146 +#include "test_compile_result.hpp"
148 +void compile_and_link_test()
150 + check_result<boost::multiprecision::cpp_bin_float_50>(boost::math::bernoulli_b2n<boost::multiprecision::cpp_bin_float_50>(4));