Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 25 Jan 1997 07:03:02 -0500 (EST)
From:      Peter Dufault <dufault@hda.com>
To:        hackers@freebsd.org
Subject:   Proofread compare-and-swap gcc-asm
Message-ID:  <199701251203.HAA01609@hda.hda.com>

next in thread | raw e-mail | index | archive | help
Can someone who groks gcc-asm proofread this?  It appears to work standalone,
but I think I'm trashing the stack in the program.  It should compile with
"-DCAS_TEST".

-- 
Peter Dufault (dufault@hda.com)   Realtime Machine Control and Simulation
HD Associates, Inc.               Voice: 508 433 6936

/* cas: Compare-and-swap using the 486 and Pentium "cmpxchg"
 *      instruction.
 *
 * Atomically change what "ptr" points to to "n" if it is "o"
 * and return success.
 */

#if defined(CAS_TEST)
	typedef int bool;
	typedef unsigned long castype;
	extern bool cas(volatile castype *, const castype, const castype);
#else
	#include "mwcas.h"
#endif

bool
cas(volatile castype *ptr, const castype o, const castype n)
{
	volatile bool result = 0;

	__asm __volatile (
	"cmpxchg %3,%1; jne 0f; movl $1,%0;0:"
	: "=m" (result)
	:  "m" (*ptr), "a" (o), "r" (n)
	: "ax", "cc"
	);

	return result;
}

#if defined(CAS_TEST)

#include <stdio.h>

int main(int argc, char *av[])
{

	while (1)
	{
		volatile castype c;
		castype o, n;
		bool result;

		printf("c old new > ");
		if (scanf("%ld %ld %ld", &c, &o, &n) != 3)
			break;

		result = cas(&c, o, n);
		printf("%d; %ld %ld %ld\n", result, c, o, n);
	}

	return 0;
}
#endif /* CAS_TEST */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199701251203.HAA01609>