s_cbrt.c revision 1.10
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.10Ssimonb__RCSID("$NetBSD: s_cbrt.c,v 1.10 1999/07/02 15:37:42 simonb 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.1Sjtc#ifdef __STDC__ 251.6Sjtcstatic const u_int32_t 261.1Sjtc#else 271.6Sjtcstatic u_int32_t 281.1Sjtc#endif 291.1Sjtc B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */ 301.1Sjtc B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ 311.1Sjtc 321.1Sjtc#ifdef __STDC__ 331.1Sjtcstatic const double 341.1Sjtc#else 351.1Sjtcstatic double 361.1Sjtc#endif 371.1SjtcC = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */ 381.1SjtcD = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */ 391.1SjtcE = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */ 401.1SjtcF = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */ 411.1SjtcG = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */ 421.1Sjtc 431.1Sjtc#ifdef __STDC__ 441.10Ssimonb double cbrt(double x) 451.1Sjtc#else 461.10Ssimonb double cbrt(x) 471.1Sjtc double x; 481.1Sjtc#endif 491.1Sjtc{ 501.6Sjtc int32_t hx; 511.1Sjtc double r,s,t=0.0,w; 521.6Sjtc u_int32_t sign; 531.6Sjtc u_int32_t high,low; 541.1Sjtc 551.5Sjtc GET_HIGH_WORD(hx,x); 561.1Sjtc sign=hx&0x80000000; /* sign= sign(x) */ 571.1Sjtc hx ^=sign; 581.1Sjtc if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */ 591.5Sjtc GET_LOW_WORD(low,x); 601.10Ssimonb if((hx|low)==0) 611.1Sjtc return(x); /* cbrt(0) is itself */ 621.1Sjtc 631.5Sjtc SET_HIGH_WORD(x,hx); /* x <- |x| */ 641.1Sjtc /* rough cbrt to 5 bits */ 651.1Sjtc if(hx<0x00100000) /* subnormal number */ 661.5Sjtc {SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */ 671.5Sjtc t*=x; GET_HIGH_WORD(high,t); SET_HIGH_WORD(t,high/3+B2); 681.1Sjtc } 691.1Sjtc else 701.5Sjtc SET_HIGH_WORD(t,hx/3+B1); 711.1Sjtc 721.1Sjtc 731.1Sjtc /* new cbrt to 23 bits, may be implemented in single precision */ 741.1Sjtc r=t*t/x; 751.1Sjtc s=C+r*t; 761.10Ssimonb t*=G+F/(s+E+D/s); 771.1Sjtc 781.10Ssimonb /* chopped to 20 bits and make it larger than cbrt(x) */ 791.5Sjtc GET_HIGH_WORD(high,t); 801.5Sjtc INSERT_WORDS(t,high+0x00000001,0); 811.1Sjtc 821.1Sjtc 831.1Sjtc /* one step newton iteration to 53 bits with error less than 0.667 ulps */ 841.1Sjtc s=t*t; /* t*t is exact */ 851.1Sjtc r=x/s; 861.1Sjtc w=t+t; 871.1Sjtc r=(r-t)/(w+r); /* r-s is exact */ 881.1Sjtc t=t+t*r; 891.1Sjtc 901.1Sjtc /* retore the sign bit */ 911.5Sjtc GET_HIGH_WORD(high,t); 921.5Sjtc SET_HIGH_WORD(t,high|sign); 931.1Sjtc return(t); 941.1Sjtc} 95