Repository navigation
Closed
Description
Summary
A review of the core vector/matrix/quaternion code found several correctness bugs and three
places where the AVX2 and NEON backends give different answers for the same input.
Every fix should land with a regression test that fails before the fix. CI now runs the suite on
x86-64 (GCC, Clang, MSVC), NEON under qemu, macOS arm64, and under ASan/UBSan, so each test covers
every backend at once.
Bugs
1. nearly_equal(quat) mixes q and -q per lane
src/sgl/avx.cppm:1528,src/sgl/neon.cppm:1380- It takes the per-lane
min(|a - b|, |a + b|), so each lane may pick a different sign.
(0.5, 0.5, 0.5, 0.5)and(0.5, -0.5, 0.5, -0.5)compare equal although they are 180 degrees
apart. - Fix: all lanes of
a - bwithin tolerance, or all lanes ofa + b(or|dot(a, b)| ~ 1).
2. inverse(transform) is wrong with non-uniform scale
src/sgl/avx.cppm:2057,src/sgl/neon.cppm:1678- The inverse of
R(S p) + tapplies the scale after the rotation, which a scale-then-rotate
transformcannot represent. Scale {2, 1, 1} with a 90 degree turn about z: a round trip of
{1, 0, 0}gives{2, 0, 0}. - Fix: return a
mat4for the general case, or assert uniform scale and useinverse_uniform.
3. nearly_equal treats infinity as equal to anything
src/sgl/scalar.cppm:50and the vector overloads at:76-:84; the SIMD templates at
src/sgl/avx.cppm:259,src/sgl/neon.cppm:473|inf - x| = inf <= inf * rel_tol:(inf, 0),(inf, -inf)and(inf, 1e30)all compare equal.- Fix: when either side is infinite, return
lhs == rhs.
4. is_parallel tolerance depends on the dimension
src/sgl/avx.cppm:666(and the vec3 specialisation after it),src/sgl/neon.cppm:827- vec2/vec3 compare
|cross|to an absolute tolerance; vec4 is relative.{1e-3, 0, 0}and
{0, 1e-3, 0}(perpendicular) report parallel. - Fix:
|cross|^2 <= tol^2 * |a|^2 * |b|^2for every dimension.
5. angle_between loses precision at small angles
- vec:
src/sgl/avx.cppm:707,src/sgl/neon.cppm:857; quat:avx.cppm:1552,neon.cppm:1387 acosnear 1 collapses: a 1e-4 rad vec3 angle returns 0; a 1e-3 rad quat angle returns 9.77e-4.- Fix:
atan2(|cross(a, b)|, dot(a, b)); for quats2 * atan2(|xyz of conj(a) * b|, |w|).
6. AVX2 and NEON disagree on edge cases
minps / maxps return the second operand on NaN; NEON vminq / vmaxq propagate NaN.
closest_point_on_segmentwith a zero-length segment (avx.cppm:1574,neon.cppm:1399):
x86 returns the segment start, NEON returns NaN.intersect_ray_boxfor a ray sliding along a box face (avx.cppm:1833,:1864;
neon.cppm:1568,:1582): x86 misses on one edge and reports a hit witht = NaNon the
opposite edge; NEON hits both with t = [1, 2]. The origin on a slab makes(min - o) * infNaN.grid_cellout of int32 range (avx.cppm:1966,neon.cppm:1640and the ivec3 overloads):
x86 gives INT_MIN, NEON saturates to INT_MAX.- Fix: define the behaviour and make both backends follow it. For the slab test, the approach
sgl::soa::hit_byalready uses: pick near/far planes once from the direction's sign, keep the new
t as the first max/min operand, and clamp t_min / t_max to the finite range. Forgrid_cell,
clamp before converting (or assert a precondition). - Test: one edge-case table asserted on every backend.
7. vecN::operator[] indexes past a scalar member
src/sgl/types.cppm:48,:53,:73,:78,:91,:96(&x)[i]for i > 0 is UB, andconstexpr float y = sgl::vec3{1, 2, 3}[1];fails to compile
(Clang: one-past-the-end read). UBSan does not flag it.- Fix: a
switchoni; same code at-O2. Add astatic_asserttest.
8. contains(box3d, vec3) is open while contains_point is closed
src/sgl/avx.cppm:630,src/sgl/neon.cppm:798- Same question, different answer on the boundary; there is no box2d
contains. - Fix: remove it in favour of
contains_point, or rename it to say it is strict.
9. The SIMD nearly_equal / nearly_zero templates never run
src/sgl/avx.cppm:259,:285;src/sgl/neon.cppm:473,:486- The non-template overloads in
scalar.cppmare exact matches and win overload resolution. - Fix: keep one implementation per operation (and fix item 3 in it).
Acceptance criteria
- Each item has a regression test that fails before its fix.
- Item 6 has one edge-case table that passes identically on AVX2, NEON and scalar paths.
- Item 7 has a
static_assertexercisingoperator[]in a constant expression. - All CI workflows green.
Follow-ups, not in this issue
- Absolute, scale-dependent tolerances: the
operator/debug assert rejects divisors below 1e-6,
andintersect_segments_2dcompares a length-squared quantity to 1e-7. transform::scaleis a vec3 butinverse_uniform/composeassume uniform scale.inverse(mat4),normalized(quat)andplane_from_pointsfail silently with inf/NaN; consider
std::optionalreturning variants.- The
winding_numbercomment (avx.cppm:1906) claims robustness for points on edges.
Metadata
Metadata
Assignees
Labels
No labels
Activity