This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Arm7 long multiply in C

Hallo to everyone.

My problem is to have the upper 4 Byte result of a multiply.
For example:

unsigned int a=1000000000;//only example value
unsigned int b=10000000000;
unsigned int c;



the result of a*b = 0x8AC7230489E80000

How to get the upper 4 Bytes:
0x8AC72304
How to get the lower 4 Bytes:
0x89E80000

Can anybody help?
I have to programm a super-fast-interrupt-routine for an microcontroller, there is not enough time for floating point.

Parents
  • Define them as long long int (unsigned int if you desire)

    long long int a;
    long long int b;
    long long int c;

    int lower32;
    int upper32;

    c = a * b;

    lower32 = *((u32 *) &c);
    upper32 = *(((u32 *) &c) + 1); // little endian assumed

    Don't worry the the last assignments look ugly. The compiler will easily optimize this to a direct 32-bit fetch of the value.

Reply
  • Define them as long long int (unsigned int if you desire)

    long long int a;
    long long int b;
    long long int c;

    int lower32;
    int upper32;

    c = a * b;

    lower32 = *((u32 *) &c);
    upper32 = *(((u32 *) &c) + 1); // little endian assumed

    Don't worry the the last assignments look ugly. The compiler will easily optimize this to a direct 32-bit fetch of the value.

Children
  • Hello,
    this is perfect but very slow code...

    long long int a;
    long long int b;
    long long int c;
    
    int lower32;
    int upper32;
    
    c = a * b;
    
    lower32 = *((u32 *) &c);
    upper32 = *(((u32 *) &c) + 1); // little endian assumed
    

    i had to replace u32 by unsigned int :-)

    Another side effect:
    Writing

    a=3162277660;// = 0XBC7C871C
    


    gives the result a=0XFFFFFFFFBC7C871C

    a=0xBC7C871C;// = 3162277660
    


    gives the result a=0X00000000BC7C871C