aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/Intersection/CubeRoot.cpp
blob: 5f785a0358a96942dcd84a3c3845d53e7f6d3afb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
// http://metamerist.com/cbrt/CubeRoot.cpp
//

#include <math.h>
#include "CubicUtilities.h"

#define TEST_ALTERNATIVES 0
#if TEST_ALTERNATIVES
typedef float  (*cuberootfnf) (float);
typedef double (*cuberootfnd) (double);

// estimate bits of precision (32-bit float case)
inline int bits_of_precision(float a, float b)
{
    const double kd = 1.0 / log(2.0);

    if (a==b)
        return 23;

    const double kdmin = pow(2.0, -23.0);

    double d = fabs(a-b);
    if (d < kdmin)
        return 23;

    return int(-log(d)*kd);
}

// estiamte bits of precision (64-bit double case)
inline int bits_of_precision(double a, double b)
{
    const double kd = 1.0 / log(2.0);

    if (a==b)
        return 52;

    const double kdmin = pow(2.0, -52.0);

    double d = fabs(a-b);
    if (d < kdmin)
        return 52;

    return int(-log(d)*kd);
}

// cube root via x^(1/3)
static float pow_cbrtf(float x)
{
    return (float) pow(x, 1.0f/3.0f);
}

// cube root via x^(1/3)
static double pow_cbrtd(double x)
{
    return pow(x, 1.0/3.0);
}

// cube root approximation using bit hack for 32-bit float
static  float cbrt_5f(float f)
{
    unsigned int* p = (unsigned int *) &f;
    *p = *p/3 + 709921077;
    return f;
}
#endif

// cube root approximation using bit hack for 64-bit float
// adapted from Kahan's cbrt
static  double cbrt_5d(double d)
{
    const unsigned int B1 = 715094163;
    double t = 0.0;
    unsigned int* pt = (unsigned int*) &t;
    unsigned int* px = (unsigned int*) &d;
    pt[1]=px[1]/3+B1;
    return t;
}

#if TEST_ALTERNATIVES
// cube root approximation using bit hack for 64-bit float
// adapted from Kahan's cbrt
#if 0
static  double quint_5d(double d)
{
    return sqrt(sqrt(d));

    const unsigned int B1 = 71509416*5/3;
    double t = 0.0;
    unsigned int* pt = (unsigned int*) &t;
    unsigned int* px = (unsigned int*) &d;
    pt[1]=px[1]/5+B1;
    return t;
}
#endif

// iterative cube root approximation using Halley's method (float)
static  float cbrta_halleyf(const float a, const float R)
{
    const float a3 = a*a*a;
    const float b= a * (a3 + R + R) / (a3 + a3 + R);
    return b;
}
#endif

// iterative cube root approximation using Halley's method (double)
static  double cbrta_halleyd(const double a, const double R)
{
    const double a3 = a*a*a;
    const double b= a * (a3 + R + R) / (a3 + a3 + R);
    return b;
}

#if TEST_ALTERNATIVES
// iterative cube root approximation using Newton's method (float)
static  float cbrta_newtonf(const float a, const float x)
{
//    return (1.0 / 3.0) * ((a + a) + x / (a * a));
    return a - (1.0f / 3.0f) * (a - x / (a*a));
}

// iterative cube root approximation using Newton's method (double)
static  double cbrta_newtond(const double a, const double x)
{
    return (1.0/3.0) * (x / (a*a) + 2*a);
}

// cube root approximation using 1 iteration of Halley's method (double)
static double halley_cbrt1d(double d)
{
    double a = cbrt_5d(d);
    return cbrta_halleyd(a, d);
}

// cube root approximation using 1 iteration of Halley's method (float)
static float halley_cbrt1f(float d)
{
    float a = cbrt_5f(d);
    return cbrta_halleyf(a, d);
}

// cube root approximation using 2 iterations of Halley's method (double)
static double halley_cbrt2d(double d)
{
    double a = cbrt_5d(d);
    a = cbrta_halleyd(a, d);
    return cbrta_halleyd(a, d);
}
#endif

// cube root approximation using 3 iterations of Halley's method (double)
static double halley_cbrt3d(double d)
{
    double a = cbrt_5d(d);
    a = cbrta_halleyd(a, d);
    a = cbrta_halleyd(a, d);
    return cbrta_halleyd(a, d);
}

#if TEST_ALTERNATIVES
// cube root approximation using 2 iterations of Halley's method (float)
static float halley_cbrt2f(float d)
{
    float a = cbrt_5f(d);
    a = cbrta_halleyf(a, d);
    return cbrta_halleyf(a, d);
}

// cube root approximation using 1 iteration of Newton's method (double)
static double newton_cbrt1d(double d)
{
    double a = cbrt_5d(d);
    return cbrta_newtond(a, d);
}

// cube root approximation using 2 iterations of Newton's method (double)
static double newton_cbrt2d(double d)
{
    double a = cbrt_5d(d);
    a = cbrta_newtond(a, d);
    return cbrta_newtond(a, d);
}

// cube root approximation using 3 iterations of Newton's method (double)
static double newton_cbrt3d(double d)
{
    double a = cbrt_5d(d);
    a = cbrta_newtond(a, d);
    a = cbrta_newtond(a, d);
    return cbrta_newtond(a, d);
}

// cube root approximation using 4 iterations of Newton's method (double)
static double newton_cbrt4d(double d)
{
    double a = cbrt_5d(d);
    a = cbrta_newtond(a, d);
    a = cbrta_newtond(a, d);
    a = cbrta_newtond(a, d);
    return cbrta_newtond(a, d);
}

