POK
cbrt.c
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Fri Jan 30 14:41:34 2009
15  */
16 
17 /* @(#)s_cbrt.c 5.1 93/09/24 */
18 /*
19  * ====================================================
20  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
21  *
22  * Developed at SunPro, a Sun Microsystems, Inc. business.
23  * Permission to use, copy, modify, and distribute this
24  * software is freely granted, provided that this notice
25  * is preserved.
26  * ====================================================
27  */
28 
29 #ifdef POK_NEEDS_LIBMATH
30 
31 #include <types.h>
32 #include <libm.h>
33 #include "math_private.h"
34 
35 /* cbrt(x)
36  * Return cube root of x
37  */
38 static const uint32_t
39  B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
40  B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
41 
42 static const double
43 C = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */
44 D = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */
45 E = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */
46 F = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */
47 G = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */
48 
49 double
50 cbrt(double x)
51 {
52  int32_t hx;
53  double r,s,t=0.0,w;
54  uint32_t sign;
55  uint32_t high,low;
56 
57  GET_HIGH_WORD(hx,x);
58  sign=hx&0x80000000; /* sign= sign(x) */
59  hx ^=sign;
60  if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */
61  GET_LOW_WORD(low,x);
62  if((hx|low)==0)
63  return(x); /* cbrt(0) is itself */
64 
65  SET_HIGH_WORD(x,hx); /* x <- |x| */
66  /* rough cbrt to 5 bits */
67  if(hx<0x00100000) /* subnormal number */
68  {SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */
69  t*=x; GET_HIGH_WORD(high,t); SET_HIGH_WORD(t,high/3+B2);
70  }
71  else
72  SET_HIGH_WORD(t,hx/3+B1);
73 
74 
75  /* new cbrt to 23 bits, may be implemented in single precision */
76  r=t*t/x;
77  s=C+r*t;
78  t*=G+F/(s+E+D/s);
79 
80  /* chopped to 20 bits and make it larger than cbrt(x) */
81  GET_HIGH_WORD(high,t);
82  INSERT_WORDS(t,high+0x00000001,0);
83 
84 
85  /* one step newton iteration to 53 bits with error less than 0.667 ulps */
86  s=t*t; /* t*t is exact */
87  r=x/s;
88  w=t+t;
89  r=(r-t)/(w+r); /* r-s is exact */
90  t=t+t*r;
91 
92  /* retore the sign bit */
93  GET_HIGH_WORD(high,t);
94  SET_HIGH_WORD(t,high|sign);
95  return(t);
96 }
97 #endif