help with __sync for "long long" types

Preeti Sharma pritis@gmail.com
Wed Jul 7 17:24:00 GMT 2010


Also wanted to check if this might have something to do with 4.1.2 ?
the /proc/cpuinfo shows a Intel(R) Xeon (R) CPU and family as 6.  So
based upon the info on gcc 4.1.2 this should work.



On Wed, Jul 7, 2010 at 8:55 AM, Preeti Sharma <pritis@gmail.com> wrote:
 Good to know that this is supposed to work (I am using 4.1.2) I get a
 link error in doing so
 here is my code:

 cat test1.c
> #include <stdio.h>
> #include <unistd.h>
> #include <errno.h>
>
> long long global_int = 0;
>
> int main()
> {
>         __sync_fetch_and_add( &global_int, 1 );
>        printf("Global int is : %d\n", global_int);
> }
> The link error I get is :
>
>  gcc -D_GNU_SOURCE -D_REENTRANT -O2 -m32 -march=i686 -o test test1.c
> /tmp/cc4pniWE.o: In function `main':
> test1.c:(.text+0x29): undefined reference to `__sync_fetch_and_add_8'
> collect2: ld returned 1 exit status
>
>
>
>
> On Wed, Jul 7, 2010 at 8:43 AM, Andrew Haley <aph@redhat.com> wrote:
>> On 07/07/2010 04:26 PM, Preeti Sharma wrote:
>>> I am trying to use __sync_fetch_and_add with type  as "long long" .
>>>
>>> This gives me the link error :
>>> undefined reference to `__sync_fetch_and_add_8'
>>>
>>> This compiles for a "int " type though. I am using the the gcc option
>>> -march=i686
>>>
>>> Is it possible to do a 64 bit integer operation atomically with the
>>> __sync .. builtin functions ?
>>
>> Works for me:
>>
>> long long a;
>>
>> t()
>> {
>>  __sync_fetch_and_add(&a, 1);
>> }
>>
>> $ gcc -S t.c -m32 -march=i686 -O2
>>
>>        .file   "t.c"
>>        .text
>>        .p2align 4,,15
>> .globl t
>>        .type   t, @function
>> t:
>>        pushl   %ebp
>>        movl    %esp, %ebp
>>        subl    $12, %esp
>>        movl    %ebx, (%esp)
>>        movl    %esi, 4(%esp)
>>        movl    %edi, 8(%esp)
>>        movl    a, %eax
>>        movl    a+4, %edx
>> .L2:
>>        movl    %eax, %esi
>>        movl    %edx, %edi
>>        addl    $1, %esi
>>        adcl    $0, %edi
>>        movl    %esi, %ebx
>>        movl    %edi, %ecx
>>        lock cmpxchg8b  a
>>        jne     .L2
>>        movl    (%esp), %ebx
>>        movl    4(%esp), %esi
>>        movl    8(%esp), %edi
>>        movl    %ebp, %esp
>>        popl    %ebp
>>        ret
>>        .size   t, .-t
>>        .comm   a,8,8
>>        .ident  "GCC: (GNU) 4.4.3 20100127 (Red Hat 4.4.3-4)"
>>        .section        .note.GNU-stack,"",@progbits
>>
>>
>



More information about the Gcc-help mailing list