Henry Jackson - 3 months ago 19

C Question

I've been poring through .NET disassemblies and the GCC source code, but can't seem to find anywhere the actual implementation of

`sin()`

Can anyone help me find them? I feel like it's unlikely that ALL hardware that C will run on supports trig functions in hardware, so there must be a software algorithm

Answer

In GNU libm, the implementation of `sin`

is system-dependent. Therefore you can find the implementation, for each platform, somewhere in the appropriate subdirectory of sysdeps.

One directory includes an implementation in C, contributed by IBM. Since October 2011, this is the code that actually runs when you call `sin()`

on a typical x86-64 Linux system. It is apparently faster than the `fsin`

assembly instruction. Source code: sysdeps/ieee754/dbl-64/s_sin.c, look for `__sin (double x)`

.

This code is very complex. No one software algorithm is as fast as possible and also accurate over the whole range of *x* values, so the library implements many different algorithms and its first job is to look at *x* and decide which algorithm to use. In some regions it uses what looks like the familiar Taylor series. Several of the algorithms first compute a quick result, then if that's not accurate enough, discard it and fall back on a slower algorithm.

Older 32-bit versions of GCC/glibc used the `fsin`

instruction, which is surprisingly inaccurate for some inputs. There's a fascinating blog post illustrating this with just 2 lines of code.

fdlibm's implementation of `sin`

in pure C is much simpler than glibc's and is nicely commented. Source code: fdlibm/s_sin.c and fdlibm/k_sin.c