<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Arm7 long multiply in C</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/20343/arm7-long-multiply-in-c</link><description> 
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</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/144138?ContentTypeID=1</link><pubDate>Thu, 31 Aug 2006 07:49:44 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:88698333-df33-4b94-972f-eaa5b0fc58cf</guid><dc:creator>Robert McNamara</dc:creator><description>&lt;p&gt;&lt;p&gt;
Looks good. I like it. Thanks for posting.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/141650?ContentTypeID=1</link><pubDate>Thu, 31 Aug 2006 07:31:45 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:a65d39e1-e528-426a-a1ba-3ab2d831c632</guid><dc:creator>Dieter Coba</dc:creator><description>&lt;p&gt;&lt;p&gt;
Hi,&lt;br /&gt;
here&amp;#39;s my solution in Assembler:&lt;/p&gt;

&lt;pre&gt;
//*********************************************************************
// A unsigned multiply in ARM Assembler
// Returns the upper 4 byte of a 32*32-bit-multiplication
//*********************************************************************
AREA ?C?bla, CODE, READONLY, ALIGN=2

PUBLIC MUL32x32_ret_upper?A
MUL32x32_ret_upper?A            PROC CODE32

        UMULL   R2,R0,R1,R0     ;R0,R2 := R1*R0
        bx      lr              ; return R0

        ENDP
        END
//*********************************************************************
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/138265?ContentTypeID=1</link><pubDate>Wed, 30 Aug 2006 10:19:01 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:8297fa4f-7c69-42a4-99d4-780a3b0d46d7</guid><dc:creator>Dieter Coba</dc:creator><description>&lt;p&gt;&lt;p&gt;
Hello,&lt;br /&gt;
this is perfect but very slow code...&lt;/p&gt;

&lt;pre&gt;
long long int a;
long long int b;
long long int c;

int lower32;
int upper32;

c = a * b;

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

&lt;p&gt;
i had to replace &lt;i&gt;u32&lt;/i&gt; by &lt;i&gt;unsigned int&lt;/i&gt; :-)&lt;/p&gt;

&lt;p&gt;
Another side effect:&lt;br /&gt;
Writing&lt;/p&gt;

&lt;pre&gt;
a=3162277660;// = 0XBC7C871C
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
gives the result a=&lt;b&gt;0XFFFFFFFFBC7C871C&lt;/b&gt;&lt;/p&gt;

&lt;pre&gt;
a=0xBC7C871C;// = 3162277660
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
gives the result a=&lt;b&gt;0X00000000BC7C871C&lt;/b&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/141649?ContentTypeID=1</link><pubDate>Wed, 30 Aug 2006 09:56:20 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:6acdc4c0-a19f-4095-b776-da7e2089f966</guid><dc:creator>Christoph Franck</dc:creator><description>&lt;p&gt;&lt;pre&gt;
r4 = bl * bl;
&lt;/pre&gt;

&lt;p&gt;
should be&lt;/p&gt;

&lt;pre&gt;
r4 = al * bl;
&lt;/pre&gt;

&lt;p&gt;
and all occurrences of msb/lsb should be msw/lsw.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/138261?ContentTypeID=1</link><pubDate>Wed, 30 Aug 2006 09:11:58 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:8aa57d2c-9355-4e02-9292-a093587a4f5e</guid><dc:creator>Christoph Franck</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;I hope you have a program-example in assembler or C&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Actually, I don&amp;#39;t.&lt;/p&gt;

&lt;p&gt;
I have a copy of the ARM instruction set here, which describes the
MUL (32x32 bit multiplication, least significant 32 bits as result)
and SMMUL (32x32 bit multiplication, most significant 32 bits as
result) assembly instructions.&lt;/p&gt;

&lt;p&gt;
If you don&amp;#39;t want to get your hands dirty doing assembly, there&amp;#39;s
enough examples here already that show how to do it in C using
&lt;b&gt;long long int&lt;/b&gt;s. Of course, this might not be
&lt;b&gt;super-fast&lt;/b&gt;, depending on the compiler. Maybe the compiler is
smart enough to recognize something along the lines of&lt;/p&gt;

&lt;pre&gt;
unsigned long long r;
unsigned long a, b;
...
r = (unsigned long long) a * b;
&lt;/pre&gt;

