Skip to content

Commit dcbc29e

Browse files
committed
Split gemmi.hpp into multiple files
1 parent 957dfd1 commit dcbc29e

12 files changed

Lines changed: 1399 additions & 1306 deletions

File tree

include/core/floating_point.hpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef GEMMI_CORE_FLOATING_POINT_HPP
2+
#define GEMMI_CORE_FLOATING_POINT_HPP
3+
4+
#include <cstdint>
5+
#include <limits>
6+
#include <cmath>
7+
8+
namespace gemmi::core {
9+
10+
/**
11+
* @brief Traits describing IEEE-754 layout properties for a floating-point type.
12+
*
13+
* This trait centralizes all compile-time metadata about floating-point types.
14+
* - StorageType: unsigned integer type for to bit-cast values.
15+
* - numExponentBits: number of bits in the exponent field.
16+
* - numSignificandBits: number of significand bits, including the implicit bit.
17+
*
18+
* @tparam fp_t Supported floating-point type.
19+
*/
20+
template <typename fp_t>
21+
struct FloatingPointTraits;
22+
23+
/**
24+
* @brief IEEE-754 traits for single-precision floating point.
25+
*/
26+
template <>
27+
struct FloatingPointTraits<float> {
28+
using StorageType = uint32_t;
29+
static constexpr size_t numExponentBits = 8;
30+
static constexpr size_t numSignificandBits = 24;
31+
};
32+
33+
/**
34+
* @brief IEEE-754 traits for double-precision floating point.
35+
*/
36+
template <>
37+
struct FloatingPointTraits<double> {
38+
using StorageType = uint64_t;
39+
static constexpr size_t numExponentBits = 11;
40+
static constexpr size_t numSignificandBits = 53;
41+
};
42+
43+
/**
44+
* @brief Get the exponent of a floating-point value.
45+
* @tparam fp_t Floating-point type (e.g., float, double).
46+
* @param value The floating-point value.
47+
* @return The value stored in the exponent field of the value, as an integer.
48+
*/
49+
template <typename fp_t>
50+
int getStoredFloatingPointExponent(fp_t value) {
51+
auto minFPExponent = std::numeric_limits<fp_t>::min_exponent;
52+
auto actualExponent = std::ilogb(std::abs(value)) + 1;
53+
return (value == 0.0) ? 0 : std::max(minFPExponent, actualExponent);
54+
}
55+
56+
} // namespace gemmi::core
57+
58+
#endif // GEMMI_CORE_FLOATING_POINT_HPP

