We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello
I am trying to execute a floating point calculation with the 8051. Something like:
Result = Val1 * 0.75 + Val2 * 0.25
The C51 compiler can take that no problem, but it takes a long time to execute, would anyone know a smarter and faster way to execute this kind of code.
Thanks a lot. Laurent
would anyone know a smarter and faster way to execute this kind of code.
sure, use fixed point and calculate like this value *= 3; value /= 4;
The C51 compiler can take that no problem, but it takes a long time to execute
of course it does the '51 is a microcontroller not a microprocessor
If you need floating point calculations done with any speed, choose a suitable processor.
Erik
If you do "Val1 * 75 + Val2 * 25" (all int) you only need one floating point operation to divide it by 100. I'm not a math wizard but if there's some clever shift to do the division you simplified it to int calculations.
Less accurate but: divide Val1 by 4 using shift and multiply by 3, divide Val2 by 4 using shift and add to first result. If you put the result into a float you can check the "fallen off" bits to compensate for the missing accuracy (using a lookup table; last two bits 00->add 0; 01-> add 0.25; 10->add 0.5; 11->add 0.75).
Depending on the accuracy and RAM size you might be able to get away with lookup tables for the whole calculation?
"you only need one floating point operation to divide it by 100."
You almost certainly don't even need that - just remember that your answer is in units of centi-whatevers!
See the last few posts in this Thread: http://www.keil.com/forum/docs/thread8278.asp
would anyone know a smarter and faster way to execute this kind of code
Not really. Because by far the smartest and fastest way to execute this kind of code on a '51 is not to do it. You don't need floating-point for it, and you shouldn't use it.
Ok maybe I'm not smart trying to make calculation with floating point and I shouldn't use it, but what can I do instead? Any idea?
"maybe I'm not smart trying to make calculation with floating point"
Not on an 8051, anyhow!
"I shouldn't use it"
It's usually best avoided on an 8051.
"what can I do instead? Any idea?"
Err... read the suggestions that have already been given!
Scale your calculations so that they can all be done with integer arithmetic; eg, see: ">http://www.keil.com/forum/docs/thread8278.asp
Your 1st suggestion helps, I've tried it and I can see the execution time going smaller. Thank you for that.
Best regards. Laurent
To reduce the time further value/=4 can be achieved by rotating result right by two times. Try it once.
To reduce the time further value/=4 can be achieved by rotating result right by two times I believe the compiler figures that one out by itself. I have seen a divide by a constant pwr of 2 converted to shift in one case. Have a look.
"value/=4 can be achieved by rotating result right"
No!
You mean shifting - not rotating!