Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
simd.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#pragma once
9
12
13#include <drjit/array.h>
14#include <drjit/array_router.h>
15#include <drjit/matrix.h>
16#include <drjit/packet.h>
17#include <drjit/util.h>
18#include <Eigen/Core>
19#include <Eigen/Geometry>
20
26#define DRJIT_VERSION_GE(major, minor, patch) \
27 ((DRJIT_VERSION_MAJOR > (major)) || \
28 (DRJIT_VERSION_MAJOR == (major) && DRJIT_VERSION_MINOR > (minor)) || \
29 (DRJIT_VERSION_MAJOR == (major) && DRJIT_VERSION_MINOR == (minor) && \
30 DRJIT_VERSION_PATCH >= (patch)))
31
52
53namespace momentum {
54
59inline constexpr size_t kSimdPacketSize = drjit::DefaultSize;
60
67inline constexpr size_t kSimdAlignment = kSimdPacketSize * sizeof(float);
68
70template <typename T>
71using Packet = drjit::Packet<T, kSimdPacketSize>;
72
79
81template <typename T, int Dim>
82using VectorP = drjit::Array<Packet<T>, Dim>;
83
84template <typename T>
86
87template <typename T>
89
94
100
102using Vector2iP = drjit::Array<IntP, 2>;
103using Vector3iP = drjit::Array<IntP, 3>;
104using Vector3bP = drjit::Array<ByteP, 3>;
105
107template <typename T>
109
112
114template <typename T, int Dim>
115using MatrixP = drjit::Matrix<Packet<T>, Dim>;
116
117template <typename T>
119
120template <typename T>
122
127
136template <size_t Alignment>
137[[nodiscard]] size_t computeOffset(const Eigen::Ref<Eigen::MatrixXf>& mat) {
138 constexpr size_t sizeOfScalar = sizeof(typename Eigen::MatrixXf::Scalar);
139 const size_t addressOffset =
140 Alignment / sizeOfScalar - (((size_t)mat.data() % Alignment) / sizeOfScalar);
141
142 // If the current alignment already meets the requirement, no offset is needed.
143 if (addressOffset == Alignment / sizeOfScalar) {
144 return 0;
145 }
146
147 return addressOffset;
148}
149
155template <size_t Alignment>
156void checkAlignment(const Eigen::Ref<Eigen::MatrixXf>& mat, size_t offset = 0) {
158 (uintptr_t(mat.data() + offset)) % Alignment != 0,
159 "Matrix ({}x{}, ptr: {}) is not aligned ({}) correctly.",
160 mat.rows(),
161 mat.cols(),
162 uintptr_t(mat.data()),
163 Alignment);
164}
165
182
190template <typename Derived, typename S>
191FloatP dot(const Eigen::MatrixBase<Derived>& v1, const Vector3P<S>& v2) {
192 return drjit::fmadd(v1.x(), v2.x(), drjit::fmadd(v1.y(), v2.y(), v1.z() * v2.z()));
193}
194
202template <typename Derived, typename S>
203FloatP dot(const Vector3P<S>& v1, const Eigen::MatrixBase<Derived>& v2) {
204 return drjit::fmadd(v1.x(), v2.x(), drjit::fmadd(v1.y(), v2.y(), v1.z() * v2.z()));
205}
206
214template <typename S, typename Derived>
215Vector3P<S> operator+(const Eigen::MatrixBase<Derived>& v1, const Vector3P<S>& v2) {
216 return {v1.x() + v2.x(), v1.y() + v2.y(), v1.z() + v2.z()};
217}
218
226template <typename S, typename Derived>
227Vector3P<S> operator+(const Vector3P<S>& v1, const Eigen::MatrixBase<Derived>& v2) {
228 return {v1.x() + v2.x(), v1.y() + v2.y(), v1.z() + v2.z()};
229}
230
238template <typename S, typename Derived>
239Vector3P<S> operator-(const Eigen::MatrixBase<Derived>& v1, const Vector3P<S>& v2) {
240 return {v1.x() - v2.x(), v1.y() - v2.y(), v1.z() - v2.z()};
241}
242
250template <typename S, typename Derived>
251Vector3P<S> operator-(const Vector3P<S>& v1, const Eigen::MatrixBase<Derived>& v2) {
252 return {v1.x() - v2.x(), v1.y() - v2.y(), v1.z() - v2.z()};
253}
254
262template <typename S>
263Vector3P<S> operator*(const Eigen::Quaternion<S>& q, const Vector3P<S>& vec) {
264 // DrJit provides quaternion-packet multiplication via overload resolution
265 return q * vec;
266}
267
275template <typename S, typename Derived>
276Vector3P<S> operator*(const Eigen::MatrixBase<Derived>& xf, const Vector3P<S>& vec) {
277 return Vector3P<S>{
278 momentum::dot(xf.row(0), vec), momentum::dot(xf.row(1), vec), momentum::dot(xf.row(2), vec)};
279}
280
286template <typename S>
287Vector3P<S> operator*(const Eigen::Transform<S, 3, Eigen::Affine>& xf, const Vector3P<S>& vec) {
288 return momentum::operator+(momentum::operator*(xf.linear(), vec), xf.translation());
289}
290
296template <typename S>
298 return momentum::operator+(momentum::operator*(xf.toLinear(), vec), xf.translation);
299}
300
308template <typename S, typename Derived>
309Vector3P<S> cross(const Eigen::MatrixBase<Derived>& v1, const Vector3P<S>& v2) {
310 return {
311 drjit::fmsub(v1.y(), v2.z(), v1.z() * v2.y()),
312 drjit::fmsub(v1.z(), v2.x(), v1.x() * v2.z()),
313 drjit::fmsub(v1.x(), v2.y(), v1.y() * v2.x()),
314 };
315}
316
324template <typename S, typename Derived>
325Vector3P<S> cross(const Vector3P<S>& v1, const Eigen::MatrixBase<Derived>& v2) {
326 return {
327 drjit::fmsub(v1.y(), v2.z(), v1.z() * v2.y()),
328 drjit::fmsub(v1.z(), v2.x(), v1.x() * v2.z()),
329 drjit::fmsub(v1.x(), v2.y(), v1.y() * v2.x()),
330 };
331}
332
333
334} // namespace momentum
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
Definition exception.h:55
Packet< int > IntP
Definition simd.h:77
MatrixP< T, 3 > Matrix3P
Definition simd.h:118
Vector4P< double > Vector4dP
Definition simd.h:111
constexpr size_t kSimdPacketSize
Number of floats in the platform's default SIMD register, chosen at compile time by DrJit based on av...
Definition simd.h:59
drjit::Packet< T, kSimdPacketSize > Packet
A fixed-size SIMD packet of kSimdPacketSize elements of type T.
Definition simd.h:71
Vector3P< S > operator-(const Eigen::MatrixBase< Derived > &v1, const Vector3P< S > &v2)
Subtracts Vector3P from Eigen::Vector3f element-wise.
Definition simd.h:239
Matrix3P< double > Matrix3dP
Definition simd.h:124
constexpr size_t kSimdAlignment
Byte alignment required for the platform's default SIMD loads/stores.
Definition simd.h:67
Matrix4P< double > Matrix4dP
Definition simd.h:126
FloatP dot(const Eigen::MatrixBase< Derived > &v1, const Vector3P< S > &v2)
Computes dot product of Eigen::Vector3f and 3-vector of packets.
Definition simd.h:191
drjit::Array< ByteP, 3 > Vector3bP
Definition simd.h:104
void checkAlignment(const Eigen::Ref< Eigen::MatrixXf > &mat, size_t offset=0)
Checks if the data of the matrix is aligned correctly.
Definition simd.h:156
drjit::Array< IntP, 3 > Vector3iP
Definition simd.h:103
drjit::Array< Packet< T >, Dim > VectorP
A Dim-dimensional vector where each component is a SIMD packet.
Definition simd.h:82
Vector2P< double > Vector2dP
Definition simd.h:95
Packet< uint32_t > UintP
Definition simd.h:99
Packet< uint8_t > ByteP
Definition simd.h:98
size_t computeOffset(const Eigen::Ref< Eigen::MatrixXf > &mat)
Computes the offset required for the matrix data to meet the alignment requirement.
Definition simd.h:137
Vector3P< double > Vector3dP
Definition simd.h:96
MatrixP< T, 4 > Matrix4P
Definition simd.h:121
drjit::Array< IntP, 2 > Vector2iP
Integer and byte vector types.
Definition simd.h:102
drjit::Matrix< Packet< T >, Dim > MatrixP
Matrix types for SIMD packets.
Definition simd.h:115
VectorP< T, 4 > Vector4P
4D vector types.
Definition simd.h:108
Vector3P< S > operator+(const Eigen::MatrixBase< Derived > &v1, const Vector3P< S > &v2)
Adds Eigen::Vector3f and Vector3P element-wise.
Definition simd.h:215
Vector3P< S > cross(const Eigen::MatrixBase< Derived > &v1, const Vector3P< S > &v2)
Computes cross product of Eigen::Vector3f and Vector3P.
Definition simd.h:309
VectorP< T, 3 > Vector3P
Definition simd.h:88
Vector3P< float > Vector3fP
Definition simd.h:93
Packet< double > DoubleP
Definition simd.h:76
Vector2P< float > Vector2fP
Definition simd.h:92
Vector3P< S > operator*(const Eigen::Quaternion< S > &q, const Vector3P< S > &vec)
Applies quaternion rotation to each 3x1 vector in packet.
Definition simd.h:263
Matrix3P< float > Matrix3fP
Definition simd.h:123
VectorP< T, 2 > Vector2P
Definition simd.h:85
Matrix4P< float > Matrix4fP
Definition simd.h:125
Vector4P< float > Vector4fP
Definition simd.h:110
Packet< float > FloatP
Definition simd.h:75
Lightweight transform with separate rotation, translation, and uniform scaling.
Definition transform.h:36
Vector3< T > translation
Definition transform.h:40
Matrix3< T > toLinear() const
Returns scale * R, the linear part of the transform.
Definition transform.h:165