Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 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> |
Peter Huewe | 0dbdd1b | 2010-05-24 12:13:20 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 13 | #include <asm/atomic.h> |
| 14 | |
| 15 | #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0) |
| 16 | static __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 Barbieri | 9efbcd5 | 2010-03-01 19:55:45 +0100 | [diff] [blame] | 108 | BUG_ON(atomic64_add_unless(&v, one, v0)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 109 | BUG_ON(v.counter != r); |
| 110 | |
| 111 | INIT(v0); |
Luca Barbieri | 9efbcd5 | 2010-03-01 19:55:45 +0100 | [diff] [blame] | 112 | BUG_ON(!atomic64_add_unless(&v, one, v1)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 113 | r += one; |
| 114 | BUG_ON(v.counter != r); |
| 115 | |
Heiko Carstens | 007d086 | 2010-06-04 14:15:02 -0700 | [diff] [blame] | 116 | #if defined(CONFIG_X86) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \ |
| 117 | defined(CONFIG_S390) || defined(_ASM_GENERIC_ATOMIC64_H) |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 118 | INIT(onestwos); |
| 119 | BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1)); |
| 120 | r -= one; |
| 121 | BUG_ON(v.counter != r); |
| 122 | |
| 123 | INIT(0); |
| 124 | BUG_ON(atomic64_dec_if_positive(&v) != -one); |
| 125 | BUG_ON(v.counter != r); |
| 126 | |
| 127 | INIT(-one); |
| 128 | BUG_ON(atomic64_dec_if_positive(&v) != (-one - one)); |
| 129 | BUG_ON(v.counter != r); |
Luca Barbieri | 8f4f202 | 2010-02-26 12:22:40 +0100 | [diff] [blame] | 130 | #else |
| 131 | #warning Please implement atomic64_dec_if_positive for your architecture, and add it to the IF above |
| 132 | #endif |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 133 | |
| 134 | INIT(onestwos); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 135 | BUG_ON(!atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 136 | r += one; |
| 137 | BUG_ON(v.counter != r); |
| 138 | |
| 139 | INIT(0); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 140 | BUG_ON(atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 141 | BUG_ON(v.counter != r); |
| 142 | |
| 143 | INIT(-one); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 144 | BUG_ON(!atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 145 | r += one; |
| 146 | BUG_ON(v.counter != r); |
| 147 | |
| 148 | #ifdef CONFIG_X86 |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 149 | printk(KERN_INFO "atomic64 test passed for %s platform %s CX8 and %s SSE\n", |
| 150 | #ifdef CONFIG_X86_64 |
| 151 | "x86-64", |
| 152 | #elif defined(CONFIG_X86_CMPXCHG64) |
| 153 | "i586+", |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 154 | #else |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 155 | "i386+", |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 156 | #endif |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 157 | boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without", |
| 158 | boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without"); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 159 | #else |
| 160 | printk(KERN_INFO "atomic64 test passed\n"); |
| 161 | #endif |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | core_initcall(test_atomic64); |