POK
cbrtf.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_cbrtf.c -- float version of s_cbrt.c.
18  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
19  */
20 
21 /*
22  * ====================================================
23  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
24  *
25  * Developed at SunPro, a Sun Microsystems, Inc. business.
26  * Permission to use, copy, modify, and distribute this
27  * software is freely granted, provided that this notice
28  * is preserved.
29  * ====================================================
30  */
31 
32 #ifdef POK_NEEDS_LIBMATH
33 
34 #include <types.h>
35 #include <libm.h>
36 #include "math_private.h"
37 
38 /* cbrtf(x)
39  * Return cube root of x
40  */
41 static const unsigned
42  B1 = 709958130, /* B1 = (84+2/3-0.03306235651)*2**23 */
43  B2 = 642849266; /* B2 = (76+2/3-0.03306235651)*2**23 */
44 
45 static const float
46 C = 5.4285717010e-01, /* 19/35 = 0x3f0af8b0 */
47 D = -7.0530611277e-01, /* -864/1225 = 0xbf348ef1 */
48 E = 1.4142856598e+00, /* 99/70 = 0x3fb50750 */
49 F = 1.6071428061e+00, /* 45/28 = 0x3fcdb6db */
50 G = 3.5714286566e-01; /* 5/14 = 0x3eb6db6e */
51 
52 float
53 cbrtf(float x)
54 {
55  float r,s,t;
56  int32_t hx;
57  uint32_t sign;
58  uint32_t high;
59 
60  GET_FLOAT_WORD(hx,x);
61  sign=hx&0x80000000; /* sign= sign(x) */
62  hx ^=sign;
63  if(hx>=0x7f800000) return(x+x); /* cbrt(NaN,INF) is itself */
64  if(hx==0)
65  return(x); /* cbrt(0) is itself */
66 
67  SET_FLOAT_WORD(x,hx); /* x <- |x| */
68  /* rough cbrt to 5 bits */
69  if(hx<0x00800000) /* subnormal number */
70  {SET_FLOAT_WORD(t,0x4b800000); /* set t= 2**24 */
71  t*=x; GET_FLOAT_WORD(high,t); SET_FLOAT_WORD(t,high/3+B2);
72  }
73  else
74  SET_FLOAT_WORD(t,hx/3+B1);
75 
76 
77  /* new cbrt to 23 bits */
78  r=t*t/x;
79  s=C+r*t;
80  t*=G+F/(s+E+D/s);
81 
82  /* retore the sign bit */
83  GET_FLOAT_WORD(high,t);
84  SET_FLOAT_WORD(t,high|sign);
85  return(t);
86 }
87 
88 #endif
89