&lt;p&gt;
as a single 32x32 multiplication, maybe it is not. Take your
chances.&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;as i havent used unions before,&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Maybe you need to learn to walk before trying to run a marathon
?&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;i&amp;#39;am afraid of getting large and slow code.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Since you&amp;#39;re not familiar with the internal architecture of ARM,
how do you know that something will generate large and slow code
?&lt;/p&gt;

&lt;p&gt;
Of course, you can also try to do everything with 32 bit integers,
using some elementary school-level maths:&lt;/p&gt;

&lt;p&gt;
(off the top of my head)&lt;/p&gt;

&lt;pre&gt;
unsigned int a, b, ah, al, bh, bl, r1, r2, r3, r4, msw, lsw;
...
ah = a &amp;gt;&amp;gt; 16;
al = a &amp;amp; 0x0000FFFF;
bh = b &amp;gt;&amp;gt; 16;
bl = b &amp;amp; 0x0000FFFF;

r1 = ah * bh;
r2 = ah * bl;
r3 = al * bh;
r4 = bl * bl;

msb = r1 + r2 &amp;gt;&amp;gt; 16 + r3 &amp;gt;&amp;gt; 16;
r2 = r2 &amp;lt;&amp;lt; 16;
r3 = r3 &amp;lt;&amp;lt; 16;
lsb = r2 + r3;
if((lsb &amp;lt; r2))
{  msb = msb + 1;  }
lsb += r4;
if((lsb &amp;lt; r4))
{  msb = msb + 1;  }
&lt;/pre&gt;

&lt;p&gt;
Do you think this kludge is going to be faster (on an ARM) than
using long long ints ?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/141651?ContentTypeID=1</link><pubDate>Wed, 30 Aug 2006 08:56:35 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:86cf675b-cdd6-448b-8c4c-efd109f96411</guid><dc:creator>Dieter Coba</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;b&gt;&lt;br /&gt;
Any particular reason you don&amp;#39;t like the examples provide
thusfar?&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
as i havent used unions before, i&amp;#39;am afraid of getting large and slow
code.&lt;/p&gt;

&lt;p&gt;
I will test it tomorrow, i promise :-)&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/138262?ContentTypeID=1</link><pubDate>Wed, 30 Aug 2006 08:51:03 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:78a10a13-463c-4c6b-a279-7b8c9a8f2670</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&amp;quot;I hope you have a program-example in assembler or C&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Any particular reason you don&amp;#39;t like the examples provide
thusfar?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/130075?ContentTypeID=1</link><pubDate>Wed, 30 Aug 2006 08:46:59 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:56d654b7-2e73-4cb8-b02e-a66f4ff5fcc5</guid><dc:creator>Dieter Coba</dc:creator><description>&lt;p&gt;&lt;p&gt;
I hope you have a program-example in assembler or C&lt;br /&gt;
for me, how to get this upper 4byte of the multiplikation (int *
int).&lt;/p&gt;

&lt;p&gt;
Of cause i know&lt;br /&gt;
&lt;b&gt;The Compiler is allways right,&lt;br /&gt;
i&amp;#39;am allways doing wrong...&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
but how to change to the other side ;-) ;-) ;-)&lt;/p&gt;

&lt;p&gt;
:-)&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/134809?ContentTypeID=1</link><pubDate>Wed, 30 Aug 2006 08:45:17 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:9ee0c1bb-0f9f-4dd0-8275-6a30e0c3bc19</guid><dc:creator>Robert McNamara</dc:creator><description>&lt;p&gt;&lt;p&gt;
Define them as long long int (unsigned int if you desire)&lt;/p&gt;

&lt;p&gt;
long long int a;&lt;br /&gt;
long long int b;&lt;br /&gt;
long long int c;&lt;/p&gt;

&lt;p&gt;
int lower32;&lt;br /&gt;
int upper32;&lt;/p&gt;

&lt;p&gt;
c = a * b;&lt;/p&gt;

&lt;p&gt;
lower32 = *((u32 *) &amp;amp;c);&lt;br /&gt;
upper32 = *(((u32 *) &amp;amp;c) + 1); // little endian assumed&lt;/p&gt;

