Faster asin() was hiding in plain sight
TL;DR Highlight
Searching beat hand-rolling: a Cg asin formula cut ray-tracer render time from 111s to 101s.
Who Should Read
C++ developers optimizing math-heavy or graphics code who may be reinventing a wheel that already exists.
Core Mechanics
- Hand-made Taylor gave only 5%
- Replacing std::asin() with a fourth-order Taylor approximation cut the render from about 111s to 105s.
- It deviated heavily past |x| of 0.8, so it had to fall back to std::asin().
- Padé was more accurate but not faster
- A [3/4] Padé with half-angle correction reduced the error.
- Render time was 104.87s, essentially the same as the Taylor version at 104.674s.
- An LLM surfaced the Cg formula
- Asked for a fast asin approximation in C++, Gemini returned a minimax-coefficient function.
- It cited the Nvidia Cg Toolkit docs, based on Abramowitz and Stegun formula 4.4.45.
- The Cg version was fastest
- The same scene rendered in 101.462s versus 110.891s with std::asin().
- The output was indistinguishable from std::asin().
- Speedup varied by chip
- In microbenchmarks the Intel i7-10750H was 1.47-1.55x faster on Ubuntu and 1.90x on Windows with MSVC.
- Apple M4 gained only 1.02-1.05x, but it was reproducible.
- The lesson: search first
- The author says he never checked his goal or looked for existing solutions.
- A search for "fast c++ asin approximation cg" returned the Cg docs first.
Community Reactions
- Taylor and its Padé are poor choices, one commenter argued
- Without the characteristic minimax wiggle in the error over the interval, you are leaving easy gains on the table.
- Padé should be built from the original function, not Taylor, with Chebyshev or Remez as alternatives.
- The original source is Hastings 1955, a commenter corrected
- The approximation comes from Hastings' 1955 Princeton work.
- It was later included in Abramowitz and Stegun, and Nvidia Cg turned it into code.
- It was already in a 7-year-old Stack Overflow answer, a commenter pointed out
- The LLM's answer matches that answer, which would have been the first result for "fast asin" before the post.
- So it was easy to find even before the article.
- SIMD should come first, one commenter suggested
- A 16x gain from SIMD (or GPU) beats a 1.05-1.90x speedup.
- The two could be orthogonal improvements.
- glibc already handles ranges separately, a commenter noted
- Its implementation uses range checks and Taylor series, so it is not missing a hidden trick.
- For tiny |x| it simply returns x.
How to Apply
- Search for existing solutions to hot functions first
- If profiling shows asin or atan near the top, search "fast c++ asin approximation" before writing your own.
- Ask an LLM the same question and verify the cited source.
- Benchmark the Cg asin against std::asin()
- Run the code below on your own CPU.
- Check both the speedup and the sum error.
- Measure on each target chip separately
- The same code was 1.5-1.9x faster on Intel but only 1.02-1.05x on M4.
- Test each CPU and compiler combination (GCC, clang, MSVC) you ship on.
- Add an output comparison test before swapping in an approximation
- Write a test that checks the new result against the old within a tolerance.
- The author compared rendered images with the std::asin() output.
Try It
// Save as C++ and run: g++ -O3 bench.cpp -o bench && ./bench
#include <cmath>
#include <chrono>
#include <cstdio>
#include <vector>
#include <random>
constexpr double HalfPi = 1.5707963267948966;
double fast_asin_cg(double x) {
const double a0 = 1.5707288, a1 = -0.2121144, a2 = 0.0742610, a3 = -0.0187293;
double ax = std::fabs(x);
double p = a3 * ax + a2;
p = p * ax + a1;
p = p * ax + a0;
return std::copysign(HalfPi - std::sqrt(1.0 - ax) * p, x);
}
template <class F>
double run(F f, const std::vector<double>& v, double& sum) {
auto t = std::chrono::steady_clock::now();
for (int r = 0; r < 20; r++) for (double x : v) sum += f(x);
return std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t).count();
}
int main() {
std::mt19937 g(1);
std::uniform_real_distribution<double> d(-1, 1);
std::vector<double> v(1000000);
for (auto& x : v) x = d(g);
double s1 = 0, s2 = 0;
double t1 = run([](double x) { return std::asin(x); }, v, s1);
double t2 = run(fast_asin_cg, v, s2);
std::printf("std %.1f ms, cg %.1f ms, speedup %.2fx, sum diff %g\n", t1, t2, t1 / t2, s1 - s2);
}Terminology
Related Papers
A Beginner's Guide to Local AI with Inkling, Qwen, and Unsloth
Install Unsloth Desktop, load a quantized Qwen model, and share a private ChatGPT-like AI on your home LAN.
Muru – AI-assisted Rust server for Python apps by mod_WSGI's creator
mod_wsgi's creator is building Muru, an AI-assisted Rust ASGI/WSGI server, documenting it in public
87% of the Anthropic power will come from gas and 11% from coal in 2028
Analysis of Anthropic's contracted data centers: 87% gas, 11% coal power in 2028
Anthropic’s Nasdaq debut
Anthropic listed on Nasdaq and closed its first day below its opening price.
OpenAI still has a WebRTC Problem
WebRTC drops late audio, hurting voice AI; MoQ retransmits and buffers instead
Gemini 4 Neon
Gemini 4 Neon cuts cost per task 32% versus Carbon, at $3 input and $15 output per million tokens
AI is killing All About Berlin
After Google AI Overviews, a Berlin guide site lost 70-75% of its traffic.
Not Everything Needs an LLM
PostHog used small decision models to skip 98.7% of expensive LLM calls
Related Resources
- https://16bpp.net/blog/post/faster-asin-was-hiding-in-plain-sight/
- https://developer.download.nvidia.com/cg/asin.html
- https://personal.math.ubc.ca/~cbm/aands/page_81.htm
- https://stackoverflow.com/a/26030435
- https://en.wikipedia.org/wiki/Remez_algorithm
- https://www.embeddedrelated.com/showarticle/152.php
- https://iquilezles.org/articles/noacos/
- https://www.netlib.org/fdlibm/e_sqrt.c
- https://forwardscattering.org/post/66