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