blob: 5f785a0358a96942dcd84a3c3845d53e7f6d3afb [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
rmistry@google.comd6176b02012-08-23 18:14:13 +00007// http://metamerist.com/cbrt/CubeRoot.cpp
caryclark@google.com639df892012-01-10 21:46:10 +00008//
9
10#include <math.h>
caryclark@google.com27accef2012-01-25 18:57:23 +000011#include "CubicUtilities.h"
caryclark@google.com639df892012-01-10 21:46:10 +000012
13#define TEST_ALTERNATIVES 0
14#if TEST_ALTERNATIVES
15typedef float (*cuberootfnf) (float);
16typedef double (*cuberootfnd) (double);
17
18// estimate bits of precision (32-bit float case)
19inline int bits_of_precision(float a, float b)
20{
rmistry@google.comd6176b02012-08-23 18:14:13 +000021 const double kd = 1.0 / log(2.0);
caryclark@google.com639df892012-01-10 21:46:10 +000022
rmistry@google.comd6176b02012-08-23 18:14:13 +000023 if (a==b)
24 return 23;
caryclark@google.com639df892012-01-10 21:46:10 +000025
rmistry@google.comd6176b02012-08-23 18:14:13 +000026 const double kdmin = pow(2.0, -23.0);
caryclark@google.com639df892012-01-10 21:46:10 +000027
rmistry@google.comd6176b02012-08-23 18:14:13 +000028 double d = fabs(a-b);
29 if (d < kdmin)
30 return 23;
caryclark@google.com639df892012-01-10 21:46:10 +000031
rmistry@google.comd6176b02012-08-23 18:14:13 +000032 return int(-log(d)*kd);
caryclark@google.com639df892012-01-10 21:46:10 +000033}
34
35// estiamte bits of precision (64-bit double case)
36inline int bits_of_precision(double a, double b)
37{
rmistry@google.comd6176b02012-08-23 18:14:13 +000038 const double kd = 1.0 / log(2.0);
caryclark@google.com639df892012-01-10 21:46:10 +000039
rmistry@google.comd6176b02012-08-23 18:14:13 +000040 if (a==b)
41 return 52;
caryclark@google.com639df892012-01-10 21:46:10 +000042
rmistry@google.comd6176b02012-08-23 18:14:13 +000043 const double kdmin = pow(2.0, -52.0);
caryclark@google.com639df892012-01-10 21:46:10 +000044
rmistry@google.comd6176b02012-08-23 18:14:13 +000045 double d = fabs(a-b);
46 if (d < kdmin)
47 return 52;
caryclark@google.com639df892012-01-10 21:46:10 +000048
rmistry@google.comd6176b02012-08-23 18:14:13 +000049 return int(-log(d)*kd);
caryclark@google.com639df892012-01-10 21:46:10 +000050}
51
52// cube root via x^(1/3)
53static float pow_cbrtf(float x)
54{
rmistry@google.comd6176b02012-08-23 18:14:13 +000055 return (float) pow(x, 1.0f/3.0f);
caryclark@google.com639df892012-01-10 21:46:10 +000056}
57
58// cube root via x^(1/3)
59static double pow_cbrtd(double x)
60{
rmistry@google.comd6176b02012-08-23 18:14:13 +000061 return pow(x, 1.0/3.0);
caryclark@google.com639df892012-01-10 21:46:10 +000062}
63
64// cube root approximation using bit hack for 32-bit float
65static float cbrt_5f(float f)
66{
rmistry@google.comd6176b02012-08-23 18:14:13 +000067 unsigned int* p = (unsigned int *) &f;
68 *p = *p/3 + 709921077;
69 return f;
caryclark@google.com639df892012-01-10 21:46:10 +000070}
71#endif
72
rmistry@google.comd6176b02012-08-23 18:14:13 +000073// cube root approximation using bit hack for 64-bit float
caryclark@google.com639df892012-01-10 21:46:10 +000074// adapted from Kahan's cbrt
75static double cbrt_5d(double d)
76{
rmistry@google.comd6176b02012-08-23 18:14:13 +000077 const unsigned int B1 = 715094163;
78 double t = 0.0;
79 unsigned int* pt = (unsigned int*) &t;
80 unsigned int* px = (unsigned int*) &d;
81 pt[1]=px[1]/3+B1;
82 return t;
caryclark@google.com639df892012-01-10 21:46:10 +000083}
84
85#if TEST_ALTERNATIVES
rmistry@google.comd6176b02012-08-23 18:14:13 +000086// cube root approximation using bit hack for 64-bit float
caryclark@google.com639df892012-01-10 21:46:10 +000087// adapted from Kahan's cbrt
88#if 0
89static double quint_5d(double d)
90{
rmistry@google.comd6176b02012-08-23 18:14:13 +000091 return sqrt(sqrt(d));
caryclark@google.com639df892012-01-10 21:46:10 +000092
rmistry@google.comd6176b02012-08-23 18:14:13 +000093 const unsigned int B1 = 71509416*5/3;
94 double t = 0.0;
95 unsigned int* pt = (unsigned int*) &t;
96 unsigned int* px = (unsigned int*) &d;
97 pt[1]=px[1]/5+B1;
98 return t;
caryclark@google.com639df892012-01-10 21:46:10 +000099}
100#endif
101
102// iterative cube root approximation using Halley's method (float)
103static float cbrta_halleyf(const float a, const float R)
104{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000105 const float a3 = a*a*a;
caryclark@google.com639df892012-01-10 21:46:10 +0000106 const float b= a * (a3 + R + R) / (a3 + a3 + R);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000107 return b;
caryclark@google.com639df892012-01-10 21:46:10 +0000108}
109#endif
110
111// iterative cube root approximation using Halley's method (double)
112static double cbrta_halleyd(const double a, const double R)
113{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000114 const double a3 = a*a*a;
caryclark@google.com639df892012-01-10 21:46:10 +0000115 const double b= a * (a3 + R + R) / (a3 + a3 + R);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116 return b;
caryclark@google.com639df892012-01-10 21:46:10 +0000117}
118
119#if TEST_ALTERNATIVES
120// iterative cube root approximation using Newton's method (float)
121static float cbrta_newtonf(const float a, const float x)
122{
123// return (1.0 / 3.0) * ((a + a) + x / (a * a));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000124 return a - (1.0f / 3.0f) * (a - x / (a*a));
caryclark@google.com639df892012-01-10 21:46:10 +0000125}
126
127// iterative cube root approximation using Newton's method (double)
128static double cbrta_newtond(const double a, const double x)
129{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000130 return (1.0/3.0) * (x / (a*a) + 2*a);
caryclark@google.com639df892012-01-10 21:46:10 +0000131}
132
133// cube root approximation using 1 iteration of Halley's method (double)
134static double halley_cbrt1d(double d)
135{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000136 double a = cbrt_5d(d);
137 return cbrta_halleyd(a, d);
caryclark@google.com639df892012-01-10 21:46:10 +0000138}
139
140// cube root approximation using 1 iteration of Halley's method (float)
141static float halley_cbrt1f(float d)
142{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000143 float a = cbrt_5f(d);
144 return cbrta_halleyf(a, d);
caryclark@google.com639df892012-01-10 21:46:10 +0000145}
146
147// cube root approximation using 2 iterations of Halley's method (double)
148static double halley_cbrt2d(double d)
149{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000150 double a = cbrt_5d(d);
151 a = cbrta_halleyd(a, d);
152 return cbrta_halleyd(a, d);
caryclark@google.com639df892012-01-10 21:46:10 +0000153}
154#endif
155
156// cube root approximation using 3 iterations of Halley's method (double)
157static double halley_cbrt3d(double d)
158{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000159 double a = cbrt_5d(d);
160 a = cbrta_halleyd(a, d);
161 a = cbrta_halleyd(a, d);
162 return cbrta_halleyd(a, d);
caryclark@google.com639df892012-01-10 21:46:10 +0000163}
164
165#if TEST_ALTERNATIVES
166// cube root approximation using 2 iterations of Halley's method (float)
167static float halley_cbrt2f(float d)
168{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000169 float a = cbrt_5f(d);
170 a = cbrta_halleyf(a, d);
171 return cbrta_halleyf(a, d);
caryclark@google.com639df892012-01-10 21:46:10 +0000172}
173
174// cube root approximation using 1 iteration of Newton's method (double)
175static double newton_cbrt1d(double d)
176{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000177 double a = cbrt_5d(d);
178 return cbrta_newtond(a, d);
caryclark@google.com639df892012-01-10 21:46:10 +0000179}
180
181// cube root approximation using 2 iterations of Newton's method (double)
182static double newton_cbrt2d(double d)
183{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000184 double a = cbrt_5d(d);
185 a = cbrta_newtond(a, d);
186 return cbrta_newtond(a, d);
caryclark@google.com639df892012-01-10 21:46:10 +0000187}
188
189// cube root approximation using 3 iterations of Newton's method (double)
190static double newton_cbrt3d(double d)
191{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000192 double a = cbrt_5d(d);
193 a = cbrta_newtond(a, d);
194 a = cbrta_newtond(a, d);
195 return cbrta_newtond(a, d);
caryclark@google.com639df892012-01-10 21:46:10 +0000196}
197
198// cube root approximation using 4 iterations of Newton's method (double)
199static double newton_cbrt4d(double d)
200{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000201 double a = cbrt_5d(d);
202 a = cbrta_newtond(a, d);
203 a = cbrta_newtond(a, d);
204 a = cbrta_newtond(a, d);
205 return cbrta_newtond(a, d);
caryclark@google.com639df892012-01-10 21:46:10 +0000206}
207
208// cube root approximation using 2 iterations of Newton's method (float)
209static float newton_cbrt1f(float d)
210{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000211 float a = cbrt_5f(d);
212 return cbrta_newtonf(a, d);
caryclark@google.com639df892012-01-10 21:46:10 +0000213}
214
215// cube root approximation using 2 iterations of Newton's method (float)
216static float newton_cbrt2f(float d)
217{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000218 float a = cbrt_5f(d);
219 a = cbrta_newtonf(a, d);
220 return cbrta_newtonf(a, d);
caryclark@google.com639df892012-01-10 21:46:10 +0000221}
222
223// cube root approximation using 3 iterations of Newton's method (float)
224static float newton_cbrt3f(float d)
225{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000226 float a = cbrt_5f(d);
227 a = cbrta_newtonf(a, d);
228 a = cbrta_newtonf(a, d);
229 return cbrta_newtonf(a, d);
caryclark@google.com639df892012-01-10 21:46:10 +0000230}
231
232// cube root approximation using 4 iterations of Newton's method (float)
233static float newton_cbrt4f(float d)
234{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000235 float a = cbrt_5f(d);
236 a = cbrta_newtonf(a, d);
237 a = cbrta_newtonf(a, d);
238 a = cbrta_newtonf(a, d);
239 return cbrta_newtonf(a, d);
caryclark@google.com639df892012-01-10 21:46:10 +0000240}
241
242static double TestCubeRootf(const char* szName, cuberootfnf cbrt, double rA, double rB, int rN)
243{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000244 const int N = rN;
caryclark@google.com639df892012-01-10 21:46:10 +0000245
rmistry@google.comd6176b02012-08-23 18:14:13 +0000246 float dd = float((rB-rA) / N);
caryclark@google.com639df892012-01-10 21:46:10 +0000247
rmistry@google.comd6176b02012-08-23 18:14:13 +0000248 // calculate 1M numbers
249 int i=0;
250 float d = (float) rA;
caryclark@google.com639df892012-01-10 21:46:10 +0000251
rmistry@google.comd6176b02012-08-23 18:14:13 +0000252 double s = 0.0;
caryclark@google.com639df892012-01-10 21:46:10 +0000253
rmistry@google.comd6176b02012-08-23 18:14:13 +0000254 for(d=(float) rA, i=0; i<N; i++, d += dd)
255 {
256 s += cbrt(d);
257 }
caryclark@google.com639df892012-01-10 21:46:10 +0000258
rmistry@google.comd6176b02012-08-23 18:14:13 +0000259 double bits = 0.0;
260 double worstx=0.0;
261 double worsty=0.0;
262 int minbits=64;
caryclark@google.com639df892012-01-10 21:46:10 +0000263
rmistry@google.comd6176b02012-08-23 18:14:13 +0000264 for(d=(float) rA, i=0; i<N; i++, d += dd)
265 {
266 float a = cbrt((float) d);
267 float b = (float) pow((double) d, 1.0/3.0);
caryclark@google.com639df892012-01-10 21:46:10 +0000268
rmistry@google.comd6176b02012-08-23 18:14:13 +0000269 int bc = bits_of_precision(a, b);
270 bits += bc;
caryclark@google.com639df892012-01-10 21:46:10 +0000271
rmistry@google.comd6176b02012-08-23 18:14:13 +0000272 if (b > 1.0e-6)
273 {
274 if (bc < minbits)
275 {
276 minbits = bc;
277 worstx = d;
278 worsty = a;
279 }
280 }
281 }
282
283 bits /= N;
caryclark@google.com639df892012-01-10 21:46:10 +0000284
285 printf(" %3d mbp %6.3f abp\n", minbits, bits);
286
rmistry@google.comd6176b02012-08-23 18:14:13 +0000287 return s;
caryclark@google.com639df892012-01-10 21:46:10 +0000288}
289
290
291static double TestCubeRootd(const char* szName, cuberootfnd cbrt, double rA, double rB, int rN)
292{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000293 const int N = rN;
caryclark@google.com639df892012-01-10 21:46:10 +0000294
rmistry@google.comd6176b02012-08-23 18:14:13 +0000295 double dd = (rB-rA) / N;
caryclark@google.com639df892012-01-10 21:46:10 +0000296
rmistry@google.comd6176b02012-08-23 18:14:13 +0000297 int i=0;
298
299 double s = 0.0;
300 double d = 0.0;
301
302 for(d=rA, i=0; i<N; i++, d += dd)
303 {
304 s += cbrt(d);
305 }
caryclark@google.com639df892012-01-10 21:46:10 +0000306
307
rmistry@google.comd6176b02012-08-23 18:14:13 +0000308 double bits = 0.0;
309 double worstx = 0.0;
310 double worsty = 0.0;
311 int minbits = 64;
312 for(d=rA, i=0; i<N; i++, d += dd)
313 {
314 double a = cbrt(d);
315 double b = pow(d, 1.0/3.0);
caryclark@google.com639df892012-01-10 21:46:10 +0000316
rmistry@google.comd6176b02012-08-23 18:14:13 +0000317 int bc = bits_of_precision(a, b); // min(53, count_matching_bitsd(a, b) - 12);
318 bits += bc;
caryclark@google.com639df892012-01-10 21:46:10 +0000319
rmistry@google.comd6176b02012-08-23 18:14:13 +0000320 if (b > 1.0e-6)
321 {
322 if (bc < minbits)
323 {
324 bits_of_precision(a, b);
325 minbits = bc;
326 worstx = d;
327 worsty = a;
328 }
329 }
330 }
caryclark@google.com639df892012-01-10 21:46:10 +0000331
rmistry@google.comd6176b02012-08-23 18:14:13 +0000332 bits /= N;
caryclark@google.com639df892012-01-10 21:46:10 +0000333
334 printf(" %3d mbp %6.3f abp\n", minbits, bits);
335
rmistry@google.comd6176b02012-08-23 18:14:13 +0000336 return s;
caryclark@google.com639df892012-01-10 21:46:10 +0000337}
338
339static int _tmain()
340{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000341 // a million uniform steps through the range from 0.0 to 1.0
342 // (doing uniform steps in the log scale would be better)
343 double a = 0.0;
344 double b = 1.0;
345 int n = 1000000;
caryclark@google.com639df892012-01-10 21:46:10 +0000346
rmistry@google.comd6176b02012-08-23 18:14:13 +0000347 printf("32-bit float tests\n");
348 printf("----------------------------------------\n");
349 TestCubeRootf("cbrt_5f", cbrt_5f, a, b, n);
350 TestCubeRootf("pow", pow_cbrtf, a, b, n);
351 TestCubeRootf("halley x 1", halley_cbrt1f, a, b, n);
352 TestCubeRootf("halley x 2", halley_cbrt2f, a, b, n);
353 TestCubeRootf("newton x 1", newton_cbrt1f, a, b, n);
354 TestCubeRootf("newton x 2", newton_cbrt2f, a, b, n);
355 TestCubeRootf("newton x 3", newton_cbrt3f, a, b, n);
356 TestCubeRootf("newton x 4", newton_cbrt4f, a, b, n);
357 printf("\n\n");
caryclark@google.com639df892012-01-10 21:46:10 +0000358
rmistry@google.comd6176b02012-08-23 18:14:13 +0000359 printf("64-bit double tests\n");
360 printf("----------------------------------------\n");
361 TestCubeRootd("cbrt_5d", cbrt_5d, a, b, n);
362 TestCubeRootd("pow", pow_cbrtd, a, b, n);
363 TestCubeRootd("halley x 1", halley_cbrt1d, a, b, n);
364 TestCubeRootd("halley x 2", halley_cbrt2d, a, b, n);
365 TestCubeRootd("halley x 3", halley_cbrt3d, a, b, n);
366 TestCubeRootd("newton x 1", newton_cbrt1d, a, b, n);
367 TestCubeRootd("newton x 2", newton_cbrt2d, a, b, n);
368 TestCubeRootd("newton x 3", newton_cbrt3d, a, b, n);
369 TestCubeRootd("newton x 4", newton_cbrt4d, a, b, n);
370 printf("\n\n");
caryclark@google.com639df892012-01-10 21:46:10 +0000371
rmistry@google.comd6176b02012-08-23 18:14:13 +0000372 return 0;
caryclark@google.com639df892012-01-10 21:46:10 +0000373}
374#endif
375
376double cube_root(double x) {
caryclark@google.com9f602912013-01-24 21:47:16 +0000377 if (approximately_zero_cubed(x)) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000378 return 0;
379 }
380 double result = halley_cbrt3d(fabs(x));
381 if (x < 0) {
382 result = -result;
383 }
384 return result;
caryclark@google.com639df892012-01-10 21:46:10 +0000385}
386
387#if TEST_ALTERNATIVES
388// http://bytes.com/topic/c/answers/754588-tips-find-cube-root-program-using-c
389/* cube root */
390int icbrt(int n) {
391 int t=0, x=(n+2)/3; /* works for n=0 and n>=1 */
392 for(; t!=x;) {
393 int x3=x*x*x;
394 t=x;
395 x*=(2*n + x3);
396 x/=(2*x3 + n);
397 }
398 return x ; /* always(?) equal to floor(n^(1/3)) */
399}
400#endif