Why is pow2() slow? Fastest way?

조회 수: 2 (최근 30일)
Jim Svensson
Jim Svensson 2023년 10월 12일
편집: Jim Svensson 2023년 10월 13일
The pow2(x, d) built in function seems unreasonable slow with a floating point vector x. It is about ten times slower than just doing x*2^d.
Is it like that because the used cpu instruction can only operate on one float at a time, but multiplication can be vectorized?
Is there a faster (fastest) way to scale a floating point vector by a power of 2?

답변 (1개)

James Tursa
James Tursa 2023년 10월 12일
편집: James Tursa 2023년 10월 12일
Another way is just to add d to the exponent bits. Probably fastest to do this in a mex routine where edge checking can easily be done and of course you can multithread it, but in m-code one can use typecast to demonstrate the process. E.g.,
x = rand(1000000,1);
d = 44;
tic
y1 = pow2(x,d);
toc
Elapsed time is 0.015516 seconds.
tic
y2 = x * 2^d;
toc
Elapsed time is 0.005390 seconds.
tic
y = typecast(x,'uint64'); % interpret double bits as uint64
p = bitshift(uint64(d),52); % shift d into exponent bits location (demo assumes d >= 0)
y3 = typecast(y+p,'double'); % but no edge checking here to see if we overrun exponent bits
toc
Elapsed time is 0.012423 seconds.
max(abs(y1-y2))
ans = 0
max(abs(y1-y3))
ans = 0
A production algorithm would have to account for overrunning to inf, or underrunning to denormals, etc. But if you are going to go through all the trouble of checking the overrun or underrun, you may as well just use the standard multiply instead because all of that is built in to the chip microcode.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by