&lt;p&gt;
Don&amp;#39;t worry the the last assignments look ugly. The compiler will
easily optimize this to a direct 32-bit fetch of the value.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/123378?ContentTypeID=1</link><pubDate>Wed, 30 Aug 2006 08:37:38 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:6458c372-df94-4872-aeff-5a81537b48c7</guid><dc:creator>Christoph Franck</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;but i get wrong result or&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
No, the result is not wrong, what you&amp;#39;re doing is wrong. You&amp;#39;re
expecting the compiler to automatically expand the result to 64 bits,
which it won&amp;#39;t do.&lt;/p&gt;

&lt;p&gt;
Either you do it in assembly (ARM has very nice multiply
instructions that can calculate the upper part, the lower part, or
all 64 bits of a 32 bit * 32 bit multiplication), or you tell the
compiler that it needs to use a 64 bit integer datatype. I wouldn&amp;#39;t
expect it to come up with optimal (&lt;b&gt;super-fast&lt;/b&gt;) code
however.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/123377?ContentTypeID=1</link><pubDate>Wed, 30 Aug 2006 08:33:20 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:8931a233-91c3-43d6-aaae-791bda450eb1</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;p&gt;
Regarding the shift operator:&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;&amp;quot;If the value of the right operand is negative or is greater
than or equal to the width of the promoted left operand, the behavior
is undefined.&amp;quot;&lt;/i&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/118883?ContentTypeID=1</link><pubDate>Wed, 30 Aug 2006 08:25:37 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:a63583e9-c861-461c-91a1-fd6b1060612f</guid><dc:creator>Dieter Coba</dc:creator><description>&lt;p&gt;&lt;p&gt;
Hi, i tried several version of something like&lt;/p&gt;

&lt;pre&gt;
unsigned int a=3162277661;
unsigned int b=3162277660;
unsigned int c;
c=((a*b)&amp;gt;&amp;gt;32)&amp;amp;0xFFFFFFFF;
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
but i get wrong result or&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;
ARM COMPILER V2.40c, ADIDEMO\LONG_LONG_7.C: &amp;#39;&amp;gt;&amp;gt;&amp;#39;: out of range
shift factor has been truncated&lt;br /&gt;
*** WARNING C200 IN LINE 16 OF ADIDEMO\LONG_LONG_7.C: &amp;#39;&amp;gt;&amp;gt;&amp;#39;: out
of range shift factor has been truncated&lt;br /&gt;&lt;/i&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/123379?ContentTypeID=1</link><pubDate>Wed, 30 Aug 2006 07:51:24 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:9487ab61-507b-45b3-ae33-c8fb55e1df87</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;p&gt;
Quickly typed, not tested :-(&lt;/p&gt;

&lt;p&gt;
s/multplicand/multiplicand/&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/112291?ContentTypeID=1</link><pubDate>Wed, 30 Aug 2006 07:45:11 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:b346c53f-004c-404a-ad66-bc2d0afeacf7</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;pre&gt;
union U64_ {
    unsigned long long  ull;
    unsigned long       ul[2];
};

unsigned long multiplier;
unsigned long multiplicand;
union U64_    product;
unsigned long product_msl;
unsigned long product_lsl;

product.ull = (unsigned long long)multplicand * multiplier;
product_msl = product.ul[1];    /* Assuming little-endian */
product_lsl = product.ul[0];    /* Assuming little-endian */
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/98397?ContentTypeID=1</link><pubDate>Wed, 30 Aug 2006 07:36:42 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:5bd88164-e7d5-4961-87e9-991be5de752c</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;p&gt;
So extract the MSLong or LSLong of the 64-bit result. Shifting and
masking, or unions work on long longs too.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/74322?ContentTypeID=1</link><pubDate>Wed, 30 Aug 2006 07:31:59 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:89fa8ef5-2450-4142-8e0a-288a7be06c26</guid><dc:creator>Dieter Coba</dc:creator><description>&lt;p&gt;&lt;p&gt;
Hello,&lt;/p&gt;

&lt;p&gt;
long long may be a way, but i only need a 32-bit result, the upper
4 byte &lt;b&gt;or&lt;/b&gt; the lower 4 byte.&lt;br /&gt;
Maybee someone has a programm-example :-)&lt;/p&gt;

&lt;p&gt;
Regards&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Arm7 long multiply in C</title><link>https://community.arm.com/thread/46697?ContentTypeID=1</link><pubDate>Wed, 30 Aug 2006 07:25:08 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:b116d64f-db71-455a-a46c-37d05145c20f</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;p&gt;
You&amp;#39;d want one of the long long integer types then.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>