2

I am studying some x86 ASM code and what the code really does, it's my understanding that a power function (x^y) internally works as a logarithm function. By internally I mean the CPU registers.

Why is this? What is the benefit? Is it a benefit that can be replicated and borrowed by other high level languages like C++?

Derek
  • 1,104
  • 13
  • 35
user2348816
  • 532
  • 4
  • 9
  • Can you perhaps show some of this code you're studying? – Nik Bougalis May 09 '13 at 01:37
  • @NikBougalis as I specified my question it's about registers, so I can't provide any code for that, it's more about internal implementations and choices made by CPU designers. – user2348816 May 09 '13 at 01:38
  • 4
    How about specifying the x86 asm instructions you have in mind? You mention `pow`, but that's a C/C++ standard math (or cmath) library call, not an assembly mnemonic. – hardmath May 09 '13 at 01:45
  • 1
    I know you "mentioned" registers. The problem was that your question didn't really make sense and it was difficult to understand what you were, actually, asking. I'm glad you got an answer though. – Nik Bougalis May 09 '13 at 10:16

2 Answers2

6

You should take a look at this MATH,

What the answer says is that the log functions can be achieved through taylor series and a look-up table combined. If there is a look-up table, then it may be hashed and the look-up will be easier than pow which only can be obtained through computation.

so for xy you can write it as

y = log<sub>10</sub> x . 

or

y = (ln x)/(ln 10);

now if there is no log implementation for pow then it should be achieved through Addition chain exponentiation Wiki which requires runtime computation which might take longer than finding log.

EDIT : Thanks to @harold exponentiation can be performed more optimally using Exponentiation by squaring.

Community
  • 1
  • 1
Koushik Shetty
  • 2,146
  • 4
  • 20
  • 31
  • Addition chain exponentiation only works for integer exponents anyway (and can be replaced by exponentiation by squaring, which is fast) – harold May 09 '13 at 07:11
5

See the answer to this question: How to: pow(real, real) in x86

Think back to your logarithm rules: the base of 2 cancels with log2(x) leaving just x^y.

Edit: We have an x86 instruction to calculate each of the components needed.

Community
  • 1
  • 1
Derek
  • 1,104
  • 13
  • 35
  • the answers in that page are about "how" not about "why". – user2348816 May 09 '13 at 01:42
  • 2
    I believe the accepted answer on that page offers an answer for both "how" and "why". Computing 2^(y*log2(x)) is an elegant and simple way to calculate x^y. It is a way to take advantage of the x86 instruction set. – Derek May 09 '13 at 01:46