core-math-sys 1.0.2

Generate bindings to CORE-MATH
Documentation
/* Correctly-rounded hyperbolic arc-tangent for bfloat16 value.

Copyright (c) 2025 Paul Zimmermann

This file is part of the CORE-MATH project
(https://core-math.gitlabpages.inria.fr/).

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include <stdint.h>
#ifdef CORE_MATH_SUPPORT_ERRNO
#include <errno.h>
#endif
#include <math.h> // only used to define atanh_bf16()

// Warning: clang also defines __GNUC__
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#endif

#pragma STDC FENV_ACCESS ON

typedef union {float f; uint32_t u;} b32u32_u;

/* For 0 <= i < 2^7, T1[i] contains a binary32 approximation to nearest
   of atanh(1/2+i/2^8). Table generated by table1() from atanh.sage. */
static const float T1[] = {
 0x1.193ea8p-1, 0x1.1beb1cp-1, 0x1.1e9b3p-1, 0x1.214ef4p-1, 0x1.24067ap-1,
 0x1.26c1d2p-1, 0x1.298112p-1, 0x1.2c444ap-1, 0x1.2f0b8ep-1, 0x1.31d6f4p-1,
 0x1.34a68cp-1, 0x1.377a6ep-1, 0x1.3a52acp-1, 0x1.3d2f6p-1, 0x1.40109ep-1,
 0x1.42f67cp-1, 0x1.45e114p-1, 0x1.48d07cp-1, 0x1.4bc4cep-1, 0x1.4ebe22p-1,
 0x1.51bc92p-1, 0x1.54c03cp-1, 0x1.57c938p-1, 0x1.5ad7a2p-1, 0x1.5deb9ap-1,
 0x1.61053cp-1, 0x1.6424a8p-1, 0x1.6749fcp-1, 0x1.6a755ap-1, 0x1.6da6e4p-1,
 0x1.70debap-1, 0x1.741dp-1, 0x1.7761dep-1, 0x1.7aad76p-1, 0x1.7dfff2p-1,
 0x1.815978p-1, 0x1.84ba32p-1, 0x1.88224cp-1, 0x1.8b91f2p-1, 0x1.8f095p-1,
 0x1.928896p-1, 0x1.960ff6p-1, 0x1.999fa2p-1, 0x1.9d37cep-1, 0x1.a0d8bp-1,
 0x1.a4828p-1, 0x1.a8357ap-1, 0x1.abf1d8p-1, 0x1.afb7d8p-1, 0x1.b387bcp-1,
 0x1.b761c6p-1, 0x1.bb463ep-1, 0x1.bf356cp-1, 0x1.c32f9ap-1, 0x1.c73516p-1,
 0x1.cb4634p-1, 0x1.cf6348p-1, 0x1.d38ca8p-1, 0x1.d7c2b4p-1, 0x1.dc05c8p-1,
 0x1.e0564ep-1, 0x1.e4b4aap-1, 0x1.e9214ap-1, 0x1.ed9ca2p-1, 0x1.f2272ap-1,
 0x1.f6c16p-1, 0x1.fb6bc6p-1, 0x1.001372p+0, 0x1.0279a8p+0, 0x1.04e8cep+0,
 0x1.076136p+0, 0x1.09e334p+0, 0x1.0c6f1ep+0, 0x1.0f0554p+0, 0x1.11a638p+0,
 0x1.145232p+0, 0x1.1709acp+0, 0x1.19cd2p+0, 0x1.1c9d06p+0, 0x1.1f79ep+0,
 0x1.226438p+0, 0x1.255ca6p+0, 0x1.2863cp+0, 0x1.2b7a34p+0, 0x1.2ea0aep+0,
 0x1.31d7f2p+0, 0x1.3520cap+0, 0x1.387c1p+0, 0x1.3beabp+0, 0x1.3f6da6p+0,
 0x1.430604p+0, 0x1.46b4fp+0, 0x1.4a7baap+0, 0x1.4e5b88p+0, 0x1.525606p+0,
 0x1.566cbap+0, 0x1.5aa164p+0, 0x1.5ef5eep+0, 0x1.636c7p+0, 0x1.68073cp+0,
 0x1.6cc8e2p+0, 0x1.71b436p+0, 0x1.76cc6p+0, 0x1.7c14eap+0, 0x1.8191cap+0,
 0x1.874776p+0, 0x1.8d3bp+0, 0x1.93722cp+0, 0x1.99f398p+0, 0x1.a0c6e8p+0,
 0x1.a7f4fcp+0, 0x1.af8836p+0, 0x1.b78ce4p+0, 0x1.c011bp+0, 0x1.c9285p+0,
 0x1.d2e66ap+0, 0x1.dd66dcp+0, 0x1.e8cb84p+0, 0x1.f53ffap+0, 0x1.017ed2p+1,
 0x1.09291ep+1, 0x1.11d56p+1, 0x1.1bd364p+1, 0x1.279ee4p+1, 0x1.36072ap+1,
 0x1.4890c4p+1, 0x1.62a41p+1, 0x1.8f20aep+1,
};

__bf16 cr_atanh_bf16 (__bf16 x)
{
  b32u32_u v = {.f = x};
  uint32_t u = v.u, au = u & 0x7fffffffu;

  if (au >= 0x3f800000u) { // NaN or Inf or |x| >= 1
    b32u32_u inf = {.u = 0x7f800000u};
    if (au == 0x3f800000u) { // |x| = 1
#ifdef CORE_MATH_SUPPORT_ERRNO
      errno = ERANGE;
#endif
      return (au == u) ? inf.f : -inf.f;
    }
    if (au > 0x7f800000u) // NaN
      return x + x;
#ifdef CORE_MATH_SUPPORT_ERRNO
    errno = EDOM;
#endif
    return 0.0f / 0.0f;
  }

  // now |x| < 1

  // for x < 0 we use atanh(-x) = -atanh(x)
  int neg = u >> 31;
  static const float s[] = {1.0f, -1.0f};

  if (au >= 0x3f000000u) { // |x| >= 1/2
    int i = (au - 0x3f000000u) >> 16;
    return s[neg] * T1[i];
  }

  // atanh(x) = 1/2 log((1+x)/(1-x))

  float t = v.f;

  /* for |x| <= 0x1.7p-4, atanh(x) rounds to x to nearest */
  if (au <= 0x3db80000u) { // |x| <= 0x1.7p-4
    if (au == 0) return x; // +/-0
#ifdef CORE_MATH_SUPPORT_ERRNO
    // we have underflow for |x| < 2^-126
    if (au < 0x800000u)
      errno = ERANGE; // underflow
#endif
    return __builtin_fmaf (t, 0x1p-9f, t);
  }

  // now 0x1.7p-4 < t < 1/2

  /* p is a degree-7 polynomial with odd-degree coefficients approximating
     atanh(x) on [0,1/2], generated by Sollya (cf atanh.sollya), where the
     degree-1 coefficient was slighly changed to avoid an exceptional case
     with |x| = 0x1.bap-2 */
  static const float p[] = {0x1.fffff8p-1f, 0x1.55ba9p-2f, 0x1.85109p-3f,
                            0x1.b04258p-3f};
  float tt = t * t;
  float c5 = __builtin_fmaf (p[3], tt, p[2]); // degree 5
  float c1 = __builtin_fmaf (p[1], tt, p[0]); // degree 1
  c1 = __builtin_fmaf (c5, tt * tt, c1);
  return t * c1;
}

// dummy function since GNU libc does not provide it
__bf16 atanh_bf16 (__bf16 x) {
  return (__bf16) atanhf ((float) x);
}