source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/lightspeed/flops_pow.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 1.4 KB
Line 
1function f = flops_pow(a)
2% FLOPS_POW    Flops for raising to real power.
3% FLOPS_POW(A) returns the number of flops for (X .^ A) where X is scalar.
4% Powers like 0, 1, 2, and 1/2 are handled specially.
5
6flops_div = 8;
7flops_sqrt = 8;
8if nargin < 1
9  a = 0.1;
10end
11f = 0;
12if a < 0
13  f = f + flops_div;
14  a = -a;
15end
16if a == 0 || a == 1
17  return;
18end
19if fix(a) == a
20  % number of multiplications to raise to integer power
21  f = f + floor(log2(a)) + num_bits(a)-1;
22elseif a == 1/2
23  % sqrt is built-in function
24  f = f + flops_sqrt;
25elseif fix(2*a) == 2*a
26  % this handles flops_pow(1/2+1)
27  f = f + flops_pow(2*a) - 1 + flops_sqrt;
28elseif a == 1/4
29  f = f + 2*flops_sqrt;
30elseif a == 3/4
31  f = f + 2*flops_sqrt+1;
32elseif fix(4*a) == 4*a
33  % this handles flops_pow(1/4+1)
34  f = f + flops_pow(2*a) - 1 + flops_sqrt;
35else
36  f = Inf;
37end
38
39% The identities
40%   exp(a) = e^a
41%   a^b = exp(b*log(a))
42% require that
43%   flops_exp < flops_pow < flops_exp+flops_log+1.
44% But in practice, I find that the runtime exceeds this upper bound.
45
46f_upper = 61;  % flops_exp+flops_log+1
47if f > f_upper
48  f = f_upper;
49end
50
51
52function b = num_bits(x)
53% Returns the number of 1 bits in the binary representation of x.
54% x must be a non-negative integer.
55
56% lookup table for 0-15
57bits = [0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4];
58
59b = 0;
60while(x > 0)
61  b = b + bits(mod(x,16)+1);
62  x = floor(x/16);
63end
Note: See TracBrowser for help on using the repository browser.