Skip to content

Fix correctness bugs in the core AoS math #6

Closed

Description

@aotodev

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 - b within tolerance, or all lanes of a + 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) + t applies the scale after the rotation, which a scale-then-rotate
    transform cannot 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 mat4 for the general case, or assert uniform scale and use inverse_uniform.

3. nearly_equal treats infinity as equal to anything

  • src/sgl/scalar.cppm:50 and 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|^2 for 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
  • acos near 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 quats 2 * 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_segment with a zero-length segment (avx.cppm:1574, neon.cppm:1399):
    x86 returns the segment start, NEON returns NaN.
  • intersect_ray_box for a ray sliding along a box face (avx.cppm:1833, :1864;
    neon.cppm:1568, :1582): x86 misses on one edge and reports a hit with t = NaN on the
    opposite edge; NEON hits both with t = [1, 2]. The origin on a slab makes (min - o) * inf NaN.
  • grid_cell out of int32 range (avx.cppm:1966, neon.cppm:1640 and 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_by already 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. For grid_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, and constexpr float y = sgl::vec3{1, 2, 3}[1]; fails to compile
    (Clang: one-past-the-end read). UBSan does not flag it.
  • Fix: a switch on i; same code at -O2. Add a static_assert test.

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.cppm are 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_assert exercising operator[] 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,
    and intersect_segments_2d compares a length-squared quantity to 1e-7.
  • transform::scale is a vec3 but inverse_uniform / compose assume uniform scale.
  • inverse(mat4), normalized(quat) and plane_from_points fail silently with inf/NaN; consider
    std::optional returning variants.
  • The winding_number comment (avx.cppm:1906) claims robustness for points on edges.

Activity

  1. linked a pull request that will close this issuefix correctness bugs #7on Sep 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions