Skip to content

SPACalculator: fast fused multiply-add for debug builds and x86 - #305

Closed
Elyahu41 wants to merge 0 commit into
KosherJava:masterfrom
Elyahu41:fix/spa-fma-performance
Closed

Elyahu41 wants to merge 0 commit into
KosherJava:masterfrom
Elyahu41:fix/spa-fma-performance

Conversation

@Elyahu41

Copy link
Copy Markdown
Contributor

On Android, Math.fma is not always done by the hardware. In debuggable apps (debug builds) and on x86 and x86_64 devices (emulators, Chromebooks), ART runs the Java fallback instead: a BigDecimal multiply and add, with several allocations per call. The nutation series calls it about 580 times per solar position, so the SPA calculator was very slow there. Release builds on arm64 phones were not affected: on a Galaxy S23 (Android 16) ART uses the hardware instruction.

SPACalculator.fma is now Boldo and Melquiond's proven FMA emulation (Dekker's exact product, TwoSum, then rounding to odd), which is bit for bit the same as Math.fma. Inputs outside its proven range still go to Math.fma. The nutation also uses plain arithmetic where the product is already exact (multipliers 0, +-1 and +-2, and zero coefficients), which gives identical results.

Results are unchanged: 236,670 rise, set, noon, midnight, azimuth and position values from 10 locations over 1990-2059 are bit for bit identical before and after, on the desktop JVM and on ART (Galaxy S23, Android 16). 180 million fma cases, including exact ties and heavy cancellation, match Math.fma exactly. A test covers this.

Three days of zmanim with a fresh calculator:

  • Debug build of an app on a Galaxy S23: 200-1080 ms before, 13-120 ms after
  • Android 17 emulator (x86_64), not debuggable: about 650-3100 ms before, 9-35 ms after
  • Release build on a Galaxy S23, and the desktop JVM: unchanged (about 1-12 ms)

@Elyahu41

Copy link
Copy Markdown
Contributor Author

@KosherJava I am going to leave this PR here for your review. It really is not that big of a deal if you do not merge it, but I had Claude review why my app was running so slowly and this was it's findings. Apparently, this only affects debug android APKs so a release will be just as fast as the regular Math.fma method, but Android for some reason uses a 20x slower method on debug builds and other devices.

Please review, and let us know if you are comfortable with this change or if anything else needs to change for the PR.

@Elyahu41 Elyahu41 closed this Nov 15, 2026
@Elyahu41
Elyahu41 force-pushed the fix/spa-fma-performance branch from d4c23a0 to 83102d5 Compare November 15, 2026 18:57
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.

1 participant