• Welcome to Valhalla Legends Archive.
 

[C++] Inline Return

Started by R.a.B.B.i.T, March 10, 2005, 10:36 AM

Previous topic - Next topic

Adron

Quote from: Mephisto on March 14, 2005, 08:56 AM
I was just curious though, why are you using inline assembler to create a power evaluation function?  Why not just use plain C++...

unsigned long EvaluatePower(unsigned long base, unsigned long exponent)
{
if(exponent == 0)
return 1;
else if(exponent == 1)
return base;
else {
unsigned long value = 0;
unsigned long tracker = base;

for(unsigned int i = 1; i < exponent; i++) {
value = base * tracker;
tracker = value;
}

return value;
}
}


Note: Does not support negative exponents.  If you want this supported you'll have to change the values to type signed and come up with some thinking as to how to process a negative exponent (e.g. taking the recipricol of the evaluated power).

Well, that loop will be much slower than his power2 function for calculating numbers times powers of two. It would be much easier to just define it:


unsigned int power2(int num, int power)
{
return num << power;
}


That is, if you at all bother to make it a subroutine :)

For a generic exponentiation function, you'd probably want to use floating point math and the built-in functions, since you'll run out of number range quickly when exponentiating ints.

R.a.B.B.i.T

I could switch over to unsigned double if I need to, but I don't.

The only reason I was doing this in assembly in the first place was to prove that I could prove to my friend that it's possible to write assembly code in MSVC++.  The only part I didn't quite understand was the return, but that's because the assembly I learned with output to some LED's, not a console.

On top of that, I like assembly :)

Mephisto

Quote from: Adron on March 14, 2005, 05:11 PM
Quote from: Mephisto on March 14, 2005, 08:56 AM
I was just curious though, why are you using inline assembler to create a power evaluation function?  Why not just use plain C++...

unsigned long EvaluatePower(unsigned long base, unsigned long exponent)
{
if(exponent == 0)
return 1;
else if(exponent == 1)
return base;
else {
unsigned long value = 0;
unsigned long tracker = base;

for(unsigned int i = 1; i < exponent; i++) {
value = base * tracker;
tracker = value;
}

return value;
}
}


Note: Does not support negative exponents.  If you want this supported you'll have to change the values to type signed and come up with some thinking as to how to process a negative exponent (e.g. taking the recipricol of the evaluated power).

Well, that loop will be much slower than his power2 function for calculating numbers times powers of two. It would be much easier to just define it:


unsigned int power2(int num, int power)
{
return num << power;
}


That is, if you at all bother to make it a subroutine :)

For a generic exponentiation function, you'd probably want to use floating point math and the built-in functions, since you'll run out of number range quickly when exponentiating ints.

Off hand, I don't think base << exponent would calculate the power.  Not sure what you meant by if you bother making it a subroutine.  :p

Adron

Quote from: Mephisto on March 14, 2005, 05:43 PM
Quote from: Adron on March 14, 2005, 05:11 PM
Well, that loop will be much slower than his power2 function for calculating numbers times powers of two. It would be much easier to just define it:


unsigned int power2(int num, int power)
{
return num << power;
}


That is, if you at all bother to make it a subroutine :)

For a generic exponentiation function, you'd probably want to use floating point math and the built-in functions, since you'll run out of number range quickly when exponentiating ints.

Off hand, I don't think base << exponent would calculate the power.  Not sure what you meant by if you bother making it a subroutine.  :p

The routine this thread is about doesn't calculate powers though. His power2 routine calculates num * 2**power. If he were to use your routine to calculate num * 2**power, he'd be doing


#define power2(num, power)  ((num)*EvaluatePower(2, power))


At least to me it's rather obvious that that's much slower than just shifting the number, and most people don't make a subroutine for shifting a number since there's a shifting operator available :)




Mephisto

Quote from: Adron on March 14, 2005, 06:10 PM
Quote from: Mephisto on March 14, 2005, 05:43 PM
Quote from: Adron on March 14, 2005, 05:11 PM
Well, that loop will be much slower than his power2 function for calculating numbers times powers of two. It would be much easier to just define it:


unsigned int power2(int num, int power)
{
return num << power;
}


That is, if you at all bother to make it a subroutine :)

For a generic exponentiation function, you'd probably want to use floating point math and the built-in functions, since you'll run out of number range quickly when exponentiating ints.

Off hand, I don't think base << exponent would calculate the power.  Not sure what you meant by if you bother making it a subroutine.  :p

The routine this thread is about doesn't calculate powers though. His power2 routine calculates num * 2**power. If he were to use your routine to calculate num * 2**power, he'd be doing


#define power2(num, power)  ((num)*EvaluatePower(2, power))


At least to me it's rather obvious that that's much slower than just shifting the number, and most people don't make a subroutine for shifting a number since there's a shifting operator available :)





Oh, I didn't even realize what his function was doing.  I just assumed by the name of it he was trying to calculate power expressions generically.  My bad.