Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Core Modules
dune-common
Commits
55370fb3
Verified
Commit
55370fb3
authored
4 months ago
by
Santiago Ospina De Los Ríos
Browse files
Options
Downloads
Patches
Plain Diff
Format document
parent
4885d6fa
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Pipeline
#75725
failed
4 months ago
Stage: .pre
Stage: test
Stage: code_quality
Stage: downstream
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
dune/common/std/cmath.hh
+47
-51
47 additions, 51 deletions
dune/common/std/cmath.hh
with
47 additions
and
51 deletions
dune/common/std/cmath.hh
+
47
−
51
View file @
55370fb3
// -*- 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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment