Skip to content

* Faster hill shading on many devices (using Math.sqrt approximation) - #1752

Merged
devemux86 merged 1 commit into
mapsforge:masterfrom
Sublimis:hs-sqrt-approx
Nov 23, 2025
Merged

devemux86 merged 1 commit into
mapsforge:masterfrom
Sublimis:hs-sqrt-approx

Conversation

@Sublimis

Copy link
Copy Markdown
Contributor

No description provided.

@devemux86 devemux86 added this to the 0.27.0 milestone Nov 23, 2025
@devemux86
devemux86 merged commit 0468375 into mapsforge:master Nov 23, 2025
@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis Thanks!

@tm-cms

tm-cms commented Jan 19, 2026

Copy link
Copy Markdown
Contributor

@devemux86
This should be reverted, because it is much slower and also numerically inaccurate. The bit-level approximation introduces large relative errors, especially for very small and very large values, and produces incorrect results for edge cases such as NaN, Infinity, and non-positive inputs. On modern JVMs, Math.sqrt(double) is a JIT intrinsic compiled directly to the CPU SQRTSD instruction, making it both faster and fully IEEE-754 compliant.

@Sublimis

Copy link
Copy Markdown
Contributor Author

Any evidence for the claims?

This sqrtApprox() function is not universal. It is only used for hill shading, which is very approximate and rough in itself. So the accuracy argument is irrelevant. The function is accurate enough for the purpose. It won't be used to send rockets to the moon.

The speed argument is also irrelevant. Do your testing on Xperia devices, for example. The reasons can be this or that, it doesn't matter. Bit-manipulating sqrtApprox() is about 10x faster on some $$$ Xperias. On some other devices the difference in speed will be much, much smaller, but Math.sqrt() will still not be faster. In other words, sqrtApprox() should be fast on almost any device (prove otherwise?), while Math.sqrt() will be extremely slow on some (proven).

Want to use Math.sqrt()? Who's stopping you from overriding the StandardClasyHillShading.sqrt() function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants