Added atan2 implementation from uClibc
authorSam Lantinga <slouken@libsdl.org>
Sun, 29 Aug 2010 16:05:34 -0700
changeset 4870d275c95ddc05
parent 4869 2b1bcf74e4c4
child 4871 81a6a9f396ba
Added atan2 implementation from uClibc
configure.in
include/SDL_config.h.in
include/SDL_stdinc.h
src/libm/e_atan2.c
src/libm/math.h
     1.1 --- a/configure.in	Sun Aug 29 15:26:12 2010 -0700
     1.2 +++ b/configure.in	Sun Aug 29 16:05:34 2010 -0700
     1.3 @@ -235,7 +235,7 @@
     1.4      AC_CHECK_FUNCS(malloc calloc realloc free getenv setenv putenv unsetenv qsort abs bcopy memset memcpy memmove strlen strlcpy strlcat strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp sscanf snprintf vsnprintf sigaction setjmp nanosleep sysconf sysctlbyname)
     1.5  
     1.6      AC_CHECK_LIB(m, pow, [LIBS="$LIBS -lm"; EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lm"])
     1.7 -    AC_CHECK_FUNCS(ceil copysign cos cosf fabs floor log pow scalbn sin sinf sqrt)
     1.8 +    AC_CHECK_FUNCS(atan2 ceil copysign cos cosf fabs floor log pow scalbn sin sinf sqrt)
     1.9  
    1.10      AC_CHECK_LIB(iconv, libiconv_open, [LIBS="$LIBS -liconv"; EXTRA_LDFLAGS="$EXTRA_LDFLAGS -liconv"])
    1.11      AC_CHECK_FUNCS(iconv)
     2.1 --- a/include/SDL_config.h.in	Sun Aug 29 15:26:12 2010 -0700
     2.2 +++ b/include/SDL_config.h.in	Sun Aug 29 16:05:34 2010 -0700
     2.3 @@ -136,6 +136,7 @@
     2.4  #undef HAVE_SNPRINTF
     2.5  #undef HAVE_VSNPRINTF
     2.6  #undef HAVE_M_PI
     2.7 +#undef HAVE_ATAN2
     2.8  #undef HAVE_CEIL
     2.9  #undef HAVE_COPYSIGN
    2.10  #undef HAVE_COS
     3.1 --- a/include/SDL_stdinc.h	Sun Aug 29 15:26:12 2010 -0700
     3.2 +++ b/include/SDL_stdinc.h	Sun Aug 29 16:05:34 2010 -0700
     3.3 @@ -694,6 +694,12 @@
     3.4  #define M_PI    3.14159265358979323846264338327950288   /* pi */
     3.5  #endif
     3.6  
     3.7 +#ifdef HAVE_ATAN2
     3.8 +#define SDL_atan2       atan2
     3.9 +#else
    3.10 +extern DECLSPEC double SDLCALL SDL_atan2(double y, double x);
    3.11 +#endif
    3.12 +
    3.13  #ifdef HAVE_CEIL
    3.14  #define SDL_ceil        ceil
    3.15  #else
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/libm/e_atan2.c	Sun Aug 29 16:05:34 2010 -0700
     4.3 @@ -0,0 +1,116 @@
     4.4 +/*
     4.5 + * ====================================================
     4.6 + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
     4.7 + *
     4.8 + * Developed at SunPro, a Sun Microsystems, Inc. business.
     4.9 + * Permission to use, copy, modify, and distribute this
    4.10 + * software is freely granted, provided that this notice
    4.11 + * is preserved.
    4.12 + * ====================================================
    4.13 + */
    4.14 +
    4.15 +/* __ieee754_atan2(y,x)
    4.16 + * Method :
    4.17 + *	1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
    4.18 + *	2. Reduce x to positive by (if x and y are unexceptional):
    4.19 + *		ARG (x+iy) = arctan(y/x)   	   ... if x > 0,
    4.20 + *		ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
    4.21 + *
    4.22 + * Special cases:
    4.23 + *
    4.24 + *	ATAN2((anything), NaN ) is NaN;
    4.25 + *	ATAN2(NAN , (anything) ) is NaN;
    4.26 + *	ATAN2(+-0, +(anything but NaN)) is +-0  ;
    4.27 + *	ATAN2(+-0, -(anything but NaN)) is +-pi ;
    4.28 + *	ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
    4.29 + *	ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;
    4.30 + *	ATAN2(+-(anything but INF and NaN), -INF) is +-pi;
    4.31 + *	ATAN2(+-INF,+INF ) is +-pi/4 ;
    4.32 + *	ATAN2(+-INF,-INF ) is +-3pi/4;
    4.33 + *	ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
    4.34 + *
    4.35 + * Constants:
    4.36 + * The hexadecimal values are the intended ones for the following
    4.37 + * constants. The decimal values may be used, provided that the
    4.38 + * compiler will convert from decimal to binary accurately enough
    4.39 + * to produce the hexadecimal values shown.
    4.40 + */
    4.41 +
    4.42 +#include "math.h"
    4.43 +#include "math_private.h"
    4.44 +
    4.45 +static const double
    4.46 +tiny  = 1.0e-300,
    4.47 +zero  = 0.0,
    4.48 +pi_o_4  = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */
    4.49 +pi_o_2  = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */
    4.50 +pi      = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */
    4.51 +pi_lo   = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
    4.52 +
    4.53 +double attribute_hidden __ieee754_atan2(double y, double x)
    4.54 +{
    4.55 +	double z;
    4.56 +	int32_t k,m,hx,hy,ix,iy;
    4.57 +	u_int32_t lx,ly;
    4.58 +
    4.59 +	EXTRACT_WORDS(hx,lx,x);
    4.60 +	ix = hx&0x7fffffff;
    4.61 +	EXTRACT_WORDS(hy,ly,y);
    4.62 +	iy = hy&0x7fffffff;
    4.63 +	if(((ix|((lx|-lx)>>31))>0x7ff00000)||
    4.64 +	   ((iy|((ly|-ly)>>31))>0x7ff00000))	/* x or y is NaN */
    4.65 +	   return x+y;
    4.66 +	if(((hx-0x3ff00000)|lx)==0) return atan(y);   /* x=1.0 */
    4.67 +	m = ((hy>>31)&1)|((hx>>30)&2);	/* 2*sign(x)+sign(y) */
    4.68 +
    4.69 +    /* when y = 0 */
    4.70 +	if((iy|ly)==0) {
    4.71 +	    switch(m) {
    4.72 +		case 0:
    4.73 +		case 1: return y; 	/* atan(+-0,+anything)=+-0 */
    4.74 +		case 2: return  pi+tiny;/* atan(+0,-anything) = pi */
    4.75 +		case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
    4.76 +	    }
    4.77 +	}
    4.78 +    /* when x = 0 */
    4.79 +	if((ix|lx)==0) return (hy<0)?  -pi_o_2-tiny: pi_o_2+tiny;
    4.80 +
    4.81 +    /* when x is INF */
    4.82 +	if(ix==0x7ff00000) {
    4.83 +	    if(iy==0x7ff00000) {
    4.84 +		switch(m) {
    4.85 +		    case 0: return  pi_o_4+tiny;/* atan(+INF,+INF) */
    4.86 +		    case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
    4.87 +		    case 2: return  3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
    4.88 +		    case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
    4.89 +		}
    4.90 +	    } else {
    4.91 +		switch(m) {
    4.92 +		    case 0: return  zero  ;	/* atan(+...,+INF) */
    4.93 +		    case 1: return -zero  ;	/* atan(-...,+INF) */
    4.94 +		    case 2: return  pi+tiny  ;	/* atan(+...,-INF) */
    4.95 +		    case 3: return -pi-tiny  ;	/* atan(-...,-INF) */
    4.96 +		}
    4.97 +	    }
    4.98 +	}
    4.99 +    /* when y is INF */
   4.100 +	if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
   4.101 +
   4.102 +    /* compute y/x */
   4.103 +	k = (iy-ix)>>20;
   4.104 +	if(k > 60) z=pi_o_2+0.5*pi_lo; 	/* |y/x| >  2**60 */
   4.105 +	else if(hx<0&&k<-60) z=0.0; 	/* |y|/x < -2**60 */
   4.106 +	else z=atan(fabs(y/x));		/* safe to do y/x */
   4.107 +	switch (m) {
   4.108 +	    case 0: return       z  ;	/* atan(+,+) */
   4.109 +	    case 1: {
   4.110 +	    	      u_int32_t zh;
   4.111 +		      GET_HIGH_WORD(zh,z);
   4.112 +		      SET_HIGH_WORD(z,zh ^ 0x80000000);
   4.113 +		    }
   4.114 +		    return       z  ;	/* atan(-,+) */
   4.115 +	    case 2: return  pi-(z-pi_lo);/* atan(+,-) */
   4.116 +	    default: /* case 3 */
   4.117 +	    	    return  (z-pi_lo)-pi;/* atan(-,-) */
   4.118 +	}
   4.119 +}
     5.1 --- a/src/libm/math.h	Sun Aug 29 15:26:12 2010 -0700
     5.2 +++ b/src/libm/math.h	Sun Aug 29 16:05:34 2010 -0700
     5.3 @@ -24,6 +24,10 @@
     5.4  
     5.5  /* Math routines from uClibc: http://www.uclibc.org */
     5.6  
     5.7 +#ifndef HAVE_ATAN2
     5.8 +#define __ieee754_atan2 SDL_atan2
     5.9 +#endif
    5.10 +
    5.11  #ifdef HAVE_COPYSIGN
    5.12  #define copysign        SDL_uclibc_copysign
    5.13  #else