k_rem_pio2.c revision 175507
1141296Sdas
2141296Sdas/* @(#)k_rem_pio2.c 1.3 95/01/18 */
32116Sjkh/*
42116Sjkh * ====================================================
52116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
62116Sjkh *
7141296Sdas * Developed at SunSoft, a Sun Microsystems, Inc. business.
82116Sjkh * Permission to use, copy, modify, and distribute this
9141296Sdas * software is freely granted, provided that this notice
102116Sjkh * is preserved.
112116Sjkh * ====================================================
122116Sjkh */
132116Sjkh
14175499Sbde#include <sys/cdefs.h>
15175499Sbde__FBSDID("$FreeBSD: head/lib/msun/src/k_rem_pio2.c 175507 2008-01-20 04:09:44Z bde $");
162116Sjkh
172116Sjkh/*
182116Sjkh * __kernel_rem_pio2(x,y,e0,nx,prec,ipio2)
192116Sjkh * double x[],y[]; int e0,nx,prec; int ipio2[];
20141296Sdas *
21141296Sdas * __kernel_rem_pio2 return the last three digits of N with
222116Sjkh *		y = x - N*pi/2
232116Sjkh * so that |y| < pi/2.
242116Sjkh *
25141296Sdas * 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:
34141296Sdas * 	x[]	The input value (must be positive) is broken into nx
352116Sjkh *		pieces of 24-bit integers in double precision format.
36141296Sdas *		x[i] will be the i-th 24 bit of x. The scaled exponent
37141296Sdas *		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[]
74141296Sdas *		integer array, contains the (24*i)-th to (24*i+23)-th
75141296Sdas *		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 *
90141296Sdas * 	jz	local integer variable indicating the number of
91141296Sdas *		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
111141296Sdas *		into 24 bits chunks.
1122116Sjkh *
113141296Sdas *	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:
127141296Sdas * The hexadecimal values are the intended ones for the following
128141296Sdas * constants. The decimal values may be used, provided that the
129141296Sdas * compiler will convert from decimal to binary accurately enough
1302116Sjkh * to produce the hexadecimal values shown.
1312116Sjkh */
1322116Sjkh
133175499Sbde#include <float.h>
134175499Sbde
1352116Sjkh#include "math.h"
1362116Sjkh#include "math_private.h"
1372116Sjkh
1382116Sjkhstatic const int init_jk[] = {2,3,4,6}; /* initial value for jk */
1392116Sjkh
1402116Sjkhstatic const double PIo2[] = {
1412116Sjkh  1.57079625129699707031e+00, /* 0x3FF921FB, 0x40000000 */
1422116Sjkh  7.54978941586159635335e-08, /* 0x3E74442D, 0x00000000 */
1432116Sjkh  5.39030252995776476554e-15, /* 0x3CF84698, 0x80000000 */
1442116Sjkh  3.28200341580791294123e-22, /* 0x3B78CC51, 0x60000000 */
1452116Sjkh  1.27065575308067607349e-29, /* 0x39F01B83, 0x80000000 */
1462116Sjkh  1.22933308981111328932e-36, /* 0x387A2520, 0x40000000 */
1472116Sjkh  2.73370053816464559624e-44, /* 0x36E38222, 0x80000000 */
1482116Sjkh  2.16741683877804819444e-51, /* 0x3569F31D, 0x00000000 */
1492116Sjkh};
1502116Sjkh
151141296Sdasstatic const double
1522116Sjkhzero   = 0.0,
1532116Sjkhone    = 1.0,
1542116Sjkhtwo24   =  1.67772160000000000000e+07, /* 0x41700000, 0x00000000 */
1552116Sjkhtwon24  =  5.96046447753906250000e-08; /* 0x3E700000, 0x00000000 */
1562116Sjkh
1578870Srgrimes	int __kernel_rem_pio2(double *x, double *y, int e0, int nx, int prec, const int32_t *ipio2)
1582116Sjkh{
1592116Sjkh	int32_t jz,jx,jv,jp,jk,carry,n,iq[20],i,j,k,m,q0,ih;
1602116Sjkh	double z,fw,f[20],fq[20],q[20];
1612116Sjkh
1622116Sjkh    /* initialize jk*/
1632116Sjkh	jk = init_jk[prec];
1642116Sjkh	jp = jk;
1652116Sjkh
1662116Sjkh    /* determine jx,jv,q0, note that 3>q0 */
1672116Sjkh	jx =  nx-1;
1682116Sjkh	jv = (e0-3)/24; if(jv<0) jv=0;
1692116Sjkh	q0 =  e0-24*(jv+1);
1702116Sjkh
1712116Sjkh    /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */
1722116Sjkh	j = jv-jx; m = jx+jk;
1732116Sjkh	for(i=0;i<=m;i++,j++) f[i] = (j<0)? zero : (double) ipio2[j];
1742116Sjkh
1752116Sjkh    /* compute q[0],q[1],...q[jk] */
1762116Sjkh	for (i=0;i<=jk;i++) {
1772116Sjkh	    for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j]; q[i] = fw;
1782116Sjkh	}
1792116Sjkh
1802116Sjkh	jz = jk;
1812116Sjkhrecompute:
1822116Sjkh    /* distill q[] into iq[] reversingly */
1832116Sjkh	for(i=0,j=jz,z=q[jz];j>0;i++,j--) {
1842116Sjkh	    fw    =  (double)((int32_t)(twon24* z));
1852116Sjkh	    iq[i] =  (int32_t)(z-two24*fw);
1862116Sjkh	    z     =  q[j-1]+fw;
1872116Sjkh	}
1882116Sjkh
1892116Sjkh    /* compute n */
1902116Sjkh	z  = scalbn(z,q0);		/* actual value of z */
1912116Sjkh	z -= 8.0*floor(z*0.125);		/* trim off integer >= 8 */
1922116Sjkh	n  = (int32_t) z;
1932116Sjkh	z -= (double)n;
1942116Sjkh	ih = 0;
1952116Sjkh	if(q0>0) {	/* need iq[jz-1] to determine n */
1962116Sjkh	    i  = (iq[jz-1]>>(24-q0)); n += i;
1972116Sjkh	    iq[jz-1] -= i<<(24-q0);
1982116Sjkh	    ih = iq[jz-1]>>(23-q0);
199141296Sdas	}
2002116Sjkh	else if(q0==0) ih = iq[jz-1]>>23;
2012116Sjkh	else if(z>=0.5) ih=2;
2022116Sjkh
2032116Sjkh	if(ih>0) {	/* q > 0.5 */
2042116Sjkh	    n += 1; carry = 0;
2052116Sjkh	    for(i=0;i<jz ;i++) {	/* compute 1-q */
2062116Sjkh		j = iq[i];
2072116Sjkh		if(carry==0) {
2082116Sjkh		    if(j!=0) {
2092116Sjkh			carry = 1; iq[i] = 0x1000000- j;
2102116Sjkh		    }
2112116Sjkh		} else  iq[i] = 0xffffff - j;
2122116Sjkh	    }
2132116Sjkh	    if(q0>0) {		/* rare case: chance is 1 in 12 */
2142116Sjkh	        switch(q0) {
2152116Sjkh	        case 1:
2162116Sjkh	    	   iq[jz-1] &= 0x7fffff; break;
2172116Sjkh	    	case 2:
2182116Sjkh	    	   iq[jz-1] &= 0x3fffff; break;
2192116Sjkh	        }
2202116Sjkh	    }
2212116Sjkh	    if(ih==2) {
2222116Sjkh		z = one - z;
2232116Sjkh		if(carry!=0) z -= scalbn(one,q0);
2242116Sjkh	    }
2252116Sjkh	}
2262116Sjkh
2272116Sjkh    /* check if recomputation is needed */
2282116Sjkh	if(z==zero) {
2292116Sjkh	    j = 0;
2302116Sjkh	    for (i=jz-1;i>=jk;i--) j |= iq[i];
2312116Sjkh	    if(j==0) { /* need recomputation */
2322116Sjkh		for(k=1;iq[jk-k]==0;k++);   /* k = no. of terms needed */
2332116Sjkh
2342116Sjkh		for(i=jz+1;i<=jz+k;i++) {   /* add q[jz+1] to q[jz+k] */
2352116Sjkh		    f[jx+i] = (double) ipio2[jv+i];
2362116Sjkh		    for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j];
2372116Sjkh		    q[i] = fw;
2382116Sjkh		}
2392116Sjkh		jz += k;
2402116Sjkh		goto recompute;
2412116Sjkh	    }
2422116Sjkh	}
2432116Sjkh
2442116Sjkh    /* chop off zero terms */
2452116Sjkh	if(z==0.0) {
2462116Sjkh	    jz -= 1; q0 -= 24;
2472116Sjkh	    while(iq[jz]==0) { jz--; q0-=24;}
2482116Sjkh	} else { /* break z into 24-bit if necessary */
2492116Sjkh	    z = scalbn(z,-q0);
250141296Sdas	    if(z>=two24) {
2512116Sjkh		fw = (double)((int32_t)(twon24*z));
2522116Sjkh		iq[jz] = (int32_t)(z-two24*fw);
2532116Sjkh		jz += 1; q0 += 24;
2542116Sjkh		iq[jz] = (int32_t) fw;
2552116Sjkh	    } else iq[jz] = (int32_t) z ;
2562116Sjkh	}
2572116Sjkh
2582116Sjkh    /* convert integer "bit" chunk to floating-point value */
2592116Sjkh	fw = scalbn(one,q0);
2602116Sjkh	for(i=jz;i>=0;i--) {
2612116Sjkh	    q[i] = fw*(double)iq[i]; fw*=twon24;
2622116Sjkh	}
2632116Sjkh
2642116Sjkh    /* compute PIo2[0,...,jp]*q[jz,...,0] */
2652116Sjkh	for(i=jz;i>=0;i--) {
2662116Sjkh	    for(fw=0.0,k=0;k<=jp&&k<=jz-i;k++) fw += PIo2[k]*q[i+k];
2672116Sjkh	    fq[jz-i] = fw;
2682116Sjkh	}
2692116Sjkh
2702116Sjkh    /* compress fq[] into y[] */
2712116Sjkh	switch(prec) {
2722116Sjkh	    case 0:
2732116Sjkh		fw = 0.0;
2742116Sjkh		for (i=jz;i>=0;i--) fw += fq[i];
275141296Sdas		y[0] = (ih==0)? fw: -fw;
2762116Sjkh		break;
2772116Sjkh	    case 1:
2782116Sjkh	    case 2:
2792116Sjkh		fw = 0.0;
280141296Sdas		for (i=jz;i>=0;i--) fw += fq[i];
281175507Sbde		STRICT_ASSIGN(double,fw,fw);
282141296Sdas		y[0] = (ih==0)? fw: -fw;
2832116Sjkh		fw = fq[0]-fw;
2842116Sjkh		for (i=1;i<=jz;i++) fw += fq[i];
285141296Sdas		y[1] = (ih==0)? fw: -fw;
2862116Sjkh		break;
2872116Sjkh	    case 3:	/* painful */
2882116Sjkh		for (i=jz;i>0;i--) {
289141296Sdas		    fw      = fq[i-1]+fq[i];
2902116Sjkh		    fq[i]  += fq[i-1]-fw;
2912116Sjkh		    fq[i-1] = fw;
2922116Sjkh		}
2932116Sjkh		for (i=jz;i>1;i--) {
294141296Sdas		    fw      = fq[i-1]+fq[i];
2952116Sjkh		    fq[i]  += fq[i-1]-fw;
2962116Sjkh		    fq[i-1] = fw;
2972116Sjkh		}
298141296Sdas		for (fw=0.0,i=jz;i>=2;i--) fw += fq[i];
2992116Sjkh		if(ih==0) {
3002116Sjkh		    y[0] =  fq[0]; y[1] =  fq[1]; y[2] =  fw;
3012116Sjkh		} else {
3022116Sjkh		    y[0] = -fq[0]; y[1] = -fq[1]; y[2] = -fw;
3032116Sjkh		}
3042116Sjkh	}
3052116Sjkh	return n&7;
3062116Sjkh}
307