2020-06-18 07:55:56 +02:00
|
|
|
// The Construct
|
2018-11-12 00:53:25 +01:00
|
|
|
//
|
2020-06-18 07:55:56 +02:00
|
|
|
// Copyright (C) The Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2020 Jason Volk <jason@zemos.net>
|
2018-11-12 00:53:25 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#pragma once
|
2020-06-18 07:55:56 +02:00
|
|
|
#define HAVE_IRCD_FPE_H
|
2018-11-12 00:53:25 +01:00
|
|
|
|
2020-06-18 07:55:56 +02:00
|
|
|
namespace ircd::fpe
|
2018-11-12 00:53:25 +01:00
|
|
|
{
|
|
|
|
struct errors_handle;
|
2020-08-09 11:10:38 +02:00
|
|
|
struct scope_round;
|
2018-11-12 00:53:25 +01:00
|
|
|
|
2021-03-07 10:34:12 +01:00
|
|
|
template<class T>
|
|
|
|
string_view classify(const T &) noexcept;
|
|
|
|
|
|
|
|
string_view reflect_sicode(const int &) noexcept;
|
|
|
|
string_view reflect(const ushort &flag) noexcept;
|
2018-11-12 00:53:25 +01:00
|
|
|
string_view reflect(const mutable_buffer &, const ushort &flags);
|
2019-03-04 20:23:25 +01:00
|
|
|
|
2020-06-18 10:01:23 +02:00
|
|
|
[[noreturn]] void _throw_errors(const ushort &flags);
|
2018-11-12 00:53:25 +01:00
|
|
|
void throw_errors(const ushort &flags);
|
2020-08-09 11:10:38 +02:00
|
|
|
|
|
|
|
void set_round(const std::float_round_style &style);
|
|
|
|
std::fexcept_t set_excepts(const ushort &flag);
|
2022-03-05 21:21:01 +01:00
|
|
|
|
|
|
|
void debug_info();
|
2020-06-18 07:55:56 +02:00
|
|
|
}
|
2018-11-12 00:53:25 +01:00
|
|
|
|
2020-08-09 11:10:38 +02:00
|
|
|
/// scope_round
|
|
|
|
///
|
|
|
|
struct ircd::fpe::scope_round
|
|
|
|
{
|
|
|
|
int theirs
|
|
|
|
{
|
|
|
|
std::fegetround()
|
|
|
|
};
|
|
|
|
|
|
|
|
scope_round(const std::float_round_style &ours) noexcept
|
|
|
|
{
|
|
|
|
std::fesetround(ours);
|
|
|
|
}
|
|
|
|
|
|
|
|
scope_round(const scope_round &) = delete;
|
|
|
|
scope_round &operator=(const scope_round &) = delete;
|
|
|
|
~scope_round() noexcept
|
|
|
|
{
|
|
|
|
std::fesetround(theirs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// errors_handle
|
|
|
|
///
|
2018-11-13 03:50:09 +01:00
|
|
|
/// Perform a single floating point operation at a time within the scope
|
|
|
|
/// of fpe::errors_handle. After each operation check the floating point
|
|
|
|
/// unit for an error status flag and throw a C++ exception.
|
2020-06-18 07:55:56 +02:00
|
|
|
struct ircd::fpe::errors_handle
|
2018-11-12 00:53:25 +01:00
|
|
|
{
|
2019-03-04 20:23:25 +01:00
|
|
|
std::fexcept_t theirs;
|
2018-11-12 00:53:25 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
ushort pending() const;
|
|
|
|
void throw_pending() const;
|
|
|
|
void clear_pending();
|
|
|
|
|
|
|
|
errors_handle();
|
|
|
|
~errors_handle() noexcept(false);
|
|
|
|
};
|
2020-06-18 10:01:23 +02:00
|
|
|
|
|
|
|
[[gnu::always_inline]] inline
|
|
|
|
ircd::fpe::errors_handle::errors_handle()
|
|
|
|
{
|
|
|
|
syscall(std::fegetexceptflag, &theirs, FE_ALL_EXCEPT);
|
|
|
|
clear_pending();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[gnu::always_inline]] inline
|
|
|
|
ircd::fpe::errors_handle::~errors_handle()
|
|
|
|
noexcept(false)
|
|
|
|
{
|
|
|
|
const unwind reset{[this]
|
|
|
|
{
|
|
|
|
syscall(std::fesetexceptflag, &theirs, FE_ALL_EXCEPT);
|
|
|
|
}};
|
|
|
|
|
|
|
|
throw_pending();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ircd::fpe::errors_handle::clear_pending()
|
|
|
|
{
|
|
|
|
syscall(std::feclearexcept, FE_ALL_EXCEPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ircd::fpe::errors_handle::throw_pending()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto pending
|
|
|
|
{
|
|
|
|
this->pending()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(pending))
|
|
|
|
_throw_errors(pending);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ushort
|
|
|
|
ircd::fpe::errors_handle::pending()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::fetestexcept(FE_ALL_EXCEPT);
|
|
|
|
}
|
2020-08-09 11:10:38 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// ircd::fpe
|
|
|
|
//
|
|
|
|
|
|
|
|
inline std::fexcept_t
|
|
|
|
ircd::fpe::set_excepts(const ushort &flags)
|
|
|
|
{
|
|
|
|
std::fexcept_t theirs;
|
|
|
|
syscall(std::fesetexceptflag, &theirs, flags);
|
|
|
|
return theirs;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ircd::fpe::set_round(const std::float_round_style &style)
|
|
|
|
{
|
|
|
|
syscall(std::fesetround, style);
|
|
|
|
}
|