cosmopolitan/libc/tinymath/internal.h
Justine Tunney 957c61cbbf
Release Cosmopolitan v3.3
This change upgrades to GCC 12.3 and GNU binutils 2.42. The GNU linker
appears to have changed things so that only a single de-duplicated str
table is present in the binary, and it gets placed wherever the linker
wants, regardless of what the linker script says. To cope with that we
need to stop using .ident to embed licenses. As such, this change does
significant work to revamp how third party licenses are defined in the
codebase, using `.section .notice,"aR",@progbits`.

This new GCC 12.3 toolchain has support for GNU indirect functions. It
lets us support __target_clones__ for the first time. This is used for
optimizing the performance of libc string functions such as strlen and
friends so far on x86, by ensuring AVX systems favor a second codepath
that uses VEX encoding. It shaves some latency off certain operations.
It's a useful feature to have for scientific computing for the reasons
explained by the test/libcxx/openmp_test.cc example which compiles for
fifteen different microarchitectures. Thanks to the upgrades, it's now
also possible to use newer instruction sets, such as AVX512FP16, VNNI.

Cosmo now uses the %gs register on x86 by default for TLS. Doing it is
helpful for any program that links `cosmo_dlopen()`. Such programs had
to recompile their binaries at startup to change the TLS instructions.
That's not great, since it means every page in the executable needs to
be faulted. The work of rewriting TLS-related x86 opcodes, is moved to
fixupobj.com instead. This is great news for MacOS x86 users, since we
previously needed to morph the binary every time for that platform but
now that's no longer necessary. The only platforms where we need fixup
of TLS x86 opcodes at runtime are now Windows, OpenBSD, and NetBSD. On
Windows we morph TLS to point deeper into the TIB, based on a TlsAlloc
assignment, and on OpenBSD/NetBSD we morph %gs back into %fs since the
kernels do not allow us to specify a value for the %gs register.

OpenBSD users are now required to use APE Loader to run Cosmo binaries
and assimilation is no longer possible. OpenBSD kernel needs to change
to allow programs to specify a value for the %gs register, or it needs
to stop marking executable pages loaded by the kernel as mimmutable().

This release fixes __constructor__, .ctor, .init_array, and lastly the
.preinit_array so they behave the exact same way as glibc.

We no longer use hex constants to define math.h symbols like M_PI.
2024-02-20 13:27:59 -08:00

84 lines
2.1 KiB
C

#ifndef COSMOPOLITAN_LIBC_TINYMATH_INTERNAL_H_
#define COSMOPOLITAN_LIBC_TINYMATH_INTERNAL_H_
#define WANT_ROUNDING 1
COSMOPOLITAN_C_START_
#define issignalingf_inline(x) 0
#define issignaling_inline(x) 0
#define asuint(f) ((union{float _f; uint32_t _i;}){f})._i
#define asfloat(i) ((union{uint32_t _i; float _f;}){i})._f
#define asuint64(f) ((union{double _f; uint64_t _i;}){f})._i
#define asdouble(i) ((union{uint64_t _i; double _f;}){i})._f
// clang-format on
static inline float eval_as_float(float x) {
float y = x;
return y;
}
static inline double eval_as_double(double x) {
double y = x;
return y;
}
static inline void fp_force_evall(long double x) {
volatile long double y;
y = x;
(void)y;
}
static inline void fp_force_evalf(float x) {
volatile float y;
y = x;
(void)y;
}
static inline void fp_force_eval(double x) {
volatile double y;
y = x;
(void)y;
}
static inline double fp_barrier(double x) {
volatile double y = x;
return y;
}
static inline float fp_barrierf(float x) {
volatile float y = x;
return y;
}
extern const uint16_t __rsqrt_tab[128];
double __math_divzero(uint32_t);
double __math_invalid(double);
double __math_oflow(uint32_t);
double __math_uflow(uint32_t);
double __math_xflow(uint32_t, double);
float __math_divzerof(uint32_t);
float __math_invalidf(float);
float __math_oflowf(uint32_t);
float __math_uflowf(uint32_t);
float __math_xflowf(uint32_t, float);
long double __polevll(long double, const long double *, int);
long double __p1evll(long double, const long double *, int);
long double __math_invalidl(long double);
#define FORCE_EVAL(x) \
do { \
if (sizeof(x) == sizeof(float)) { \
fp_force_evalf(x); \
} else if (sizeof(x) == sizeof(double)) { \
fp_force_eval(x); \
} else { \
fp_force_evall(x); \
} \
} while (0)
COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_TINYMATH_INTERNAL_H_ */