This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
other/10184: unsigned long long arithmetics incorrect
- From: franord at msn dot com
- To: gcc-gnats at gcc dot gnu dot org
- Date: 21 Mar 2003 14:46:19 -0000
- Subject: other/10184: unsigned long long arithmetics incorrect
- Reply-to: franord at msn dot com
>Number: 10184
>Category: other
>Synopsis: unsigned long long arithmetics incorrect
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Fri Mar 21 14:56:01 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator: franord at msn dot com
>Release: gcc version 2.9-PentiumIII-010221 (2.96+) VxWorks 5.5
>Organization:
>Environment:
>Description:
When arithmetic on two unsigned long long variables (64-bit) are done, and the result is assigned to unsigned long (32-bit var), the arithmetic is only done on 32-bit. This happens even if trying to cast the result to unsigned long long. However, if first assigning the result to an unsigned long long variable, then assigning to unsigned long, correct code is generated.
>How-To-Repeat:
>Fix:
Work-around: Assign arithmetic result to a temporary 64-bit variable first, then cast to 32-bit.
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="gcc unsigned long long error example.txt"
Content-Disposition: inline; filename="gcc unsigned long long error example.txt"
unsigned long long TscRef; /* Reference value of TSC register, set on each timestamp count */
UINT32 sysTimestamp (void)
{
unsigned long long TscVal;
/* Fill 64-bit value into TscVal */
pentiumTscGet64 (&TscVal);
/* Various combinations of casts and no casts at all have been tried */
return (UINT32) (unsigned long long) (TscVal - TscRef);
}
The following code is generated
0060a830 55 PUSH EBP
0060a831 89 e5 MOV EBP, ESP
0060a833 83 ec 18 SUB ESP, 24
0060a836 83 ec 0c SUB ESP, 12
0060a839 8d 45 f8 LEA EAX, [EBP-8]
0060a83c 50 PUSH EAX
0060a83d e8 5e 29 01 00 CALL pentiumTscGet64
0060a842 83 c4 10 ADD ESP, 16
0060a845 8b 55 f8 MOV EDX, [EBP-8]
; Here it is --> Only the lower 32 bits of the 64-bit variable is subtracted
0060a848 2b 15 88 c7 6f 00 SUB EDX, 0x006fc788
0060a84e 89 d0 MOV EAX, EDX
0060a850 eb 00 JMP sysTimestamp + 0x22
0060a852 89 ec MOV ESP, EBP
0060a854 5d POP EBP
0060a855 c3 RET