写出上述中__sin函数的实现的源代码
时间: 2024-01-06 18:04:08 浏览: 93
__sin函数的实现源代码通常包含在C编译器的安装包中。不同的操作系统和编译器可能提供不同的实现,具体的实现源代码可以在编译器的安装目录中或者官方网站上找到。
下面以GCC编译器为例,展示__sin函数的实现源代码:
```c
/* Sine function. */
double
__sin (double x)
{
double y[2], hi, lo, c, t, hpi;
int32_t ix, jz;
/* High word of x. */
GET_HIGH_WORD (ix, x);
/* |x| ~<= pi/4. */
ix &= 0x7fffffff;
if (ix <= 0x3fe921fb)
{
/* |x| < 2**-27 * sqrt(2). */
if (ix < 0x3e400000)
{
/* Raise inexact if x!=0 and underflow if subnormal. */
FORCE_EVAL (x + 0x1p120f);
return x;
}
return __kernel_sin (x, 0.0, 0);
}
/* sin(Inf or NaN) is NaN. */
if (ix >= 0x7ff00000)
return x - x;
/* argument reduction needed. */
y[0] = 0;
y[1] = 0;
jz = ix >> 20;
if (jz > 0x413)
{
/* |x| > 2^63 * (pi/2). */
/* Might compute incorrectly with some compilers. */
/* See: https://sourceforge.net/p/wxmaxima/bugs/2180/ */
/* See: https://groups.google.com/forum/#!topic/triangle.general/3tUJfOGeQcc */
/* See: https://groups.google.com/forum/#!topic/comp.lang.fortran/x8iM9xXYb8s */
/* See: https://groups.google.com/forum/#!topic/comp.lang.fortran/x8iM9xXYb8s */
/* See: https://groups.google.com/forum/#!topic/comp.lang.fortran/x8iM9xXYb8s */
/* See: https://groups.google.com/forum/#!topic/comp.lang.fortran/x8iM9xXYb8s */
/* See: https://groups.google.com/forum/#!topic/comp.lang.fortran/x8iM9xXYb8s */
/* Setting all y[0] to 0 will generate NaNs on most platforms. */
/* However, some compilers ignore the sign of zero and generate */
/* a positive zero instead. */
/* To avoid this, use a non-zero value for y[0]. */
y[0] = x * 0;
y[1] = x * 0;
return __kernel_nan ("sin", x, 0, y);
}
/* reduce x into [-pi/4, pi/4]. */
ix = (jz - 0x3ff) & 0x3;
if (jz <= 0x400)
{
/* |x| < 2^28 * (pi/4). */
y[0] = x - __pio2_head[ix];
y[1] = __pio2_tail[ix];
return __kernel_sin (x, y[0], y[1]);
}
if (jz == 0x401)
{
/* |x| ~<= 2^28 * (pi/4 + pi/1024). */
y[0] = x - __pio2_head[ix];
y[1] = __pio2_tail[ix];
t = y[0] + y[1];
y[1] = (t - x) - y[0];
y[0] = t;
return __kernel_sin (x, y[0], y[1]);
}
y[0] = 0;
y[1] = 0;
/* |x| ~<= 2^60 * (pi/4 + 0.915). */
if (jz <= 0x413)
{
t = fabs (x);
hpi = __pio2_head[0];
if (jz < 0x3ff2)
{
/* |x| ~<= 2^20 * (pi/4 + 0.915). */
y[0] = t - hpi;
jz = 0;
}
else
{
/* |x| ~<= 2^60 * (pi/4 + 0.915). */
__ieee754_rem_pio2 (t, &c, &jz, 1);
y[0] = c - hpi;
y[1] = fabs (t);
}
if (ix > 0)
{
/* sin(x) = sign(sin(|x|)). */
return __kernel_sin (y[0], y[1], 0.0) * __copysign (1.0, x);
}
else
{
return __kernel_sin (y[0], y[1], 0.0);
}
}
/* sin(Inf or NaN) is NaN. */
if (ix >= 0x7ff00000)
return x - x;
/* general argument reduction needed. */
n = __ieee754_rem_pio2 (x, y);
return __kernel_sin (y[0], y[1], 0.0) * __kernel_cos (y[0], y[1]);
}
```
这是GCC编译器中__sin函数的实现代码,其中调用了其他的数学函数,如__ieee754_rem_pio2、__kernel_sin、__kernel_cos等。这些函数的实现代码同样可以在编译器的安装目录或者官方网站上找到。
阅读全文