shithub: snippets

Download patch

ref: 930433a467b174dd86081fbec4079805f31cb388
parent: fa63d0c70294c1e4a50d81528c59e0f0bb95e95f
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Thu Nov 25 17:13:57 EST 2021

add a small set of atomic for Plan 9 (amd64 only for now)

--- a/README.md
+++ b/README.md
@@ -4,6 +4,7 @@
 copy and use for whatever you want.  Except for doing any kind of
 harm.
 
+* `atomics9*` a set of atomics for Plan 9
 * `c_builtins*` a few __builtin_* for Plan 9, useful for porting other software
 * `lrint.c` lrint* implementation
 * `msr.c` MSR reading tool
--- /dev/null
+++ b/atomics9.h
@@ -1,0 +1,6 @@
+long atomic_fetch_add(long *i, long v);
+long atomic_fetch_sub(long *i, long v);
+long atomic_fetch_and(long *i, long v);
+long atomic_fetch_or(long *i, long v);
+long atomic_fetch_xor(long *i, long v);
+long atomic_exchange(long *i, long v);
--- /dev/null
+++ b/atomics9_amd64.s
@@ -1,0 +1,63 @@
+TEXT atomic_fetch_add(SB), 1, $0
+afap:
+	MOVL	(RARG), AX	/* exp */
+	MOVL	AX, CX
+	MOVL	AX, BX
+	ADDL	d+8(FP), BX	/* new */
+	LOCK; CMPXCHGL BX, (RARG)
+	JNZ	afap
+	MOVL	CX, AX
+	RET
+
+TEXT atomic_fetch_sub(SB), 1, $0
+afsp:
+	MOVL	(RARG), AX	/* exp */
+	MOVL	AX, CX
+	MOVL	AX, BX
+	SUBL	d+8(FP), BX	/* new */
+	LOCK; CMPXCHGL BX, (RARG)
+	JNZ	afsp
+	MOVL	CX, AX
+	RET
+
+TEXT atomic_fetch_and(SB), 1, $0
+afandp:
+	MOVL	(RARG), AX	/* exp */
+	MOVL	AX, CX
+	MOVL	AX, BX
+	ANDL	d+8(FP), BX	/* new */
+	LOCK; CMPXCHGL BX, (RARG)
+	JNZ	afandp
+	MOVL	CX, AX
+	RET
+
+TEXT atomic_fetch_or(SB), 1, $0
+aforp:
+	MOVL	(RARG), AX	/* exp */
+	MOVL	AX, CX
+	MOVL	AX, BX
+	ORL		d+8(FP), BX	/* new */
+	LOCK; CMPXCHGL BX, (RARG)
+	JNZ	aforp
+	MOVL	CX, AX
+	RET
+
+TEXT atomic_fetch_xor(SB), 1, $0
+afxorp:
+	MOVL	(RARG), AX	/* exp */
+	MOVL	AX, CX
+	MOVL	AX, BX
+	XORL	d+8(FP), BX	/* new */
+	LOCK; CMPXCHGL BX, (RARG)
+	JNZ	afxorp
+	MOVL	CX, AX
+	RET
+
+TEXT atomic_exchange(SB), 1, $0
+afexp:
+	MOVL	(RARG), AX	/* exp */
+	MOVL	AX, CX
+	LOCK; CMPXCHGL AX, (RARG)
+	JNZ	afexp
+	MOVL	CX, AX
+	RET