Skip to content
Snippets Groups Projects
Verified Commit 55370fb3 authored by Santiago Ospina De Los Ríos's avatar Santiago Ospina De Los Ríos
Browse files

Format document

parent 4885d6fa
Branches
Tags
No related merge requests found
Pipeline #75725 failed
// -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set ts=8 sw=2 et sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file
// LICENSE.md in module root SPDX-License-Identifier:
// LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_COMMON_STD_CMATH_HH
#define DUNE_COMMON_STD_CMATH_HH
......@@ -9,68 +10,63 @@
#if __has_include(<version>)
#include <version>
#if (__cpp_lib_constexpr_cmath < 202306L) and (__cpp_lib_is_constant_evaluated >= 201811L)
#include <type_traits>
#if (__cpp_lib_constexpr_cmath < 202306L) and \
(__cpp_lib_is_constant_evaluated >= 201811L)
#include <limits>
#include <type_traits>
#include <utility>
#endif
#endif
namespace Dune
{
namespace Dune {
namespace Std
{
namespace Std {
#if (__cpp_lib_constexpr_cmath < 202202L) and (__cpp_lib_is_constant_evaluated >= 201811L)
// backport for constexpr functions between C++20 and C++23
template <class T>
constexpr T abs(T t)
{
if (std::is_constant_evaluated())
{
return (t < 0) ? -t : t;
}
else
{
using std::abs;
return std::abs(t);
}
}
#if (__cpp_lib_constexpr_cmath < 202202L) and \
(__cpp_lib_is_constant_evaluated >= 201811L)
// backport for constexpr functions between C++20 and C++23
template<class T>
constexpr T
abs(T t)
{
if (std::is_constant_evaluated()) {
return (t < 0) ? -t : t;
} else {
using std::abs;
return std::abs(t);
}
}
template <class T>
constexpr auto sqrt(T t)
requires (std::is_floating_point_v<T> || std::is_integral_v<T>)
{
if (std::is_constant_evaluated())
{
using TT = std::conditional_t<std::is_floating_point_v<T>, T, double>;
if (t >= TT{0} and t < std::numeric_limits<TT>::infinity())
{
// use Heron's method: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Heron's_method
TT curr = t, prev = 0;
while (curr != prev)
prev = std::exchange(curr, TT{0.5} * (curr + TT{t} / curr));
return curr;
}
else
{
return std::numeric_limits<TT>::quiet_NaN();
}
}
else {
using std::sqrt;
return sqrt(t);
}
};
template<class T>
constexpr auto
sqrt(T t)
requires(std::is_floating_point_v<T> || std::is_integral_v<T>)
{
if (std::is_constant_evaluated()) {
using TT = std::conditional_t<std::is_floating_point_v<T>, T, double>;
if (t >= TT{ 0 } and t < std::numeric_limits<TT>::infinity()) {
// use Heron's method:
// https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Heron's_method
TT curr = t, prev = 0;
while (curr != prev)
prev = std::exchange(curr, TT{ 0.5 } * (curr + TT{ t } / curr));
return curr;
} else {
return std::numeric_limits<TT>::quiet_NaN();
}
} else {
using std::sqrt;
return sqrt(t);
}
};
#else
using std::abs;
using std::sqrt;
using std::abs;
using std::sqrt;
#endif
#endif
} // namespace Std
} // namespace Std
} // namespace Dune
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment