summaryrefslogtreecommitdiff
path: root/src/pkg/sync/atomic/asm_386.s
blob: a9360efae9997d9759d9d5e650f0aee91db95fe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2011 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

TEXT ·CompareAndSwapInt32(SB),7,$0
	JMP	·CompareAndSwapUint32(SB)

TEXT ·CompareAndSwapUint32(SB),7,$0
	MOVL	valptr+0(FP), BP
	MOVL	old+4(FP), AX
	MOVL	new+8(FP), CX
	// CMPXCHGL was introduced on the 486.
	LOCK
	CMPXCHGL	CX, 0(BP)
	SETEQ	ret+12(FP)
	RET

TEXT ·CompareAndSwapUintptr(SB),7,$0
	JMP	·CompareAndSwapUint32(SB)

TEXT ·CompareAndSwapInt64(SB),7,$0
	JMP	·CompareAndSwapUint64(SB)

TEXT ·CompareAndSwapUint64(SB),7,$0
	MOVL	valptr+0(FP), BP
	MOVL	oldlo+4(FP), AX
	MOVL	oldhi+8(FP), DX
	MOVL	newlo+12(FP), BX
	MOVL	newhi+16(FP), CX
	// CMPXCHG8B was introduced on the Pentium.
	LOCK
	CMPXCHG8B	0(BP)
	SETEQ	ret+20(FP)
	RET

TEXT ·AddInt32(SB),7,$0
	JMP	·AddUint32(SB)

TEXT ·AddUint32(SB),7,$0
	MOVL	valptr+0(FP), BP
	MOVL	delta+4(FP), AX
	MOVL	AX, CX
	// XADD was introduced on the 486.
	LOCK
	XADDL	AX, 0(BP)
	ADDL	AX, CX
	MOVL	CX, ret+8(FP)
	RET

TEXT ·AddUintptr(SB),7,$0
	JMP	·AddUint32(SB)

TEXT ·AddInt64(SB),7,$0
	JMP	·AddUint64(SB)

TEXT ·AddUint64(SB),7,$0
	// no XADDQ so use CMPXCHG8B loop
	MOVL	valptr+0(FP), BP
	// DI:SI = delta
	MOVL	deltalo+4(FP), SI
	MOVL	deltahi+8(FP), DI
	// DX:AX = *valptr
	MOVL	0(BP), AX
	MOVL	4(BP), DX
addloop:
	// CX:BX = DX:AX (*valptr) + DI:SI (delta)
	MOVL	AX, BX
	MOVL	DX, CX
	ADDL	SI, BX
	ADCL	DI, CX

	// if *valptr == DX:AX {
	//	*valptr = CX:BX
	// } else {
	//	DX:AX = *valptr
	// }
	// all in one instruction
	LOCK
	CMPXCHG8B	0(BP)

	JNZ	addloop

	// success
	// return CX:BX
	MOVL	BX, retlo+12(FP)
	MOVL	CX, rethi+16(FP)
	RET