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 << 16; // i = f.e. 0x012345
u = ((signed long long) (psensor_c4 * dT)) / 128; // u = 0x900000
line3: Off = (signed long long) 0x900000 + 0x012345; line4: Off = (signed long long) i + u;
line3 gives me as result 0x912345 line4 gives me as result 0xffffffff00912345
My problem is: I have a signed integer with a maximum of 40 Bit length. So I need 64 Bit math. And I tell this the compiler with the hint (signed long long). Why does exactly the same line of code give different results ? I expect the line3 result and don t understand why in line 4 the ffffffff are added for the high word.
Do the experts recommend me to write this in assembler, so that I can be sure that it is done exactly the way I want to have my 64 Bit math. Or are there compiler specialist, who know, how to tell the compiler, that he should add a signed 64 bit value to a 32 bit integer.
Any hints are welcome.
Sorry about the gh. I wanted to say ffffffff01234567, but I hope that some people interpreted this like I wanted. Yes I know, what 2 complement is. AND my line3,4 example both work with positive values, but in the debugger 2 different results are shown.
How to get a SMULL R4,R5,R0,R1 in C is now the question ?
"but in the debugger 2 different results are shown"
Different from what? In what way(s) do they differ?
Show some specific examples!
line 3 shows me: 0000000000912345 line 4 shows me: ffffffff00912345 The ffffffff is the difference.
There have been too many discussions bugs in the debugger when it comes to displaying variables lately.
Assign values to your two input variables. Perform the add. Print out the value of the variables and the value of the result.
The compiler should not be able to produce the result you claim, with the input values you claim.
signed unsigned hex 9511749 9511749 912345 <= expected result of u+i -4285455547 18446744069424096069 ffffffff00912345 <= your result of u+i -4294892731 18446744069414658885 ffffffff00012345 <= your result - u -4285530112 18446744069424021504 ffffffff00900000 <= your result - i 9437184 9437184 900000 <= expected result - u 74565 74565 12345 <= expected result - i But if: i = 0x80012345; <= a negative value u = 0xffffffff80900000ull; <= a negative value You get a sign-extend of i to: 0xffffffff80012345ull; And the sum: -4285455547 18446744069424096069 ffffffff00912345 <= this matches your result
Are you sure that not one of your input variables are what you think they are and not actually negative numbers?
Please post a minimum program (complete source including printout) that gives your results.
Please show the specific code lines, the variable declaraions, and the specific input and output values.
State clearly how you are observing the values.
Show a couple of cases which "work", and a couple which you think "don't work"
Line 3 involves only constants. Chances are that the compiler simply assigns the result to Off. If you know enough about ARM assembler to expect an SMULL instruction, why don't you tell us what the compiler generates instead? All I can tell is that the result seems fine here.
signed long long mult64_32_32 (signed long a, signed long b) { return (signed long long)a * b; // same when casting both operators }
mult64_32_32 PROC MOV r2,r0 SMULL r0,r1,r2,r1 BX lr ENDP
Regards Marcus http://www.doulos.com/arm/
Due to your help I could figure out my problem. It has been not, where I expected the problem. I used for my debugging purposes unfortunately just i (which simply has been declared as int). But that has been the root cause, for all followup failures. All math works fine, if I declare i in my code as unsigned int. (which it should be) I am sorry about such a novice mistake, which causes so much confusion to me.
Thanks again for your goodwill to help me as novice.
The good thing is, hopefully, that you have learned a few things in the process?
Particularly about the handling & representation of signed numbers?