83c33a5bcffb1a33b44bb5872f94f44fe4d05352
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); \
30 static __init
void test_atomic(void)
34 int onestwos
= 0x11112222;
40 TEST(, add
, +=, onestwos
);
41 TEST(, add
, +=, -one
);
42 TEST(, sub
, -=, onestwos
);
43 TEST(, sub
, -=, -one
);
47 TEST(, andnot
, &= ~, v1
);
50 #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
51 static __init
void test_atomic64(void)
53 long long v0
= 0xaaa31337c001d00dLL
;
54 long long v1
= 0xdeadbeefdeafcafeLL
;
55 long long v2
= 0xfaceabadf00df001LL
;
56 long long onestwos
= 0x1111111122222222LL
;
59 atomic64_t v
= ATOMIC64_INIT(v0
);
61 BUG_ON(v
.counter
!= r
);
65 BUG_ON(v
.counter
!= r
);
66 BUG_ON(atomic64_read(&v
) != r
);
68 TEST(64, add
, +=, onestwos
);
69 TEST(64, add
, +=, -one
);
70 TEST(64, sub
, -=, onestwos
);
71 TEST(64, sub
, -=, -one
);
73 TEST(64, and, &=, v1
);
74 TEST(64, xor, ^=, v1
);
75 TEST(64, andnot
, &= ~, v1
);
79 BUG_ON(atomic64_add_return(onestwos
, &v
) != r
);
80 BUG_ON(v
.counter
!= r
);
84 BUG_ON(atomic64_add_return(-one
, &v
) != r
);
85 BUG_ON(v
.counter
!= r
);
89 BUG_ON(atomic64_sub_return(onestwos
, &v
) != r
);
90 BUG_ON(v
.counter
!= r
);
94 BUG_ON(atomic64_sub_return(-one
, &v
) != r
);
95 BUG_ON(v
.counter
!= r
);
100 BUG_ON(v
.counter
!= r
);
104 BUG_ON(atomic64_inc_return(&v
) != r
);
105 BUG_ON(v
.counter
!= r
);
110 BUG_ON(v
.counter
!= r
);
114 BUG_ON(atomic64_dec_return(&v
) != r
);
115 BUG_ON(v
.counter
!= r
);
118 BUG_ON(atomic64_xchg(&v
, v1
) != v0
);
120 BUG_ON(v
.counter
!= r
);
123 BUG_ON(atomic64_cmpxchg(&v
, v0
, v1
) != v0
);
125 BUG_ON(v
.counter
!= r
);
128 BUG_ON(atomic64_cmpxchg(&v
, v2
, v1
) != v0
);
129 BUG_ON(v
.counter
!= r
);
132 BUG_ON(atomic64_add_unless(&v
, one
, v0
));
133 BUG_ON(v
.counter
!= r
);
136 BUG_ON(!atomic64_add_unless(&v
, one
, v1
));
138 BUG_ON(v
.counter
!= r
);
140 #ifdef CONFIG_ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
142 BUG_ON(atomic64_dec_if_positive(&v
) != (onestwos
- 1));
144 BUG_ON(v
.counter
!= r
);
147 BUG_ON(atomic64_dec_if_positive(&v
) != -one
);
148 BUG_ON(v
.counter
!= r
);
151 BUG_ON(atomic64_dec_if_positive(&v
) != (-one
- one
));
152 BUG_ON(v
.counter
!= r
);
154 #warning Please implement atomic64_dec_if_positive for your architecture and select the above Kconfig symbol
158 BUG_ON(!atomic64_inc_not_zero(&v
));
160 BUG_ON(v
.counter
!= r
);
163 BUG_ON(atomic64_inc_not_zero(&v
));
164 BUG_ON(v
.counter
!= r
);
167 BUG_ON(!atomic64_inc_not_zero(&v
));
169 BUG_ON(v
.counter
!= r
);
172 static __init
int test_atomics(void)
178 pr_info("passed for %s platform %s CX8 and %s SSE\n",
181 #elif defined(CONFIG_X86_CMPXCHG64)
186 boot_cpu_has(X86_FEATURE_CX8
) ? "with" : "without",
187 boot_cpu_has(X86_FEATURE_XMM
) ? "with" : "without");
195 core_initcall(test_atomics
);