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

Merge branch 'feature/force-inline' into 'master'

Add preprocessor macro DUNE_FORCE_INLINE

See merge request !1502
parents bec28d49 6d0b8766
No related branches found
No related tags found
1 merge request!1502Add preprocessor macro DUNE_FORCE_INLINE
Pipeline #76286 waiting for manual action
......@@ -25,6 +25,8 @@ SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
## C++: Changelog
- Add preprocessor macro `DUNE_FORCE_INLINE` as a portable attribute to force inlining of functions (if supported).
- Add `bit_width` and `countl_zero` overloads for `bigunsignedint` objects.
- `DUNE_THROW` no longer prevents functions from being used in `constexpr` contexts,
......
......@@ -55,6 +55,7 @@ install(FILES
float_cmp.hh
fmatrix.hh
fmatrixev.hh
forceinline.hh
ftraits.hh
fvector.hh
genericiterator.hh
......
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 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
#ifndef DUNE_COMMON_FORCEINLINE_HH
#define DUNE_COMMON_FORCEINLINE_HH
/**
* \file Provide the macro `DUNE_FORCE_INLINE` that expands to attribute `always_inline`
* or similar depending on the compiler (version). Some information is extracted from
* https://meghprkh.github.io/blog/posts/c++-force-inline.
*
* The effect of DUNE_FORCE_INLINE is typically as follows:
* - Inlining heuristics are disabled and inlining is always attempted regardless
* of optimization level.
* - Ignore `-fno-inline`
* - Ignore the inlining limits hence inlining the function regardless. It also
* inlines functions with allocation calls, which `inline` keyword never does.
*
* Even when marked with DUNE_FORCE_INLINE, the compiler cannot always inline a
* function. Examples for such exceptions are
* - The function is virtual and is called virtually.
* - The program calls a function indirectly via a pointer to the function.
* - The function uses a variable argument list.
*
* \b Example:
* \code{.cpp}
DUNE_FORCE_INLINE int maxInt (int a, int b) { return a < b ? b : a; }
* \endcode
**/
#if defined(__clang__)
// Clang does not generate an error for non-inlinable `always_inline` functions.
#define DUNE_FORCE_INLINE __attribute__((always_inline)) inline
#elif defined(__GNUC__)
// GCC would generate an error if it can't always_inline
#define DUNE_FORCE_INLINE __attribute__((always_inline)) inline
#elif defined(_MSC_VER)
// MSVC generates a warning for for non-inlinable `__forceinline` functions. But
// only if compiled with any "inline expansion" optimization (`/Ob<n>`). This is
// present with `/O1` or `/O2`. We promote this warning to an error.
#pragma warning(error: 4714)
#define DUNE_FORCE_INLINE [[msvc::forceinline]]
#else
// fallback to `inline` in case the compiler cannot be detected.
#define DUNE_FORCE_INLINE inline
#endif
#endif // DUNE_COMMON_FORCEINLINE_HH
......@@ -169,6 +169,9 @@ dune_add_test(SOURCES fmatrixtest.cc
LABELS quick)
add_dune_vc_flags(fmatrixtest)
dune_add_test(SOURCES forceinlinetest.cc
LABELS quick)
dune_add_test(SOURCES fvectortest.cc
LABELS quick)
......
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 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
#include <dune/common/forceinline.hh>
#include <dune/common/test/testsuite.hh>
template <unsigned int n>
DUNE_FORCE_INLINE int compute_sum ()
{
if constexpr (n <= 1)
return n;
else
return n + compute_sum<n-1>();
}
int main ()
{
Dune::TestSuite test;
test.check(compute_sum<100>() == (100 * 101)/2);
return test.exit();
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment