<?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>64 bit math novice question</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/26261/64-bit-math-novice-question</link><description> 
Hello, 

 
I am new and I would highly appreciate help for a 64 Bit math
question: 
signed long long Off = 0; 
signed long long u = 0; 
unsigned int psensor_c2 = 0; 
unsigned int psensor_c4 = 0; 
signed int dT = 0; 
int i = 0; 

 
i = psensor_c2 &amp;lt;&amp;lt;</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: 64 bit math novice question</title><link>https://community.arm.com/thread/139757?ContentTypeID=1</link><pubDate>Wed, 24 Nov 2010 03:22:51 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:f03ad618-a512-4c2c-b498-c63aa6528602</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
The good thing is, hopefully, that you have learned a few things
in the process?&lt;/p&gt;

&lt;p&gt;
Particularly about the handling &amp;amp; representation of signed
numbers?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: 64 bit math novice question</title><link>https://community.arm.com/thread/130709?ContentTypeID=1</link><pubDate>Wed, 24 Nov 2010 03:09:58 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:1b1d28b8-38a4-4cd3-8337-5f618acfeae8</guid><dc:creator>Uwe Renschler</dc:creator><description>&lt;p&gt;&lt;p&gt;
Due to your help I could figure out my problem.&lt;br /&gt;
It has been not, where I expected the problem.&lt;br /&gt;
I used for my debugging purposes unfortunately just i (which simply
has been declared as int).&lt;br /&gt;
But that has been the root cause, for all followup failures.&lt;br /&gt;
All math works fine, if I declare i in my code as unsigned int.
(which it should be)&lt;br /&gt;
I am sorry about such a novice mistake, which causes so much
confusion to me.&lt;/p&gt;

&lt;p&gt;
Thanks again for your goodwill to help me as novice.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: 64 bit math novice question</title><link>https://community.arm.com/thread/126516?ContentTypeID=1</link><pubDate>Wed, 24 Nov 2010 02:04:29 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:6a862679-301a-4970-814b-13187b203be7</guid><dc:creator>Marcus Harnisch</dc:creator><description>&lt;p&gt;&lt;p&gt;
Line 3 involves only constants. Chances are that the compiler
simply assigns the result to &lt;i&gt;Off&lt;/i&gt;. If you know enough about ARM
assembler to expect an SMULL instruction, why don&amp;#39;t you tell us what
the compiler generates instead? All I can tell is that the result
seems fine here.&lt;/p&gt;

&lt;pre&gt;
signed long long mult64_32_32 (signed long a, signed long b)
{
    return (signed long long)a * b; // same when casting both operators
}
&lt;/pre&gt;

&lt;p&gt;
&lt;/p&gt;

&lt;pre&gt;
mult64_32_32 PROC
        MOV      r2,r0
        SMULL    r0,r1,r2,r1
        BX       lr
        ENDP
&lt;/pre&gt;

&lt;p&gt;
Regards&lt;br /&gt;
Marcus&lt;br /&gt;
&lt;a href="http://www.doulos.com/arm/"&gt;http://www.doulos.com/arm/&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: 64 bit math novice question</title><link>https://community.arm.com/thread/139755?ContentTypeID=1</link><pubDate>Wed, 24 Nov 2010 01:47:09 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:eaba51c1-5bf3-445b-887d-d019eeb9e80d</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
Please show the specific code lines, the variable declaraions, and
the specific input and output values.&lt;/p&gt;

&lt;p&gt;
State clearly how you are observing the values.&lt;/p&gt;

&lt;p&gt;
Show a couple of cases which &amp;quot;work&amp;quot;, and a couple which you think
&amp;quot;don&amp;#39;t work&amp;quot;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: 64 bit math novice question</title><link>https://community.arm.com/thread/136330?ContentTypeID=1</link><pubDate>Wed, 24 Nov 2010 01:45:05 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:f6f1e66e-d337-4c69-bffa-2ebcc45cf840</guid><dc:creator>Per Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
There have been too many discussions bugs in the debugger when it
comes to displaying variables lately.&lt;/p&gt;

&lt;p&gt;
Assign values to your two input variables.&lt;br /&gt;
Perform the add.&lt;br /&gt;
Print out the value of the variables and the value of the result.&lt;/p&gt;

&lt;p&gt;
The compiler should not be able to produce the result you claim,
with the input values you claim.&lt;/p&gt;

&lt;pre&gt;
     signed             unsigned                 hex
    9511749              9511749              912345  &amp;lt;= expected result of u+i
-4285455547 18446744069424096069    ffffffff00912345  &amp;lt;= your result of u+i
-4294892731 18446744069414658885    ffffffff00012345  &amp;lt;= your result - u
-4285530112 18446744069424021504    ffffffff00900000  &amp;lt;= your result - i
    9437184              9437184              900000  &amp;lt;= expected result - u
      74565                74565               12345  &amp;lt;= expected result - i
But if:
    i = 0x80012345;              &amp;lt;= a negative value
    u = 0xffffffff80900000ull;   &amp;lt;= a negative value
You get a sign-extend of i to:
    0xffffffff80012345ull;
And the sum:
-4285455547 18446744069424096069    ffffffff00912345  &amp;lt;= this matches your result
&lt;/pre&gt;

&lt;p&gt;
Are you sure that not one of your input variables are what you
think they are and not actually negative numbers?&lt;/p&gt;

&lt;p&gt;
Please post a minimum program (complete source including printout)
that gives your results.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: 64 bit math novice question</title><link>https://community.arm.com/thread/130708?ContentTypeID=1</link><pubDate>Wed, 24 Nov 2010 01:21:42 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:d9cc55ce-b6ab-41d8-ad8a-f5b6c51f4662</guid><dc:creator>Uwe Renschler</dc:creator><description>&lt;p&gt;&lt;p&gt;
line 3 shows me: 0000000000912345&lt;br /&gt;
line 4 shows me: ffffffff00912345&lt;br /&gt;
The ffffffff is the difference.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: 64 bit math novice question</title><link>https://community.arm.com/thread/126515?ContentTypeID=1</link><pubDate>Wed, 24 Nov 2010 01:11:44 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:4a45a762-3005-4e28-8692-2cce5641ddb6</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&amp;quot;but in the debugger 2 different results are shown&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Different from what? In what way(s) do they differ?&lt;/p&gt;

&lt;p&gt;
Show some specific examples!&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: 64 bit math novice question</title><link>https://community.arm.com/thread/116139?ContentTypeID=1</link><pubDate>Wed, 24 Nov 2010 00:29:57 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:6e2e0a3b-db6e-43d1-bd91-018c44179000</guid><dc:creator>Uwe Renschler</dc:creator><description>&lt;p&gt;&lt;p&gt;
Sorry about the gh. I wanted to say ffffffff01234567, but I hope
that some people interpreted this like I wanted.&lt;br /&gt;
Yes I know, what 2 complement is.&lt;br /&gt;
AND my line3,4 example both work with positive values, but in the
debugger 2 different results are shown.&lt;/p&gt;

&lt;p&gt;
How to get a SMULL R4,R5,R0,R1 in C is now the question ?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: 64 bit math novice question</title><link>https://community.arm.com/thread/104404?ContentTypeID=1</link><pubDate>Wed, 24 Nov 2010 00:03:40 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:1f175be3-e70f-4850-a4e8-b13cfba24daf</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
That result would, indeed, be very strange - since &amp;#39;g&amp;#39; and &amp;#39;h&amp;#39; are
not valid hex digits!&lt;/p&gt;

&lt;p&gt;
Do you understand how negative values are stored - using 2&amp;#39;s
complement form?&lt;/p&gt;

&lt;p&gt;
As Per said, this is why you get the leading fff... for a negative
number.&lt;/p&gt;

&lt;p&gt;
Your debugger seems to be displaying the value as &amp;quot;raw&amp;quot; hex,
rather than interpreting it as a signed decimal value.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: 64 bit math novice question</title><link>https://community.arm.com/thread/78820?ContentTypeID=1</link><pubDate>Tue, 23 Nov 2010 23:42:52 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:83975ad5-0888-4789-84fd-db1dd2739385</guid><dc:creator>Uwe Renschler</dc:creator><description>&lt;p&gt;&lt;p&gt;
I am using the Keil tools and I never had any doubt on the values,
which are shown to me using the debugger.&lt;br /&gt;
I am also aware that I didn t show everything so maybe I change my
question.&lt;br /&gt;
I know what I want as a result in assembler:&lt;/p&gt;

&lt;p&gt;
SMULL R4,R5,R0,R1 ; which means I want a 64 Bit result from
multiplication of 2 signed integers. (R0 and R1)&lt;/p&gt;

&lt;p&gt;
In C I say something like:&lt;br /&gt;
signed integer variable0;&lt;br /&gt;
signed integer variable1;&lt;br /&gt;
signed long long 64Bitresult1;&lt;/p&gt;

&lt;p&gt;
Then I do a:&lt;/p&gt;

&lt;p&gt;
64Bitresult1 = (signed long long) variable0 * variable1;&lt;/p&gt;

&lt;p&gt;
Then I have my mouse on the 64Bitresult1 and wonder why there is a
ffffffffabcdefgh result.&lt;/p&gt;

&lt;p&gt;
So my question is:&lt;br /&gt;
What are the magic words in C, which make the compiler to generate a
SMULL R4,R5,R0,R1 for me ?&lt;br /&gt;
What compiler hints are necessary, that I get my C construct
translated into SMULL R4,R5,R0,R1 ?&lt;/p&gt;

&lt;p&gt;
I hope I have been more clear now. Thanks for your help.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: 64 bit math novice question</title><link>https://community.arm.com/thread/58910?ContentTypeID=1</link><pubDate>Tue, 23 Nov 2010 13:13:43 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e2d34db7-2c47-43dc-a9ab-602063cf9bb0</guid><dc:creator>Per Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
0xffffffff00912345 is the same as&lt;br /&gt;
unsigned 18446744069424096069&lt;br /&gt;
signed -4285455547&lt;/p&gt;

&lt;p&gt;
But you are not showing us everything.&lt;/p&gt;

&lt;p&gt;
i can&amp;#39;t be 0x12345 if you created it by shifting something else 16
steps left. The low four digits must be zero from the shift.&lt;/p&gt;

&lt;p&gt;
When computing u, you are playing with signed and unsigned, but
not letting us see what values you use - if you got a sign extend
because the high but of a signed entity was set.&lt;/p&gt;

&lt;p&gt;
Printing a negative value with unsigned formatting results in a
printed number that starts with the high bit(s) set.&lt;/p&gt;

&lt;p&gt;
You are not showing if you are using any formatted printing, or if
you are taking the variable values from the debugger.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>