Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions include/hyperjet/hyperjet.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ namespace hyperjet {

#if defined(_MSC_VER)
#define HYPERJET_INLINE __forceinline
#define HYPERJET_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
#else
#define HYPERJET_INLINE __attribute__((always_inline)) inline
#define HYPERJET_NO_UNIQUE_ADDRESS [[no_unique_address]]
#endif

using index = std::ptrdiff_t;
Expand Down Expand Up @@ -47,13 +49,19 @@ class DDScalar {
using DynamicStorage = std::vector<TScalar>;
using StaticStorage = std::array<TScalar, StaticDataSize>;

// A statically sized scalar knows its size from TSize, so it carries no
// field for it. The empty stand-in keeps m_size spellable in both variants.
struct NoSize {};

public:
using Type = DDScalar<TOrder, TScalar, TSize>;
using Scalar = TScalar;
using Data = typename std::conditional<TSize == Dynamic, DynamicStorage,
StaticStorage>::type;

index m_size;
using SizeStorage = std::conditional_t<TSize == Dynamic, index, NoSize>;

HYPERJET_NO_UNIQUE_ADDRESS SizeStorage m_size;
Data m_data;

public:
Expand Down Expand Up @@ -331,13 +339,15 @@ class DDScalar {
static_assert(is_dynamic());
}

DDScalar(std::initializer_list<TScalar> data)
: m_size(size_from_data_length(length(data))) {
DDScalar(std::initializer_list<TScalar> data) {
static_assert(0 < order() && order() <= 2);

check_valid_size(m_size);
const index s = size_from_data_length(length(data));

check_valid_size(s);

if constexpr (is_dynamic()) {
m_size = s;
m_data.assign(data.begin(), data.end());
} else {
std::copy(data.begin(), data.end(), m_data.begin());
Expand Down
22 changes: 20 additions & 2 deletions test/src/test_variants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

#include <hyperjet/hyperjet.h>

#include <array> // array
#include <vector> // vector
#include <array> // array
#include <type_traits> // is_trivially_copyable
#include <vector> // vector

// Coverage across all four combinations of order and sizing.
//
Expand Down Expand Up @@ -106,6 +107,23 @@ void check(const T &actual, const std::vector<double> &expected) {
}
}

// A statically sized scalar is exactly its data. The runtime size is only
// meaningful for the dynamic variant, where it also rules out trivial copying.
// Both properties are what an element type has to offer a NumPy dtype.

TEST_CASE("Static scalars are exactly their data") {
CHECK(sizeof(DDScalar<1, double, 0>) == sizeof(DDScalar<1, double, 0>::Data));
CHECK(sizeof(DDScalar<1, double, 3>) == sizeof(DDScalar<1, double, 3>::Data));
CHECK(sizeof(DDScalar<2, double, 3>) == sizeof(DDScalar<2, double, 3>::Data));
CHECK(sizeof(DDScalar<2, double, 8>) == sizeof(DDScalar<2, double, 8>::Data));

CHECK(std::is_trivially_copyable_v<DDScalar<1, double, 3>>);
CHECK(std::is_trivially_copyable_v<DDScalar<2, double, 3>>);
CHECK(std::is_standard_layout_v<DDScalar<2, double, 3>>);

CHECK_FALSE(std::is_trivially_copyable_v<DDScalar<2, double, Dynamic>>);
}

TEST_CASE_TEMPLATE("variants: values", T, D1, X1, D2, X2) {
auto a = make<T>(A);

Expand Down
Loading