src/libm/s_tan.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 09 Apr 2015 22:33:39 -0400
branchiOS-improvements
changeset 9542 618662dc9e82
parent 8842 f587255157cf
permissions -rw-r--r--
Closing the iOS-improvements branch.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8840
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     2
 * ====================================================
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     3
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
 *
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
 * Developed at SunPro, a Sun Microsystems, Inc. business.
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
 * Permission to use, copy, modify, and distribute this
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
 * software is freely granted, provided that this notice
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
 * is preserved.
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
 * ====================================================
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
 */
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
/* tan(x)
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
 * Return tangent function of x.
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
 *
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
 * kernel function:
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
 *	__kernel_tan		... tangent function on [-pi/4,pi/4]
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
 *	__ieee754_rem_pio2	... argument reduction routine
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
 *
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
 * Method.
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
 *      Let S,C and T denote the sin, cos and tan respectively on
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
 *	[-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
 *	in [-pi/4 , +pi/4], and let n = k mod 4.
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
 *	We have
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
 *
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
 *          n        sin(x)      cos(x)        tan(x)
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
 *     ----------------------------------------------------------
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
 *	    0	       S	   C		 T
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
 *	    1	       C	  -S		-1/T
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
 *	    2	      -S	  -C		 T
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
 *	    3	      -C	   S		-1/T
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
 *     ----------------------------------------------------------
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
 *
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
 * Special cases:
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
 *      Let trig be any of sin, cos, or tan.
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
 *      trig(+-INF)  is NaN, with signals;
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
 *      trig(NaN)    is that NaN;
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
 *
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
 * Accuracy:
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
 *	TRIG(x) returns trig(x) nearly rounded
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
 */
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
8842
f587255157cf Fixed building on command line Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 8840
diff changeset
    42
#include "math_libm.h"
8840
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
#include "math_private.h"
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
double tan(double x)
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
{
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
	double y[2],z=0.0;
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
	int32_t n, ix;
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
    /* High word of x. */
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
	GET_HIGH_WORD(ix,x);
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
    /* |x| ~< pi/4 */
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
	ix &= 0x7fffffff;
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
	if(ix <= 0x3fe921fb) return __kernel_tan(x,z,1);
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
    /* tan(Inf or NaN) is NaN */
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
	else if (ix>=0x7ff00000) return x-x;		/* NaN */
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
    /* argument reduction needed */
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
	else {
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
	    n = __ieee754_rem_pio2(x,y);
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
	    return __kernel_tan(y[0],y[1],1-((n&1)<<1)); /*   1 -- n even
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
							-1 -- n odd */
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
	}
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
}
9b6ddcbdea65 Added SDL_sqrtf(), SDL_tan(), SDL_tanf()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
libm_hidden_def(tan)