/*        $NetBSD$  */

/*
 * Copyright (c) 1995  Ken Nakata
 *        All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *        @(#)fpu_trig.c      10/24/95
 */
#include "types.h"
#include "fpu_emul.h"


struct fpn *fpu_calc_tan(struct fpemu *);

extern struct xtable {
	int	sp_exp;
	u_int	sp_m0,sp_m1,sp_single;
} pitbl[];



static u_int tanQ4[] = { 0x3EA0B759,0xF50F8688 };
static u_int tanP3[] = { 0xBEF2BAA5,0xA8924F04 };
static u_int tanQ3[] = { 0xBF346F59,0xB39BA65F,0x00000000 };
static u_int tanP2[] = { 0x3FF60000,0xE073D3FC,0x199C4A00 };
static u_int tanQ2[] = { 0x3FF90000,0xD23CD684,0x15D95FA1 };
static u_int tanP1[] = { 0xBFFC0000,0x8895A6C5,0xFB423BCA };
static u_int tanQ1[] = { 0xBFFD0000,0xEEF57E0D,0xA84BC8CE };

u_int TWOBYPI[]= { 0x3FE45F30, 0x6DC9C883 };
u_int TWOPI[]= 	{ 0x40010000, 0xC90FDAA2,0x2168C235 };



struct fpn *
fpu_acos(fe)
     struct fpemu *fe;
{
  /* stub */
  return &fe->fe_f2;
}

struct fpn *
fpu_asin(fe)
     struct fpemu *fe;
{
  /* stub */
  return &fe->fe_f2;
}

struct fpn *
fpu_atan(fe)
     struct fpemu *fe;
{
  /* stub */
  return &fe->fe_f2;
}


struct fpn *fpu_tan(struct fpemu *fe) 
{

   	u_int fpsr;
        struct fpn *fp= &fe->fe_f2;
        
        
        fpsr=fe->fe_fpsr&~FPSR_EXCP;
        
        if(fp->fp_class== FPC_NUM || fp->fp_class == FPC_ZERO) {
          
  		fp=fpu_calc_tan(fe);
        
        } else if(fp->fp_class == FPC_INF) {
        	fp=fpu_newnan(fe);
        	fpsr|=FPSR_OPERR;
        
        } else if(fp->fp_class == FPC_SNAN) {
        
         	fpsr|=FPSR_SNAN;
         	fp=fpu_newnan(fe);
         	
        } else {
         	fp=fpu_newnan(fe);
        }
        
        fe->fe_fpsr =  fpsr;
        return fp;

}
struct fpn *
fpu_calc_tan(fe)
     struct fpemu *fe;
{
  
  
	static struct fpn X,S,U,V;
	static struct fpn *d;
	int 	N;
	
		

	CPYFPN(&X, &fe->fe_f2);

	/* reduce argument if necessary */
	if(X.fp_exp > 5 || (X.fp_exp == 5 &&  X.fp_mant[0] >= (0xbc7e00 >> (31 - FP_LG))) ) {
	 	CPYFPN(&fe->fe_f1,&fe->fe_f2);
	 	fpu_explode(fe, &fe->fe_f2,FTYPE_EXT, TWOPI);
	 	
	 	d=fpu_rem(fe);
	 	CPYFPN(&X ,d);
	 	
	};
        if(X.fp_exp <= -40) {
         		
         			return &fe->fe_f2;         	
        };
        
        

	
       
        fpu_explode(fe, &fe->fe_f1, FTYPE_DBL, TWOBYPI);
	CPYFPN(&fe->fe_f2,&X)
        d = fpu_mul(fe);
        fpu_implode(fe, d, FTYPE_LNG, &N);
        
  	

	CPYFPN(&fe->fe_f1, &X);
       	fpu_explode(fe, &fe->fe_f2, FTYPE_EXT, &(pitbl[32+N].sp_exp));
     	fe->fe_f2.fp_sign= ! fe->fe_f2.fp_sign; 
       	d=fpu_add(fe);
       	 
