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 | |
Luca Barbieri | d7f6de1 | 2010-02-26 12:22:41 +0100 | [diff] [blame] | 116 | #if defined(CONFIG_X86) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(_ASM_GENERIC_ATOMIC64_H) |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 117 | 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 Barbieri | 8f4f202 | 2010-02-26 12:22:40 +0100 | [diff] [blame] | 129 | #else |
| 130 | #warning Please implement atomic64_dec_if_positive for your architecture, and add it to the IF above |
| 131 | #endif |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 132 | |
| 133 | INIT(onestwos); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 134 | BUG_ON(!atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 135 | r += one; |
| 136 | BUG_ON(v.counter != r); |
| 137 | |
| 138 | INIT(0); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 139 | BUG_ON(atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 140 | BUG_ON(v.counter != r); |
| 141 | |
| 142 | INIT(-one); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 143 | BUG_ON(!atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 144 | r += one; |
| 145 | BUG_ON(v.counter != r); |
| 146 | |
| 147 | #ifdef CONFIG_X86 |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 148 | 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 Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 153 | #else |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 154 | "i386+", |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 155 | #endif |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 156 | boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without", |
| 157 | boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without"); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 158 | #else |
| 159 | printk(KERN_INFO "atomic64 test passed\n"); |
| 160 | #endif |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | core_initcall(test_atomic64); |