Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
precision_scope.hpp
Go to the documentation of this file.
1#pragma once
2
3// This header belongs only at the external-provider boundary. MPLAPACK must be
4// included before its bundled wrapper so both use the same context ABI.
5#include <mpblas_mpfr.h>
7
8namespace uni20::mplapack::detail
9{
12class precision_scope {
13 public:
14 explicit precision_scope(Precision p)
15 : precision_(mpfr_get_default_prec()), rounding_(mpfr_get_default_rounding_mode()), emin_(mpfr_get_emin()),
16 emax_(mpfr_get_emax()), stable_rounding_(gmpfrxx_mkII::detail::stable_mpfr_rounding_mode_storage()),
17 real_initialized_(gmpfrxx_mkII::detail::mpfr_defaults_initialized_storage()),
18 complex_initialized_(mpfrxx::mpc_defaults_initialized_storage()),
19 complex_precision_(mpfrxx::mpc_precision_override_storage()),
20 complex_rounding_(mpfrxx::mpc_rounding_override_storage())
21 {
22 auto const bits = uni20::detail::mpfr_precision(p); // Validate before mutating provider defaults.
23 mpfrxx::initialize_thread_defaults();
24 mpfrxx::initialize_mpc_defaults_for_current_thread();
25 // Wrapper first-use initialization may read exponent bounds from the
26 // environment. Preserve Uni20's executing-thread exponent range instead.
27 gmpfrxx_mkII::detail::set_mpfr_default_exponent_range(emin_, emax_);
28 mpfrxx::set_default_precision_bits(bits);
29 mpfrxx::set_default_rounding_mode(MPFR_RNDN);
30 mpfrxx::set_default_mpc_precision_bits(bits);
31 mpfrxx::set_default_mpc_rounding_mode(MPFR_RNDN);
32 }
33 ~precision_scope() noexcept
34 {
35 mpfr_set_default_prec(precision_);
36 mpfr_set_default_rounding_mode(rounding_);
37 gmpfrxx_mkII::detail::set_mpfr_default_exponent_range(emin_, emax_);
38 gmpfrxx_mkII::detail::stable_mpfr_rounding_mode_storage() = stable_rounding_;
39 gmpfrxx_mkII::detail::mpfr_defaults_initialized_storage() = real_initialized_;
40 mpfrxx::mpc_defaults_initialized_storage() = complex_initialized_;
41 mpfrxx::mpc_precision_override_storage() = complex_precision_;
42 mpfrxx::mpc_rounding_override_storage() = complex_rounding_;
43 }
44 precision_scope(precision_scope const&) = delete;
45 precision_scope& operator=(precision_scope const&) = delete;
46
47 private:
48 mpfr_prec_t precision_;
49 mpfr_rnd_t rounding_;
50 mpfr_exp_t emin_, emax_;
51 mpfr_rnd_t stable_rounding_;
52 bool real_initialized_, complex_initialized_;
53 mpfrxx::mpc_precision_override_state complex_precision_;
54 mpfrxx::mpc_rounding_override_state complex_rounding_;
55};
56} // namespace uni20::mplapack::detail
Definition data_table.cpp:8