        CPYFPN(&fe->fe_f1, d);
        fpu_explode(fe, &fe->fe_f2, FTYPE_SNG, &(pitbl[32+N].sp_single));
       	fe->fe_f2.fp_sign= ! fe->fe_f2.fp_sign;       	
       	d=fpu_add(fe);

      	
       	
       	CPYFPN(&X, d);
        CPYFPN(&fe->fe_f1, d);
 	CPYFPN(&fe->fe_f2, d);
 	d=fpu_mul(fe);
 	CPYFPN(&S, d);      	 
	
	
	/* calc U */
	
	CPYFPN(&fe->fe_f1, &S);
	fpu_explode(fe, &fe->fe_f2, FTYPE_DBL, tanP3);
	d=fpu_mul(fe);
	CPYFPN(&fe->fe_f2, d);
       	fpu_explode(fe, &fe->fe_f1, FTYPE_EXT, tanP2);
       	d=fpu_add(fe);
       	CPYFPN(&fe->fe_f2, d);
       
       	CPYFPN(&fe->fe_f1, &S);
	d=fpu_mul(fe);
	CPYFPN(&fe->fe_f2,d);
       	fpu_explode(fe, &fe->fe_f1, FTYPE_EXT, tanP1);
       	d=fpu_add(fe);
       	CPYFPN(&fe->fe_f2, d);
        
         
       	CPYFPN(&fe->fe_f1, &S);
	d=fpu_mul(fe);
	CPYFPN(&fe->fe_f2,d);       
      
	 
       	CPYFPN(&fe->fe_f1, &X);
	d=fpu_mul(fe);
	CPYFPN(&fe->fe_f2, d);
       	CPYFPN(&fe->fe_f1, &X);
       	d=fpu_add(fe);
       	CPYFPN(&U, d);

	/* calc V */
	
	
	CPYFPN(&fe->fe_f1, &S);
	fpu_explode(fe, &fe->fe_f2, FTYPE_DBL, tanQ4);
	d=fpu_mul(fe);
	CPYFPN(&fe->fe_f2, d);
       	fpu_explode(fe, &fe->fe_f1, FTYPE_EXT, tanQ3);
       	d=fpu_add(fe);
       	CPYFPN(&fe->fe_f2, d);
       
       	CPYFPN(&fe->fe_f1, &S);
	d=fpu_mul(fe);
	CPYFPN(&fe->fe_f2,d);
       	fpu_explode(fe, &fe->fe_f1, FTYPE_EXT, tanQ2);
       	d=fpu_add(fe);
       	CPYFPN(&fe->fe_f2, d);
       	
       	  
       	CPYFPN(&fe->fe_f1, &S);
	d=fpu_mul(fe);
	CPYFPN(&fe->fe_f2,d);
       	fpu_explode(fe, &fe->fe_f1, FTYPE_EXT, tanQ1);
       	d=fpu_add(fe);
       	CPYFPN(&fe->fe_f2, d);
       	        
         
       	CPYFPN(&fe->fe_f1, &S);
	d=fpu_mul(fe);
	CPYFPN(&fe->fe_f2,d);       
      
	 
       	fpu_const(&fe->fe_f1, 0x32);
	d=fpu_add(fe);
	CPYFPN(&V, d);
        
        if (N & 0xfffffffe == N) {
         	CPYFPN(&fe->fe_f1, &U);
         	CPYFPN(&fe->fe_f2, &V);
         	d=fpu_div(fe);
		CPYFPN(&fe->fe_f2, d);
        
        } else {
            	CPYFPN(&fe->fe_f1, &V);
         	CPYFPN(&fe->fe_f2, &U);
         	d=fpu_div(fe);
		CPYFPN(&fe->fe_f2, d);
		fe->fe_f2.fp_sign= ! fe->fe_f2.fp_sign;
        
        }
       
  	return &fe->fe_f2;
}

