#ifndef FP_LIB_HEADER
#define FP_LIB_HEADER
#include "int_lib.h"
#include "int_math.h"
#include "int_types.h"
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#if defined SINGLE_PRECISION
half_rep_t;
rep_t;
twice_rep_t;
srep_t;
fp_t;
#define HALF_REP_C …
#define REP_C …
#define significandBits …
static __inline int rep_clz(rep_t a) { … }
static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) { … }
COMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b);
#elif defined DOUBLE_PRECISION
typedef uint32_t half_rep_t;
typedef uint64_t rep_t;
typedef int64_t srep_t;
typedef double fp_t;
#define HALF_REP_C …
#define REP_C …
#define significandBits …
static inline int rep_clz(rep_t a) { return __builtin_clzll(a); }
#define loWord …
#define hiWord …
static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
const uint64_t plolo = loWord(a) * loWord(b);
const uint64_t plohi = loWord(a) * hiWord(b);
const uint64_t philo = hiWord(a) * loWord(b);
const uint64_t phihi = hiWord(a) * hiWord(b);
const uint64_t r0 = loWord(plolo);
const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo);
*lo = r0 + (r1 << 32);
*hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi;
}
#undef loWord
#undef hiWord
COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b);
#elif defined QUAD_PRECISION
#if defined(CRT_HAS_F128) && defined(CRT_HAS_128BIT)
typedef uint64_t half_rep_t;
typedef __uint128_t rep_t;
typedef __int128_t srep_t;
typedef tf_float fp_t;
#define HALF_REP_C …
#define REP_C …
#if defined(CRT_HAS_IEEE_TF)
#define significandBits …
#define TF_MANT_DIG …
static __inline int rep_clz(rep_t a) {
const union {
__uint128_t ll;
#if _YUGA_BIG_ENDIAN
struct {
uint64_t high, low;
} s;
#else
struct {
uint64_t low, high;
} s;
#endif
} uu = {.ll = a};
uint64_t word;
uint64_t add;
if (uu.s.high) {
word = uu.s.high;
add = 0;
} else {
word = uu.s.low;
add = 64;
}
return __builtin_clzll(word) + add;
}
#define Word_LoMask …
#define Word_HiMask …
#define Word_FullMask …
#define Word_1 …
#define Word_2 …
#define Word_3 …
#define Word_4 …
static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
const uint64_t product11 = Word_1(a) * Word_1(b);
const uint64_t product12 = Word_1(a) * Word_2(b);
const uint64_t product13 = Word_1(a) * Word_3(b);
const uint64_t product14 = Word_1(a) * Word_4(b);
const uint64_t product21 = Word_2(a) * Word_1(b);
const uint64_t product22 = Word_2(a) * Word_2(b);
const uint64_t product23 = Word_2(a) * Word_3(b);
const uint64_t product24 = Word_2(a) * Word_4(b);
const uint64_t product31 = Word_3(a) * Word_1(b);
const uint64_t product32 = Word_3(a) * Word_2(b);
const uint64_t product33 = Word_3(a) * Word_3(b);
const uint64_t product34 = Word_3(a) * Word_4(b);
const uint64_t product41 = Word_4(a) * Word_1(b);
const uint64_t product42 = Word_4(a) * Word_2(b);
const uint64_t product43 = Word_4(a) * Word_3(b);
const uint64_t product44 = Word_4(a) * Word_4(b);
const __uint128_t sum0 = (__uint128_t)product44;
const __uint128_t sum1 = (__uint128_t)product34 + (__uint128_t)product43;
const __uint128_t sum2 =
(__uint128_t)product24 + (__uint128_t)product33 + (__uint128_t)product42;
const __uint128_t sum3 = (__uint128_t)product14 + (__uint128_t)product23 +
(__uint128_t)product32 + (__uint128_t)product41;
const __uint128_t sum4 =
(__uint128_t)product13 + (__uint128_t)product22 + (__uint128_t)product31;
const __uint128_t sum5 = (__uint128_t)product12 + (__uint128_t)product21;
const __uint128_t sum6 = (__uint128_t)product11;
const __uint128_t r0 = (sum0 & Word_FullMask) + ((sum1 & Word_LoMask) << 32);
const __uint128_t r1 = (sum0 >> 64) + ((sum1 >> 32) & Word_FullMask) +
(sum2 & Word_FullMask) + ((sum3 << 32) & Word_HiMask);
*lo = r0 + (r1 << 64);
const bool carry = *lo < r0;
*hi = (r1 >> 64) + (sum1 >> 96) + (sum2 >> 64) + (sum3 >> 32) + sum4 +
(sum5 << 32) + (sum6 << 64) + carry;
}
#undef Word_1
#undef Word_2
#undef Word_3
#undef Word_4
#undef Word_HiMask
#undef Word_LoMask
#undef Word_FullMask
#endif
#else
typedef long double fp_t;
#endif
#else
#error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined.
#endif
#if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) || \
(defined(QUAD_PRECISION) && defined(CRT_HAS_TF_MODE))
#define typeWidth …
static __inline rep_t toRep(fp_t x) { … }
static __inline fp_t fromRep(rep_t x) { … }
#if !defined(QUAD_PRECISION) || defined(CRT_HAS_IEEE_TF)
#define exponentBits …
#define maxExponent …
#define exponentBias …
#define implicitBit …
#define significandMask …
#define signBit …
#define absMask …
#define exponentMask …
#define oneRep …
#define infRep …
#define quietBit …
#define qnanRep …
static __inline int normalize(rep_t *significand) { … }
static __inline void wideLeftShift(rep_t *hi, rep_t *lo, unsigned int count) { … }
static __inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo,
unsigned int count) { … }
static __inline fp_t __compiler_rt_logbX(fp_t x) { … }
static __inline fp_t __compiler_rt_scalbnX(fp_t x, int y) { … }
#endif
static __inline fp_t __compiler_rt_fmaxX(fp_t x, fp_t y) { … }
#endif
#if defined(SINGLE_PRECISION)
static __inline fp_t __compiler_rt_logbf(fp_t x) { … }
static __inline fp_t __compiler_rt_scalbnf(fp_t x, int y) { … }
#elif defined(DOUBLE_PRECISION)
static __inline fp_t __compiler_rt_logb(fp_t x) {
return __compiler_rt_logbX(x);
}
static __inline fp_t __compiler_rt_scalbn(fp_t x, int y) {
return __compiler_rt_scalbnX(x, y);
}
static __inline fp_t __compiler_rt_fmax(fp_t x, fp_t y) {
#if defined(__aarch64__)
return __builtin_fmax(x, y);
#else
return __compiler_rt_fmaxX(x, y);
#endif
}
#elif defined(QUAD_PRECISION) && defined(CRT_HAS_TF_MODE)
#if defined(CRT_HAS_IEEE_TF)
static __inline tf_float __compiler_rt_logbtf(tf_float x) {
return __compiler_rt_logbX(x);
}
static __inline tf_float __compiler_rt_scalbntf(tf_float x, int y) {
return __compiler_rt_scalbnX(x, y);
}
static __inline tf_float __compiler_rt_fmaxtf(tf_float x, tf_float y) {
return __compiler_rt_fmaxX(x, y);
}
#define __compiler_rt_logbl …
#define __compiler_rt_scalbnl …
#define __compiler_rt_fmaxl …
#define crt_fabstf …
#define crt_copysigntf …
#elif defined(CRT_LDBL_128BIT)
static __inline tf_float __compiler_rt_logbtf(tf_float x) {
return crt_logbl(x);
}
static __inline tf_float __compiler_rt_scalbntf(tf_float x, int y) {
return crt_scalbnl(x, y);
}
static __inline tf_float __compiler_rt_fmaxtf(tf_float x, tf_float y) {
return crt_fmaxl(x, y);
}
#define __compiler_rt_logbl …
#define __compiler_rt_scalbnl …
#define __compiler_rt_fmaxl …
#define crt_fabstf …
#define crt_copysigntf …
#else
#error Unsupported TF mode type
#endif
#endif
#endif