include/core/matrix_view.hpp

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#ifndef GEMMI_CORE_MATRIX_VIEW_HPP
2+
#define GEMMI_CORE_MATRIX_VIEW_HPP
3+
4+
#include <vector>
5+
#include <stdexcept>
6+
#include <type_traits>
7+
8+
namespace gemmi::core {
9+
10+
/**
11+
* @brief Enum to specify the layout of the matrix in memory.
12+
*/
13+
enum class matrixLayout {
14+
rowMajor, ///< Matrix stored in row-major order.
15+
columnMajor ///< Matrix stored in column-major order.
16+
};
17+
18+
/**
19+
* @brief Lightweight view of a dense matrix.
20+
*
21+
* It does not own the underlying memory but provides read and write access to it.
22+
*
23+
* @tparam value_t Element type.
24+
*/
25+
template <typename value_t>
26+
struct MatrixView {
27+
value_t* data; ///< Pointer to the matrix data.
28+
size_t rows; ///< Number of rows in the matrix.
29+
size_t cols; ///< Number of columns in the matrix.
30+
matrixLayout layout; ///< Layout of the matrix in memory.
31+
32+
/**
33+
* @brief Default constructor.
34+
*/
35+
MatrixView() :
36+
data(nullptr), rows(0), cols(0), layout(matrixLayout::rowMajor) {}
37+
38+
/**
39+
* @brief Construct a matrix view from raw parts.
40+
*
41+
* @param data Pointer to the first matrix element.
42+
* @param rows Number of rows.
43+
* @param cols Number of columns.
44+
* @param layout Memory layout.
45+
*/
46+
MatrixView(value_t* data, size_t rows, size_t cols, matrixLayout layout) :
47+
data(data), rows(rows), cols(cols), layout(layout) {}
48+
49+
/**
50+
* @brief Construct a mutable matrix view from a vector.
51+
*
52+
* @param vec Vector containing the matrix data.
53+
* @param rows Number of rows.
54+
* @param cols Number of columns.
55+
* @param layout Memory layout.
56+
*/
57+
MatrixView(std::vector<value_t>& vec, size_t rows, size_t cols, matrixLayout layout) :
58+
data(vec.data()), rows(rows), cols(cols), layout(layout) {}
59+
60+
/**
61+
* @brief Construct a read-only matrix view from a const vector.
62+
*
63+
* Only participates in overload resolution when @c value_t is const-qualified.
64+
*
65+
* @param vec Const vector containing the matrix data.
66+
* @param rows Number of rows.
67+
* @param cols Number of columns.
68+
* @param layout Memory layout.
69+
*/
70+
template <typename = std::enable_if_t<std::is_const_v<value_t>>>
71+
MatrixView(const std::vector<std::remove_const_t<value_t>>& vec, size_t rows, size_t cols, matrixLayout layout) :
72+
data(vec.data()), rows(rows), cols(cols), layout(layout) {}
73+
74+
template <typename other_t,
75+
typename = std::enable_if_t<
76+
std::is_const_v<value_t> &&
77+
std::is_same_v<std::remove_const_t<value_t>, other_t>>>
78+
MatrixView(const MatrixView<other_t>& other) :
79+
data(other.data), rows(other.rows), cols(other.cols), layout(other.layout) {}
80+
81+
/**
82+
* @brief Return the number of stored elements.
83+
*
84+
* @return Number of rows by number of columns.
85+
*/
86+
size_t size() const {
87+
return rows * cols;
88+
}
89+
90+
/**
91+
* @brief Return true if the view is empty.
92+
*
93+
* @return `true` if `rows == 0` or `cols == 0`
94+
*/
95+
bool empty() const {
96+
return rows == 0 || cols == 0;
97+
}
98+
99+
/**
100+
* @brief Compute the linear index of element (i, j).
101+
*
102+
* @param i Row index.
103+
* @param j Column index.
104+
* @return Linear index into the underlying data array.
105+
*/
106+
size_t index(size_t i, size_t j) const {
107+
return (layout == matrixLayout::rowMajor)
108+
? (i * cols + j)
109+
: (j * rows + i);
110+
}
111+
112+
/**
113+
* @brief Access element (i, j).
114+
*
115+
* The `const` qualifier applies to the view metadata (pointer, dimensions,
116+
* and layout), and not to the pointed-to data. Mutability of the returned
117+
* reference is controlled by `value_t`. `MatrixView<const T>` should be
118+
* used for a read-only view.
119+
*
120+
* @param i Row index.
121+
* @param j Column index.
122+
* @return Reference to the element.
123+
*/
124+
value_t& operator()(size_t i, size_t j) const {
125+
return data[index(i, j)];
126+
}
127+
128+
/**
129+
* @brief Access element (i, j) explicitly.
130+
*
131+
* This method throws an exception if the access is out of bounds.
132+
* It is a safe alternative to operator(), which does not perform
133+
* bounds checking.
134+
*
135+
* @param i Row index.
136+
* @param j Column index.
137+
* @return Reference to the element.
138+
*/
139+
value_t& at(size_t i, size_t j) const {
140+
if (i >= rows || j >= cols) {
141+
throw std::out_of_range("MatrixView index out of range");
142+
}
143+
return data[index(i, j)];
144+
}
145+
146+
/**
147+
* @brief Access element by linear index.
148+
* @param idx Linear index.
149+
* @return Reference to the element.
150+
*/
151+
value_t& linear(size_t idx) const {
152+
return data[idx];
153+
}
154+
};
155+
156+
} // namespace gemmi::core
157+
158+
#endif // GEMMI_CORE_MATRIX_VIEW_HPP

0 commit comments

Comments
 (0)