Boost logo

Boost :

Subject: [boost] Boost.Atomic on OSX Leopard, x86-32?
From: Josh Faust (jfaust_at_[hidden])
Date: 2010-03-04 03:15:08


Hey all,

I'm having some trouble getting Boost.Atomic working properly on OSX
Leopard. After wrangling compilation (all instances of "lock" -> "lock;",
and "+r" -> "+q"), I'm hitting what looks like bad generated assembly.

The following code, compiled with -O2:

#include <boost/atomic.hpp>
#include <stdio.h>

using namespace boost;

int main()
{
  signed char a1, c1;
  char a2, c2;

  {
    atomic<char> a(41);
    char c = a++;
    a1 = a.load();
    c1 = c;
  }

  {
    atomic<signed char> a(41);
    signed char c = a++;
    a2 = a.load();
    c2 = c;
  }

  printf("%d %d\n", a1, c1);
  printf("%d %d\n", a2, c2);
}

produces:
Macintosh-2:test josh$ ./simple2
42 41
93 41

Changing the first atomic to be the same type as the second (atomic<signed
char>) produces the correct output (42 41 for both). This same problem
shows up with int vs. unsigned int as well (have only tested those two).

The assembly that produces the correct output:
LCFI2:
        movl $1, %ecx
^^^^^^^^^^^^^^^^ Move 1 into ecx

        movl %esp, %ebp
LCFI3:
        movl %ecx, %eax
^^^^^^^^^^^^^^^^ And eax

        subl $72, %esp
LCFI4:
        movl %ebx, -12(%ebp)
LCFI5:
        movl %esi, -8(%ebp)
LCFI6:
        movl %edi, -4(%ebp)
LCFI7:
        call L11
"L00000000001$pb":
L11:
        popl %ebx
        movb $41, -25(%ebp)
        lock; xaddb %al, -25(%ebp)
^^^^^^^^^^^^^^^^^ first reference after assignment to eax (through al)

        movzbl -25(%ebp), %edx
        lock; addl $0, (%esp)
        movb $41, -25(%ebp)
        lock; xaddb %cl, -25(%ebp)
^^^^^^^^^^^^^^^^^ first reference after assignment to ecx (through cl)

        movl %ecx, %edi
        movzbl -25(%ebp), %ecx
        movb %cl, -41(%ebp)
        lock; addl $0, (%esp)

The assembly that produces the incorrect output (when one is atomic<char>
and the other is atomic<signed char>):

        movl $1, %eax
^^^^^^^^^^^^^^^^^ Move 1 into eax

        movl %esp, %ebp
LCFI3:
        subl $72, %esp
LCFI4:
        movl %ebx, -12(%ebp)
LCFI5:
        movl %esi, -8(%ebp)
LCFI6:
        movl %edi, -4(%ebp)
LCFI7:
        call L11
"L00000000001$pb":
L11:
        popl %ebx
        movb $41, -25(%ebp)
        lock; xaddb %al, -25(%ebp)
^^^^^^^^^^^^^^^^^^ First reference after assignment to eax (through al)
        movzbl -25(%ebp), %edx
        lock; addl $0, (%esp)
        movb $41, -25(%ebp)
        lock; xaddb %cl, -25(%ebp)
^^^^^^^^^^^^^^^^^^ First reference to ecx (through cl), but NO ASSIGNMENT.
cl is garbage?

        movl %ecx, %edi
        movzbl -25(%ebp), %ecx
        movb %cl, -41(%ebp)
        lock; addl $0, (%esp)

I am by no means an assembly expert (this has been a huge learning
experience in fact) but this doesn't look right.

I'm on:
gcc version 4.0.1 (Apple Inc. build 5493)
OSX 10.5.8, Intel Core 2 Duo (2.16 GHz)
Just pulled a git clone of Boost.Atomic from
http://git.chaoticmind.net/boost.atomic, rev.
886a675aa982ab5875c7bc98697b2eb26c134d03 (I think... I have no experience
with git, just grabbed the top guid from "git log").

building without -O2 yields correct results.

Anyone with more knowledge than I have any ideas? Am I being stupid and
something else is wrong?

Josh


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk