Line | |
---|
1 | function 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 |
|
---|
6 | flops_div = 8;
|
---|
7 | flops_sqrt = 8;
|
---|
8 | if nargin < 1
|
---|
9 | a = 0.1;
|
---|
10 | end
|
---|
11 | f = 0;
|
---|
12 | if a < 0
|
---|
13 | f = f + flops_div;
|
---|
14 | a = -a;
|
---|
15 | end
|
---|
16 | if a == 0 || a == 1
|
---|
17 | return;
|
---|
18 | end
|
---|
19 | if fix(a) == a
|
---|
20 | % number of multiplications to raise to integer power
|
---|
21 | f = f + floor(log2(a)) + num_bits(a)-1;
|
---|
22 | elseif a == 1/2
|
---|
23 | % sqrt is built-in function
|
---|
24 | f = f + flops_sqrt;
|
---|
25 | elseif fix(2*a) == 2*a
|
---|
26 | % this handles flops_pow(1/2+1)
|
---|
27 | f = f + flops_pow(2*a) - 1 + flops_sqrt;
|
---|
28 | elseif a == 1/4
|
---|
29 | f = f + 2*flops_sqrt;
|
---|
30 | elseif a == 3/4
|
---|
31 | f = f + 2*flops_sqrt+1;
|
---|
32 | elseif fix(4*a) == 4*a
|
---|
33 | % this handles flops_pow(1/4+1)
|
---|
34 | f = f + flops_pow(2*a) - 1 + flops_sqrt;
|
---|
35 | else
|
---|
36 | f = Inf;
|
---|
37 | end
|
---|
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 |
|
---|
46 | f_upper = 61; % flops_exp+flops_log+1
|
---|
47 | if f > f_upper
|
---|
48 | f = f_upper;
|
---|
49 | end
|
---|
50 |
|
---|
51 |
|
---|
52 | function 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
|
---|
57 | bits = [0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4];
|
---|
58 |
|
---|
59 | b = 0;
|
---|
60 | while(x > 0)
|
---|
61 | b = b + bits(mod(x,16)+1);
|
---|
62 | x = floor(x/16);
|
---|
63 | end
|
---|
Note: See
TracBrowser
for help on using the repository browser.