blob: 9087d71537ddef84c011b86988e17202b007a291 [file] [log] [blame]
Luca Barbieri86a89382010-02-24 10:54:24 +01001/*
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>
Peter Huewe0dbdd1b2010-05-24 12:13:20 -070012#include <linux/kernel.h>
Luca Barbieri86a89382010-02-24 10:54:24 +010013#include <asm/atomic.h>
14
15#define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
16static __init int test_atomic64(void)
17{
18 long long v0 = 0xaaa31337c001d00dLL;
19 long long v1 = 0xdeadbeefdeafcafeLL;
20 long long v2 = 0xfaceabadf00df001LL;
21 long long onestwos = 0x1111111122222222LL;
22 long long one = 1LL;
23
24 atomic64_t v = ATOMIC64_INIT(v0);
25 long long r = v0;
26 BUG_ON(v.counter != r);
27
28 atomic64_set(&v, v1);
29 r = v1;
30 BUG_ON(v.counter != r);
31 BUG_ON(atomic64_read(&v) != r);
32
33 INIT(v0);
34 atomic64_add(onestwos, &v);
35 r += onestwos;
36 BUG_ON(v.counter != r);
37
38 INIT(v0);
39 atomic64_add(-one, &v);
40 r += -one;
41 BUG_ON(v.counter != r);
42
43 INIT(v0);
44 r += onestwos;
45 BUG_ON(atomic64_add_return(onestwos, &v) != r);
46 BUG_ON(v.counter != r);
47
48 INIT(v0);
49 r += -one;
50 BUG_ON(atomic64_add_return(-one, &v) != r);
51 BUG_ON(v.counter != r);
52
53 INIT(v0);
54 atomic64_sub(onestwos, &v);
55 r -= onestwos;
56 BUG_ON(v.counter != r);
57
58 INIT(v0);
59 atomic64_sub(-one, &v);
60 r -= -one;
61 BUG_ON(v.counter != r);
62
63 INIT(v0);
64 r -= onestwos;
65 BUG_ON(atomic64_sub_return(onestwos, &v) != r);
66 BUG_ON(v.counter != r);
67
68 INIT(v0);
69 r -= -one;
70 BUG_ON(atomic64_sub_return(-one, &v) != r);
71 BUG_ON(v.counter != r);
72
73 INIT(v0);
74 atomic64_inc(&v);
75 r += one;
76 BUG_ON(v.counter != r);
77
78 INIT(v0);
79 r += one;
80 BUG_ON(atomic64_inc_return(&v) != r);
81 BUG_ON(v.counter != r);
82
83 INIT(v0);
84 atomic64_dec(&v);
85 r -= one;
86 BUG_ON(v.counter != r);
87
88 INIT(v0);
89 r -= one;
90 BUG_ON(atomic64_dec_return(&v) != r);
91 BUG_ON(v.counter != r);
92
93 INIT(v0);
94 BUG_ON(atomic64_xchg(&v, v1) != v0);
95 r = v1;
96 BUG_ON(v.counter != r);
97
98 INIT(v0);
99 BUG_ON(atomic64_cmpxchg(&v, v0, v1) != v0);
100 r = v1;
101 BUG_ON(v.counter != r);
102
103 INIT(v0);
104 BUG_ON(atomic64_cmpxchg(&v, v2, v1) != v0);
105 BUG_ON(v.counter != r);
106
107 INIT(v0);
Luca Barbieri9efbcd52010-03-01 19:55:45 +0100108 BUG_ON(atomic64_add_unless(&v, one, v0));
Luca Barbieri86a89382010-02-24 10:54:24 +0100109 BUG_ON(v.counter != r);
110
111 INIT(v0);
Luca Barbieri9efbcd52010-03-01 19:55:45 +0100112 BUG_ON(!atomic64_add_unless(&v, one, v1));
Luca Barbieri86a89382010-02-24 10:54:24 +0100113 r += one;
114 BUG_ON(v.counter != r);
115
Luca Barbierid7f6de12010-02-26 12:22:41 +0100116#if defined(CONFIG_X86) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(_ASM_GENERIC_ATOMIC64_H)
Luca Barbieri86a89382010-02-24 10:54:24 +0100117 INIT(onestwos);
118 BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1));
119 r -= one;
120 BUG_ON(v.counter != r);
121
122 INIT(0);
123 BUG_ON(atomic64_dec_if_positive(&v) != -one);
124 BUG_ON(v.counter != r);
125
126 INIT(-one);
127 BUG_ON(atomic64_dec_if_positive(&v) != (-one - one));
128 BUG_ON(v.counter != r);
Luca Barbieri8f4f2022010-02-26 12:22:40 +0100129#else
130#warning Please implement atomic64_dec_if_positive for your architecture, and add it to the IF above
131#endif
Luca Barbieri86a89382010-02-24 10:54:24 +0100132
133 INIT(onestwos);
Luca Barbieri25a304f2010-03-01 19:55:48 +0100134 BUG_ON(!atomic64_inc_not_zero(&v));
Luca Barbieri86a89382010-02-24 10:54:24 +0100135 r += one;
136 BUG_ON(v.counter != r);
137
138 INIT(0);
Luca Barbieri25a304f2010-03-01 19:55:48 +0100139 BUG_ON(atomic64_inc_not_zero(&v));
Luca Barbieri86a89382010-02-24 10:54:24 +0100140 BUG_ON(v.counter != r);
141
142 INIT(-one);
Luca Barbieri25a304f2010-03-01 19:55:48 +0100143 BUG_ON(!atomic64_inc_not_zero(&v));
Luca Barbieri86a89382010-02-24 10:54:24 +0100144 r += one;
145 BUG_ON(v.counter != r);
146
147#ifdef CONFIG_X86
H. Peter Anvina5c91612010-03-01 11:49:23 -0800148 printk(KERN_INFO "atomic64 test passed for %s platform %s CX8 and %s SSE\n",
149#ifdef CONFIG_X86_64
150 "x86-64",
151#elif defined(CONFIG_X86_CMPXCHG64)
152 "i586+",
Luca Barbieri86a89382010-02-24 10:54:24 +0100153#else
H. Peter Anvina5c91612010-03-01 11:49:23 -0800154 "i386+",
Luca Barbieri86a89382010-02-24 10:54:24 +0100155#endif
H. Peter Anvina5c91612010-03-01 11:49:23 -0800156 boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without",
157 boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without");
Luca Barbieri86a89382010-02-24 10:54:24 +0100158#else
159 printk(KERN_INFO "atomic64 test passed\n");
160#endif
161
162 return 0;
163}
164
165core_initcall(test_atomic64);