s_cbrt.c revision 1.1
11.1Sjtc
21.1Sjtc/* @(#)s_cbrt.c 5.1 93/09/24 */
31.1Sjtc/*
41.1Sjtc * ====================================================
51.1Sjtc * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
61.1Sjtc *
71.1Sjtc * Developed at SunPro, a Sun Microsystems, Inc. business.
81.1Sjtc * Permission to use, copy, modify, and distribute this
91.1Sjtc * software is freely granted, provided that this notice
101.1Sjtc * is preserved.
111.1Sjtc * ====================================================
121.1Sjtc *
131.1Sjtc */
141.1Sjtc
151.1Sjtc#include "fdlibm.h"
161.1Sjtc
171.1Sjtc/* cbrt(x)
181.1Sjtc * Return cube root of x
191.1Sjtc */
201.1Sjtc#ifdef __STDC__
211.1Sjtcstatic const unsigned
221.1Sjtc#else
231.1Sjtcstatic unsigned
241.1Sjtc#endif
251.1Sjtc	B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
261.1Sjtc	B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
271.1Sjtc
281.1Sjtc#ifdef __STDC__
291.1Sjtcstatic const double
301.1Sjtc#else
311.1Sjtcstatic double
321.1Sjtc#endif
331.1SjtcC =  5.42857142857142815906e-01, /* 19/35     = 0x3FE15F15, 0xF15F15F1 */
341.1SjtcD = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */
351.1SjtcE =  1.41428571428571436819e+00, /* 99/70     = 0x3FF6A0EA, 0x0EA0EA0F */
361.1SjtcF =  1.60714285714285720630e+00, /* 45/28     = 0x3FF9B6DB, 0x6DB6DB6E */
371.1SjtcG =  3.57142857142857150787e-01; /* 5/14      = 0x3FD6DB6D, 0xB6DB6DB7 */
381.1Sjtc
391.1Sjtc#ifdef __STDC__
401.1Sjtc	double cbrt(double x)
411.1Sjtc#else
421.1Sjtc	double cbrt(x)
431.1Sjtc	double x;
441.1Sjtc#endif
451.1Sjtc{
461.1Sjtc	double	one	= 1.0;
471.1Sjtc	int	n0,hx;
481.1Sjtc	double r,s,t=0.0,w;
491.1Sjtc	unsigned *pt = (unsigned *) &t, sign;
501.1Sjtc
511.1Sjtc
521.1Sjtc	n0 = ((*(int*)&one)>>29)^1;	/* index of high word */
531.1Sjtc	hx = *( n0 + (int*)&x);		/* high word of x */
541.1Sjtc	sign=hx&0x80000000; 		/* sign= sign(x) */
551.1Sjtc	hx  ^=sign;
561.1Sjtc	if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */
571.1Sjtc	if((hx|*(1-n0+(int*)&x))==0)
581.1Sjtc	    return(x);		/* cbrt(0) is itself */
591.1Sjtc
601.1Sjtc	*(n0+(int*)&x) = hx;	/* x <- |x| */
611.1Sjtc    /* rough cbrt to 5 bits */
621.1Sjtc	if(hx<0x00100000) 		/* subnormal number */
631.1Sjtc	  {pt[n0]=0x43500000; 		/* set t= 2**54 */
641.1Sjtc	   t*=x; pt[n0]=pt[n0]/3+B2;
651.1Sjtc	  }
661.1Sjtc	else
671.1Sjtc	  pt[n0]=hx/3+B1;
681.1Sjtc
691.1Sjtc
701.1Sjtc    /* new cbrt to 23 bits, may be implemented in single precision */
711.1Sjtc	r=t*t/x;
721.1Sjtc	s=C+r*t;
731.1Sjtc	t*=G+F/(s+E+D/s);
741.1Sjtc
751.1Sjtc    /* chopped to 20 bits and make it larger than cbrt(x) */
761.1Sjtc	pt[1-n0]=0; pt[n0]+=0x00000001;
771.1Sjtc
781.1Sjtc
791.1Sjtc    /* one step newton iteration to 53 bits with error less than 0.667 ulps */
801.1Sjtc	s=t*t;		/* t*t is exact */
811.1Sjtc	r=x/s;
821.1Sjtc	w=t+t;
831.1Sjtc	r=(r-t)/(w+r);	/* r-s is exact */
841.1Sjtc	t=t+t*r;
851.1Sjtc
861.1Sjtc    /* retore the sign bit */
871.1Sjtc	pt[n0] |= sign;
881.1Sjtc	return(t);
891.1Sjtc}
90