From b9b570b78d58ceecc704f7a4e19b84dc45937ef4 Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Tue, 27 Oct 2026 00:42:26 +0300 Subject: [PATCH] fix armv7 quaternion normalization --- include/cglm/quat.h | 7 +++++++ test/src/test_quat.h | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/cglm/quat.h b/include/cglm/quat.h index 56a98bf..9a25c17 100644 --- a/include/cglm/quat.h +++ b/include/cglm/quat.h @@ -277,6 +277,13 @@ glm_quat_normalize_to(const versor q, versor dest) { dot = glm_vec4_norm2(q); +# if defined(CGLM_NEON_FP) && !CGLM_ARM64 + if (dot == 0.0f) { + /* armv7 neon flushes subnormal squared norms to zero */ + dot = (q[0] * q[0] + q[1] * q[1]) + (q[2] * q[2] + q[3] * q[3]); + } +# endif + if (dot <= 0.0f) { glm_quat_identity(dest); return; diff --git a/test/src/test_quat.h b/test/src/test_quat.h index 5273332..e497971 100644 --- a/test/src/test_quat.h +++ b/test/src/test_quat.h @@ -241,6 +241,15 @@ TEST_IMPL(GLM_PREFIX, quat_normalize_to) { GLM(quat_normalize_to)(v1, v2); ASSERTIFY(test_assert_quat_eq_identity(v2)) + /* tiny nonzero inputs must remain rotations on armv7 neon. */ + v1[2] = 1e-20f; + GLM(quat_normalize_to)(v1, v2); + ASSERT(test_eq(v2[0], 0.0f)) + ASSERT(test_eq(v2[1], 0.0f)) + ASSERT(test_eq(v2[2], 1.0f)) + ASSERT(test_eq(v2[3], 0.0f)) + ASSERT(v1[2] == 1e-20f) + TEST_SUCCESS } @@ -269,6 +278,15 @@ TEST_IMPL(GLM_PREFIX, quat_normalize) { GLM(quat_normalize)(v1); ASSERTIFY(test_assert_quat_eq_identity(v1)) + /* cover the in-place path for the same tiny rotation. */ + glm_vec4_zero(v1); + v1[2] = 1e-20f; + GLM(quat_normalize)(v1); + ASSERT(test_eq(v1[0], 0.0f)) + ASSERT(test_eq(v1[1], 0.0f)) + ASSERT(test_eq(v1[2], 1.0f)) + ASSERT(test_eq(v1[3], 0.0f)) + TEST_SUCCESS }