Skip to content

Optimized math hot-path (deg2rad, rad2deg, arc_hav)#15

Merged
gistrec merged 1 commit intomasterfrom
optimize-math-hot-path
May 9, 2026
Merged

Optimized math hot-path (deg2rad, rad2deg, arc_hav)#15
gistrec merged 1 commit intomasterfrom
optimize-math-hot-path

Conversation

@gistrec
Copy link
Copy Markdown
Owner

@gistrec gistrec commented May 9, 2026

Optimize math hot-path

Micro-optimizations in geo/detail/math.hpp — these helpers are called on every geodesic computation, so per-instruction cost matters.

deg2rad / rad2deg: extract kDegToRad / kRadToDeg constants
The expression degrees * kPi / 180.0 parses as (degrees * kPi) / 180.0. Without -ffast-math, the compiler is not allowed to fold kPi / 180.0 at compile time (IEEE 754 forbids reordering), so it emits an fdiv at runtime. A named constexpr factor produces a single fmul instead of fmul + fdiv.

arc_hav: std::clampstd::min / std::max
std::clamp uses operator<, which on ARM64 lowers to fcmp + fcsel. std::min / std::max on double map to a single fmin / fmax instruction. Saves a couple of cycles on the hot path.

Behavior is unchanged — codegen only.

@gistrec gistrec requested a review from MrHerrn May 9, 2026 13:06
@gistrec gistrec self-assigned this May 9, 2026
@gistrec gistrec merged commit 348c1f4 into master May 9, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants