2001-03-12 00:04:52 +00:00
|
|
|
/*
|
|
|
|
* IBM Accurate Mathematical Library
|
2002-07-06 06:36:39 +00:00
|
|
|
* written by International Business Machines Corp.
|
2013-01-02 19:01:50 +00:00
|
|
|
* Copyright (C) 2001-2013 Free Software Foundation, Inc.
|
2001-03-12 00:04:52 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
2002-08-26 22:40:48 +00:00
|
|
|
* the Free Software Foundation; either version 2.1 of the License, or
|
2001-03-12 00:04:52 +00:00
|
|
|
* (at your option) any later version.
|
2001-03-12 07:57:09 +00:00
|
|
|
*
|
2001-03-12 00:04:52 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2002-08-20 21:51:55 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2001-03-12 00:04:52 +00:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2012-02-09 23:18:22 +00:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2001-03-12 00:04:52 +00:00
|
|
|
*/
|
|
|
|
/****************************************************************/
|
|
|
|
/* MODULE_NAME: sincos32.c */
|
|
|
|
/* */
|
|
|
|
/* FUNCTIONS: ss32 */
|
|
|
|
/* cc32 */
|
|
|
|
/* c32 */
|
|
|
|
/* sin32 */
|
|
|
|
/* cos32 */
|
|
|
|
/* mpsin */
|
|
|
|
/* mpcos */
|
|
|
|
/* mpranred */
|
|
|
|
/* mpsin1 */
|
|
|
|
/* mpcos1 */
|
|
|
|
/* */
|
|
|
|
/* FILES NEEDED: endian.h mpa.h sincos32.h */
|
|
|
|
/* mpa.c */
|
|
|
|
/* */
|
|
|
|
/* Multi Precision sin() and cos() function with p=32 for sin()*/
|
|
|
|
/* cos() arcsin() and arccos() routines */
|
|
|
|
/* In addition mpranred() routine performs range reduction of */
|
|
|
|
/* a double number x into multi precision number y, */
|
|
|
|
/* such that y=x-n*pi/2, abs(y)<pi/4, n=0,+-1,+-2,.... */
|
|
|
|
/****************************************************************/
|
|
|
|
#include "endian.h"
|
|
|
|
#include "mpa.h"
|
|
|
|
#include "sincos32.h"
|
2012-03-09 19:29:16 +00:00
|
|
|
#include <math_private.h>
|
2001-03-12 00:04:52 +00:00
|
|
|
|
2011-10-25 04:56:33 +00:00
|
|
|
#ifndef SECTION
|
|
|
|
# define SECTION
|
|
|
|
#endif
|
|
|
|
|
2001-03-12 00:04:52 +00:00
|
|
|
/****************************************************************/
|
|
|
|
/* Compute Multi-Precision sin() function for given p. Receive */
|
|
|
|
/* Multi Precision number x and result stored at y */
|
|
|
|
/****************************************************************/
|
2011-10-25 04:56:33 +00:00
|
|
|
static void
|
|
|
|
SECTION
|
|
|
|
ss32(mp_no *x, mp_no *y, int p) {
|
2001-03-12 00:04:52 +00:00
|
|
|
int i;
|
2001-03-12 07:57:09 +00:00
|
|
|
double a;
|
|
|
|
mp_no mpt1,x2,gor,sum ,mpk={1,{1.0}};
|
2001-03-12 00:04:52 +00:00
|
|
|
for (i=1;i<=p;i++) mpk.d[i]=0;
|
|
|
|
|
2013-02-14 06:09:14 +00:00
|
|
|
__sqr(x,&x2,p);
|
2001-03-13 02:01:34 +00:00
|
|
|
__cpy(&oofac27,&gor,p);
|
|
|
|
__cpy(&gor,&sum,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
for (a=27.0;a>1.0;a-=2.0) {
|
|
|
|
mpk.d[1]=a*(a-1.0);
|
2001-03-13 02:01:34 +00:00
|
|
|
__mul(&gor,&mpk,&mpt1,p);
|
|
|
|
__cpy(&mpt1,&gor,p);
|
|
|
|
__mul(&x2,&sum,&mpt1,p);
|
|
|
|
__sub(&gor,&mpt1,&sum,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
}
|
2001-03-13 02:01:34 +00:00
|
|
|
__mul(x,&sum,y,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
/* Compute Multi-Precision cos() function for given p. Receive Multi */
|
|
|
|
/* Precision number x and result stored at y */
|
|
|
|
/**********************************************************************/
|
2011-10-25 04:56:33 +00:00
|
|
|
static void
|
|
|
|
SECTION
|
|
|
|
cc32(mp_no *x, mp_no *y, int p) {
|
2001-03-12 00:04:52 +00:00
|
|
|
int i;
|
2001-03-12 07:57:09 +00:00
|
|
|
double a;
|
|
|
|
mp_no mpt1,x2,gor,sum ,mpk={1,{1.0}};
|
2001-03-12 00:04:52 +00:00
|
|
|
for (i=1;i<=p;i++) mpk.d[i]=0;
|
|
|
|
|
2013-02-14 06:09:14 +00:00
|
|
|
__sqr(x,&x2,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
mpk.d[1]=27.0;
|
2001-03-13 02:01:34 +00:00
|
|
|
__mul(&oofac27,&mpk,&gor,p);
|
|
|
|
__cpy(&gor,&sum,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
for (a=26.0;a>2.0;a-=2.0) {
|
|
|
|
mpk.d[1]=a*(a-1.0);
|
2001-03-13 02:01:34 +00:00
|
|
|
__mul(&gor,&mpk,&mpt1,p);
|
|
|
|
__cpy(&mpt1,&gor,p);
|
|
|
|
__mul(&x2,&sum,&mpt1,p);
|
|
|
|
__sub(&gor,&mpt1,&sum,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
}
|
2001-03-13 02:01:34 +00:00
|
|
|
__mul(&x2,&sum,y,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
/* c32() computes both sin(x), cos(x) as Multi precision numbers */
|
|
|
|
/***************************************************************************/
|
2011-10-25 04:56:33 +00:00
|
|
|
void
|
|
|
|
SECTION
|
|
|
|
__c32(mp_no *x, mp_no *y, mp_no *z, int p) {
|
2001-03-12 00:04:52 +00:00
|
|
|
mp_no u,t,t1,t2,c,s;
|
|
|
|
int i;
|
2001-03-13 02:01:34 +00:00
|
|
|
__cpy(x,&u,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
u.e=u.e-1;
|
|
|
|
cc32(&u,&c,p);
|
|
|
|
ss32(&u,&s,p);
|
|
|
|
for (i=0;i<24;i++) {
|
2001-03-13 02:01:34 +00:00
|
|
|
__mul(&c,&s,&t,p);
|
|
|
|
__sub(&s,&t,&t1,p);
|
|
|
|
__add(&t1,&t1,&s,p);
|
2013-02-26 09:45:27 +00:00
|
|
|
__sub(&mptwo,&c,&t1,p);
|
2001-03-13 02:01:34 +00:00
|
|
|
__mul(&t1,&c,&t2,p);
|
|
|
|
__add(&t2,&t2,&c,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
}
|
2013-02-26 09:45:27 +00:00
|
|
|
__sub(&mpone,&c,y,p);
|
2001-03-13 02:01:34 +00:00
|
|
|
__cpy(&s,z,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/*Routine receive double x and two double results of sin(x) and return */
|
|
|
|
/*result which is more accurate */
|
|
|
|
/*Computing sin(x) with multi precision routine c32 */
|
|
|
|
/************************************************************************/
|
2011-10-25 04:56:33 +00:00
|
|
|
double
|
|
|
|
SECTION
|
|
|
|
__sin32(double x, double res, double res1) {
|
2001-03-12 00:04:52 +00:00
|
|
|
int p;
|
|
|
|
mp_no a,b,c;
|
|
|
|
p=32;
|
2001-03-13 02:01:34 +00:00
|
|
|
__dbl_mp(res,&a,p);
|
|
|
|
__dbl_mp(0.5*(res1-res),&b,p);
|
|
|
|
__add(&a,&b,&c,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
if (x>0.8)
|
2001-03-13 02:01:34 +00:00
|
|
|
{ __sub(&hp,&c,&a,p);
|
2001-03-12 07:57:09 +00:00
|
|
|
__c32(&a,&b,&c,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
}
|
2001-03-12 07:57:09 +00:00
|
|
|
else __c32(&c,&a,&b,p); /* b=sin(0.5*(res+res1)) */
|
2001-03-13 02:01:34 +00:00
|
|
|
__dbl_mp(x,&c,p); /* c = x */
|
|
|
|
__sub(&b,&c,&a,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
/* if a>0 return min(res,res1), otherwise return max(res,res1) */
|
|
|
|
if (a.d[0]>0) return (res<res1)?res:res1;
|
|
|
|
else return (res>res1)?res:res1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/*Routine receive double x and two double results of cos(x) and return */
|
|
|
|
/*result which is more accurate */
|
|
|
|
/*Computing cos(x) with multi precision routine c32 */
|
|
|
|
/************************************************************************/
|
2011-10-25 04:56:33 +00:00
|
|
|
double
|
|
|
|
SECTION
|
|
|
|
__cos32(double x, double res, double res1) {
|
2001-03-12 00:04:52 +00:00
|
|
|
int p;
|
|
|
|
mp_no a,b,c;
|
|
|
|
p=32;
|
2001-03-13 02:01:34 +00:00
|
|
|
__dbl_mp(res,&a,p);
|
|
|
|
__dbl_mp(0.5*(res1-res),&b,p);
|
|
|
|
__add(&a,&b,&c,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
if (x>2.4)
|
2001-03-13 02:01:34 +00:00
|
|
|
{ __sub(&pi,&c,&a,p);
|
2001-03-12 07:57:09 +00:00
|
|
|
__c32(&a,&b,&c,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
b.d[0]=-b.d[0];
|
|
|
|
}
|
2001-03-12 07:57:09 +00:00
|
|
|
else if (x>0.8)
|
2001-03-13 02:01:34 +00:00
|
|
|
{ __sub(&hp,&c,&a,p);
|
2011-10-25 04:56:33 +00:00
|
|
|
__c32(&a,&c,&b,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
}
|
2001-03-12 07:57:09 +00:00
|
|
|
else __c32(&c,&b,&a,p); /* b=cos(0.5*(res+res1)) */
|
2001-03-13 02:01:34 +00:00
|
|
|
__dbl_mp(x,&c,p); /* c = x */
|
|
|
|
__sub(&b,&c,&a,p);
|
2011-10-25 04:56:33 +00:00
|
|
|
/* if a>0 return max(res,res1), otherwise return min(res,res1) */
|
2001-03-12 00:04:52 +00:00
|
|
|
if (a.d[0]>0) return (res>res1)?res:res1;
|
|
|
|
else return (res<res1)?res:res1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************/
|
|
|
|
/*Compute sin(x+dx) as Multi Precision number and return result as */
|
|
|
|
/* double */
|
|
|
|
/*******************************************************************/
|
2011-10-25 04:56:33 +00:00
|
|
|
double
|
|
|
|
SECTION
|
|
|
|
__mpsin(double x, double dx) {
|
2001-03-12 00:04:52 +00:00
|
|
|
int p;
|
|
|
|
double y;
|
|
|
|
mp_no a,b,c;
|
|
|
|
p=32;
|
2001-03-13 02:01:34 +00:00
|
|
|
__dbl_mp(x,&a,p);
|
|
|
|
__dbl_mp(dx,&b,p);
|
|
|
|
__add(&a,&b,&c,p);
|
|
|
|
if (x>0.8) { __sub(&hp,&c,&a,p); __c32(&a,&b,&c,p); }
|
2001-03-12 07:57:09 +00:00
|
|
|
else __c32(&c,&a,&b,p); /* b = sin(x+dx) */
|
|
|
|
__mp_dbl(&b,&y,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************/
|
|
|
|
/* Compute cos()of double-length number (x+dx) as Multi Precision */
|
|
|
|
/* number and return result as double */
|
|
|
|
/*******************************************************************/
|
2011-10-25 04:56:33 +00:00
|
|
|
double
|
|
|
|
SECTION
|
|
|
|
__mpcos(double x, double dx) {
|
2001-03-12 00:04:52 +00:00
|
|
|
int p;
|
|
|
|
double y;
|
|
|
|
mp_no a,b,c;
|
|
|
|
p=32;
|
2001-03-13 02:01:34 +00:00
|
|
|
__dbl_mp(x,&a,p);
|
|
|
|
__dbl_mp(dx,&b,p);
|
|
|
|
__add(&a,&b,&c,p);
|
2001-03-12 07:57:09 +00:00
|
|
|
if (x>0.8)
|
2001-03-13 02:01:34 +00:00
|
|
|
{ __sub(&hp,&c,&b,p);
|
2003-01-20 05:25:30 +00:00
|
|
|
__c32(&b,&c,&a,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
}
|
2001-03-12 07:57:09 +00:00
|
|
|
else __c32(&c,&a,&b,p); /* a = cos(x+dx) */
|
|
|
|
__mp_dbl(&a,&y,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************/
|
|
|
|
/* mpranred() performs range reduction of a double number x into */
|
|
|
|
/* multi precision number y, such that y=x-n*pi/2, abs(y)<pi/4, */
|
|
|
|
/* n=0,+-1,+-2,.... */
|
|
|
|
/* Return int which indicates in which quarter of circle x is */
|
|
|
|
/******************************************************************/
|
2011-10-25 04:56:33 +00:00
|
|
|
int
|
|
|
|
SECTION
|
|
|
|
__mpranred(double x, mp_no *y, int p)
|
2001-03-12 00:04:52 +00:00
|
|
|
{
|
|
|
|
number v;
|
|
|
|
double t,xn;
|
|
|
|
int i,k,n;
|
|
|
|
mp_no a,b,c;
|
2001-03-12 07:57:09 +00:00
|
|
|
|
2001-03-12 00:04:52 +00:00
|
|
|
if (ABS(x) < 2.8e14) {
|
|
|
|
t = (x*hpinv.d + toint.d);
|
|
|
|
xn = t - toint.d;
|
|
|
|
v.d = t;
|
|
|
|
n =v.i[LOW_HALF]&3;
|
2001-03-13 02:01:34 +00:00
|
|
|
__dbl_mp(xn,&a,p);
|
|
|
|
__mul(&a,&hp,&b,p);
|
|
|
|
__dbl_mp(x,&c,p);
|
|
|
|
__sub(&c,&b,y,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
else { /* if x is very big more precision required */
|
2001-03-13 02:01:34 +00:00
|
|
|
__dbl_mp(x,&a,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
a.d[0]=1.0;
|
|
|
|
k = a.e-5;
|
|
|
|
if (k < 0) k=0;
|
|
|
|
b.e = -k;
|
|
|
|
b.d[0] = 1.0;
|
|
|
|
for (i=0;i<p;i++) b.d[i+1] = toverp[i+k];
|
2001-03-13 02:01:34 +00:00
|
|
|
__mul(&a,&b,&c,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
t = c.d[c.e];
|
|
|
|
for (i=1;i<=p-c.e;i++) c.d[i]=c.d[i+c.e];
|
|
|
|
for (i=p+1-c.e;i<=p;i++) c.d[i]=0;
|
|
|
|
c.e=0;
|
2013-03-21 07:37:44 +00:00
|
|
|
if (c.d[1] >= HALFRAD)
|
2001-03-12 00:04:52 +00:00
|
|
|
{ t +=1.0;
|
2013-02-26 09:45:27 +00:00
|
|
|
__sub(&c,&mpone,&b,p);
|
2001-03-13 02:01:34 +00:00
|
|
|
__mul(&b,&hp,y,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
}
|
2001-03-13 02:01:34 +00:00
|
|
|
else __mul(&c,&hp,y,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
n = (int) t;
|
|
|
|
if (x < 0) { y->d[0] = - y->d[0]; n = -n; }
|
|
|
|
return (n&3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************/
|
|
|
|
/* Multi-Precision sin() function subroutine, for p=32. It is */
|
|
|
|
/* based on the routines mpranred() and c32(). */
|
|
|
|
/*******************************************************************/
|
2011-10-25 04:56:33 +00:00
|
|
|
double
|
|
|
|
SECTION
|
|
|
|
__mpsin1(double x)
|
2001-03-12 00:04:52 +00:00
|
|
|
{
|
|
|
|
int p;
|
|
|
|
int n;
|
|
|
|
mp_no u,s,c;
|
|
|
|
double y;
|
|
|
|
p=32;
|
2001-03-13 02:01:34 +00:00
|
|
|
n=__mpranred(x,&u,p); /* n is 0, 1, 2 or 3 */
|
2001-03-12 07:57:09 +00:00
|
|
|
__c32(&u,&c,&s,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
switch (n) { /* in which quarter of unit circle y is*/
|
|
|
|
case 0:
|
2001-03-12 07:57:09 +00:00
|
|
|
__mp_dbl(&s,&y,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
return y;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2001-03-12 07:57:09 +00:00
|
|
|
__mp_dbl(&s,&y,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
return -y;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2001-03-12 07:57:09 +00:00
|
|
|
__mp_dbl(&c,&y,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
return y;
|
|
|
|
break;
|
2001-03-12 07:57:09 +00:00
|
|
|
|
2001-03-12 00:04:52 +00:00
|
|
|
case 3:
|
2001-03-12 07:57:09 +00:00
|
|
|
__mp_dbl(&c,&y,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
return -y;
|
|
|
|
break;
|
2001-03-12 07:57:09 +00:00
|
|
|
|
2001-03-12 00:04:52 +00:00
|
|
|
}
|
|
|
|
return 0; /* unreachable, to make the compiler happy */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************/
|
|
|
|
/* Multi-Precision cos() function subroutine, for p=32. It is */
|
|
|
|
/* based on the routines mpranred() and c32(). */
|
|
|
|
/*****************************************************************/
|
|
|
|
|
2011-10-25 04:56:33 +00:00
|
|
|
double
|
|
|
|
SECTION
|
|
|
|
__mpcos1(double x)
|
2001-03-12 00:04:52 +00:00
|
|
|
{
|
|
|
|
int p;
|
|
|
|
int n;
|
|
|
|
mp_no u,s,c;
|
|
|
|
double y;
|
2001-03-12 07:57:09 +00:00
|
|
|
|
2001-03-12 00:04:52 +00:00
|
|
|
p=32;
|
2001-03-13 02:01:34 +00:00
|
|
|
n=__mpranred(x,&u,p); /* n is 0, 1, 2 or 3 */
|
2001-03-12 07:57:09 +00:00
|
|
|
__c32(&u,&c,&s,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
switch (n) { /* in what quarter of unit circle y is*/
|
2001-03-12 07:57:09 +00:00
|
|
|
|
2001-03-12 00:04:52 +00:00
|
|
|
case 0:
|
2001-03-12 07:57:09 +00:00
|
|
|
__mp_dbl(&c,&y,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
return y;
|
|
|
|
break;
|
2001-03-12 07:57:09 +00:00
|
|
|
|
2001-03-12 00:04:52 +00:00
|
|
|
case 2:
|
2001-03-12 07:57:09 +00:00
|
|
|
__mp_dbl(&c,&y,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
return -y;
|
|
|
|
break;
|
2001-03-12 07:57:09 +00:00
|
|
|
|
2001-03-12 00:04:52 +00:00
|
|
|
case 1:
|
2001-03-12 07:57:09 +00:00
|
|
|
__mp_dbl(&s,&y,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
return -y;
|
|
|
|
break;
|
2001-03-12 07:57:09 +00:00
|
|
|
|
2001-03-12 00:04:52 +00:00
|
|
|
case 3:
|
2001-03-12 07:57:09 +00:00
|
|
|
__mp_dbl(&s,&y,p);
|
2001-03-12 00:04:52 +00:00
|
|
|
return y;
|
|
|
|
break;
|
2001-03-12 07:57:09 +00:00
|
|
|
|
2001-03-12 00:04:52 +00:00
|
|
|
}
|
|
|
|
return 0; /* unreachable, to make the compiler happy */
|
|
|
|
}
|
|
|
|
/******************************************************************/
|