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> |
Paul Gortmaker | 50af5ea | 2012-01-20 18:35:53 -0500 | [diff] [blame^] | 12 | #include <linux/bug.h> |
Peter Huewe | 0dbdd1b | 2010-05-24 12:13:20 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 14 | #include <linux/atomic.h> |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 15 | |
| 16 | #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0) |
| 17 | static __init int test_atomic64(void) |
| 18 | { |
| 19 | long long v0 = 0xaaa31337c001d00dLL; |
| 20 | long long v1 = 0xdeadbeefdeafcafeLL; |
| 21 | long long v2 = 0xfaceabadf00df001LL; |
| 22 | long long onestwos = 0x1111111122222222LL; |
| 23 | long long one = 1LL; |
| 24 | |
| 25 | atomic64_t v = ATOMIC64_INIT(v0); |
| 26 | long long r = v0; |
| 27 | BUG_ON(v.counter != r); |
| 28 | |
| 29 | atomic64_set(&v, v1); |
| 30 | r = v1; |
| 31 | BUG_ON(v.counter != r); |
| 32 | BUG_ON(atomic64_read(&v) != r); |
| 33 | |
| 34 | INIT(v0); |
| 35 | atomic64_add(onestwos, &v); |
| 36 | r += onestwos; |
| 37 | BUG_ON(v.counter != r); |
| 38 | |
| 39 | INIT(v0); |
| 40 | atomic64_add(-one, &v); |
| 41 | r += -one; |
| 42 | BUG_ON(v.counter != r); |
| 43 | |
| 44 | INIT(v0); |
| 45 | r += onestwos; |
| 46 | BUG_ON(atomic64_add_return(onestwos, &v) != r); |
| 47 | BUG_ON(v.counter != r); |
| 48 | |
| 49 | INIT(v0); |
| 50 | r += -one; |
| 51 | BUG_ON(atomic64_add_return(-one, &v) != r); |
| 52 | BUG_ON(v.counter != r); |
| 53 | |
| 54 | INIT(v0); |
| 55 | atomic64_sub(onestwos, &v); |
| 56 | r -= onestwos; |
| 57 | BUG_ON(v.counter != r); |
| 58 | |
| 59 | INIT(v0); |
| 60 | atomic64_sub(-one, &v); |
| 61 | r -= -one; |
| 62 | BUG_ON(v.counter != r); |
| 63 | |
| 64 | INIT(v0); |
| 65 | r -= onestwos; |
| 66 | BUG_ON(atomic64_sub_return(onestwos, &v) != r); |
| 67 | BUG_ON(v.counter != r); |
| 68 | |
| 69 | INIT(v0); |
| 70 | r -= -one; |
| 71 | BUG_ON(atomic64_sub_return(-one, &v) != r); |
| 72 | BUG_ON(v.counter != r); |
| 73 | |
| 74 | INIT(v0); |
| 75 | atomic64_inc(&v); |
| 76 | r += one; |
| 77 | BUG_ON(v.counter != r); |
| 78 | |
| 79 | INIT(v0); |
| 80 | r += one; |
| 81 | BUG_ON(atomic64_inc_return(&v) != r); |
| 82 | BUG_ON(v.counter != r); |
| 83 | |
| 84 | INIT(v0); |
| 85 | atomic64_dec(&v); |
| 86 | r -= one; |
| 87 | BUG_ON(v.counter != r); |
| 88 | |
| 89 | INIT(v0); |
| 90 | r -= one; |
| 91 | BUG_ON(atomic64_dec_return(&v) != r); |
| 92 | BUG_ON(v.counter != r); |
| 93 | |
| 94 | INIT(v0); |
| 95 | BUG_ON(atomic64_xchg(&v, v1) != v0); |
| 96 | r = v1; |
| 97 | BUG_ON(v.counter != r); |
| 98 | |
| 99 | INIT(v0); |
| 100 | BUG_ON(atomic64_cmpxchg(&v, v0, v1) != v0); |
| 101 | r = v1; |
| 102 | BUG_ON(v.counter != r); |
| 103 | |
| 104 | INIT(v0); |
| 105 | BUG_ON(atomic64_cmpxchg(&v, v2, v1) != v0); |
| 106 | BUG_ON(v.counter != r); |
| 107 | |
| 108 | INIT(v0); |
Luca Barbieri | 9efbcd5 | 2010-03-01 19:55:45 +0100 | [diff] [blame] | 109 | BUG_ON(atomic64_add_unless(&v, one, v0)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 110 | BUG_ON(v.counter != r); |
| 111 | |
| 112 | INIT(v0); |
Luca Barbieri | 9efbcd5 | 2010-03-01 19:55:45 +0100 | [diff] [blame] | 113 | BUG_ON(!atomic64_add_unless(&v, one, v1)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 114 | r += one; |
| 115 | BUG_ON(v.counter != r); |
| 116 | |
Heiko Carstens | 007d086 | 2010-06-04 14:15:02 -0700 | [diff] [blame] | 117 | #if defined(CONFIG_X86) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \ |
Will Deacon | c58bbd3 | 2010-07-08 10:59:59 +0100 | [diff] [blame] | 118 | defined(CONFIG_S390) || defined(_ASM_GENERIC_ATOMIC64_H) || defined(CONFIG_ARM) |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 119 | INIT(onestwos); |
| 120 | BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1)); |
| 121 | r -= one; |
| 122 | BUG_ON(v.counter != r); |
| 123 | |
| 124 | INIT(0); |
| 125 | BUG_ON(atomic64_dec_if_positive(&v) != -one); |
| 126 | BUG_ON(v.counter != r); |
| 127 | |
| 128 | INIT(-one); |
| 129 | BUG_ON(atomic64_dec_if_positive(&v) != (-one - one)); |
| 130 | BUG_ON(v.counter != r); |
Luca Barbieri | 8f4f202 | 2010-02-26 12:22:40 +0100 | [diff] [blame] | 131 | #else |
| 132 | #warning Please implement atomic64_dec_if_positive for your architecture, and add it to the IF above |
| 133 | #endif |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 134 | |
| 135 | INIT(onestwos); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 136 | BUG_ON(!atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 137 | r += one; |
| 138 | BUG_ON(v.counter != r); |
| 139 | |
| 140 | INIT(0); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 141 | BUG_ON(atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 142 | BUG_ON(v.counter != r); |
| 143 | |
| 144 | INIT(-one); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 145 | BUG_ON(!atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 146 | r += one; |
| 147 | BUG_ON(v.counter != r); |
| 148 | |
| 149 | #ifdef CONFIG_X86 |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 150 | printk(KERN_INFO "atomic64 test passed for %s platform %s CX8 and %s SSE\n", |
| 151 | #ifdef CONFIG_X86_64 |
| 152 | "x86-64", |
| 153 | #elif defined(CONFIG_X86_CMPXCHG64) |
| 154 | "i586+", |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 155 | #else |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 156 | "i386+", |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 157 | #endif |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 158 | boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without", |
| 159 | boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without"); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 160 | #else |
| 161 | printk(KERN_INFO "atomic64 test passed\n"); |
| 162 | #endif |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | core_initcall(test_atomic64); |