473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

mulitplication in embedded(integer) system

can anyone help here?

I am working on an embedded system with a C-like programming language,
therefore floating point operation is not allowed (because it is time consuming),
and floating point variable has to be represented by integer variable, with F stands for the length of bits representing the fractional part.

e.g. value 4.5 with F=5 is represent by 10010000 (where the higher 3 bits representing integer value, and the lower 5 bits representing fractional value)

now I need to write a function for the multiplication operation of two variables A and B (note they can have different F values: Fa and Fb ), and the result C with the F value Fc.

my code is given as followed and it works. But the problem is that this code still takes too long time to run on an embedded system (multiplication operations dominate the activitives ). Can any one provide a better algorithm ?

------------------------------------------

int16_t multiInt(int16_t a, uint8_t Fa, int16_t b, uint8_t Fb, uint8_t Fr)
{
// assume the maximum amount of available digits for integer variable is 16
//
// Fa --- lenght of fractional bit of value "a"
// Fb --- lenght of fractional bit of value "b"
// Fr --- lenght of fractional bit of result/output "res"

uint8_t isNeg=0, stepB;
int16_t res; // result/output
uint8_t Ha, La, Hb, Lb; // higher (and lower) significant bits for a and b
uint16_t HaHb, LaLb,LH; // temporal value
uint16_t Hr, Lr; // higher (and lower) significant bits for the result/output

if (a<0)
{
a=-a;
isNeg ++;
}
if (b<0)
{
b=-b;
isNeg ++;
}
stepB=Fa+Fb - Fr


Ha = a>>8;
La = a-(Ha<<8);
Hb = b>>8;
Lb = b-(Hb<<8);

HaHb = Ha*Hb;
LH = La*Hb + Lb*Ha;
LaLb = La*Lb;

Hr = HaHb + (LH>>8);
Lr = LaLb + (LH<<8);

if ((Hr>>stepB)>0) //if overflow
res = 0x7fff;
else
res = (Hr<<(16-stepB)) + (Lr>>stepB)

return res*(1-2*(isNeg%2));
}
Sep 11 '08 #1
6 3368
newb16
687 512MB
How long is 'long" ( machine cycles) ? Does the system have hardware multiplication? (16*16->16, or 8*8->16 that is mostly like can't be utilized by compiler )? Did you examine generated assembly output for this code? What is uint16>>8 ( <<8) operation like - does it fetch higher byte or really shift 8 times by 1 bit?

And this one
if ((Hr>>stepB)>0)

is better to be substituted with something like
if((Hr & mask) != 0 )
where mask is from array of 16 pre-calculated values of 'all N highest bits set to 1' for different N.

Regarding 2*(isNeg%2)) --
Check that compiler is smart enough to optimize 2*x and x%2 to shift and
x&1 , if not, rewrite.

If MCU has 16*16->32 HW multiply, it may be better to rewrite it utilizing this feature.
Sep 11 '08 #2
Many thanks for the answer !!!

How long is 'long" ( machine cycles) ? Does the system have hardware multiplication? (16*16->16, or 8*8->16 that is mostly like can't be utilized by compiler )?
"long" is my objective meaning, what I am looking for is a fast (as fast as possible)
algorithm/code to do the multiplication. The system i am working on is the TelosB sensor mote, which so far as i know only has very limited computation power and ability. it can only handle up to 16-bit operations.

Did you examine generated assembly output for this code? What is uint16>>8 ( <<8) operation like - does it fetch higher byte or really shift 8 times by 1 bit?
"uint16_t" stands for unsign integer with 16 bits;

>> and << basically do the "shift" job:

by "uint16>>8 " , it results in a 16-bit integer with the lower 8 bits equal to the previous higher 8 bits, and the new higher 8 bits are filled with zero.
by "uint16<<8 " , it results in a 16-bit integer with the higher 8 bits equal to the previous lower 8 bits, and the new lower 8 bits are filled with zero.



