core-math-sys 1.3.0

Generate bindings to CORE-MATH
Documentation
/* Correctly-rounded hyperbolic arc-sine 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> // for sqrtf

// 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^6, T1[i] approximates log(1+i/2^6) to nearest.
   Table generated by table1() in asinh.sage. */
static const float T1[] = {
 0x0p+0, 0x1.fc0a8cp-7, 0x1.f829bp-6, 0x1.77459p-5, 0x1.f0a30cp-5,
 0x1.341d7ap-4, 0x1.6f0d28p-4, 0x1.a926d4p-4, 0x1.e27076p-4, 0x1.0d77e8p-3,
 0x1.29553p-3, 0x1.44d2b6p-3, 0x1.5ff308p-3, 0x1.7ab89p-3, 0x1.9525aap-3,
 0x1.af3c94p-3, 0x1.c8ff7cp-3, 0x1.e27076p-3, 0x1.fb9186p-3, 0x1.0a324ep-2,
 0x1.1675cap-2, 0x1.22942p-2, 0x1.2e8e2cp-2, 0x1.3a64c6p-2, 0x1.4618bcp-2,
 0x1.51aad8p-2, 0x1.5d1bdcp-2, 0x1.686c82p-2, 0x1.739d8p-2, 0x1.7eaf84p-2,
 0x1.89a338p-2, 0x1.947942p-2, 0x1.9f323ep-2, 0x1.a9cecap-2, 0x1.b44f78p-2,
 0x1.beb4dap-2, 0x1.c8ff7cp-2, 0x1.d32fe8p-2, 0x1.dd46ap-2, 0x1.e74426p-2,
 0x1.f128f6p-2, 0x1.faf588p-2, 0x1.02552ap-1, 0x1.0723e6p-1, 0x1.0be72ep-1,
 0x1.109f3ap-1, 0x1.154c3ep-1, 0x1.19ee6cp-1, 0x1.1e85f6p-1, 0x1.23130ep-1,
 0x1.2795e2p-1, 0x1.2c0e9ep-1, 0x1.307d74p-1, 0x1.34e28ap-1, 0x1.393e0ep-1,
 0x1.3d9026p-1, 0x1.41d8fep-1, 0x1.4618bcp-1, 0x1.4a4f86p-1, 0x1.4e7d82p-1,
 0x1.52a2d2p-1, 0x1.56bf9ep-1, 0x1.5ad404p-1, 0x1.5ee02ap-1,
};

/* For 0 <= i < 2^6, T2[i] approximates 1/(1+i/2^6) to nearest.
   Table generated by table2() in asinh.sage. */
static const float T2[] = {
 0x1p+0, 0x1.f81f82p-1, 0x1.f07c2p-1, 0x1.e9131ap-1, 0x1.e1e1e2p-1,
 0x1.dae608p-1, 0x1.d41d42p-1, 0x1.cd8568p-1, 0x1.c71c72p-1, 0x1.c0e07p-1,
 0x1.bacf92p-1, 0x1.b4e81cp-1, 0x1.af286cp-1, 0x1.a98ef6p-1, 0x1.a41a42p-1,
 0x1.9ec8eap-1, 0x1.99999ap-1, 0x1.948b1p-1, 0x1.8f9c18p-1, 0x1.8acb9p-1,
 0x1.861862p-1, 0x1.818182p-1, 0x1.7d05f4p-1, 0x1.78a4c8p-1, 0x1.745d18p-1,
 0x1.702e06p-1, 0x1.6c16c2p-1, 0x1.681682p-1, 0x1.642c86p-1, 0x1.605816p-1,
 0x1.5c9882p-1, 0x1.58ed24p-1, 0x1.555556p-1, 0x1.51d07ep-1, 0x1.4e5e0ap-1,
 0x1.4afd6ap-1, 0x1.47ae14p-1, 0x1.446f86p-1, 0x1.414142p-1, 0x1.3e22ccp-1,
 0x1.3b13b2p-1, 0x1.381382p-1, 0x1.3521dp-1, 0x1.323e34p-1, 0x1.2f684cp-1,
 0x1.2c9fb4p-1, 0x1.29e412p-1, 0x1.27350cp-1, 0x1.24924ap-1, 0x1.21fb78p-1,
 0x1.1f7048p-1, 0x1.1cf06ap-1, 0x1.1a7b96p-1, 0x1.181182p-1, 0x1.15b1e6p-1,
 0x1.135c82p-1, 0x1.111112p-1, 0x1.0ecf56p-1, 0x1.0c9714p-1, 0x1.0a681p-1,
 0x1.08421p-1, 0x1.0624dep-1, 0x1.041042p-1, 0x1.020408p-1,
};

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

  if (au >= 0x7f800000u) return x + x; // NaN, Inf

  // asinh(x) = log(x + sqrt(x^2+1))

  float st = v.f;
  v.u = au; // discard sign
  float t = v.f;
  uint32_t j = 0;
  if (au <= 0x42a30000u) // x <= 0x1.46p+6
  {
    // for |x| <= 0x1.d2p-4, asinh(x) rounds to x to nearest
    if (au <= 0x3de90000u) {
      if (au == 0) return x; // case x = 0
      float res = __builtin_fmaf (st, -0x1p-25f, st);
#ifdef CORE_MATH_SUPPORT_ERRNO
      /* We get underflow for |x| < 2^-126, and for |x| = 2^-126 and
         rounding towards zero. */
      if (au < 0x800000u || (au == 0x800000u && res != x))
        errno = ERANGE; // underflow
#endif
      return res;
    }
    v.f = t + sqrtf (__builtin_fmaf (t, t, 1.0f));
  }
  else
    // for x large, log(x + sqrt(x^2+1)) ~ log(x) + log(2)
    j = 1; // will add j*log(2)

  // now evaluate log(t) where t means v.f
  uint32_t k = (v.u >> 23) - 127;
  v.u -= k << 23;
  /* now 1 <= v.f < 2 and log(t) = k*log(2) + log(v.f) */

  int i = (v.u >> 17) & 0x3f;
  t = __builtin_fmaf (T2[i], v.f, -1.0f);

  // return (j+k) * log(2) + T1[i] + log(1+t)
  // where we approximate log(1+t) by t-t^2/2
  t -= t * t * 0.5f;

#define LOG2 0x1.62e43p-1f
  static float s[] = {1.0f, -1.0f};
  float res = s[u>>31] * __builtin_fmaf (j + k, LOG2, T1[i] + t);
  return res;
}

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