A fast approximation of the sRGB gamma function

First attempt

Γ(x) = 1.138 * sqrt(x) - 0.138 * x

Evaluation

The definition of a gamma function error depends on the purpose. The absolute difference of \( \Gamma_{\rm approx.} ( {\Gamma_{\rm precise}}^{-1}(x) ) \) and \( {\Gamma_{\rm precise}}^{-1}( \Gamma_{\rm approx.} (x) ) \) are shown below. Blue lines indicate proposed approximation and green ones indicate \( x^{1 / 2.2} \) for comparison. Gamma corrected values are mapped from [0, 1] to [0, 255].

The proposed approximation has less error than \( x^{1 / 2.2} \), at least those definitions of the error.

Second attempt

Recent CPUs have the instructions to calculate (approximate) inverse square root, which are much faster than square root ones (See the throughputs of "sqrtss" and "rsqrtss" in agner.org instruction tables). Using these instructions, (1.138 * rsqrt(x) - 0.138) * x costs only a few CPU cycles.

We can avoid singlarity at x = 0 and improve the approximation simultaneously with a small modification:

Γ(x) = (b * rsqrt(x + a) - c) * x,
where
    a = 0.00279491,
    b = 1.15907984,
    c = b * rsqrt(1 + a) - 1.

cd ../

Yasuhiro Fujii <y-fujii at mimosa-pudica.net>