s_cbrt.c revision 1.11
11.1Sjtc/* @(#)s_cbrt.c 5.1 93/09/24 */ 21.1Sjtc/* 31.1Sjtc * ==================================================== 41.1Sjtc * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 51.1Sjtc * 61.1Sjtc * Developed at SunPro, a Sun Microsystems, Inc. business. 71.1Sjtc * Permission to use, copy, modify, and distribute this 81.10Ssimonb * software is freely granted, provided that this notice 91.1Sjtc * is preserved. 101.1Sjtc * ==================================================== 111.1Sjtc */ 121.3Sjtc 131.9Slukem#include <sys/cdefs.h> 141.7Sjtc#if defined(LIBM_SCCS) && !defined(lint) 151.11Swiz__RCSID("$NetBSD: s_cbrt.c,v 1.11 2002/05/26 22:01:54 wiz Exp $"); 161.3Sjtc#endif 171.1Sjtc 181.5Sjtc#include "math.h" 191.5Sjtc#include "math_private.h" 201.1Sjtc 211.1Sjtc/* cbrt(x) 221.1Sjtc * Return cube root of x 231.1Sjtc */ 241.6Sjtcstatic const u_int32_t 251.1Sjtc B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */ 261.1Sjtc B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ 271.1Sjtc 281.1Sjtcstatic const double 291.1SjtcC = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */ 301.1SjtcD = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */ 311.1SjtcE = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */ 321.1SjtcF = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */ 331.1SjtcG = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */ 341.1Sjtc 351.11Swizdouble 361.11Swizcbrt(double x) 371.1Sjtc{ 381.6Sjtc int32_t hx; 391.1Sjtc double r,s,t=0.0,w; 401.6Sjtc u_int32_t sign; 411.6Sjtc u_int32_t high,low; 421.1Sjtc 431.5Sjtc GET_HIGH_WORD(hx,x); 441.1Sjtc sign=hx&0x80000000; /* sign= sign(x) */ 451.1Sjtc hx ^=sign; 461.1Sjtc if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */ 471.5Sjtc GET_LOW_WORD(low,x); 481.10Ssimonb if((hx|low)==0) 491.1Sjtc return(x); /* cbrt(0) is itself */ 501.1Sjtc 511.5Sjtc SET_HIGH_WORD(x,hx); /* x <- |x| */ 521.1Sjtc /* rough cbrt to 5 bits */ 531.1Sjtc if(hx<0x00100000) /* subnormal number */ 541.5Sjtc {SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */ 551.5Sjtc t*=x; GET_HIGH_WORD(high,t); SET_HIGH_WORD(t,high/3+B2); 561.1Sjtc } 571.1Sjtc else 581.5Sjtc SET_HIGH_WORD(t,hx/3+B1); 591.1Sjtc 601.1Sjtc 611.1Sjtc /* new cbrt to 23 bits, may be implemented in single precision */ 621.1Sjtc r=t*t/x; 631.1Sjtc s=C+r*t; 641.10Ssimonb t*=G+F/(s+E+D/s); 651.1Sjtc 661.10Ssimonb /* chopped to 20 bits and make it larger than cbrt(x) */ 671.5Sjtc GET_HIGH_WORD(high,t); 681.5Sjtc INSERT_WORDS(t,high+0x00000001,0); 691.1Sjtc 701.1Sjtc 711.1Sjtc /* one step newton iteration to 53 bits with error less than 0.667 ulps */ 721.1Sjtc s=t*t; /* t*t is exact */ 731.1Sjtc r=x/s; 741.1Sjtc w=t+t; 751.1Sjtc r=(r-t)/(w+r); /* r-s is exact */ 761.1Sjtc t=t+t*r; 771.1Sjtc 781.1Sjtc /* retore the sign bit */ 791.5Sjtc GET_HIGH_WORD(high,t); 801.5Sjtc SET_HIGH_WORD(t,high|sign); 811.1Sjtc return(t); 821.1Sjtc} 83