And this one
if ((Hr>>stepB)>0)
is better to be substituted with something like
if((Hr & mask) != 0 )
where mask is from array of 16 pre-calculated values of 'all N highest bits set to 1' for different N.
great !!! I will try this out. (i am new to this integer-operation programming)


Regarding 2*(isNeg%2)) --
Check that compiler is smart enough to optimize 2*x and x%2 to shift and
x&1 , if not, rewrite.
I will check it. thanks ...

If MCU has 16*16->32 HW multiply, it may be better to rewrite it utilizing this feature.
unfortunately, the processor only support 16-bit operations .
Sep 11 '08 #3
donbock
2,426 Expert 2GB
I am working on an embedded system with a C-like programming language, therefore floating point operation is not allowed (because it is time consuming), and floating point variable has to be represented by integer variable, with F stands for the length of bits representing the fractional part.

e.g. value 4.5 with F=5 is represent by 10010000 (where the higher 3 bits representing integer value, and the lower 5 bits representing fractional value)

now I need to write a function for the multiplication operation of two variables A and B (note they can have different F values: Fa and Fb ), and the result C with the F value Fc.
What you're doing is called "fixed-point" arithmetic. You might want to google "fixed-point" or "fixed-point multiplication" to get an overview of the subject. Your example is a little more complicated because the fixed point isn't really fixed (that is, F varies).

Another approach you might look into [if you were willing to consider a complete rewrite] is "rational" arithmetic. In this case, all numbers are expressed by two values, a numerator and a denominator. Again, you google the term for an overview. You could model your fixed-point arithmetic as rational arithmetic, where the denominator is 2^F.

I don't know that any of this will help; I'm just making some suggestions for where you can look for background information.

Cheers,
donbock
Sep 11 '08 #4
newb16
687 512MB
"uint16_t" stands for unsign integer with 16 bits;

>> and << basically do the "shift" job:
I am... uhm... aware. What I was asking is whether MCU in question (+compiler used ) generate fast code for such shift operation - is there constant-time operation for shift-by-8-bits, or direct move from higher byte to lower byte? Or machine code generated is dumb and shifts it 8 times by 1 bit?

if ((Hr>>stepB)>0)
is better to be substituted with something like ...
Edit - this is valid if there is no constant-time multibit shift operation.

unfortunately, the processor only support 16-bit operations .
That's strange. Only 16*16 -> 16 multiply?
Sep 11 '08 #5
donbock
2,426 Expert 2GB
Did you examine generated assembly output for this code?
For example, the -S command line switch tells gcc to stop after compilation and leave the assembly source file present so you can examine it. Whatever compiler you're using, there should be something equivalent.
Cheers,
donbock
Sep 11 '08 #6
Thank you all for the tips given above.

I have checked the specification of the hardware of the embedded system.
The MCU does be able to handle multiplication between two 16-bit variables, even though the processor is a 16bit processor.

Now it is much faster than before by utilizing this feature.
And I am satisfied with the result, so is my boss !!!

Thank you all for the inspirations, this is a very nice forum .
Oct 7 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Stefan Arentz | last post by:
Howdy. I'm looking at embedding python in a little embedded system. The device (a linksys wrt54g router, popular hack object since it runs linux), has limited resources. Just 4MB flash and 16MB...
5
by: Confused User | last post by:
I am working on device that utilizes a Motorola 68HC16 microcontroller. I am using an old unsupported piece of crap Whitesmith's / Intermetrics / Tasking compiler. The embedded compiler business...
41
by: Baron Samedi | last post by:
I want to produce a piece of software for embedded systems, generally telecoms based, mostly running on ARM processors, but I can't guarantee that, of course. My software should work along with...
1
by: duedony | last post by:
Dear All: Do you know how to analysis C++ memory usage in embedded system ? I want to know how many heap need for C++ objects creation in the system? I use arm c++ library.
2
by: ykokloon | last post by:
Hi, I’m trying to implement Unicode in an embedded system, but do I need to apply or pay some licensing fee in order to implement Unicode in a system that will be sold in the consumer market? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.