The Langevin function is a special function that occurs in computations involving paramagnetic materials and elastomeres. It is defined as L(x) = coth(x) - 1/x. In most practical use cases, the function of interest is really the inverse of the Langevin function, i.e. L⁻¹(x), for which there is no analytic closed-form expression.
Traditionally, various simple approximation of low accuracy (about 1%) have been used for the inverse Langevin function. However, researchers have noticed more recently that this can have a noticeable negative impact on the overall accuracy of simulations. For example:
https://hal.archives-ouvertes.fr/hal-01361406
Amine Ammar, “Effect of the inverse Langevin approximation on the solution of the Fokker-Planck equation of non-linear dilute polymer”, Journal of Non-Newtonian Fluid Mechanics, No 231, 2016, pp. 1-5
As it turns out, both the Langevin function and its inverse allow for the use of the special function unit in GPUs (also known as the multifunction unit, or MUFU), resulting in accurate single-precision approximations that are also fast. Given that the Langevin function is only used in a small corner of the scientific computation application space, a GPU implementation will be useful to only a handful of people, but I figured there is no harm in sharing what I already have :-)
Code below updated 9/10/2021
/*
Copyright (c) 2018-2021, Norbert Juffa
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Implementation of the Langevin function L(x) = coth(x) - 1/x.
maximum error: 1.54 ulp
*/
__device__ float langevinf (float a)
{
float r, t;
t = fabsf (a);
if (t > 1.8125f) {
float e;
e = 2.0f * 1.442695f * t;
asm ("ex2.approx.ftz.f32 %0,%0;" : "+f"(e));
e = e - 1.0f;
asm ("rcp.approx.ftz.f32 %0,%0;" : "+f"(e));
asm ("rcp.approx.ftz.f32 %0,%0;" : "+f"(t));
r = fmaf (2.0f, e, 1.0f - t);
r = copysignf (r, a);
} else {
float s;
s = a * a;
r = 7.70960469e-8f;
r = fmaf (r, s, -1.65101926e-6f);
r = fmaf (r, s, 2.03457112e-5f);
r = fmaf (r, s, -2.10521728e-4f);
r = fmaf (r, s, 2.11580913e-3f);
r = fmaf (r, s, -2.22220998e-2f);
r = fmaf (r, s, 8.33333284e-2f);
r = fmaf (r, a, 0.25f * a);
}
return r;
}
/* Inverse Langevin function L⁻¹(x). Maximum error: 3.925 ulp */
__device__ float langevininvf (float x)
{
float fa, p, r, t;
fa = fabsf (x);
if ((fa > (57.0f / 64.0f)) && (fa <= 1.0f)) {
t = fa - 1.0f;
asm ("rcp.approx.ftz.f32 %0,%0;" : "+f"(t));
r = copysignf (t, x);
} else {
t = fmaf (x, -x, 1.0f);
asm ("lg2.approx.ftz.f32 %0,%0;" : "+f"(t));
p = 2.69152224e-6f; // 0x1.694000p-19
p = fmaf (p, t, -1.40199758e-4f); // -0x1.26052cp-13
p = fmaf (p, t, -1.82510854e-3f); // -0x1.de70f6p-10
p = fmaf (p, t, -8.87932349e-3f); // -0x1.22f52ap-7
p = fmaf (p, t, -2.20804960e-2f); // -0x1.69c450p-6
p = fmaf (p, t, -3.11916713e-2f); // -0x1.ff0b5ap-6
p = fmaf (p, t, -2.84342300e-2f); // -0x1.d1ddcep-6
p = fmaf (p, t, -1.95302274e-2f); // -0x1.3ffbb6p-6
p = fmaf (p, t, 1.15530221e-2f); // 0x1.7a91c6p-7
p = fmaf (p, t, -6.13459945e-2f); // -0x1.f68be0p-5
p = fmaf (p, t, 1.91382647e-1f); // 0x1.87f3a0p-3
p = fmaf (p, t, -6.23836875e-1f); // -0x1.3f678cp-1
p = fmaf (p, t, 5.00000000e-1f); // 0x1.000000p-1
t = x + x;
r = fmaf (p, t, t);
}
return r;
}