Skip to content

Commit a311fcb

Browse files
committed
Add explicit scalar no-intrinsics codepath to XMMatrixRotationQuaternion, XMQuaternionRotationRollPitchYaw(FromVector)
1 parent dc17b61 commit a311fcb

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

Inc/DirectXMathMatrix.inl

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1779,8 +1779,42 @@ inline XMMATRIX XM_CALLCONV XMMatrixRotationAxis
17791779

17801780
inline XMMATRIX XM_CALLCONV XMMatrixRotationQuaternion(FXMVECTOR Quaternion) noexcept
17811781
{
1782-
#if defined(_XM_NO_INTRINSICS_) || defined(_XM_ARM_NEON_INTRINSICS_)
1782+
#if defined(_XM_NO_INTRINSICS_)
1783+
1784+
float qx = Quaternion.vector4_f32[0];
1785+
float qxx = qx * qx;
1786+
1787+
float qy = Quaternion.vector4_f32[1];
1788+
float qyy = qy * qy;
1789+
1790+
float qz = Quaternion.vector4_f32[2];
1791+
float qzz = qz * qz;
1792+
1793+
float qw = Quaternion.vector4_f32[3];
17831794

1795+
XMMATRIX M;
1796+
M.m[0][0] = 1.f - 2.f * qyy - 2.f * qzz;
1797+
M.m[0][1] = 2.f * qx * qy + 2.f * qz * qw;
1798+
M.m[0][2] = 2.f * qx * qz - 2.f * qy * qw;
1799+
M.m[0][3] = 0.f;
1800+
1801+
M.m[1][0] = 2.f * qx * qy - 2.f * qz * qw;
1802+
M.m[1][1] = 1.f - 2.f * qxx - 2.f * qzz;
1803+
M.m[1][2] = 2.f * qy * qz + 2.f * qx * qw;
1804+
M.m[1][3] = 0.f;
1805+
1806+
M.m[2][0] = 2.f * qx * qz + 2.f * qy * qw;
1807+
M.m[2][1] = 2.f * qy * qz - 2.f * qx * qw;
1808+
M.m[2][2] = 1.f - 2.f * qxx - 2.f * qyy;
1809+
M.m[2][3] = 0.f;
1810+
1811+
M.m[3][0] = 0.f;
1812+
M.m[3][1] = 0.f;
1813+
M.m[3][2] = 0.f;
1814+
M.m[3][3] = 1.0f;
1815+
return M;
1816+
1817+
#elif defined(_XM_ARM_NEON_INTRINSICS_)
17841818
static const XMVECTORF32 Constant1110 = { { { 1.0f, 1.0f, 1.0f, 0.0f } } };
17851819

17861820
XMVECTOR Q0 = XMVectorAdd(Quaternion, Quaternion);

Inc/DirectXMathMisc.inl

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,14 @@ inline XMVECTOR XM_CALLCONV XMQuaternionConjugate(FXMVECTOR Q) noexcept
228228

229229
inline XMVECTOR XM_CALLCONV XMQuaternionInverse(FXMVECTOR Q) noexcept
230230
{
231-
const XMVECTOR Zero = XMVectorZero();
232-
233231
XMVECTOR L = XMVector4LengthSq(Q);
234232
XMVECTOR Conjugate = XMQuaternionConjugate(Q);
235233

236234
XMVECTOR Control = XMVectorLessOrEqual(L, g_XMEpsilon.v);
237235

238236
XMVECTOR Result = XMVectorDivide(Conjugate, L);
239237

240-
Result = XMVectorSelect(Result, Zero, Control);
238+
Result = XMVectorSelect(Result, g_XMZero, Control);
241239

242240
return Result;
243241
}
@@ -582,9 +580,30 @@ inline XMVECTOR XM_CALLCONV XMQuaternionRotationRollPitchYaw
582580
float Roll
583581
) noexcept
584582
{
583+
#if defined(_XM_NO_INTRINSICS_)
584+
const float halfpitch = Pitch * 0.5f;
585+
float cp = cosf(halfpitch);
586+
float sp = sinf(halfpitch);
587+
588+
const float halfyaw = Yaw * 0.5f;
589+
float cy = cosf(halfyaw);
590+
float sy = sinf(halfyaw);
591+
592+
const float halfroll = Roll * 0.5f;
593+
float cr = cosf(halfroll);
594+
float sr = sinf(halfroll);
595+
596+
XMVECTORF32 vResult = { { {
597+
cr * sp * cy + sr * cp * sy,
598+
cr * cp * sy - sr * sp * cy,
599+
sr * cp * cy - cr * sp * sy,
600+
cr * cp * cy + sr * sp * sy
601+
} } };
602+
return vResult;
603+
#else
585604
XMVECTOR Angles = XMVectorSet(Pitch, Yaw, Roll, 0.0f);
586-
XMVECTOR Q = XMQuaternionRotationRollPitchYawFromVector(Angles);
587-
return Q;
605+
return XMQuaternionRotationRollPitchYawFromVector(Angles);
606+
#endif
588607
}
589608

590609
//------------------------------------------------------------------------------
@@ -594,6 +613,27 @@ inline XMVECTOR XM_CALLCONV XMQuaternionRotationRollPitchYawFromVector
594613
FXMVECTOR Angles // <Pitch, Yaw, Roll, 0>
595614
) noexcept
596615
{
616+
#if defined(_XM_NO_INTRINSICS_)
617+
const float halfpitch = Angles.vector4_f32[0] * 0.5f;
618+
float cp = cosf(halfpitch);
619+
float sp = sinf(halfpitch);
620+
621+
const float halfyaw = Angles.vector4_f32[1] * 0.5f;
622+
float cy = cosf(halfyaw);
623+
float sy = sinf(halfyaw);
624+
625+
const float halfroll = Angles.vector4_f32[2] * 0.5f;
626+
float cr = cosf(halfroll);
627+
float sr = sinf(halfroll);
628+
629+
XMVECTORF32 vResult = { { {
630+
cr * sp * cy + sr * cp * sy,
631+
cr * cp * sy - sr * sp * cy,
632+
sr * cp * cy - cr * sp * sy,
633+
cr * cp * cy + sr * sp * sy
634+
} } };
635+
return vResult;
636+
#else
597637
static const XMVECTORF32 Sign = { { { 1.0f, -1.0f, -1.0f, 1.0f } } };
598638

599639
XMVECTOR HalfAngles = XMVectorMultiply(Angles, g_XMOneHalf.v);
@@ -615,6 +655,7 @@ inline XMVECTOR XM_CALLCONV XMQuaternionRotationRollPitchYawFromVector
615655
XMVECTOR Q = XMVectorMultiplyAdd(Q1, R1, Q0);
616656

617657
return Q;
658+
#endif
618659
}
619660

620661
//------------------------------------------------------------------------------

0 commit comments

Comments
 (0)