2 * Testsuite for atomic64_t functions
4 * Copyright © 2010 Luca Barbieri
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/init.h>
15 #include <linux/bug.h>
16 #include <linux/kernel.h>
17 #include <linux/atomic.h>
19 #define TEST(bit, op, c_op, val) \
21 atomic##bit##_set(&v, v0); \
23 atomic##bit##_##op(val, &v); \
25 WARN(atomic##bit##_read(&v) != r, "%Lx != %Lx\n", \
26 (unsigned long long)atomic##bit##_read(&v), \
27 (unsigned long long)r); \
31 * Test for a atomic operation family,
32 * @test should be a macro accepting parameters (bit, op, ...)
35 #define FAMILY_TEST(test, bit, op, args...) \
37 test(bit, op, ##args); \
38 test(bit, op##_acquire, ##args); \
39 test(bit, op##_release, ##args); \
40 test(bit, op##_relaxed, ##args); \
43 #define TEST_RETURN(bit, op, c_op, val) \
45 atomic##bit##_set(&v, v0); \
48 BUG_ON(atomic##bit##_##op(val, &v) != r); \
49 BUG_ON(atomic##bit##_read(&v) != r); \
52 #define RETURN_FAMILY_TEST(bit, op, c_op, val) \
54 FAMILY_TEST(TEST_RETURN, bit, op, c_op, val); \
57 #define TEST_ARGS(bit, op, init, ret, expect, args...) \
59 atomic##bit##_set(&v, init); \
60 BUG_ON(atomic##bit##_##op(&v, ##args) != ret); \
61 BUG_ON(atomic##bit##_read(&v) != expect); \
64 #define XCHG_FAMILY_TEST(bit, init, new) \
66 FAMILY_TEST(TEST_ARGS, bit, xchg, init, init, new, new); \
69 #define CMPXCHG_FAMILY_TEST(bit, init, new, wrong) \
71 FAMILY_TEST(TEST_ARGS, bit, cmpxchg, \
72 init, init, new, init, new); \
73 FAMILY_TEST(TEST_ARGS, bit, cmpxchg, \
74 init, init, init, wrong, new); \
77 #define INC_RETURN_FAMILY_TEST(bit, i) \
79 FAMILY_TEST(TEST_ARGS, bit, inc_return, \
80 i, (i) + one, (i) + one); \
83 #define DEC_RETURN_FAMILY_TEST(bit, i) \
85 FAMILY_TEST(TEST_ARGS, bit, dec_return, \
86 i, (i) - one, (i) - one); \
89 static __init
void test_atomic(void)
93 int onestwos
= 0x11112222;
99 TEST(, add
, +=, onestwos
);
100 TEST(, add
, +=, -one
);
101 TEST(, sub
, -=, onestwos
);
102 TEST(, sub
, -=, -one
);
106 TEST(, andnot
, &= ~, v1
);
108 RETURN_FAMILY_TEST(, add_return
, +=, onestwos
);
109 RETURN_FAMILY_TEST(, add_return
, +=, -one
);
110 RETURN_FAMILY_TEST(, sub_return
, -=, onestwos
);
111 RETURN_FAMILY_TEST(, sub_return
, -=, -one
);
113 INC_RETURN_FAMILY_TEST(, v0
);
114 DEC_RETURN_FAMILY_TEST(, v0
);
116 XCHG_FAMILY_TEST(, v0
, v1
);
117 CMPXCHG_FAMILY_TEST(, v0
, v1
, onestwos
);
121 #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
122 static __init
void test_atomic64(void)
124 long long v0
= 0xaaa31337c001d00dLL
;
125 long long v1
= 0xdeadbeefdeafcafeLL
;
126 long long v2
= 0xfaceabadf00df001LL
;
127 long long onestwos
= 0x1111111122222222LL
;
130 atomic64_t v
= ATOMIC64_INIT(v0
);
132 BUG_ON(v
.counter
!= r
);
134 atomic64_set(&v
, v1
);
136 BUG_ON(v
.counter
!= r
);
137 BUG_ON(atomic64_read(&v
) != r
);
139 TEST(64, add
, +=, onestwos
);
140 TEST(64, add
, +=, -one
);
141 TEST(64, sub
, -=, onestwos
);
142 TEST(64, sub
, -=, -one
);
143 TEST(64, or, |=, v1
);
144 TEST(64, and, &=, v1
);
145 TEST(64, xor, ^=, v1
);
146 TEST(64, andnot
, &= ~, v1
);
148 RETURN_FAMILY_TEST(64, add_return
, +=, onestwos
);
149 RETURN_FAMILY_TEST(64, add_return
, +=, -one
);
150 RETURN_FAMILY_TEST(64, sub_return
, -=, onestwos
);
151 RETURN_FAMILY_TEST(64, sub_return
, -=, -one
);
156 BUG_ON(v
.counter
!= r
);
161 BUG_ON(v
.counter
!= r
);
163 INC_RETURN_FAMILY_TEST(64, v0
);
164 DEC_RETURN_FAMILY_TEST(64, v0
);
166 XCHG_FAMILY_TEST(64, v0
, v1
);
167 CMPXCHG_FAMILY_TEST(64, v0
, v1
, v2
);
170 BUG_ON(atomic64_add_unless(&v
, one
, v0
));
171 BUG_ON(v
.counter
!= r
);
174 BUG_ON(!atomic64_add_unless(&v
, one
, v1
));
176 BUG_ON(v
.counter
!= r
);
178 #ifdef CONFIG_ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
180 BUG_ON(atomic64_dec_if_positive(&v
) != (onestwos
- 1));
182 BUG_ON(v
.counter
!= r
);
185 BUG_ON(atomic64_dec_if_positive(&v
) != -one
);
186 BUG_ON(v
.counter
!= r
);
189 BUG_ON(atomic64_dec_if_positive(&v
) != (-one
- one
));
190 BUG_ON(v
.counter
!= r
);
192 #warning Please implement atomic64_dec_if_positive for your architecture and select the above Kconfig symbol
196 BUG_ON(!atomic64_inc_not_zero(&v
));
198 BUG_ON(v
.counter
!= r
);
201 BUG_ON(atomic64_inc_not_zero(&v
));
202 BUG_ON(v
.counter
!= r
);
205 BUG_ON(!atomic64_inc_not_zero(&v
));
207 BUG_ON(v
.counter
!= r
);
210 static __init
int test_atomics(void)
216 pr_info("passed for %s platform %s CX8 and %s SSE\n",
219 #elif defined(CONFIG_X86_CMPXCHG64)
224 boot_cpu_has(X86_FEATURE_CX8
) ? "with" : "without",
225 boot_cpu_has(X86_FEATURE_XMM
) ? "with" : "without");
233 core_initcall(test_atomics
);
This page took 0.055217 seconds and 5 git commands to generate.