core-math-sys 1.0.2

Generate bindings to CORE-MATH
Documentation
/* Correctly-rounded cubic root of 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>
#include <math.h> // only used during performance tests

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

#pragma STDC FENV_ACCESS ON

/* For 0 <= i1 < 2^8, T1[i1] approximates cbrt(x1), where the 16-bit
   encoding of x1 is i1*2^7 (i1 encodes the exponent of x1).
   Table generated by table1() in cbrt.sage. */
static const float T1[] = {
 0, 0x1p-42, 0x1.428a3p-42, 0x1.965feap-42, 0x1p-41, 0x1.428a3p-41,
 0x1.965feap-41, 0x1p-40, 0x1.428a3p-40, 0x1.965feap-40, 0x1p-39,
 0x1.428a3p-39, 0x1.965feap-39, 0x1p-38, 0x1.428a3p-38, 0x1.965feap-38,
 0x1p-37, 0x1.428a3p-37, 0x1.965feap-37, 0x1p-36, 0x1.428a3p-36,
 0x1.965feap-36, 0x1p-35, 0x1.428a3p-35, 0x1.965feap-35, 0x1p-34,
 0x1.428a3p-34, 0x1.965feap-34, 0x1p-33, 0x1.428a3p-33, 0x1.965feap-33,
 0x1p-32, 0x1.428a3p-32, 0x1.965feap-32, 0x1p-31, 0x1.428a3p-31,
 0x1.965feap-31, 0x1p-30, 0x1.428a3p-30, 0x1.965feap-30, 0x1p-29,
 0x1.428a3p-29, 0x1.965feap-29, 0x1p-28, 0x1.428a3p-28, 0x1.965feap-28,
 0x1p-27, 0x1.428a3p-27, 0x1.965feap-27, 0x1p-26, 0x1.428a3p-26,
 0x1.965feap-26, 0x1p-25, 0x1.428a3p-25, 0x1.965feap-25, 0x1p-24,
 0x1.428a3p-24, 0x1.965feap-24, 0x1p-23, 0x1.428a3p-23, 0x1.965feap-23,
 0x1p-22, 0x1.428a3p-22, 0x1.965feap-22, 0x1p-21, 0x1.428a3p-21,
 0x1.965feap-21, 0x1p-20, 0x1.428a3p-20, 0x1.965feap-20, 0x1p-19,
 0x1.428a3p-19, 0x1.965feap-19, 0x1p-18, 0x1.428a3p-18, 0x1.965feap-18,
 0x1p-17, 0x1.428a3p-17, 0x1.965feap-17, 0x1p-16, 0x1.428a3p-16,
 0x1.965feap-16, 0x1p-15, 0x1.428a3p-15, 0x1.965feap-15, 0x1p-14,
 0x1.428a3p-14, 0x1.965feap-14, 0x1p-13, 0x1.428a3p-13, 0x1.965feap-13,
 0x1p-12, 0x1.428a3p-12, 0x1.965feap-12, 0x1p-11, 0x1.428a3p-11,
 0x1.965feap-11, 0x1p-10, 0x1.428a3p-10, 0x1.965feap-10, 0x1p-9, 0x1.428a3p-9,
 0x1.965feap-9, 0x1p-8, 0x1.428a3p-8, 0x1.965feap-8, 0x1p-7, 0x1.428a3p-7,
 0x1.965feap-7, 0x1p-6, 0x1.428a3p-6, 0x1.965feap-6, 0x1p-5, 0x1.428a3p-5,
 0x1.965feap-5, 0x1p-4, 0x1.428a3p-4, 0x1.965feap-4, 0x1p-3, 0x1.428a3p-3,
 0x1.965feap-3, 0x1p-2, 0x1.428a3p-2, 0x1.965feap-2, 0x1p-1, 0x1.428a3p-1,
 0x1.965feap-1, 0x1p+0, 0x1.428a3p+0, 0x1.965feap+0, 0x1p+1, 0x1.428a3p+1,
 0x1.965feap+1, 0x1p+2, 0x1.428a3p+2, 0x1.965feap+2, 0x1p+3, 0x1.428a3p+3,
 0x1.965feap+3, 0x1p+4, 0x1.428a3p+4, 0x1.965feap+4, 0x1p+5, 0x1.428a3p+5,
 0x1.965feap+5, 0x1p+6, 0x1.428a3p+6, 0x1.965feap+6, 0x1p+7, 0x1.428a3p+7,
 0x1.965feap+7, 0x1p+8, 0x1.428a3p+8, 0x1.965feap+8, 0x1p+9, 0x1.428a3p+9,
 0x1.965feap+9, 0x1p+10, 0x1.428a3p+10, 0x1.965feap+10, 0x1p+11,
 0x1.428a3p+11, 0x1.965feap+11, 0x1p+12, 0x1.428a3p+12, 0x1.965feap+12,
 0x1p+13, 0x1.428a3p+13, 0x1.965feap+13, 0x1p+14, 0x1.428a3p+14,
 0x1.965feap+14, 0x1p+15, 0x1.428a3p+15, 0x1.965feap+15, 0x1p+16,
 0x1.428a3p+16, 0x1.965feap+16, 0x1p+17, 0x1.428a3p+17, 0x1.965feap+17,
 0x1p+18, 0x1.428a3p+18, 0x1.965feap+18, 0x1p+19, 0x1.428a3p+19,
 0x1.965feap+19, 0x1p+20, 0x1.428a3p+20, 0x1.965feap+20, 0x1p+21,
 0x1.428a3p+21, 0x1.965feap+21, 0x1p+22, 0x1.428a3p+22, 0x1.965feap+22,
 0x1p+23, 0x1.428a3p+23, 0x1.965feap+23, 0x1p+24, 0x1.428a3p+24,
 0x1.965feap+24, 0x1p+25, 0x1.428a3p+25, 0x1.965feap+25, 0x1p+26,
 0x1.428a3p+26, 0x1.965feap+26, 0x1p+27, 0x1.428a3p+27, 0x1.965feap+27,
 0x1p+28, 0x1.428a3p+28, 0x1.965feap+28, 0x1p+29, 0x1.428a3p+29,
 0x1.965feap+29, 0x1p+30, 0x1.428a3p+30, 0x1.965feap+30, 0x1p+31,
 0x1.428a3p+31, 0x1.965feap+31, 0x1p+32, 0x1.428a3p+32, 0x1.965feap+32,
 0x1p+33, 0x1.428a3p+33, 0x1.965feap+33, 0x1p+34, 0x1.428a3p+34,
 0x1.965feap+34, 0x1p+35, 0x1.428a3p+35, 0x1.965feap+35, 0x1p+36,
 0x1.428a3p+36, 0x1.965feap+36, 0x1p+37, 0x1.428a3p+37, 0x1.965feap+37,
 0x1p+38, 0x1.428a3p+38, 0x1.965feap+38, 0x1p+39, 0x1.428a3p+39,
 0x1.965feap+39, 0x1p+40, 0x1.428a3p+40, 0x1.965feap+40, 0x1p+41,
 0x1.428a3p+41, 0x1.965feap+41, 0x1p+42, 0x1.428a3p+42, 0,
};

