amath  1.8.5
Simple command line calculator
cbrt.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Project homepage:
26  * https://amath.innolan.net
27  *
28  * The original source code can be obtained from:
29  * http://www.netlib.org/fdlibm/s_cbrt.c
30  *
31  * =================================================================
32  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
33  *
34  * Developed at SunSoft, a Sun Microsystems, Inc. business.
35  * Permission to use, copy, modify, and distribute this
36  * software is freely granted, provided that this notice
37  * is preserved.
38  * =================================================================
39  */
40 
41 /**
42  * @file cbrt.c
43  * @brief Cube root function
44  */
45 
46 #include "prim.h"
47 
48 static const unsigned
49  B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
50  B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
51 
52 static const double
53  C = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */
54  D = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */
55  E = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */
56  F = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */
57  G = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */
58 
59 /**
60  * @brief Cube root function
61  */
62 double cbrt(double x)
63 {
64  int32_t hx, lx, ht;
65  double r, s, t = 0.0, w;
66  uint32_t sign;
67 
68  GET_HIGH_WORD(hx, x);
69 
70  // sign = sign(x)
71  sign = hx & 0x80000000;
72  hx ^= sign;
73 
74  // cbrt(NaN,INF) is itself
75  if (hx >= 0x7FF00000)
76  {
77  return x + x;
78  }
79 
80  GET_LOW_WORD(lx, x);
81 
82  // cbrt(0) is itself
83  if ((hx | lx) == 0)
84  {
85  return x;
86  }
87 
88  // x <- |x|
89  SET_HIGH_WORD(x, hx);
90 
91  // rough cbrt to 5 bits
92  if (hx < 0x00100000) // subnormal number
93  {
94  SET_HIGH_WORD(t, 0x43500000); // set t= 2**54
95  t *= x;
96  GET_HIGH_WORD(ht, t);
97  SET_HIGH_WORD(t, ht / 3 + B2);
98  }
99  else
100  {
101  SET_HIGH_WORD(t, hx / 3 + B1);
102  }
103 
104  // new cbrt to 23 bits, may be implemented in single precision
105  r = t * t / x;
106  s = C + r * t;
107  t *= G + F / (s + E + D / s);
108 
109  // chopped to 20 bits and make it larger than cbrt(x)
110  SET_LOW_WORD(t, 0);
111  GET_HIGH_WORD(ht, t);
112  SET_HIGH_WORD(t, ht + 0x00000001);
113 
114  // one step newton iteration to 53 bits with error less than 0.667 ulps
115  s = t * t; // t*t is exact
116  r = x / s;
117  w = t + t;
118  r = (r - t) / (w + r); // r-s is exact
119  t = t + t * r;
120 
121  // retore the sign bit
122  GET_HIGH_WORD(ht, t);
123  SET_HIGH_WORD(t, ht | sign);
124 
125  return (t);
126 }
static const double D
Definition: cbrt.c:54
static const double E
Definition: cbrt.c:55
double cbrt(double x)
Cube root function.
Definition: cbrt.c:62
#define GET_HIGH_WORD(i, d)
Get the more significant 32 bit int from a double.
Definition: prim.h:167
#define GET_LOW_WORD(i, d)
Get the less significant 32 bit int from a double.
Definition: prim.h:177
static const double F
Definition: cbrt.c:56
static const unsigned B2
Definition: cbrt.c:50
static const double G
Definition: cbrt.c:57
static const unsigned B1
Definition: cbrt.c:49
static const double C
Definition: cbrt.c:53
#define SET_LOW_WORD(d, v)
Set the less significant 32 bits of a double from an int.
Definition: prim.h:209
#define SET_HIGH_WORD(d, v)
Set the more significant 32 bits of a double from an int.
Definition: prim.h:198