core-math-sys 1.3.0

Generate bindings to CORE-MATH
Documentation
/* Correctly-rounded reciprocal square root function 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>
#include <stdio.h>

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

#pragma STDC FENV_ACCESS ON

typedef union {__bf16 f; uint16_t u;} b16u16_u;
typedef union {float  f; uint32_t u;} b32u32_u;

/* For 0 <= i1 < 2^8, T1[i1] approximates 1/sqrt(x1), where the binary16
   encoding of x1 is i1*2^7 (i1 encodes the exponent of x1).
   The values for i1 = 0 and i1 = 255 are arbitrary, these cases are treated
   elsewhere.
   Table generated by table1() in rsqrt.sage. */
static const float T1[] = {
 0, 0x1p+63, 0x1.6a09e6p+62, 0x1p+62, 0x1.6a09e6p+61, 0x1p+61, 0x1.6a09e6p+60,
 0x1p+60, 0x1.6a09e6p+59, 0x1p+59, 0x1.6a09e6p+58, 0x1p+58, 0x1.6a09e6p+57,
 0x1p+57, 0x1.6a09e6p+56, 0x1p+56, 0x1.6a09e6p+55, 0x1p+55, 0x1.6a09e6p+54,
 0x1p+54, 0x1.6a09e6p+53, 0x1p+53, 0x1.6a09e6p+52, 0x1p+52, 0x1.6a09e6p+51,
 0x1p+51, 0x1.6a09e6p+50, 0x1p+50, 0x1.6a09e6p+49, 0x1p+49, 0x1.6a09e6p+48,
 0x1p+48, 0x1.6a09e6p+47, 0x1p+47, 0x1.6a09e6p+46, 0x1p+46, 0x1.6a09e6p+45,
 0x1p+45, 0x1.6a09e6p+44, 0x1p+44, 0x1.6a09e6p+43, 0x1p+43, 0x1.6a09e6p+42,
 0x1p+42, 0x1.6a09e6p+41, 0x1p+41, 0x1.6a09e6p+40, 0x1p+40, 0x1.6a09e6p+39,
 0x1p+39, 0x1.6a09e6p+38, 0x1p+38, 0x1.6a09e6p+37, 0x1p+37, 0x1.6a09e6p+36,
 0x1p+36, 0x1.6a09e6p+35, 0x1p+35, 0x1.6a09e6p+34, 0x1p+34, 0x1.6a09e6p+33,
 0x1p+33, 0x1.6a09e6p+32, 0x1p+32, 0x1.6a09e6p+31, 0x1p+31, 0x1.6a09e6p+30,
 0x1p+30, 0x1.6a09e6p+29, 0x1p+29, 0x1.6a09e6p+28, 0x1p+28, 0x1.6a09e6p+27,
 0x1p+27, 0x1.6a09e6p+26, 0x1p+26, 0x1.6a09e6p+25, 0x1p+25, 0x1.6a09e6p+24,
 0x1p+24, 0x1.6a09e6p+23, 0x1p+23, 0x1.6a09e6p+22, 0x1p+22, 0x1.6a09e6p+21,
 0x1p+21, 0x1.6a09e6p+20, 0x1p+20, 0x1.6a09e6p+19, 0x1p+19, 0x1.6a09e6p+18,
 0x1p+18, 0x1.6a09e6p+17, 0x1p+17, 0x1.6a09e6p+16, 0x1p+16, 0x1.6a09e6p+15,
 0x1p+15,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-31, 0x1.6a09e6p-32, 0x1p-32, 0x1.6a09e6p-33, 0x1p-33,
 0x1.6a09e6p-34, 0x1p-34, 0x1.6a09e6p-35, 0x1p-35, 0x1.6a09e6p-36, 0x1p-36,
 0x1.6a09e6p-37, 0x1p-37, 0x1.6a09e6p-38, 0x1p-38, 0x1.6a09e6p-39, 0x1p-39,
 0x1.6a09e6p-40, 0x1p-40, 0x1.6a09e6p-41, 0x1p-41, 0x1.6a09e6p-42, 0x1p-42,
 0x1.6a09e6p-43, 0x1p-43, 0x1.6a09e6p-44, 0x1p-44, 0x1.6a09e6p-45, 0x1p-45,
 0x1.6a09e6p-46, 0x1p-46, 0x1.6a09e6p-47, 0x1p-47, 0x1.6a09e6p-48, 0x1p-48,
 0x1.6a09e6p-49, 0x1p-49, 0x1.6a09e6p-50, 0x1p-50, 0x1.6a09e6p-51, 0x1p-51,
 0x1.6a09e6p-52, 0x1p-52, 0x1.6a09e6p-53, 0x1p-53, 0x1.6a09e6p-54, 0x1p-54,
 0x1.6a09e6p-55, 0x1p-55, 0x1.6a09e6p-56, 0x1p-56, 0x1.6a09e6p-57, 0x1p-57,
 0x1.6a09e6p-58, 0x1p-58, 0x1.6a09e6p-59, 0x1p-59, 0x1.6a09e6p-60, 0x1p-60,
 0x1.6a09e6p-61, 0x1p-61, 0x1.6a09e6p-62, 0x1p-62, 0x1.6a09e6p-63, 0x1p-63,
 0x1.6a09e6p-64, 0,
};

