Commit | Line | Data |
---|---|---|
86a89380 LB |
1 | /* |
2 | * Testsuite for atomic64_t functions | |
3 | * | |
4 | * Copyright © 2010 Luca Barbieri | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | */ | |
11 | #include <linux/init.h> | |
12 | #include <asm/atomic.h> | |
13 | ||
14 | #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0) | |
15 | static __init int test_atomic64(void) | |
16 | { | |
17 | long long v0 = 0xaaa31337c001d00dLL; | |
18 | long long v1 = 0xdeadbeefdeafcafeLL; | |
19 | long long v2 = 0xfaceabadf00df001LL; | |
20 | long long onestwos = 0x1111111122222222LL; | |
21 | long long one = 1LL; | |
22 | ||
23 | atomic64_t v = ATOMIC64_INIT(v0); | |
24 | long long r = v0; | |
25 | BUG_ON(v.counter != r); | |
26 | ||
27 | atomic64_set(&v, v1); | |
28 | r = v1; | |
29 | BUG_ON(v.counter != r); | |
30 | BUG_ON(atomic64_read(&v) != r); | |
31 | ||
32 | INIT(v0); | |
33 | atomic64_add(onestwos, &v); | |
34 | r += onestwos; | |
35 | BUG_ON(v.counter != r); | |
36 | ||
37 | INIT(v0); | |
38 | atomic64_add(-one, &v); | |
39 | r += -one; | |
40 | BUG_ON(v.counter != r); | |
41 | ||
42 | INIT(v0); | |
43 | r += onestwos; | |
44 | BUG_ON(atomic64_add_return(onestwos, &v) != r); | |
45 | BUG_ON(v.counter != r); | |
46 | ||
47 | INIT(v0); | |
48 | r += -one; | |
49 | BUG_ON(atomic64_add_return(-one, &v) != r); | |
50 | BUG_ON(v.counter != r); | |
51 | ||
52 | INIT(v0); | |
53 | atomic64_sub(onestwos, &v); | |
54 | r -= onestwos; | |
55 | BUG_ON(v.counter != r); | |
56 | ||
57 | INIT(v0); | |
58 | atomic64_sub(-one, &v); | |
59 | r -= -one; | |
60 | BUG_ON(v.counter != r); | |
61 | ||
62 | INIT(v0); | |
63 | r -= onestwos; | |
64 | BUG_ON(atomic64_sub_return(onestwos, &v) != r); | |
65 | BUG_ON(v.counter != r); | |
66 | ||
67 | INIT(v0); | |
68 | r -= -one; | |
69 | BUG_ON(atomic64_sub_return(-one, &v) != r); | |
70 | BUG_ON(v.counter != r); | |
71 | ||
72 | INIT(v0); | |
73 | atomic64_inc(&v); | |
74 | r += one; | |
75 | BUG_ON(v.counter != r); | |
76 | ||
77 | INIT(v0); | |
78 | r += one; | |
79 | BUG_ON(atomic64_inc_return(&v) != r); | |
80 | BUG_ON(v.counter != r); | |
81 | ||
82 | INIT(v0); | |
83 | atomic64_dec(&v); | |
84 | r -= one; | |
85 | BUG_ON(v.counter != r); | |
86 | ||
87 | INIT(v0); | |
88 | r -= one; | |
89 | BUG_ON(atomic64_dec_return(&v) != r); | |
90 | BUG_ON(v.counter != r); | |
91 | ||
92 | INIT(v0); | |
93 | BUG_ON(atomic64_xchg(&v, v1) != v0); | |
94 | r = v1; | |
95 | BUG_ON(v.counter != r); | |
96 | ||
97 | INIT(v0); | |
98 | BUG_ON(atomic64_cmpxchg(&v, v0, v1) != v0); | |
99 | r = v1; | |
100 | BUG_ON(v.counter != r); | |
101 | ||
102 | INIT(v0); | |
103 | BUG_ON(atomic64_cmpxchg(&v, v2, v1) != v0); | |
104 | BUG_ON(v.counter != r); | |
105 | ||
106 | INIT(v0); | |
9efbcd59 | 107 | BUG_ON(atomic64_add_unless(&v, one, v0)); |
86a89380 LB |
108 | BUG_ON(v.counter != r); |
109 | ||
110 | INIT(v0); | |
9efbcd59 | 111 | BUG_ON(!atomic64_add_unless(&v, one, v1)); |
86a89380 LB |
112 | r += one; |
113 | BUG_ON(v.counter != r); | |
114 | ||
d7f6de1e | 115 | #if defined(CONFIG_X86) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(_ASM_GENERIC_ATOMIC64_H) |
86a89380 LB |
116 | INIT(onestwos); |
117 | BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1)); | |
118 | r -= one; | |
119 | BUG_ON(v.counter != r); | |
120 | ||
121 | INIT(0); | |
122 | BUG_ON(atomic64_dec_if_positive(&v) != -one); | |
123 | BUG_ON(v.counter != r); | |
124 | ||
125 | INIT(-one); | |
126 | BUG_ON(atomic64_dec_if_positive(&v) != (-one - one)); | |
127 | BUG_ON(v.counter != r); | |
8f4f202b LB |
128 | #else |
129 | #warning Please implement atomic64_dec_if_positive for your architecture, and add it to the IF above | |
130 | #endif | |
86a89380 LB |
131 | |
132 | INIT(onestwos); | |
25a304f2 | 133 | BUG_ON(!atomic64_inc_not_zero(&v)); |
86a89380 LB |
134 | r += one; |
135 | BUG_ON(v.counter != r); | |
136 | ||
137 | INIT(0); | |
25a304f2 | 138 | BUG_ON(atomic64_inc_not_zero(&v)); |
86a89380 LB |
139 | BUG_ON(v.counter != r); |
140 | ||
141 | INIT(-one); | |
25a304f2 | 142 | BUG_ON(!atomic64_inc_not_zero(&v)); |
86a89380 LB |
143 | r += one; |
144 | BUG_ON(v.counter != r); | |
145 | ||
146 | #ifdef CONFIG_X86 | |
a5c9161f PA |
147 | printk(KERN_INFO "atomic64 test passed for %s platform %s CX8 and %s SSE\n", |
148 | #ifdef CONFIG_X86_64 | |
149 | "x86-64", | |
150 | #elif defined(CONFIG_X86_CMPXCHG64) | |
151 | "i586+", | |
86a89380 | 152 | #else |
a5c9161f | 153 | "i386+", |
86a89380 | 154 | #endif |
a5c9161f PA |
155 | boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without", |
156 | boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without"); | |
86a89380 LB |
157 | #else |
158 | printk(KERN_INFO "atomic64 test passed\n"); | |
159 | #endif | |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
164 | core_initcall(test_atomic64); |