k_rem_pio2.c revision 2116
12116Sjkh/* @(#)k_rem_pio2.c 5.1 93/09/24 */
22116Sjkh/*
32116Sjkh * ====================================================
42116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
52116Sjkh *
62116Sjkh * Developed at SunPro, a Sun Microsystems, Inc. business.
72116Sjkh * Permission to use, copy, modify, and distribute this
82116Sjkh * software is freely granted, provided that this notice
92116Sjkh * is preserved.
102116Sjkh * ====================================================
112116Sjkh */
122116Sjkh
132116Sjkh#ifndef lint
142116Sjkhstatic char rcsid[] = "$Id: k_rem_pio2.c,v 1.5 1994/08/18 23:06:11 jtc Exp $";
152116Sjkh#endif
162116Sjkh
172116Sjkh/*
182116Sjkh * __kernel_rem_pio2(x,y,e0,nx,prec,ipio2)
192116Sjkh * double x[],y[]; int e0,nx,prec; int ipio2[];
202116Sjkh *
212116Sjkh * __kernel_rem_pio2 return the last three digits of N with
222116Sjkh *		y = x - N*pi/2
232116Sjkh * so that |y| < pi/2.
242116Sjkh *
252116Sjkh * The method is to compute the integer (mod 8) and fraction parts of
262116Sjkh * (2/pi)*x without doing the full multiplication. In general we
272116Sjkh * skip the part of the product that are known to be a huge integer (
282116Sjkh * more accurately, = 0 mod 8 ). Thus the number of operations are
292116Sjkh * independent of the exponent of the input.
302116Sjkh *
312116Sjkh * (2/pi) is represented by an array of 24-bit integers in ipio2[].
322116Sjkh *
332116Sjkh * Input parameters:
342116Sjkh * 	x[]	The input value (must be positive) is broken into nx
352116Sjkh *		pieces of 24-bit integers in double precision format.
362116Sjkh *		x[i] will be the i-th 24 bit of x. The scaled exponent
372116Sjkh *		of x[0] is given in input parameter e0 (i.e., x[0]*2^e0
382116Sjkh *		match x's up to 24 bits.
392116Sjkh *
402116Sjkh *		Example of breaking a double positive z into x[0]+x[1]+x[2]:
412116Sjkh *			e0 = ilogb(z)-23
422116Sjkh *			z  = scalbn(z,-e0)
432116Sjkh *		for i = 0,1,2
442116Sjkh *			x[i] = floor(z)
452116Sjkh *			z    = (z-x[i])*2**24
462116Sjkh *
472116Sjkh *
482116Sjkh *	y[]	ouput result in an array of double precision numbers.
492116Sjkh *		The dimension of y[] is:
502116Sjkh *			24-bit  precision	1
512116Sjkh *			53-bit  precision	2
522116Sjkh *			64-bit  precision	2
532116Sjkh *			113-bit precision	3
542116Sjkh *		The actual value is the sum of them. Thus for 113-bit
552116Sjkh *		precison, one may have to do something like:
562116Sjkh *
572116Sjkh *		long double t,w,r_head, r_tail;
582116Sjkh *		t = (long double)y[2] + (long double)y[1];
592116Sjkh *		w = (long double)y[0];
602116Sjkh *		r_head = t+w;
612116Sjkh *		r_tail = w - (r_head - t);
622116Sjkh *
632116Sjkh *	e0	The exponent of x[0]
642116Sjkh *
652116Sjkh *	nx	dimension of x[]
662116Sjkh *
672116Sjkh *  	prec	an integer indicating the precision:
682116Sjkh *			0	24  bits (single)
692116Sjkh *			1	53  bits (double)
702116Sjkh *			2	64  bits (extended)
712116Sjkh *			3	113 bits (quad)
722116Sjkh *
732116Sjkh *	ipio2[]
742116Sjkh *		integer array, contains the (24*i)-th to (24*i+23)-th
752116Sjkh *		bit of 2/pi after binary point. The corresponding
762116Sjkh *		floating value is
772116Sjkh *
782116Sjkh *			ipio2[i] * 2^(-24(i+1)).
792116Sjkh *
802116Sjkh * External function:
812116Sjkh *	double scalbn(), floor();
822116Sjkh *
832116Sjkh *
842116Sjkh * Here is the description of some local variables:
852116Sjkh *
862116Sjkh * 	jk	jk+1 is the initial number of terms of ipio2[] needed
872116Sjkh *		in the computation. The recommended value is 2,3,4,
882116Sjkh *		6 for single, double, extended,and quad.
892116Sjkh *
902116Sjkh * 	jz	local integer variable indicating the number of
912116Sjkh *		terms of ipio2[] used.
922116Sjkh *
932116Sjkh *	jx	nx - 1
942116Sjkh *
952116Sjkh *	jv	index for pointing to the suitable ipio2[] for the
962116Sjkh *		computation. In general, we want
972116Sjkh *			( 2^e0*x[0] * ipio2[jv-1]*2^(-24jv) )/8
982116Sjkh *		is an integer. Thus
992116Sjkh *			e0-3-24*jv >= 0 or (e0-3)/24 >= jv
1002116Sjkh *		Hence jv = max(0,(e0-3)/24).
1012116Sjkh *
1022116Sjkh *	jp	jp+1 is the number of terms in PIo2[] needed, jp = jk.
1032116Sjkh *
1042116Sjkh * 	q[]	double array with integral value, representing the
1052116Sjkh *		24-bits chunk of the product of x and 2/pi.
1062116Sjkh *
1072116Sjkh *	q0	the corresponding exponent of q[0]. Note that the
1082116Sjkh *		exponent for q[i] would be q0-24*i.
1092116Sjkh *
1102116Sjkh *	PIo2[]	double precision array, obtained by cutting pi/2
1112116Sjkh *		into 24 bits chunks.
1122116Sjkh *
1132116Sjkh *	f[]	ipio2[] in floating point
1142116Sjkh *
1152116Sjkh *	iq[]	integer array by breaking up q[] in 24-bits chunk.
1162116Sjkh *
1172116Sjkh *	fq[]	final product of x*(2/pi) in fq[0],..,fq[jk]
1182116Sjkh *
1192116Sjkh *	ih	integer. If >0 it indicates q[] is >= 0.5, hence
1202116Sjkh *		it also indicates the *sign* of the result.
1212116Sjkh *
1222116Sjkh */
1232116Sjkh
1242116Sjkh
1252116Sjkh/*
1262116Sjkh * Constants:
1272116Sjkh * The hexadecimal values are the intended ones for the following
1282116Sjkh * constants. The decimal values may be used, provided that the
1292116Sjkh * compiler will convert from decimal to binary accurately enough
1302116Sjkh * to produce the hexadecimal values shown.
1312116Sjkh */
1322116Sjkh
1332116Sjkh#include "math.h"
1342116Sjkh#include "math_private.h"
1352116Sjkh
1362116Sjkh#ifdef __STDC__
1372116Sjkhstatic const int init_jk[] = {2,3,4,6}; /* initial value for jk */
1382116Sjkh#else
1392116Sjkhstatic int init_jk[] = {2,3,4,6};
1402116Sjkh#endif
1412116Sjkh
1422116Sjkh#ifdef __STDC__
1432116Sjkhstatic const double PIo2[] = {
1442116Sjkh#else
1452116Sjkhstatic double PIo2[] = {
1462116Sjkh#endif
1472116Sjkh  1.57079625129699707031e+00, /* 0x3FF921FB, 0x40000000 */
1482116Sjkh  7.54978941586159635335e-08, /* 0x3E74442D, 0x00000000 */
1492116Sjkh  5.39030252995776476554e-15, /* 0x3CF84698, 0x80000000 */
1502116Sjkh  3.28200341580791294123e-22, /* 0x3B78CC51, 0x60000000 */
1512116Sjkh  1.27065575308067607349e-29, /* 0x39F01B83, 0x80000000 */
1522116Sjkh  1.22933308981111328932e-36, /* 0x387A2520, 0x40000000 */
1532116Sjkh  2.73370053816464559624e-44, /* 0x36E38222, 0x80000000 */
1542116Sjkh  2.16741683877804819444e-51, /* 0x3569F31D, 0x00000000 */
1552116Sjkh};
1562116Sjkh
1572116Sjkh#ifdef __STDC__
1582116Sjkhstatic const double
1592116Sjkh#else
1602116Sjkhstatic double
1612116Sjkh#endif
1622116Sjkhzero   = 0.0,
1632116Sjkhone    = 1.0,
1642116Sjkhtwo24   =  1.67772160000000000000e+07, /* 0x41700000, 0x00000000 */
1652116Sjkhtwon24  =  5.96046447753906250000e-08; /* 0x3E700000, 0x00000000 */
1662116Sjkh
1672116Sjkh#ifdef __STDC__
1682116Sjkh	int __kernel_rem_pio2(double *x, double *y, int e0, int nx, int prec, const int32_t *ipio2)
1692116Sjkh#else
1702116Sjkh	int __kernel_rem_pio2(x,y,e0,nx,prec,ipio2)
1712116Sjkh	double x[], y[]; int e0,nx,prec; int32_t ipio2[];
1722116Sjkh#endif
1732116Sjkh{
1742116Sjkh	int32_t jz,jx,jv,jp,jk,carry,n,iq[20],i,j,k,m,q0,ih;
1752116Sjkh	double z,fw,f[20],fq[20],q[20];
1762116Sjkh
1772116Sjkh    /* initialize jk*/
1782116Sjkh	jk = init_jk[prec];
1792116Sjkh	jp = jk;
1802116Sjkh
1812116Sjkh    /* determine jx,jv,q0, note that 3>q0 */
1822116Sjkh	jx =  nx-1;
1832116Sjkh	jv = (e0-3)/24; if(jv<0) jv=0;
1842116Sjkh	q0 =  e0-24*(jv+1);
1852116Sjkh
1862116Sjkh    /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */
1872116Sjkh	j = jv-jx; m = jx+jk;
1882116Sjkh	for(i=0;i<=m;i++,j++) f[i] = (j<0)? zero : (double) ipio2[j];
1892116Sjkh
1902116Sjkh    /* compute q[0],q[1],...q[jk] */
1912116Sjkh	for (i=0;i<=jk;i++) {
1922116Sjkh	    for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j]; q[i] = fw;
1932116Sjkh	}
1942116Sjkh
1952116Sjkh	jz = jk;
1962116Sjkhrecompute:
1972116Sjkh    /* distill q[] into iq[] reversingly */
1982116Sjkh	for(i=0,j=jz,z=q[jz];j>0;i++,j--) {
1992116Sjkh	    fw    =  (double)((int32_t)(twon24* z));
2002116Sjkh	    iq[i] =  (int32_t)(z-two24*fw);
2012116Sjkh	    z     =  q[j-1]+fw;
2022116Sjkh	}
2032116Sjkh
2042116Sjkh    /* compute n */
2052116Sjkh	z  = scalbn(z,q0);		/* actual value of z */
2062116Sjkh	z -= 8.0*floor(z*0.125);		/* trim off integer >= 8 */
2072116Sjkh	n  = (int32_t) z;
2082116Sjkh	z -= (double)n;
2092116Sjkh	ih = 0;
2102116Sjkh	if(q0>0) {	/* need iq[jz-1] to determine n */
2112116Sjkh	    i  = (iq[jz-1]>>(24-q0)); n += i;
2122116Sjkh	    iq[jz-1] -= i<<(24-q0);
2132116Sjkh	    ih = iq[jz-1]>>(23-q0);
2142116Sjkh	}
2152116Sjkh	else if(q0==0) ih = iq[jz-1]>>23;
2162116Sjkh	else if(z>=0.5) ih=2;
2172116Sjkh
2182116Sjkh	if(ih>0) {	/* q > 0.5 */
2192116Sjkh	    n += 1; carry = 0;
2202116Sjkh	    for(i=0;i<jz ;i++) {	/* compute 1-q */
2212116Sjkh		j = iq[i];
2222116Sjkh		if(carry==0) {
2232116Sjkh		    if(j!=0) {
2242116Sjkh			carry = 1; iq[i] = 0x1000000- j;
2252116Sjkh		    }
2262116Sjkh		} else  iq[i] = 0xffffff - j;
2272116Sjkh	    }
2282116Sjkh	    if(q0>0) {		/* rare case: chance is 1 in 12 */
2292116Sjkh	        switch(q0) {
2302116Sjkh	        case 1:
2312116Sjkh	    	   iq[jz-1] &= 0x7fffff; break;
2322116Sjkh	    	case 2:
2332116Sjkh	    	   iq[jz-1] &= 0x3fffff; break;
2342116Sjkh	        }
2352116Sjkh	    }
2362116Sjkh	    if(ih==2) {
2372116Sjkh		z = one - z;
2382116Sjkh		if(carry!=0) z -= scalbn(one,q0);
2392116Sjkh	    }
2402116Sjkh	}
2412116Sjkh
2422116Sjkh    /* check if recomputation is needed */
2432116Sjkh	if(z==zero) {
2442116Sjkh	    j = 0;
2452116Sjkh	    for (i=jz-1;i>=jk;i--) j |= iq[i];
2462116Sjkh	    if(j==0) { /* need recomputation */
2472116Sjkh		for(k=1;iq[jk-k]==0;k++);   /* k = no. of terms needed */
2482116Sjkh
2492116Sjkh		for(i=jz+1;i<=jz+k;i++) {   /* add q[jz+1] to q[jz+k] */
2502116Sjkh		    f[jx+i] = (double) ipio2[jv+i];
2512116Sjkh		    for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j];
2522116Sjkh		    q[i] = fw;
2532116Sjkh		}
2542116Sjkh		jz += k;
2552116Sjkh		goto recompute;
2562116Sjkh	    }
2572116Sjkh	}
2582116Sjkh
2592116Sjkh    /* chop off zero terms */
2602116Sjkh	if(z==0.0) {
2612116Sjkh	    jz -= 1; q0 -= 24;
2622116Sjkh	    while(iq[jz]==0) { jz--; q0-=24;}
2632116Sjkh	} else { /* break z into 24-bit if necessary */
2642116Sjkh	    z = scalbn(z,-q0);
2652116Sjkh	    if(z>=two24) {
2662116Sjkh		fw = (double)((int32_t)(twon24*z));
2672116Sjkh		iq[jz] = (int32_t)(z-two24*fw);
2682116Sjkh		jz += 1; q0 += 24;
2692116Sjkh		iq[jz] = (int32_t) fw;
2702116Sjkh	    } else iq[jz] = (int32_t) z ;
2712116Sjkh	}
2722116Sjkh
2732116Sjkh    /* convert integer "bit" chunk to floating-point value */
2742116Sjkh	fw = scalbn(one,q0);
2752116Sjkh	for(i=jz;i>=0;i--) {
2762116Sjkh	    q[i] = fw*(double)iq[i]; fw*=twon24;
2772116Sjkh	}
2782116Sjkh
2792116Sjkh    /* compute PIo2[0,...,jp]*q[jz,...,0] */
2802116Sjkh	for(i=jz;i>=0;i--) {
2812116Sjkh	    for(fw=0.0,k=0;k<=jp&&k<=jz-i;k++) fw += PIo2[k]*q[i+k];
2822116Sjkh	    fq[jz-i] = fw;
2832116Sjkh	}
2842116Sjkh
2852116Sjkh    /* compress fq[] into y[] */
2862116Sjkh	switch(prec) {
2872116Sjkh	    case 0:
2882116Sjkh		fw = 0.0;
2892116Sjkh		for (i=jz;i>=0;i--) fw += fq[i];
2902116Sjkh		y[0] = (ih==0)? fw: -fw;
2912116Sjkh		break;
2922116Sjkh	    case 1:
2932116Sjkh	    case 2:
2942116Sjkh		fw = 0.0;
2952116Sjkh		for (i=jz;i>=0;i--) fw += fq[i];
2962116Sjkh		y[0] = (ih==0)? fw: -fw;
2972116Sjkh		fw = fq[0]-fw;
2982116Sjkh		for (i=1;i<=jz;i++) fw += fq[i];
2992116Sjkh		y[1] = (ih==0)? fw: -fw;
3002116Sjkh		break;
3012116Sjkh	    case 3:	/* painful */
3022116Sjkh		for (i=jz;i>0;i--) {
3032116Sjkh		    fw      = fq[i-1]+fq[i];
3042116Sjkh		    fq[i]  += fq[i-1]-fw;
3052116Sjkh		    fq[i-1] = fw;
3062116Sjkh		}
3072116Sjkh		for (i=jz;i>1;i--) {
3082116Sjkh		    fw      = fq[i-1]+fq[i];
3092116Sjkh		    fq[i]  += fq[i-1]-fw;
3102116Sjkh		    fq[i-1] = fw;
3112116Sjkh		}
3122116Sjkh		for (fw=0.0,i=jz;i>=2;i--) fw += fq[i];
3132116Sjkh		if(ih==0) {
3142116Sjkh		    y[0] =  fq[0]; y[1] =  fq[1]; y[2] =  fw;
3152116Sjkh		} else {
3162116Sjkh		    y[0] = -fq[0]; y[1] = -fq[1]; y[2] = -fw;
3172116Sjkh		}
3182116Sjkh	}
3192116Sjkh	return n&7;
3202116Sjkh}
321