/* For 0 <= i2 < 2^7, T2[i2] approximates cbrt(x2), where 1 <= x2 < 2
   and i2 matches the 7 lower bits of the 16-bit encoding of x2.
   Table generated by table2() in cbrt.sage. */
static const float T2[] = {
 0x1p+0, 0x1.00aa3ap+0, 0x1.015392p+0, 0x1.01fc0ep+0, 0x1.02a3aep+0,
 0x1.034a76p+0, 0x1.03f068p+0, 0x1.049588p+0, 0x1.0539d6p+0, 0x1.05dd58p+0,
 0x1.06800ep+0, 0x1.0721fcp+0, 0x1.07c324p+0, 0x1.086386p+0, 0x1.090328p+0,
 0x1.09a20ap+0, 0x1.0a403p+0, 0x1.0add9ap+0, 0x1.0b7a4cp+0, 0x1.0c1646p+0,
 0x1.0cb18cp+0, 0x1.0d4c1ep+0, 0x1.0de602p+0, 0x1.0e7f34p+0, 0x1.0f17bcp+0,
 0x1.0faf98p+0, 0x1.1046ccp+0, 0x1.10dd56p+0, 0x1.11733ep+0, 0x1.12088p+0,
 0x1.129d22p+0, 0x1.133122p+0, 0x1.13c484p+0, 0x1.14574ap+0, 0x1.14e974p+0,
 0x1.157b04p+0, 0x1.160bfcp+0, 0x1.169c5ep+0, 0x1.172c2ap+0, 0x1.17bb64p+0,
 0x1.184a0ap+0, 0x1.18d822p+0, 0x1.1965a8p+0, 0x1.19f2a2p+0, 0x1.1a7f0ep+0,
 0x1.1b0afp+0, 0x1.1b9648p+0, 0x1.1c2118p+0, 0x1.1cab62p+0, 0x1.1d3524p+0,
 0x1.1dbe62p+0, 0x1.1e471ep+0, 0x1.1ecf56p+0, 0x1.1f570ep+0, 0x1.1fde46p+0,
 0x1.2065p+0, 0x1.20eb3cp+0, 0x1.2170fcp+0, 0x1.21f64p+0, 0x1.227b0ap+0,
 0x1.22ff5cp+0, 0x1.238336p+0, 0x1.240698p+0, 0x1.248986p+0, 0x1.250bfep+0,
 0x1.258e02p+0, 0x1.260f94p+0, 0x1.2690b4p+0, 0x1.271164p+0, 0x1.2791a2p+0,
 0x1.281174p+0, 0x1.2890d6p+0, 0x1.290fcap+0, 0x1.298e54p+0, 0x1.2a0c72p+0,
 0x1.2a8a26p+0, 0x1.2b077p+0, 0x1.2b845p+0, 0x1.2c00cap+0, 0x1.2c7cdcp+0,
 0x1.2cf88ap+0, 0x1.2d73dp+0, 0x1.2deeb2p+0, 0x1.2e6932p+0, 0x1.2ee34ep+0,
 0x1.2f5d08p+0, 0x1.2fd66p+0, 0x1.304f58p+0, 0x1.30c7fp+0, 0x1.314028p+0,
 0x1.31b804p+0, 0x1.322f8p+0, 0x1.32a6ap+0, 0x1.331d64p+0, 0x1.3393cap+0,
 0x1.3409d8p+0, 0x1.347f8ap+0, 0x1.34f4e4p+0, 0x1.3569e4p+0, 0x1.35de8cp+0,
 0x1.3652dep+0, 0x1.36c6d6p+0, 0x1.373a7ap+0, 0x1.37adc8p+0, 0x1.3820cp+0,
 0x1.389364p+0, 0x1.3905b4p+0, 0x1.3977b2p+0, 0x1.39e95cp+0, 0x1.3a5ab4p+0,
 0x1.3acbbcp+0, 0x1.3b3c72p+0, 0x1.3bacd6p+0, 0x1.3c1cecp+0, 0x1.3c8cb2p+0,
 0x1.3cfc2ap+0, 0x1.3d6b54p+0, 0x1.3dda3p+0, 0x1.3e48bep+0, 0x1.3eb7p+0,
 0x1.3f24f6p+0, 0x1.3f92ap+0, 0x1.4p+0, 0x1.406d14p+0, 0x1.40d9ep+0,
 0x1.41466p+0, 0x1.41b298p+0, 0x1.421e88p+0,
};

