]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Simplify a function's implementation for tensors. 16460/head
authorWolfgang Bangerth <bangerth@colostate.edu>
Fri, 12 Jan 2024 00:53:59 +0000 (17:53 -0700)
committerWolfgang Bangerth <bangerth@colostate.edu>
Fri, 12 Jan 2024 00:53:59 +0000 (17:53 -0700)
Specifically, now that we can use 'if constexpr', we can avoid using template
dispatch which almost certainly is slower to compile and leads to larger
libraries.

include/deal.II/base/tensor.h

index 56eaa02ef572c367d22dab0a22cef7c6499f2ad5..0f2ff7de515bbbeeae50a525d4b04cec83b7f207 100644 (file)
@@ -2103,54 +2103,6 @@ DEAL_II_HOST_DEVICE constexpr inline DEAL_II_ALWAYS_INLINE
 }
 
 
-namespace internal
-{
-  namespace TensorImplementation
-  {
-    template <int rank,
-              int dim,
-              typename Number,
-              typename OtherNumber,
-              std::enable_if_t<
-                !std::is_integral<
-                  typename ProductType<Number, OtherNumber>::type>::value,
-                int> = 0>
-    constexpr DEAL_II_HOST_DEVICE inline DEAL_II_ALWAYS_INLINE
-      Tensor<rank, dim, typename ProductType<Number, OtherNumber>::type>
-      division_operator(const Tensor<rank, dim, Number> &t,
-                        const OtherNumber               &factor)
-    {
-      Tensor<rank, dim, typename ProductType<Number, OtherNumber>::type> tt;
-      const Number inverse_factor = Number(1.) / factor;
-      // recurse over the base objects
-      for (unsigned int d = 0; d < dim; ++d)
-        tt[d] = t[d] * inverse_factor;
-      return tt;
-    }
-
-
-    template <int rank,
-              int dim,
-              typename Number,
-              typename OtherNumber,
-              std::enable_if_t<
-                std::is_integral<
-                  typename ProductType<Number, OtherNumber>::type>::value,
-                int> = 0>
-    constexpr DEAL_II_HOST_DEVICE inline DEAL_II_ALWAYS_INLINE
-      Tensor<rank, dim, typename ProductType<Number, OtherNumber>::type>
-      division_operator(const Tensor<rank, dim, Number> &t,
-                        const OtherNumber               &factor)
-    {
-      Tensor<rank, dim, typename ProductType<Number, OtherNumber>::type> tt;
-      // recurse over the base objects
-      for (unsigned int d = 0; d < dim; ++d)
-        tt[d] = t[d] / factor;
-      return tt;
-    }
-  } // namespace TensorImplementation
-} // namespace internal
-
 
 /**
  * Division of a tensor of general rank with a scalar number. See the
@@ -2169,7 +2121,22 @@ constexpr DEAL_II_HOST_DEVICE inline DEAL_II_ALWAYS_INLINE
                               typename EnableIfScalar<OtherNumber>::type>::type>
   operator/(const Tensor<rank, dim, Number> &t, const OtherNumber &factor)
 {
-  return internal::TensorImplementation::division_operator(t, factor);
+  Tensor<rank, dim, typename ProductType<Number, OtherNumber>::type> tt;
+  if constexpr (std::is_integral<
+                  typename ProductType<Number, OtherNumber>::type>::value)
+    {
+      // recurse over the base objects
+      for (unsigned int d = 0; d < dim; ++d)
+        tt[d] = t[d] / factor;
+    }
+  else
+    {
+      const Number inverse_factor = Number(1.) / factor;
+      // recurse over the base objects
+      for (unsigned int d = 0; d < dim; ++d)
+        tt[d] = t[d] * inverse_factor;
+    }
+  return tt;
 }
 
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.