DSPRelated.com
Forums

Compare 2 32bit numbers

Started by Saba Taseer May 30, 2008
Hi all,
I am using adsp 2188 and need to compare two 32bits numbers. I have both the numbers in 4 16 bit regs. I want to know the most optimized way of comparing them, as I can only compare 16 bits at a time (as it is a 16 bit processor). I have a solution in mind but it is taking a lot of extra conditions,

compare the msbs first.
if msbs are equal, (note that the lsbs are supposed to be unsigned if msbs are not zero)
if msbs are zero then simply compare the lsbs)
if msbs are not zero then ?????? (abs or negate wont work) what should be done to make sure they are +ve and give the right result.
thanks and regards,

Saba Taseer
#1 If your crosscompiler is supporting 32 bit then define a 32 bit local variable then do shift opertaion to get 2 16 bit variables.
i.e temp1= (uint32) ((((uint32)MSB) << 0x08 ) | LSB);
temp2= (uint32) ((((uint32)MSB) << 0x08 ) | LSB);
switch(compair(temp1,temp2))
case1:
break;
........
........

#2 If it doesn't support then your idea of compairing msbs and then lsbs can be taken up

i.e switch( compare(temp_msb1,temp_msb2))
case1: compare(temp_lsb1,temp_lsb2)
........
........
Jobs in bosch
Employee Referral/Internal Job Market
Immediate requirement for Project Managers .
The skill-sets required, are as follows:
Embedded Software Development
Qualification: BE/ M Tech in Electronics/ Electrical/ Instrumentation/Telecommunication.
Experience : 4- 10 years in Embedded software development in C, Protocols: CAN, KWP2000.
Knowledge of : Electronic hardware and usage of tools like Debuggers, Emulators , Logic Analyzer , Boot loaders etc.
Familiarity with: Microcontrollers, Firmware, RTOS is expected.

Embedded Software Development
Qualification: BE/B-Tech/ M Tech in Electronics/ Electrical/ Instrumentation/Mechanical/Automobile
Experience : 3-6 years in Embedded software development in C, Assembly, Bus Protocols: IC, SPI, CAN, KWP2000.
Knowledge of the following is required:
Electronic hardware and usage of tools like Debugger, Emulator etc, Control Systems.
Familiarity with Microcontrollers, Firmware, RTOS, Software Architecture is expected.

Send profiles to v...@in.bosch.com
Best Regards,
Vinay Raiker
-----Original Message-----
From: a... [mailto:a...] On Behalf Of Saba Taseer
Sent: Friday, 30. May 2008 2:02 PM
To: a...
Subject: [adsp] Compare 2 32bit numbers

Hi all,
I am using adsp 2188 and need to compare two 32bits numbers. I have both the numbers in 4 16 bit regs. I want to know the most optimized way of comparing them, as I can only compare 16 bits at a time (as it is a 16 bit processor). I have a solution in mind but it is taking a lot of extra conditions,

compare the msbs first.
if msbs are equal, (note that the lsbs are supposed to be unsigned if msbs are not zero)
if msbs are zero then simply compare the lsbs)
if msbs are not zero then ?????? (abs or negate wont work) what should be done to make sure they are +ve and give the right result.
thanks and regards,

Saba Taseer
On Fri, 30 May 2008, Saba Taseer wrote:

> Hi all,
> I am using adsp 2188 and need to compare two 32bits numbers. I have both the numbers in 4 16 bit regs. I want to know the most optimized way of comparing them, as I can only compare 16 bits at a time (as it is a 16 bit processor). I have a solution in mind but it is taking a lot of extra conditions,
>
> compare the msbs first.
> if msbs are equal, (note that the lsbs are supposed to be unsigned if msbs are not zero)
> if msbs are zero then simply compare the lsbs)
> if msbs are not zero then ?????? (abs or negate wont work) what should be done to make sure they are +ve and give the right result.
> thanks and regards,

Howdy Saba,

You are on the right track. First thing to do would be to do a comparison
of the most significant bits of the highest order words. If one is + and
the other is - you don't need to know anything else.

If they both have the same sign, the simplest thing to do next is to make
them both positive, but keep a sign flag. To negate, just complement both
words, then add 1 to lower and the carry bit of that to the upper word.

Then proceed as you describe. If both uppers are equal, the lowers
determine which is larger. If the sign flag was set, the result is
flipped. It should be quick and simple.

Patience, persistence, truth,
Dr. mike