EmbeddedRelated.com
Forums

IAR: How to divide/modulo efficiently (DivModNNX)

Started by Dirk Reymann May 12, 2009
Dear all,

anyone got a clue how to make the compiler use both return values
from its DivModNNX calls?

Right now code like "digit = val%10; val/" makes the compiler
call the division function twice, each time throwing one of the
results away.
How do I get it to call the function only once and use both results
straight?

Thanks for your ideas
Dirk
PS: I already got a workaround "val_=val/10; digit=val-10*val_;
val=val_;" but I'm looking for something elegant.

Beginning Microcontrollers with the MSP430

On Tue, 12 May 2009 23:53:12 +0200, you wrote:

>Dear all,
>
>anyone got a clue how to make the compiler use both return values
>from its DivModNNX calls?
>
>Right now code like "digit = val%10; val/" makes the compiler
>call the division function twice, each time throwing one of the
>results away.
>How do I get it to call the function only once and use both results
>straight?
>
>Thanks for your ideas
>Dirk
>PS: I already got a workaround "val_=val/10; digit=val-10*val_;
>val=val_;" but I'm looking for something elegant.

If my very cursory glance didn't completely lead me astray, I've
already posted routines in the Files section that provide both
quotient and remainder for divisions.

Jon
It is early morning and I am just heating up my brain, so may be I
don't get the point.
There shall be some reason that makes you do not want to use the ldiv
function, is it so?
regards
A_M

At 11:53 PM 5/12/2009, you wrote:
>Dear all,
>
>anyone got a clue how to make the compiler use both return values
>from its DivModNNX calls?
>
>Right now code like "digit = val%10; val/" makes the compiler
>call the division function twice, each time throwing one of the
>results away.
>How do I get it to call the function only once and use both results
>straight?
>
>Thanks for your ideas
>Dirk
>PS: I already got a workaround "val_=val/10; digit=val-10*val_;
>val=val_;" but I'm looking for something elegant.

> There shall be some reason that makes you do not want to use the ldiv
> function, is it so?

Yes there is.
The whole thing is inside a template member function of a class
template. So in this situation I have no idea what the type
of the variable might be. I let the compiler decide what way
to do the division and modulo in order to always get the right
algorithm.
This works actually quite fine. The routine is close to the
optimum for any of the (POD) build-in types whether it is signed or
unsigned.
It would even work with custom data types as long as they provide
"operator/(int)".

Right now I'm looking for improvements to make the routine
really "beautiful". I just don't like this "divide by ten and then
muliply again" hack.

But anyway, maybe I should also have a closer look at div()/ldiv().

Regards
Dirk

template
template
MyClass::myfunction(ANY_TYPE val)
{
....
// not beautiful
ANY_TYPE val_ = val/10;
digit = val - 10 * val_;
val = val_;
....
// not efficient
digit = val%10;
val /= 10;
....
}