// cube root approximation using 2 iterations of Newton's method (float)
static float newton_cbrt1f(float d)
{
    float a = cbrt_5f(d);
    return cbrta_newtonf(a, d);
}

// cube root approximation using 2 iterations of Newton's method (float)
static float newton_cbrt2f(float d)
{
    float a = cbrt_5f(d);
    a = cbrta_newtonf(a, d);
    return cbrta_newtonf(a, d);
}

// cube root approximation using 3 iterations of Newton's method (float)
static float newton_cbrt3f(float d)
{
    float a = cbrt_5f(d);
    a = cbrta_newtonf(a, d);
    a = cbrta_newtonf(a, d);
    return cbrta_newtonf(a, d);
}

// cube root approximation using 4 iterations of Newton's method (float)
static float newton_cbrt4f(float d)
{
    float a = cbrt_5f(d);
    a = cbrta_newtonf(a, d);
    a = cbrta_newtonf(a, d);
    a = cbrta_newtonf(a, d);
    return cbrta_newtonf(a, d);
}

static double TestCubeRootf(const char* szName, cuberootfnf cbrt, double rA, double rB, int rN)
{
    const int N = rN;

    float dd = float((rB-rA) / N);

    // calculate 1M numbers
    int i=0;
    float d = (float) rA;

    double s = 0.0;

    for(d=(float) rA, i=0; i<N; i++, d += dd)
    {
        s += cbrt(d);
    }

    double bits = 0.0;
    double worstx=0.0;
    double worsty=0.0;
    int minbits=64;

    for(d=(float) rA, i=0; i<N; i++, d += dd)
    {
        float a = cbrt((float) d);
        float b = (float) pow((double) d, 1.0/3.0);

        int bc = bits_of_precision(a, b);
        bits += bc;

        if (b > 1.0e-6)
        {
            if (bc < minbits)
            {
                minbits = bc;
                worstx = d;
                worsty = a;
            }
        }
    }

    bits /= N;

    printf(" %3d mbp  %6.3f abp\n", minbits, bits);

    return s;
}


static double TestCubeRootd(const char* szName, cuberootfnd cbrt, double rA, double rB, int rN)
{
    const int N = rN;

    double dd = (rB-rA) / N;

    int i=0;

    double s = 0.0;
    double d = 0.0;

    for(d=rA, i=0; i<N; i++, d += dd)
    {
        s += cbrt(d);
    }


    double bits = 0.0;
    double worstx = 0.0;
    double worsty = 0.0;
    int minbits = 64;
    for(d=rA, i=0; i<N; i++, d += dd)
    {
        double a = cbrt(d);
        double b = pow(d, 1.0/3.0);

        int bc = bits_of_precision(a, b); // min(53, count_matching_bitsd(a, b) - 12);
        bits += bc;

        if (b > 1.0e-6)
        {
            if (bc < minbits)
            {
                bits_of_precision(a, b);
                minbits = bc;
                worstx = d;
                worsty = a;
            }
        }
    }

    bits /= N;

    printf(" %3d mbp  %6.3f abp\n", minbits, bits);

    return s;
}

static int _tmain()
{
    // a million uniform steps through the range from 0.0 to 1.0
    // (doing uniform steps in the log scale would be better)
    double a = 0.0;
    double b = 1.0;
    int n = 1000000;

    printf("32-bit float tests\n");
    printf("----------------------------------------\n");
    TestCubeRootf("cbrt_5f", cbrt_5f, a, b, n);
    TestCubeRootf("pow", pow_cbrtf, a, b, n);
    TestCubeRootf("halley x 1", halley_cbrt1f, a, b, n);
    TestCubeRootf("halley x 2", halley_cbrt2f, a, b, n);
    TestCubeRootf("newton x 1", newton_cbrt1f, a, b, n);
    TestCubeRootf("newton x 2", newton_cbrt2f, a, b, n);
    TestCubeRootf("newton x 3", newton_cbrt3f, a, b, n);
    TestCubeRootf("newton x 4", newton_cbrt4f, a, b, n);
    printf("\n\n");

    printf("64-bit double tests\n");
    printf("----------------------------------------\n");
    TestCubeRootd("cbrt_5d", cbrt_5d, a, b, n);
    TestCubeRootd("pow", pow_cbrtd, a, b, n);
    TestCubeRootd("halley x 1", halley_cbrt1d, a, b, n);
    TestCubeRootd("halley x 2", halley_cbrt2d, a, b, n);
    TestCubeRootd("halley x 3", halley_cbrt3d, a, b, n);
    TestCubeRootd("newton x 1", newton_cbrt1d, a, b, n);
    TestCubeRootd("newton x 2", newton_cbrt2d, a, b, n);
    TestCubeRootd("newton x 3", newton_cbrt3d, a, b, n);
    TestCubeRootd("newton x 4", newton_cbrt4d, a, b, n);
    printf("\n\n");

    return 0;
}
#endif

double cube_root(double x) {
    if (approximately_zero_cubed(x)) {
        return 0;
    }
    double result = halley_cbrt3d(fabs(x));
    if (x < 0) {
        result = -result;
    }
    return result;
}

#if TEST_ALTERNATIVES
// http://bytes.com/topic/c/answers/754588-tips-find-cube-root-program-using-c
/* cube root */
int icbrt(int n) {
    int t=0, x=(n+2)/3; /* works for n=0 and n>=1 */
    for(; t!=x;) {
        int x3=x*x*x;
        t=x;
        x*=(2*n + x3);
        x/=(2*x3 + n);
    }
    return x ; /* always(?) equal to floor(n^(1/3)) */
}
#endif