/* Platform-specific declarations of math functions. wasm32 version. Copyright (C) 2026 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, see . */ #ifndef _MATH_H # error "Never include directly;\ include instead." #endif /* No SIMD variants: the default empty definitions. */ #include /* THE FUNCTIONS WASM HAS AN INSTRUCTION FOR, INLINE IN A PROGRAM. This toolchain compiles programs with -fno-builtin, so the compiler does not know that sqrt is sqrt, and every call is an import from libc.so: a cross-instance call that cannot be inlined, about 20 ns against 2.3 ns for the f64.sqrt a statically linked program gets after LTO. -fno-math-errno does not help, because without the builtin there is nothing for it to relax. So the optimizing compile of a C program gets these definitions, which are the instruction, and the library's answer: this port has no rounding modes and no exception flags (bits/fenv.h), and the instruction is correctly rounded. sqrt of a negative number sets errno to EDOM, as the library does, and returns the instruction's NaN, as the library does. gnu_inline, as glibc's own header inlines are: the definition is never emitted, a program that defines its own sqrt still may, and taking the address names libc.so's. Not for C++, whose brings these names in with using-declarations and overloads of its own, and not inside glibc. */ #if defined __OPTIMIZE__ && !defined __cplusplus && !defined _LIBC \ && !defined __NO_MATH_INLINES && defined __extern_always_inline \ && defined __has_builtin # if __has_builtin (__builtin_elementwise_sqrt) \ && __has_builtin (__builtin_elementwise_floor) \ && __has_builtin (__builtin_elementwise_ceil) \ && __has_builtin (__builtin_elementwise_trunc) \ && __has_builtin (__builtin_elementwise_roundeven) /* glibc's errno.h declaration, repeated so that does not define errno's names. 33 is EDOM on Linux (asm-generic/errno-base.h). The negative case cannot call the library's sqrt by an asm label instead: the compiler drops an inline definition that calls its own symbol and calls the library every time, which is how this was measured the first time. */ extern int *__errno_location (void) __THROW __attribute_const__; /* The errno store costs more than it looks: a sqrt-only loop ran 2M calls in 11.0 ms with it and 4.7 ms without (7.1 ms calling libc.so), because the cold call in the body is still a call; in the compute suite's fp kernel the inline won anyway, 3.6 ms against 5.1. A program that compiles with -fno-math-errno has said it does not read errno, and gets the bare instruction. */ # ifdef __NO_MATH_ERRNO__ # define __WASM32_SQRT_EDOM(x) ((void) 0) # else # define __WASM32_SQRT_EDOM(x) \ (__builtin_isless ((x), 0) ? (void) (*__errno_location () = 33) : (void) 0) # endif __extern_always_inline double __NTH (sqrt (double __x)) { __WASM32_SQRT_EDOM (__x); return __builtin_elementwise_sqrt (__x); } # define __WASM32_MATH_INLINE(type, name, op) \ __extern_always_inline type __NTH (name (type __x)) { return op (__x); } __WASM32_MATH_INLINE (double, floor, __builtin_elementwise_floor) __WASM32_MATH_INLINE (double, ceil, __builtin_elementwise_ceil) /* The C99 names only where the program asked for C99: in C89 they are the program's. */ # ifdef __USE_ISOC99 __extern_always_inline float __NTH (sqrtf (float __x)) { __WASM32_SQRT_EDOM (__x); return __builtin_elementwise_sqrt (__x); } __WASM32_MATH_INLINE (float, floorf, __builtin_elementwise_floor) __WASM32_MATH_INLINE (float, ceilf, __builtin_elementwise_ceil) __WASM32_MATH_INLINE (double, trunc, __builtin_elementwise_trunc) __WASM32_MATH_INLINE (float, truncf, __builtin_elementwise_trunc) /* Round to nearest, ties to even: the only rounding mode there is. */ __WASM32_MATH_INLINE (double, rint, __builtin_elementwise_roundeven) __WASM32_MATH_INLINE (float, rintf, __builtin_elementwise_roundeven) __WASM32_MATH_INLINE (double, nearbyint, __builtin_elementwise_roundeven) __WASM32_MATH_INLINE (float, nearbyintf, __builtin_elementwise_roundeven) # endif # undef __WASM32_MATH_INLINE # undef __WASM32_SQRT_EDOM # endif #endif