LADD and LSUB in XC ??

Technical questions regarding the XTC tools and programming with XMOS.
sivakumar
Member
Posts: 11
Joined: Tue Aug 02, 2011 3:24 pm

Post by sivakumar »

The long long constant literals need to have the LL suffix or they will be truncated to int.
eg: result = 8589934592ll;

sprintf/printf functions should use the "%lld" formatter for printing long longs. There are some issues with the print library in that "%lld" is not working. we are working on that and it would be fixed in the future version of XDE.


User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

sivakumar wrote:The long long constant literals need to have the LL suffix or they will be truncated to int.
eg: result = 8589934592ll;
That's incorrect. Note the question was about C, not XC (how did it end up in
this thread in this forum, heh); see 6.4.4.1/5 in the C standard: if a plain decimal
constant does not fit in an int, it will be long int, and if that doesn't fit, long long int.
Nothing ever gets truncated.
sprintf/printf functions should use the "%lld" formatter for printing long longs. There are some issues with the print library in that "%lld" is not working. we are working on that and it would be fixed in the future version of XDE.
Thanks!
ale500
Respected Member
Posts: 259
Joined: Thu Sep 16, 2010 9:15 am

Post by ale500 »

It will not be truncated unless it does not fit :). So yes truncation will occur in some cases:

Code: Select all

#include <stdio.h>
#include <stdlib.h>

long long int lli;

int i = 0x12345678901234LL;

int main( void )
{
    int j = 1;
    lli = i;
    printf("lli = %lld  i = %d\n", lli, i);
    printf("constant lld %lld constant int %d\n", 0x12345678901234, 0x12345678901234);
    printf("constant(LL) lld %lld constant(LL) int %d\n", 0x12345678901234LL, 0x12345678901234LL);
    printf("constant lld %lld constant int %d\n", 0x12345678901234 + j, 0x12345678901234 + j);
    printf("constant(LL) lld %lld constant(LL) int %d\n", 0x12345678901234LL + j, 0x12345678901234LL + j);
    exit(0);
}

Code: Select all

[ale@Maulotaur test]$ gcc test.c 
test.c:6: warning: overflow in implicit constant conversion
[ale@Maulotaur test]$ ./a.out
lli = 2022707764  i = 2022707764
constant lld 5124095575331380 constant int 2022707764
constant(LL) lld 5124095575331380 constant(LL) int 2022707764
constant lld 5124095575331381 constant int 2022707765
constant(LL) lld 5124095575331381 constant(LL) int 2022707765
[ale@Maulotaur test]$ 
[ale@Maulotaur test]$ gcc --version
gcc (GCC) 4.4.4 20100726 (Red Hat 4.4.4-13)
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[ale@Maulotaur test]$
User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

ale500 wrote:It will not be truncated unless it does not fit :). So yes truncation will occur in some cases:
That's something wholly different.

Code: Select all

int i = 0x12345678901234LL;
There's an implicit cast in this assignment, which the compiler told you about btw.

Code: Select all

    printf("constant lld %lld constant int %d\n", 0x12345678901234, 0x12345678901234);
Not valid C code, passing a long long to a varargs function that reads it as int.
Similar for the rest of the printf()s.
ale500
Respected Member
Posts: 259
Joined: Thu Sep 16, 2010 9:15 am

Post by ale500 »

Not valid... it is valid as long as the compiler accepts it, due in this case to an implicit cast. The problem is portability.
User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

There is no implicit cast. printf() is varargs function: any integer arg gets
the integer promotions (i.e. char and short to int), and that's it. long long
is passed as long long, never as int.

Passing a type that is not comatible with the type read is undefined
behaviour, not implementation-defined behaviour (see 7.15.1.1/2 in
the C standard), so the problem is not that this code is not portable,
but this code has no defined meaning _on any platform_. You could
get lucky and the code does what you want every friday at four o'clock
on your computer (as long as you hold it above your head); or instead
it might eat your cat. You don't want your programs to devour your
feline companion (or so I hope). Don't write code like this :-)
ale500
Respected Member
Posts: 259
Joined: Thu Sep 16, 2010 9:15 am

Post by ale500 »

I don't write code like that, it was an example of silent truncation. The standard is one thing but the compiler will determine what you get, and we know there are plenty of extensions built in many compilers and how programs behave... I prefer explicit casts when needed :).