34#if defined(__STDC_IEC_559__) and defined(__GLIBC__) and defined(__x86_64__)
35#define ESPRESSO_FPE_USING_GLIBC_X86_64
36#elif defined(__arm64__) and defined(__APPLE__)
37#define ESPRESSO_FPE_USING_APPLE_ARM_64
40fe_trap::global_state_params fe_trap::global_state{{}, {}};
43#if defined(ESPRESSO_FPE_USING_GLIBC_X86_64)
45 m_flags = parse_excepts(
excepts);
50#elif defined(ESPRESSO_FPE_USING_APPLE_ARM_64)
52 using fpcr_t =
decltype(std::fenv_t::__fpcr);
53 m_flags = parse_excepts(
excepts);
59 env.__fpcr |=
static_cast<fpcr_t>(m_flags);
66#error "FE not supported"
72#if defined(ESPRESSO_FPE_USING_GLIBC_X86_64)
78#elif defined(ESPRESSO_FPE_USING_APPLE_ARM_64)
80 using fpcr_t =
decltype(std::fenv_t::__fpcr);
87 static_cast<fpcr_t>(m_flags));
95#error "FE not supported"
99int fe_trap::parse_excepts(std::optional<int>
excepts) {
102#if defined(ESPRESSO_FPE_USING_APPLE_ARM_64)
108fe_trap::scoped_instance
110 std::lock_guard<std::mutex>
lock(fe_trap::global_state.mutex);
111 if (fe_trap::global_state.observer.lock()) {
112 throw std::runtime_error(
"Cannot create more than 1 instance of fe_trap");
116 fe_trap::global_state.observer =
watched;
117 return fe_trap::scoped_instance(
watched);
120fe_trap::scoped_instance
122 std::lock_guard<std::mutex>
lock(fe_trap::global_state.mutex);
123 if (
auto watched = fe_trap::global_state.observer.lock()) {
125 throw std::runtime_error(
"Cannot create more than 1 instance of fe_trap");
128 throw std::invalid_argument(
129 "Cannot mix different exceptions with fe_trap");
131 return fe_trap::scoped_instance(
watched);
135 fe_trap::global_state.observer =
watched;
136 return fe_trap::scoped_instance(
watched);
Floating-point exception trap.
static scoped_instance make_shared_scoped(std::optional< int > excepts=std::nullopt)
Generate a shared trap with the lifetime of the current scope.
static scoped_instance make_unique_scoped(std::optional< int > excepts=std::nullopt)
Generate a unique trap with the lifetime of the current scope.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.