/* For 0 <= i2 < 2^7, T3[i2] approximates cbrt(x2), where x2 is
   subnormal, and i2 equals the 16-bit encoding of x2.
   Table generated by table3() in cbrt.sage. */
static const float T3[] = {
 0x0p+0, 0x1.965feap-45, 0x1p-44, 0x1.250bfep-44, 0x1.428a3p-44,
 0x1.5b720ap-44, 0x1.713744p-44, 0x1.84aef2p-44, 0x1.965feap-44,
 0x1.a6a58ep-44, 0x1.b5c0fcp-44, 0x1.c3e274p-44, 0x1.d12edp-44,
 0x1.ddc2ecp-44, 0x1.e9b5dcp-44, 0x1.f51a62p-44, 0x1p-43, 0x1.0539d6p-43,
 0x1.0a403p-43, 0x1.0f17bcp-43, 0x1.13c484p-43, 0x1.184a0ap-43,
 0x1.1cab62p-43, 0x1.20eb3cp-43, 0x1.250bfep-43, 0x1.290fcap-43,
 0x1.2cf888p-43, 0x1.30c7fp-43, 0x1.347f8ap-43, 0x1.3820cp-43, 0x1.3bacd6p-43,
 0x1.3f24f6p-43, 0x1.428a3p-43, 0x1.45dd7cp-43, 0x1.491fc2p-43,
 0x1.4c51d2p-43, 0x1.4f7474p-43, 0x1.52885ap-43, 0x1.558e3p-43,
 0x1.58868ep-43, 0x1.5b720ap-43, 0x1.5e512ap-43, 0x1.61246ep-43,
 0x1.63ec4ep-43, 0x1.66a93ap-43, 0x1.695b9cp-43, 0x1.6c03d6p-43,
 0x1.6ea246p-43, 0x1.713744p-43, 0x1.73c326p-43, 0x1.764636p-43,
 0x1.78c0c4p-43, 0x1.7b3312p-43, 0x1.7d9d66p-43, 0x1.8p-43, 0x1.825b1cp-43,
 0x1.84aef2p-43, 0x1.86fbbcp-43, 0x1.8941aep-43, 0x1.8b80f8p-43,
 0x1.8db9ccp-43, 0x1.8fec56p-43, 0x1.9218c2p-43, 0x1.943f3cp-43,
 0x1.965feap-43, 0x1.987af4p-43, 0x1.9a907cp-43, 0x1.9ca0a8p-43,
 0x1.9eab9ap-43, 0x1.a0b17p-43, 0x1.a2b24ep-43, 0x1.a4ae4cp-43,
 0x1.a6a58ep-43, 0x1.a8982ap-43, 0x1.aa863ep-43, 0x1.ac6fe6p-43,
 0x1.ae5536p-43, 0x1.b0364ap-43, 0x1.b21338p-43, 0x1.b3ec16p-43,
 0x1.b5c0fcp-43, 0x1.b791fep-43, 0x1.b95f2ep-43, 0x1.bb28a4p-43,
 0x1.bcee7p-43, 0x1.beb0a8p-43, 0x1.c06f58p-43, 0x1.c22a98p-43,
 0x1.c3e274p-43, 0x1.c596fep-43, 0x1.c74848p-43, 0x1.c8f65ep-43,
 0x1.caa15p-43, 0x1.cc492ep-43, 0x1.cdee04p-43, 0x1.cf8fep-43, 0x1.d12edp-43,
 0x1.d2cae2p-43, 0x1.d46422p-43, 0x1.d5fa9ap-43, 0x1.d78e58p-43,
 0x1.d91f68p-43, 0x1.daadd4p-43, 0x1.dc39a8p-43, 0x1.ddc2ecp-43,
 0x1.df49bp-43, 0x1.e0cdf8p-43, 0x1.e24fd2p-43, 0x1.e3cf48p-43, 0x1.e54c6p-43,
 0x1.e6c726p-43, 0x1.e83fa2p-43, 0x1.e9b5dcp-43, 0x1.eb29dep-43,
 0x1.ec9bbp-43, 0x1.ee0b5ap-43, 0x1.ef78e2p-43, 0x1.f0e454p-43,
 0x1.f24db4p-43, 0x1.f3b50cp-43, 0x1.f51a62p-43, 0x1.f67dbcp-43,
 0x1.f7df24p-43, 0x1.f93e9ep-43, 0x1.fa9c32p-43, 0x1.fbf7e4p-43,
 0x1.fd51cp-43, 0x1.fea9c6p-43,
};

