12fe8fb19SBen Gras /* @(#)s_cbrt.c 5.1 93/09/24 */
22fe8fb19SBen Gras /*
32fe8fb19SBen Gras * ====================================================
42fe8fb19SBen Gras * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
52fe8fb19SBen Gras *
62fe8fb19SBen Gras * Developed at SunPro, a Sun Microsystems, Inc. business.
72fe8fb19SBen Gras * Permission to use, copy, modify, and distribute this
82fe8fb19SBen Gras * software is freely granted, provided that this notice
92fe8fb19SBen Gras * is preserved.
102fe8fb19SBen Gras * ====================================================
112fe8fb19SBen Gras */
122fe8fb19SBen Gras
132fe8fb19SBen Gras #include <sys/cdefs.h>
142fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
15*84d9c625SLionel Sambuc __RCSID("$NetBSD: s_cbrt.c,v 1.13 2013/11/20 12:29:13 joerg Exp $");
162fe8fb19SBen Gras #endif
172fe8fb19SBen Gras
18*84d9c625SLionel Sambuc #include "namespace.h"
192fe8fb19SBen Gras #include "math.h"
202fe8fb19SBen Gras #include "math_private.h"
212fe8fb19SBen Gras
22*84d9c625SLionel Sambuc #ifndef __HAVE_LONG_DOUBLE
23*84d9c625SLionel Sambuc __strong_alias(_cbrtl, cbrt)
24*84d9c625SLionel Sambuc __weak_alias(cbrtl, _cbrtl)
25*84d9c625SLionel Sambuc #endif
26*84d9c625SLionel Sambuc
272fe8fb19SBen Gras /* cbrt(x)
282fe8fb19SBen Gras * Return cube root of x
292fe8fb19SBen Gras */
302fe8fb19SBen Gras static const u_int32_t
312fe8fb19SBen Gras B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
322fe8fb19SBen Gras B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
332fe8fb19SBen Gras
342fe8fb19SBen Gras static const double
352fe8fb19SBen Gras C = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */
362fe8fb19SBen Gras D = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */
372fe8fb19SBen Gras E = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */
382fe8fb19SBen Gras F = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */
392fe8fb19SBen Gras G = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */
402fe8fb19SBen Gras
412fe8fb19SBen Gras double
cbrt(double x)422fe8fb19SBen Gras cbrt(double x)
432fe8fb19SBen Gras {
442fe8fb19SBen Gras int32_t hx;
452fe8fb19SBen Gras double r,s,t=0.0,w;
462fe8fb19SBen Gras u_int32_t sign;
472fe8fb19SBen Gras u_int32_t high,low;
482fe8fb19SBen Gras
492fe8fb19SBen Gras GET_HIGH_WORD(hx,x);
502fe8fb19SBen Gras sign=hx&0x80000000; /* sign= sign(x) */
512fe8fb19SBen Gras hx ^=sign;
522fe8fb19SBen Gras if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */
532fe8fb19SBen Gras GET_LOW_WORD(low,x);
542fe8fb19SBen Gras if((hx|low)==0)
552fe8fb19SBen Gras return(x); /* cbrt(0) is itself */
562fe8fb19SBen Gras
572fe8fb19SBen Gras SET_HIGH_WORD(x,hx); /* x <- |x| */
582fe8fb19SBen Gras /* rough cbrt to 5 bits */
592fe8fb19SBen Gras if(hx<0x00100000) /* subnormal number */
602fe8fb19SBen Gras {SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */
612fe8fb19SBen Gras t*=x; GET_HIGH_WORD(high,t); SET_HIGH_WORD(t,high/3+B2);
622fe8fb19SBen Gras }
632fe8fb19SBen Gras else
642fe8fb19SBen Gras SET_HIGH_WORD(t,hx/3+B1);
652fe8fb19SBen Gras
662fe8fb19SBen Gras
672fe8fb19SBen Gras /* new cbrt to 23 bits, may be implemented in single precision */
682fe8fb19SBen Gras r=t*t/x;
692fe8fb19SBen Gras s=C+r*t;
702fe8fb19SBen Gras t*=G+F/(s+E+D/s);
712fe8fb19SBen Gras
722fe8fb19SBen Gras /* chopped to 20 bits and make it larger than cbrt(x) */
732fe8fb19SBen Gras GET_HIGH_WORD(high,t);
742fe8fb19SBen Gras INSERT_WORDS(t,high+0x00000001,0);
752fe8fb19SBen Gras
762fe8fb19SBen Gras
772fe8fb19SBen Gras /* one step newton iteration to 53 bits with error less than 0.667 ulps */
782fe8fb19SBen Gras s=t*t; /* t*t is exact */
792fe8fb19SBen Gras r=x/s;
802fe8fb19SBen Gras w=t+t;
812fe8fb19SBen Gras r=(r-t)/(w+r); /* r-s is exact */
822fe8fb19SBen Gras t=t+t*r;
832fe8fb19SBen Gras
842fe8fb19SBen Gras /* retore the sign bit */
852fe8fb19SBen Gras GET_HIGH_WORD(high,t);
862fe8fb19SBen Gras SET_HIGH_WORD(t,high|sign);
872fe8fb19SBen Gras return(t);
882fe8fb19SBen Gras }
89