Pozz said:

I have a simple (16-bit) embedded platform and I have to make

the following calculations.

unsigned int

func(unsigned int adc, unsigned int pwr, unsigned int pt, double exp) {

return (unsigned int)(pow((double)adc, exp) *

(double)pwr / pow((double)pt, exp));

}

adc and pt parameters are in the range 0..1023,

exp is in the range 1.00-3.00, and

pwr can be 50-10000.

I have to make another restriction. pt and adc parameters could be

in the range 0..1023, but I'm using pt=800.

At the moment I don't need other values for pt, but I'm interested to

generalize this calculation with different values

for pt in a small range around 800 (for example, 700-900).

Could you state what in the context is constant, or could be subject to

one-time precomputation, and what is variable (like, I guess, ADC?)

Also: does your environment has powf, which is to float what pow

is to double? It might be much faster.

If pt is constant we can save a little with

#define pt 800

unsigned func(unsigned adc, unsigned pwr, double exp) {

return pwr * pow( (1./pt) * adc, exp );

}

Similarly, if pt is a runtime parameter that seldom vary, we can do

double ginvpt;

// call when pt changes

void reparameterize( unsigned pt ) {

ginvpt = 1./pt;

}

unsigned func(unsigned adc, unsigned pwr, double exp) {

return pwr * pow( ginvpt * adc, exp );

}

We would do significantly better, including getting rid of pow, and perhaps

of double or float, if exp is constant (or seldom vary); and avoid the

final multiplication, and I guess double altogether, if pwr is constant

(or seldom vary).

Francois Grieu