~angelsl/ubuntu/wily/gcc-5/mips-cross

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# DP: libgo: Fix PR go/67508, rewrite lfstack packing/unpacking
# DP: to look more like that in Go

From 95eb3733500feffc2eeeec5bcfac7e34583514e2 Mon Sep 17 00:00:00 2001
From: Michael Hudson-Doyle <michael.hudson@canonical.com>
Date: Fri, 31 Jul 2015 11:45:45 +1200
Subject: [PATCH] runtime: rewrite lfstack packing/unpacking to look more like that in Go

Change-Id: I4ca61240c3f69c6dce1fde8d859f8507dfab80fc
---

diff --git a/libgo/runtime/lfstack.goc b/libgo/runtime/lfstack.goc
index 060a0cc..9eb80d9 100644
--- a/src/libgo/runtime/lfstack.goc
+++ b/src/libgo/runtime/lfstack.goc
@@ -9,25 +9,41 @@
 #include "arch.h"
 
 #if __SIZEOF_POINTER__ == 8
-// Amd64 uses 48-bit virtual addresses, 47-th bit is used as kernel/user flag.
-// So we use 17msb of pointers as ABA counter.
-# define PTR_BITS 47
-#else
-# define PTR_BITS 32
-#endif
-#define PTR_MASK ((1ull<<PTR_BITS)-1)
-#define CNT_MASK (0ull-1)
-
-#if __SIZEOF_POINTER__ == 8 && (defined(__sparc__) || (defined(__sun__) && defined(__amd64__)))
 // SPARC64 and Solaris on AMD64 uses all 64 bits of virtual addresses.
 // Use low-order three bits as ABA counter.
 // http://docs.oracle.com/cd/E19120-01/open.solaris/816-5138/6mba6ua5p/index.html
-#undef PTR_BITS
-#undef CNT_MASK
-#undef PTR_MASK
-#define PTR_BITS 0
-#define CNT_MASK 7
-#define PTR_MASK ((0ull-1)<<3)
+# if defined(__sparc__) || (defined(__sun__) && defined(__amd64__))
+static inline uint64 lfPack(LFNode *node, uintptr cnt) {
+	return ((uint64)(node)) | ((cnt)&7);
+}
+static inline LFNode* lfUnpack(uint64 val) {
+	return (LFNode*)(val&~7);
+}
+# else
+#  if defined(__aarch64__)
+// Depending on the kernel options, pointers on arm64 can have up to 48 significant
+// bits (see https://www.kernel.org/doc/Documentation/arm64/memory.txt).
+#   define PTR_BITS 48
+#  else
+// Amd64 uses 48-bit virtual addresses, 47-th bit is used as kernel/user flag.
+// So we use 17msb of pointers as ABA counter.
+#   define PTR_BITS 47
+#  endif
+# endif
+# define CNT_BITS (64 - PTR_BITS + 3)
+static inline uint64 lfPack(LFNode *node, uintptr cnt) {
+	return ((uint64)(node)<<(64-PTR_BITS)) | (cnt&(((1<<CNT_BITS)-1)));
+}
+static inline LFNode* lfUnpack(uint64 val) {
+	return (LFNode*)((val >> CNT_BITS) << 3);
+}
+#else
+static inline uint64 lfPack(LFNode *node, uintptr cnt) {
+	return ((uint64)(uintptr)(node)<<32) | cnt;
+}
+static inline LFNode* lfUnpack(uint64 val) {
+	return (LFNode*)(uintptr)(val >> 32);
+}
 #endif
 
 void
@@ -35,16 +51,16 @@
 {
 	uint64 old, new;
 
-	if((uintptr)node != ((uintptr)node&PTR_MASK)) {
+	if(node != lfUnpack(lfPack(node, 0))) {
 		runtime_printf("p=%p\n", node);
 		runtime_throw("runtime_lfstackpush: invalid pointer");
 	}
 
 	node->pushcnt++;
-	new = (uint64)(uintptr)node|(((uint64)node->pushcnt&CNT_MASK)<<PTR_BITS);
+	new = lfPack(node, node->pushcnt);
 	for(;;) {
 		old = runtime_atomicload64(head);
-		node->next = (LFNode*)(uintptr)(old&PTR_MASK);
+		node->next = lfUnpack(old);
 		if(runtime_cas64(head, old, new))
 			break;
 	}
@@ -60,11 +76,11 @@
 		old = runtime_atomicload64(head);
 		if(old == 0)
 			return nil;
-		node = (LFNode*)(uintptr)(old&PTR_MASK);
+		node = lfUnpack(old);
 		node2 = runtime_atomicloadp(&node->next);
 		new = 0;
 		if(node2 != nil)
-			new = (uint64)(uintptr)node2|(((uint64)node2->pushcnt&CNT_MASK)<<PTR_BITS);
+			new = lfPack(node2, node2->pushcnt);
 		if(runtime_cas64(head, old, new))
 			return node;
 	}