src/libm/s_sin.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 15 Sep 2008 06:33:23 +0000
changeset 2756 a98604b691c8
child 3162 dc1eb82ffdaa
permissions -rw-r--r--
Expanded the libm support and put it into a separate directory.
slouken@2756
     1
/* @(#)s_sin.c 5.1 93/09/24 */
slouken@2756
     2
/*
slouken@2756
     3
 * ====================================================
slouken@2756
     4
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
slouken@2756
     5
 *
slouken@2756
     6
 * Developed at SunPro, a Sun Microsystems, Inc. business.
slouken@2756
     7
 * Permission to use, copy, modify, and distribute this
slouken@2756
     8
 * software is freely granted, provided that this notice
slouken@2756
     9
 * is preserved.
slouken@2756
    10
 * ====================================================
slouken@2756
    11
 */
slouken@2756
    12
slouken@2756
    13
#if defined(LIBM_SCCS) && !defined(lint)
slouken@2756
    14
static char rcsid[] = "$NetBSD: s_sin.c,v 1.7 1995/05/10 20:48:15 jtc Exp $";
slouken@2756
    15
#endif
slouken@2756
    16
slouken@2756
    17
/* sin(x)
slouken@2756
    18
 * Return sine function of x.
slouken@2756
    19
 *
slouken@2756
    20
 * kernel function:
slouken@2756
    21
 *	__kernel_sin		... sine function on [-pi/4,pi/4]
slouken@2756
    22
 *	__kernel_cos		... cose function on [-pi/4,pi/4]
slouken@2756
    23
 *	__ieee754_rem_pio2	... argument reduction routine
slouken@2756
    24
 *
slouken@2756
    25
 * Method.
slouken@2756
    26
 *      Let S,C and T denote the sin, cos and tan respectively on
slouken@2756
    27
 *	[-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
slouken@2756
    28
 *	in [-pi/4 , +pi/4], and let n = k mod 4.
slouken@2756
    29
 *	We have
slouken@2756
    30
 *
slouken@2756
    31
 *          n        sin(x)      cos(x)        tan(x)
slouken@2756
    32
 *     ----------------------------------------------------------
slouken@2756
    33
 *	    0	       S	   C		 T
slouken@2756
    34
 *	    1	       C	  -S		-1/T
slouken@2756
    35
 *	    2	      -S	  -C		 T
slouken@2756
    36
 *	    3	      -C	   S		-1/T
slouken@2756
    37
 *     ----------------------------------------------------------
slouken@2756
    38
 *
slouken@2756
    39
 * Special cases:
slouken@2756
    40
 *      Let trig be any of sin, cos, or tan.
slouken@2756
    41
 *      trig(+-INF)  is NaN, with signals;
slouken@2756
    42
 *      trig(NaN)    is that NaN;
slouken@2756
    43
 *
slouken@2756
    44
 * Accuracy:
slouken@2756
    45
 *	TRIG(x) returns trig(x) nearly rounded
slouken@2756
    46
 */
slouken@2756
    47
slouken@2756
    48
#include "math.h"
slouken@2756
    49
#include "math_private.h"
slouken@2756
    50
slouken@2756
    51
libm_hidden_proto(sin)
slouken@2756
    52
#ifdef __STDC__
slouken@2756
    53
     double sin(double x)
slouken@2756
    54
#else
slouken@2756
    55
     double sin(x)
slouken@2756
    56
     double x;
slouken@2756
    57
#endif
slouken@2756
    58
{
slouken@2756
    59
    double y[2], z = 0.0;
slouken@2756
    60
    int32_t n, ix;
slouken@2756
    61
slouken@2756
    62
    /* High word of x. */
slouken@2756
    63
    GET_HIGH_WORD(ix, x);
slouken@2756
    64
slouken@2756
    65
    /* |x| ~< pi/4 */
slouken@2756
    66
    ix &= 0x7fffffff;
slouken@2756
    67
    if (ix <= 0x3fe921fb)
slouken@2756
    68
        return __kernel_sin(x, z, 0);
slouken@2756
    69
slouken@2756
    70
    /* sin(Inf or NaN) is NaN */
slouken@2756
    71
    else if (ix >= 0x7ff00000)
slouken@2756
    72
        return x - x;
slouken@2756
    73
slouken@2756
    74
    /* argument reduction needed */
slouken@2756
    75
    else {
slouken@2756
    76
        n = __ieee754_rem_pio2(x, y);
slouken@2756
    77
        switch (n & 3) {
slouken@2756
    78
        case 0:
slouken@2756
    79
            return __kernel_sin(y[0], y[1], 1);
slouken@2756
    80
        case 1:
slouken@2756
    81
            return __kernel_cos(y[0], y[1]);
slouken@2756
    82
        case 2:
slouken@2756
    83
            return -__kernel_sin(y[0], y[1], 1);
slouken@2756
    84
        default:
slouken@2756
    85
            return -__kernel_cos(y[0], y[1]);
slouken@2756
    86
        }
slouken@2756
    87
    }
slouken@2756
    88
}
slouken@2756
    89
slouken@2756
    90
libm_hidden_def(sin)