Skip to content

Commit 6d7216f

Browse files
committed
Mark scalar and initialiser list construction constexpr
1 parent a40de1e commit 6d7216f

4 files changed

Lines changed: 29 additions & 16 deletions

File tree

Fastor/tensor/AbstractTensor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ template<class Derived, FASTOR_INDEX Rank>
1717
class AbstractTensor {
1818
public:
1919
constexpr FASTOR_INLINE AbstractTensor() = default;
20-
FASTOR_INLINE const Derived& self() const {return *static_cast<const Derived*>(this);}
20+
constexpr FASTOR_INLINE const Derived& self() const {return *static_cast<const Derived*>(this);}
2121
FASTOR_INLINE Derived& self() {return *static_cast<Derived*>(this);}
2222

2323
static constexpr FASTOR_INDEX Dimension = Rank;

Fastor/tensor/InitializerListConstructors.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
// Initialiser list constructors
55
//----------------------------------------------------------------------------------------------------------//
66
template<typename U=T, enable_if_t_<is_primitive_v_<U>,bool> = false >
7+
constexpr
78
FASTOR_INLINE Tensor(const std::initializer_list<U> &lst) {
89
static_assert(sizeof...(Rest)==1,"TENSOR RANK MISMATCH WITH LIST-INITIALISER");
9-
#if FASTOR_BOUNDS_CHECK
10+
#if (!defined(NDEBUG) && !defined(FASTOR_ZERO_INITIALISE))
1011
FASTOR_ASSERT(pack_prod<Rest...>::value==lst.size(), "TENSOR SIZE MISMATCH WITH LIST-INITIALISER");
1112
#endif
1213
auto counter = 0;
1314
for (const auto &i: lst) {_data[counter] = i; counter++;}
1415
}
1516

1617
template<typename U=T, enable_if_t_<is_primitive_v_<U>,bool> = false >
18+
constexpr
1719
FASTOR_INLINE Tensor(const std::initializer_list<std::initializer_list<U>> &lst2d) {
1820
static_assert(sizeof...(Rest)==2,"TENSOR RANK MISMATCH WITH LIST-INITIALISER");
19-
#ifndef NDEBUG
21+
#if (!defined(NDEBUG) && !defined(FASTOR_ZERO_INITIALISE))
2022
constexpr FASTOR_INDEX M = get_value<1,Rest...>::value;
2123
constexpr FASTOR_INDEX N = get_value<2,Rest...>::value;
2224
auto size_ = 0;
@@ -38,9 +40,10 @@
3840
}
3941

4042
template<typename U=T, enable_if_t_<is_primitive_v_<U>,bool> = false >
43+
constexpr
4144
FASTOR_INLINE Tensor(const std::initializer_list<std::initializer_list<std::initializer_list<U>>> &lst3d) {
4245
static_assert(sizeof...(Rest)==3,"TENSOR RANK MISMATCH WITH LIST-INITIALISER");
43-
#ifndef NDEBUG
46+
#if (!defined(NDEBUG) && !defined(FASTOR_ZERO_INITIALISE))
4447
constexpr FASTOR_INDEX M = get_value<1,Rest...>::value;
4548
constexpr FASTOR_INDEX N = get_value<2,Rest...>::value;
4649
constexpr FASTOR_INDEX P = get_value<3,Rest...>::value;
@@ -68,9 +71,10 @@
6871
}
6972

7073
template<typename U=T, enable_if_t_<is_primitive_v_<U>,bool> = false >
74+
constexpr
7175
FASTOR_INLINE Tensor(const std::initializer_list<std::initializer_list<std::initializer_list<std::initializer_list<U>>>> &lst4d) {
7276
static_assert(sizeof...(Rest)==4,"TENSOR RANK MISMATCH WITH LIST-INITIALISER");
73-
#ifndef NDEBUG
77+
#if (!defined(NDEBUG) && !defined(FASTOR_ZERO_INITIALISE))
7478
constexpr FASTOR_INDEX M = get_value<1,Rest...>::value;
7579
constexpr FASTOR_INDEX N = get_value<2,Rest...>::value;
7680
constexpr FASTOR_INDEX P = get_value<3,Rest...>::value;

Fastor/tensor/Tensor.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,17 @@ class Tensor: public AbstractTensor<Tensor<T,Rest...>,sizeof...(Rest)> {
4949

5050
// Constructor from a scalar
5151
template<typename U=T, enable_if_t_<is_primitive_v_<U>,bool> = false>
52-
FASTOR_INLINE Tensor(U num) {
52+
constexpr FASTOR_INLINE Tensor(U num) {
53+
#ifdef FASTOR_ZERO_INITIALISE
54+
// This is for compile time initialisation, so it is fine
55+
scalar_type cnum = (scalar_type)num;
56+
FASTOR_INDEX i = 0;
57+
for (; i<size(); ++i) {
58+
_data[i] = cnum;
59+
}
60+
#else
5361
assign(*this, num);
62+
#endif
5463
}
5564

5665
// Initialiser list constructors

Fastor/tensor/TensorAssignment.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,50 +245,50 @@ FASTOR_INLINE void trivial_assign_div(AbstractTensor<Derived,DIM> &dst, U num) {
245245

246246
//----------------------------------------------------------------------------------------------------------//
247247
template<typename Derived, size_t DIM, typename T, size_t ...Rest>
248-
FASTOR_INLINE void assign(AbstractTensor<Derived,DIM> &dst, const Tensor<T,Rest...> &src) {
248+
constexpr FASTOR_INLINE void assign(AbstractTensor<Derived,DIM> &dst, const Tensor<T,Rest...> &src) {
249249
if (dst.self().data()==src.data()) return;
250250
trivial_assign(dst.self(),src);
251251
}
252252
template<typename Derived, size_t DIM, typename T, size_t ...Rest>
253-
FASTOR_INLINE void assign_add(AbstractTensor<Derived,DIM> &dst, const Tensor<T,Rest...> &src) {
253+
constexpr FASTOR_INLINE void assign_add(AbstractTensor<Derived,DIM> &dst, const Tensor<T,Rest...> &src) {
254254
trivial_assign_add(dst.self(),src);
255255
}
256256
template<typename Derived, size_t DIM, typename T, size_t ...Rest>
257-
FASTOR_INLINE void assign_sub(AbstractTensor<Derived,DIM> &dst, const Tensor<T,Rest...> &src) {
257+
constexpr FASTOR_INLINE void assign_sub(AbstractTensor<Derived,DIM> &dst, const Tensor<T,Rest...> &src) {
258258
trivial_assign_sub(dst.self(),src);
259259
}
260260
template<typename Derived, size_t DIM, typename T, size_t ...Rest>
261-
FASTOR_INLINE void assign_mul(AbstractTensor<Derived,DIM> &dst, const Tensor<T,Rest...> &src) {
261+
constexpr FASTOR_INLINE void assign_mul(AbstractTensor<Derived,DIM> &dst, const Tensor<T,Rest...> &src) {
262262
trivial_assign_mul(dst.self(),src);
263263
}
264264
template<typename Derived, size_t DIM, typename T, size_t ...Rest>
265-
FASTOR_INLINE void assign_div(AbstractTensor<Derived,DIM> &dst, const Tensor<T,Rest...> &src) {
265+
constexpr FASTOR_INLINE void assign_div(AbstractTensor<Derived,DIM> &dst, const Tensor<T,Rest...> &src) {
266266
trivial_assign_div(dst.self(),src);
267267
}
268268

269269
template<typename Derived, size_t DIM, typename U,
270270
enable_if_t_<is_primitive_v_<U>,bool> = false>
271-
FASTOR_INLINE void assign(AbstractTensor<Derived,DIM> &dst, U num) {
271+
constexpr FASTOR_INLINE void assign(AbstractTensor<Derived,DIM> &dst, U num) {
272272
trivial_assign(dst.self(),num);
273273
}
274274
template<typename Derived, size_t DIM, typename U,
275275
enable_if_t_<is_primitive_v_<U>,bool> = false>
276-
FASTOR_INLINE void assign_add(AbstractTensor<Derived,DIM> &dst, U num) {
276+
constexpr FASTOR_INLINE void assign_add(AbstractTensor<Derived,DIM> &dst, U num) {
277277
trivial_assign_add(dst.self(),num);
278278
}
279279
template<typename Derived, size_t DIM, typename U,
280280
enable_if_t_<is_primitive_v_<U>,bool> = false>
281-
FASTOR_INLINE void assign_sub(AbstractTensor<Derived,DIM> &dst, U num) {
281+
constexpr FASTOR_INLINE void assign_sub(AbstractTensor<Derived,DIM> &dst, U num) {
282282
trivial_assign_sub(dst.self(),num);
283283
}
284284
template<typename Derived, size_t DIM, typename U,
285285
enable_if_t_<is_primitive_v_<U>,bool> = false>
286-
FASTOR_INLINE void assign_mul(AbstractTensor<Derived,DIM> &dst, U num) {
286+
constexpr FASTOR_INLINE void assign_mul(AbstractTensor<Derived,DIM> &dst, U num) {
287287
trivial_assign_mul(dst.self(),num);
288288
}
289289
template<typename Derived, size_t DIM, typename U,
290290
enable_if_t_<is_primitive_v_<U>,bool> = false>
291-
FASTOR_INLINE void assign_div(AbstractTensor<Derived,DIM> &dst, U num) {
291+
constexpr FASTOR_INLINE void assign_div(AbstractTensor<Derived,DIM> &dst, U num) {
292292
trivial_assign_div(dst.self(),num);
293293
}
294294
//----------------------------------------------------------------------------------------------------------//

0 commit comments

Comments
 (0)