/* For 0 <= i2 < 2^7, T2[i2] approximates 1/sqrt(x2), where 1 <= x2 < 2
   and i2 matches the 7 lower bits of the binary16 encoding of x2.
   Table generated by table2() in rsqrt.sage. */
static const float T2[] = {
 0x1p+0, 0x1.fe02fcp-1, 0x1.fc0bd8p-1, 0x1.fa1a7ap-1, 0x1.f82ec8p-1,
 0x1.f648a2p-1, 0x1.f467f4p-1, 0x1.f28c9ap-1, 0x1.f0b686p-1, 0x1.eee594p-1,
 0x1.ed19b8p-1, 0x1.eb52d2p-1, 0x1.e990cep-1, 0x1.e7d394p-1, 0x1.e61b16p-1,
 0x1.e46732p-1, 0x1.e2b7ep-1, 0x1.e10d02p-1, 0x1.df668ap-1, 0x1.ddc462p-1,
 0x1.dc267cp-1, 0x1.da8cc2p-1, 0x1.d8f71ep-1, 0x1.d7658ap-1, 0x1.d5d7eap-1,
 0x1.d44e34p-1, 0x1.d2c854p-1, 0x1.d1463ap-1, 0x1.cfc7dcp-1, 0x1.ce4d22p-1,
 0x1.ccd606p-1, 0x1.cb6272p-1, 0x1.c9f25cp-1, 0x1.c885b6p-1, 0x1.c71c72p-1,
 0x1.c5b682p-1, 0x1.c453d8p-1, 0x1.c2f46cp-1, 0x1.c1982cp-1, 0x1.c03f0cp-1,
 0x1.bee904p-1, 0x1.bd9606p-1, 0x1.bc460ap-1, 0x1.baf8fep-1, 0x1.b9aedcp-1,
 0x1.b86796p-1, 0x1.b72326p-1, 0x1.b5e17ep-1, 0x1.b4a294p-1, 0x1.b3665ep-1,
 0x1.b22cd6p-1, 0x1.b0f5eep-1, 0x1.afc19ep-1, 0x1.ae8fdcp-1, 0x1.ad60ap-1,
 0x1.ac33e4p-1, 0x1.ab099ap-1, 0x1.a9e1bep-1, 0x1.a8bc44p-1, 0x1.a79926p-1,
 0x1.a6785ap-1, 0x1.a559dcp-1, 0x1.a43dap-1, 0x1.a323ap-1, 0x1.a20bd6p-1,
 0x1.a0f63ap-1, 0x1.9fe2c2p-1, 0x1.9ed16cp-1, 0x1.9dc22cp-1, 0x1.9cb4fcp-1,
 0x1.9ba9dcp-1, 0x1.9aa0bap-1, 0x1.99999ap-1, 0x1.98946ep-1, 0x1.979138p-1,
 0x1.968feap-1, 0x1.95908p-1, 0x1.9492f4p-1, 0x1.939744p-1, 0x1.929d64p-1,
 0x1.91a556p-1, 0x1.90af1p-1, 0x1.8fba8ap-1, 0x1.8ec7c2p-1, 0x1.8dd6b4p-1,
 0x1.8ce75ap-1, 0x1.8bf9acp-1, 0x1.8b0da8p-1, 0x1.8a2346p-1, 0x1.893a84p-1,
 0x1.88535ep-1, 0x1.876dccp-1, 0x1.8689ccp-1, 0x1.85a75ap-1, 0x1.84c66ep-1,
 0x1.83e708p-1, 0x1.83092p-1, 0x1.822cb2p-1, 0x1.8151bcp-1, 0x1.807838p-1,
 0x1.7fa024p-1, 0x1.7ec97ap-1, 0x1.7df438p-1, 0x1.7d2058p-1, 0x1.7c4dd6p-1,
 0x1.7b7cb2p-1, 0x1.7aace2p-1, 0x1.79de68p-1, 0x1.79113ep-1, 0x1.784562p-1,
 0x1.777acep-1, 0x1.76b18p-1, 0x1.75e974p-1, 0x1.7522a8p-1, 0x1.745d18p-1,
 0x1.7398bep-1, 0x1.72d59ep-1, 0x1.7213aap-1, 0x1.7152eap-1, 0x1.709354p-1,
 0x1.6fd4e8p-1, 0x1.6f17ap-1, 0x1.6e5b7ep-1, 0x1.6da078p-1, 0x1.6ce694p-1,
 0x1.6c2dc8p-1, 0x1.6b7614p-1, 0x1.6abf74p-1,
};

