Is the math.h library written in binary or c language?

Can anyone tell me if the implementation of functions of cmath library is in binary or C?
Last edited on
Math.h is a header file. Thus, it is written in C. Also, this forum is for C++.
glibc actually may have platform-specific ASM for some functionality. Not necessarily fully written in ASM, but some internal functions may be.

https://ftp.gnu.org/gnu/libc/
For example, in
glibc-2.31/sysdeps/x86_64/fpu

There is assembly.

e_sqrt.c
1
2
3
4
5
6
7
8
9
double
__ieee754_sqrt (double x)
{
  double res;

  asm ("sqrtsd %1, %0" : "=x" (res) : "xm" (x));

  return res;
}


Edit: To clarify, the directory listed above also has full .S assembly files, that show more complicated calculations.
I showed sqrt because it's the simplest.
Last edited on
You can also browse the sources online: https://github.com/lattera/glibc/tree/master/sysdeps/x86_64/fpu
@TheToaster
C++ is a proper superset of C. I see no reason why C-related questions would be unwelcome here.

@shubham1355
New thread for the same question? Or are we following an X-Y problem? (What is is you are really trying to do?)

@Ganado
That example is just the C-to-assembly interface for using the processor sqrt function over doubles.

Every compiler vendor will generally provide assembly routines for math functions on the architectures it compiles for. Some functions, like cube root, are unlikely to have a direct processor instruction to compute, necessitating a simple, tight assembly routine. Everything else will, as you posted, simply have a small function that simply frobs the assembler.
Topic archived. No new replies allowed.