typedef union {__bf16 f; uint16_t u;} b16u16_u;

__bf16 cr_cbrt_bf16 (__bf16 x){
  b16u16_u v = {.f = x};
  uint16_t u = v.u, au = u & 0x7fff;
  uint16_t i1 = au >> 7;
  uint16_t i2 = au & 0x7f;

  if ((au >> 7) == 0xff) return x + x; // NaN or Inf

  /* Exact cases in the normal range are of the form:
     0x1p+0    (encoding 0x3f80)
     0x1.f4p+0 (encoding 0x3ffa)
     0x1.bp+1  (encoding 0x4058)
     and similarly for other sets of 3 consecutive binades.
     The first case x=2^(3k) is properly dealt with the code
     below, since T1[i1] is exact and T2[i2]=1.
     Same for the second case since the "exponent part" is 2^(3k)
     thus the "low part" T2[i2] is exact.
     We thus only have to deal with the last case.
  */
  uint16_t e = au >> 7, e3 = e % 3;
  if ((au & 0x7f) == 0x58 && e3 == 2) { // case 0x1.bp+1
    v.u = (u & 0x8000) | 0x3fc0; // 0x1.8p+0 with sign bit
    v.u += ((e - 0x80) / 3) << 7;
    return v.f;
  }

  static const float s[] = {1.0f, -1.0f};
  if (i1 == 0) // subnormal case
    return s[u>>15] * T3[i2];
  return s[u>>15] * T1[i1] * T2[i2];
}

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