/* For 0 <= i2 < 2^7, T3[i2] approximates 1/sqrt(x2), where x2 is
   subnormal, and i2 equals the binary16 encoding of x2.
   Table generated by table3() in rsqrt.sage. */
static const b32u32_u T3[] = {
 {.u = 0x7f800000}, {0x1.6a09e6p+66}, {0x1p+66}, {0x1.a20bd6p+65},
 {0x1.6a09e6p+65}, {0x1.43d136p+65}, {0x1.279a74p+65}, {0x1.11aceep+65},
 {0x1p+65}, {0x1.e2b7ep+64}, {0x1.c9f25cp+64}, {0x1.b4a294p+64},
 {0x1.a20bd6p+64}, {0x1.91a556p+64}, {0x1.83092p+64}, {0x1.75e974p+64},
 {0x1.6a09e6p+64}, {0x1.5f3aa8p+64}, {0x1.555556p+64}, {0x1.4c3abep+64},
 {0x1.43d136p+64}, {0x1.3c0366p+64}, {0x1.34bf64p+64}, {0x1.2df60cp+64},
 {0x1.279a74p+64}, {0x1.21a186p+64}, {0x1.1c01aap+64}, {0x1.16b29p+64},
 {0x1.11aceep+64}, {0x1.0cea62p+64}, {0x1.08654ap+64}, {0x1.0418a4p+64},
 {0x1p+64}, {0x1.f82ec8p+63}, {0x1.f0b686p+63}, {0x1.e990cep+63},
 {0x1.e2b7ep+63}, {0x1.dc267cp+63}, {0x1.d5d7eap+63}, {0x1.cfc7dcp+63},
 {0x1.c9f25cp+63}, {0x1.c453d8p+63}, {0x1.bee904p+63}, {0x1.b9aedcp+63},
 {0x1.b4a294p+63}, {0x1.afc19ep+63}, {0x1.ab099ap+63}, {0x1.a6785ap+63},
 {0x1.a20bd6p+63}, {0x1.9dc22cp+63}, {0x1.99999ap+63}, {0x1.95908p+63},
 {0x1.91a556p+63}, {0x1.8dd6b4p+63}, {0x1.8a2346p+63}, {0x1.8689ccp+63},
 {0x1.83092p+63}, {0x1.7fa024p+63}, {0x1.7c4dd6p+63}, {0x1.79113ep+63},
 {0x1.75e974p+63}, {0x1.72d59ep+63}, {0x1.6fd4e8p+63}, {0x1.6ce694p+63},
 {0x1.6a09e6p+63}, {0x1.673e32p+63}, {0x1.6482d4p+63}, {0x1.61d72cp+63},
 {0x1.5f3aa8p+63}, {0x1.5cacb8p+63}, {0x1.5a2cd8p+63}, {0x1.57ba8cp+63},
 {0x1.555556p+63}, {0x1.52fcc4p+63}, {0x1.50b06ap+63}, {0x1.4e6fdep+63},
 {0x1.4c3abep+63}, {0x1.4a10aap+63}, {0x1.47f146p+63}, {0x1.45dc3ap+63},
 {0x1.43d136p+63}, {0x1.41cfe8p+63}, {0x1.3fd808p+63}, {0x1.3de948p+63},
 {0x1.3c0366p+63}, {0x1.3a261cp+63}, {0x1.38512cp+63}, {0x1.368458p+63},
 {0x1.34bf64p+63}, {0x1.330218p+63}, {0x1.314c3ep+63}, {0x1.2f9dap+63},
 {0x1.2df60cp+63}, {0x1.2c5552p+63}, {0x1.2abb44p+63}, {0x1.2927b2p+63},
 {0x1.279a74p+63}, {0x1.26135ep+63}, {0x1.24924ap+63}, {0x1.23170ep+63},
 {0x1.21a186p+63}, {0x1.20318cp+63}, {0x1.1ec702p+63}, {0x1.1d61cp+63},
 {0x1.1c01aap+63}, {0x1.1aa69ep+63}, {0x1.19507ep+63}, {0x1.17ff2ep+63},
 {0x1.16b29p+63}, {0x1.156a88p+63}, {0x1.1426fcp+63}, {0x1.12e7dp+63},
 {0x1.11aceep+63}, {0x1.10763cp+63}, {0x1.0f43a4p+63}, {0x1.0e150ep+63},
 {0x1.0cea62p+63}, {0x1.0bc39p+63}, {0x1.0aa07cp+63}, {0x1.098116p+63},
 {0x1.08654ap+63}, {0x1.074d06p+63}, {0x1.063832p+63}, {0x1.0526c4p+63},
 {0x1.0418a4p+63}, {0x1.030dc6p+63}, {0x1.020614p+63}, {0x1.010184p+63},
};

__bf16 cr_rsqrt_bf16 (__bf16 x) {
  b16u16_u v = {.f = x};
  uint16_t u = v.u;
  uint16_t i1 = u >> 7;
  uint16_t i2 = u & 0x7f;
  static const b16u16_u nan = {.u = 0x7fc0};       // qNaN
  static const b16u16_u minus_inf = {.u = 0xff80}; // -Inf
  if (u >> 15) { // case -0 or x < 0
#ifdef CORE_MATH_SUPPORT_ERRNO
    errno = (x != 0) ? EDOM : ERANGE;
#endif
    // x + nan.f raises invalid if x = -sNaN
    return (u == 0x8000) ? minus_inf.f : x + nan.f;
  }
  if ((u >> 7) == 0xff) return 1.0f / x; // NaN or Inf
  if (i1 == 0) { // subnormal case
#ifdef CORE_MATH_SUPPORT_ERRNO
    if (i2 == 0)
      errno = ERANGE; // case x = +0
#endif
    return T3[i2].f;
  }
  return T1[i1] * T2[i2];
}

// dummy function since GNU libc does not provide it
__bf16 rsqrt_bf16 (__bf16 x) {
	return (__bf16) (1.0f / sqrtf ((float) x));
}