| 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); |
| 3 | const expect = std.testing.expect; |
| 4 | |
| 5 | const supports_128_bit_atomics = switch (builtin.cpu.arch) { |
| 6 | // TODO: Ideally this could be sync'd with the logic in Sema. |
| 7 | .aarch64 => true, |
| 8 | .aarch64_be => false, // Fails due to LLVM issues. |
| 9 | .x86_64 => builtin.cpu.has(.x86, .cx16), |
| 10 | else => false, |
| 11 | }; |
| 12 | |
| 13 | test "cmpxchg" { |
| 14 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 15 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 16 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 17 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 18 | |
| 19 | try testCmpxchg(); |
| 20 | try comptime testCmpxchg(); |
| 21 | } |
| 22 | |
| 23 | fn testCmpxchg() !void { |
| 24 | var x: i32 = 1234; |
| 25 | if (@cmpxchgWeak(i32, &x, 99, 5678, .seq_cst, .seq_cst)) |x1| { |
| 26 | try expect(x1 == 1234); |
| 27 | } else { |
| 28 | @panic("cmpxchg should have failed"); |
| 29 | } |
| 30 | |
| 31 | while (@cmpxchgWeak(i32, &x, 1234, 5678, .seq_cst, .seq_cst)) |x1| { |
| 32 | try expect(x1 == 1234); |
| 33 | } |
| 34 | try expect(x == 5678); |
| 35 | |
| 36 | try expect(@cmpxchgStrong(i32, &x, 5678, 42, .seq_cst, .seq_cst) == null); |
| 37 | try expect(x == 42); |
| 38 | } |
| 39 | |
| 40 | test "atomicrmw and atomicload" { |
| 41 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 42 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 43 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 44 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 45 | if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; |
| 46 | |
| 47 | var data: u8 = 200; |
| 48 | try testAtomicRmw(&data); |
| 49 | try expect(data == 42); |
| 50 | try testAtomicLoad(&data); |
| 51 | } |
| 52 | |
| 53 | fn testAtomicRmw(ptr: *u8) !void { |
| 54 | const prev_value = @atomicRmw(u8, ptr, .Xchg, 42, .seq_cst); |
| 55 | try expect(prev_value == 200); |
| 56 | comptime { |
| 57 | var x: i32 = 1234; |
| 58 | const y: i32 = 12345; |
| 59 | try expect(@atomicLoad(i32, &x, .seq_cst) == 1234); |
| 60 | try expect(@atomicLoad(i32, &y, .seq_cst) == 12345); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | fn testAtomicLoad(ptr: *u8) !void { |
| 65 | const x = @atomicLoad(u8, ptr, .seq_cst); |
| 66 | try expect(x == 42); |
| 67 | } |
| 68 | |
| 69 | test "cmpxchg with ptr" { |
| 70 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 71 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 72 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 73 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 74 | if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; |
| 75 | |
| 76 | var data1: i32 = 1234; |
| 77 | var data2: i32 = 5678; |
| 78 | var data3: i32 = 9101; |
| 79 | var x: *i32 = &data1; |
| 80 | if (@cmpxchgWeak(*i32, &x, &data2, &data3, .seq_cst, .seq_cst)) |x1| { |
| 81 | try expect(x1 == &data1); |
| 82 | } else { |
| 83 | @panic("cmpxchg should have failed"); |
| 84 | } |
| 85 | |
| 86 | while (@cmpxchgWeak(*i32, &x, &data1, &data3, .seq_cst, .seq_cst)) |x1| { |
| 87 | try expect(x1 == &data1); |
| 88 | } |
| 89 | try expect(x == &data3); |
| 90 | |
| 91 | try expect(@cmpxchgStrong(*i32, &x, &data3, &data2, .seq_cst, .seq_cst) == null); |
| 92 | try expect(x == &data2); |
| 93 | } |
| 94 | |
| 95 | test "cmpxchg with ignored result" { |
| 96 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 97 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 98 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 99 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 100 | |
| 101 | var x: i32 = 1234; |
| 102 | |
| 103 | _ = @cmpxchgStrong(i32, &x, 1234, 5678, .monotonic, .monotonic); |
| 104 | |
| 105 | try expect(5678 == x); |
| 106 | } |
| 107 | |
| 108 | test "128-bit cmpxchg" { |
| 109 | // TODO: this must appear first |
| 110 | if (!supports_128_bit_atomics) return error.SkipZigTest; |
| 111 | |
| 112 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 113 | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO |
| 114 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 115 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 116 | |
| 117 | try test_u128_cmpxchg(); |
| 118 | try comptime test_u128_cmpxchg(); |
| 119 | } |
| 120 | |
| 121 | fn test_u128_cmpxchg() !void { |
| 122 | var x: u128 align(16) = 1234; |
| 123 | if (@cmpxchgWeak(u128, &x, 99, 5678, .seq_cst, .seq_cst)) |x1| { |
| 124 | try expect(x1 == 1234); |
| 125 | } else { |
| 126 | @panic("cmpxchg should have failed"); |
| 127 | } |
| 128 | |
| 129 | while (@cmpxchgWeak(u128, &x, 1234, 5678, .seq_cst, .seq_cst)) |x1| { |
| 130 | try expect(x1 == 1234); |
| 131 | } |
| 132 | try expect(x == 5678); |
| 133 | |
| 134 | try expect(@cmpxchgStrong(u128, &x, 5678, 42, .seq_cst, .seq_cst) == null); |
| 135 | try expect(x == 42); |
| 136 | } |
| 137 | |
| 138 | var a_global_variable = @as(u32, 1234); |
| 139 | |
| 140 | test "cmpxchg on a global variable" { |
| 141 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 142 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 143 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 144 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 145 | |
| 146 | _ = @cmpxchgStrong(u32, &a_global_variable, 1234, 42, .acquire, .monotonic); |
| 147 | try expect(a_global_variable == 42); |
| 148 | } |
| 149 | |
| 150 | test "atomic load and rmw with enum" { |
| 151 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 152 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 153 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 154 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 155 | if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; // TODO |
| 156 | |
| 157 | const Value = enum(u8) { a, b, c }; |
| 158 | var x = Value.a; |
| 159 | |
| 160 | try expect(@atomicLoad(Value, &x, .seq_cst) != .b); |
| 161 | |
| 162 | _ = @atomicRmw(Value, &x, .Xchg, .c, .seq_cst); |
| 163 | try expect(@atomicLoad(Value, &x, .seq_cst) == .c); |
| 164 | try expect(@atomicLoad(Value, &x, .seq_cst) != .a); |
| 165 | try expect(@atomicLoad(Value, &x, .seq_cst) != .b); |
| 166 | } |
| 167 | |
| 168 | test "atomic store" { |
| 169 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 170 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 171 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 172 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 173 | |
| 174 | try comptime testAtomicStore(); |
| 175 | try testAtomicStore(); |
| 176 | } |
| 177 | |
| 178 | fn testAtomicStore() !void { |
| 179 | var x: u32 = 0; |
| 180 | @atomicStore(u32, &x, 1, .seq_cst); |
| 181 | try expect(@atomicLoad(u32, &x, .seq_cst) == 1); |
| 182 | @atomicStore(u32, &x, 12345678, .seq_cst); |
| 183 | try expect(@atomicLoad(u32, &x, .seq_cst) == 12345678); |
| 184 | } |
| 185 | |
| 186 | test "atomicrmw with floats" { |
| 187 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 188 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 189 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 190 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 191 | if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; // TODO |
| 192 | |
| 193 | try testAtomicRmwFloat(); |
| 194 | try comptime testAtomicRmwFloat(); |
| 195 | } |
| 196 | |
| 197 | fn testAtomicRmwFloat() !void { |
| 198 | var x: f32 = 0; |
| 199 | try expect(x == 0); |
| 200 | _ = @atomicRmw(f32, &x, .Xchg, 1, .seq_cst); |
| 201 | try expect(x == 1); |
| 202 | _ = @atomicRmw(f32, &x, .Add, 5, .seq_cst); |
| 203 | try expect(x == 6); |
| 204 | _ = @atomicRmw(f32, &x, .Sub, 2, .seq_cst); |
| 205 | try expect(x == 4); |
| 206 | _ = @atomicRmw(f32, &x, .Max, 13, .seq_cst); |
| 207 | try expect(x == 13); |
| 208 | _ = @atomicRmw(f32, &x, .Min, 42, .seq_cst); |
| 209 | try expect(x == 13); |
| 210 | } |
| 211 | |
| 212 | test "atomicrmw with ints" { |
| 213 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 214 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 215 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 216 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 217 | if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; |
| 218 | |
| 219 | try testAtomicRmwInts(); |
| 220 | try comptime testAtomicRmwInts(); |
| 221 | } |
| 222 | |
| 223 | fn testAtomicRmwInts() !void { |
| 224 | // TODO: Use the max atomic bit size for the target, maybe builtin? |
| 225 | try testAtomicRmwInt(.unsigned, 8); |
| 226 | |
| 227 | if (builtin.cpu.arch == .x86_64) { |
| 228 | try testAtomicRmwInt(.unsigned, 16); |
| 229 | try testAtomicRmwInt(.unsigned, 32); |
| 230 | try testAtomicRmwInt(.unsigned, 64); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | fn testAtomicRmwInt(comptime signedness: std.builtin.Signedness, comptime N: usize) !void { |
| 235 | const int = @Int(signedness, N); |
| 236 | |
| 237 | var x: int = 1; |
| 238 | var res = @atomicRmw(int, &x, .Xchg, 3, .seq_cst); |
| 239 | try expect(x == 3 and res == 1); |
| 240 | |
| 241 | res = @atomicRmw(int, &x, .Add, 3, .seq_cst); |
| 242 | var y: int = 3; |
| 243 | try expect(res == y); |
| 244 | y = y + 3; |
| 245 | try expect(x == y); |
| 246 | |
| 247 | res = @atomicRmw(int, &x, .Sub, 1, .seq_cst); |
| 248 | try expect(res == y); |
| 249 | y = y - 1; |
| 250 | try expect(x == y); |
| 251 | |
| 252 | res = @atomicRmw(int, &x, .And, 4, .seq_cst); |
| 253 | try expect(res == y); |
| 254 | y = y & 4; |
| 255 | try expect(x == y); |
| 256 | |
| 257 | res = @atomicRmw(int, &x, .Nand, 4, .seq_cst); |
| 258 | try expect(res == y); |
| 259 | y = ~(y & 4); |
| 260 | try expect(x == y); |
| 261 | |
| 262 | res = @atomicRmw(int, &x, .Or, 6, .seq_cst); |
| 263 | try expect(res == y); |
| 264 | y = y | 6; |
| 265 | try expect(x == y); |
| 266 | |
| 267 | res = @atomicRmw(int, &x, .Xor, 2, .seq_cst); |
| 268 | try expect(res == y); |
| 269 | y = y ^ 2; |
| 270 | try expect(x == y); |
| 271 | |
| 272 | res = @atomicRmw(int, &x, .Max, 1, .seq_cst); |
| 273 | try expect(res == y); |
| 274 | y = @max(y, 1); |
| 275 | try expect(x == y); |
| 276 | |
| 277 | res = @atomicRmw(int, &x, .Min, 1, .seq_cst); |
| 278 | try expect(res == y); |
| 279 | y = @min(y, 1); |
| 280 | try expect(x == y); |
| 281 | } |
| 282 | |
| 283 | test "atomicrmw with 128-bit ints" { |
| 284 | // TODO: this must appear first |
| 285 | if (!supports_128_bit_atomics) return error.SkipZigTest; |
| 286 | |
| 287 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 288 | |
| 289 | try testAtomicRmwInt128(.signed); |
| 290 | try testAtomicRmwInt128(.unsigned); |
| 291 | try comptime testAtomicRmwInt128(.signed); |
| 292 | try comptime testAtomicRmwInt128(.unsigned); |
| 293 | } |
| 294 | |
| 295 | fn testAtomicRmwInt128(comptime signedness: std.builtin.Signedness) !void { |
| 296 | const uint = @Int(.unsigned, 128); |
| 297 | const int = @Int(signedness, 128); |
| 298 | |
| 299 | const initial: int = @as(int, @bitCast(@as(uint, 0xaaaaaaaa_bbbbbbbb_cccccccc_dddddddd))); |
| 300 | const replacement: int = 0x00000000_00000005_00000000_00000003; |
| 301 | |
| 302 | var x: int align(16) = initial; |
| 303 | var res = @atomicRmw(int, &x, .Xchg, replacement, .seq_cst); |
| 304 | try expect(x == replacement and res == initial); |
| 305 | |
| 306 | var operator: int = 0x00000001_00000000_20000000_00000000; |
| 307 | res = @atomicRmw(int, &x, .Add, operator, .seq_cst); |
| 308 | var y: int = replacement; |
| 309 | try expect(res == y); |
| 310 | y = y + operator; |
| 311 | try expect(x == y); |
| 312 | |
| 313 | operator = 0x00000000_10000000_00000000_20000000; |
| 314 | res = @atomicRmw(int, &x, .Sub, operator, .seq_cst); |
| 315 | try expect(res == y); |
| 316 | y = y - operator; |
| 317 | try expect(x == y); |
| 318 | |
| 319 | operator = 0x12345678_87654321_12345678_87654321; |
| 320 | res = @atomicRmw(int, &x, .And, operator, .seq_cst); |
| 321 | try expect(res == y); |
| 322 | y = y & operator; |
| 323 | try expect(x == y); |
| 324 | |
| 325 | operator = 0x00000000_10000000_00000000_20000000; |
| 326 | res = @atomicRmw(int, &x, .Nand, operator, .seq_cst); |
| 327 | try expect(res == y); |
| 328 | y = ~(y & operator); |
| 329 | try expect(x == y); |
| 330 | |
| 331 | operator = 0x12340000_56780000_67890000_98760000; |
| 332 | res = @atomicRmw(int, &x, .Or, operator, .seq_cst); |
| 333 | try expect(res == y); |
| 334 | y = y | operator; |
| 335 | try expect(x == y); |
| 336 | |
| 337 | operator = 0x0a0b0c0d_0e0f0102_03040506_0708090a; |
| 338 | res = @atomicRmw(int, &x, .Xor, operator, .seq_cst); |
| 339 | try expect(res == y); |
| 340 | y = y ^ operator; |
| 341 | try expect(x == y); |
| 342 | |
| 343 | operator = 0x00000000_10000000_00000000_20000000; |
| 344 | res = @atomicRmw(int, &x, .Max, operator, .seq_cst); |
| 345 | try expect(res == y); |
| 346 | y = @max(y, operator); |
| 347 | try expect(x == y); |
| 348 | |
| 349 | res = @atomicRmw(int, &x, .Min, operator, .seq_cst); |
| 350 | try expect(res == y); |
| 351 | y = @min(y, operator); |
| 352 | try expect(x == y); |
| 353 | } |
| 354 | |
| 355 | test "atomics with different types" { |
| 356 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 357 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 358 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 359 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 360 | if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; |
| 361 | |
| 362 | try testAtomicsWithType(bool, true, false); |
| 363 | |
| 364 | try testAtomicsWithType(u1, 0, 1); |
| 365 | try testAtomicsWithType(i4, 2, 1); |
| 366 | try testAtomicsWithType(u5, 2, 1); |
| 367 | try testAtomicsWithType(i15, 2, 1); |
| 368 | try testAtomicsWithType(u24, 2, 1); |
| 369 | |
| 370 | try testAtomicsWithType(u0, 0, 0); |
| 371 | |
| 372 | try testAtomicsWithType(enum(u32) { x = 1234, y = 5678 }, .x, .y); |
| 373 | try testAtomicsWithType(enum(u19) { x = 1234, y = 5678 }, .x, .y); |
| 374 | |
| 375 | try testAtomicsWithPackedStruct( |
| 376 | packed struct { x: u7, y: u24, z: bool }, |
| 377 | .{ .x = 1, .y = 2, .z = true }, |
| 378 | .{ .x = 3, .y = 4, .z = false }, |
| 379 | ); |
| 380 | try testAtomicsWithPackedStruct( |
| 381 | packed struct { x: u19, y: bool }, |
| 382 | .{ .x = 1, .y = true }, |
| 383 | .{ .x = 3, .y = false }, |
| 384 | ); |
| 385 | } |
| 386 | |
| 387 | fn testAtomicsWithType(comptime T: type, a: T, b: T) !void { |
| 388 | var x: T = b; |
| 389 | @atomicStore(T, &x, a, .seq_cst); |
| 390 | try expect(x == a); |
| 391 | try expect(@atomicLoad(T, &x, .seq_cst) == a); |
| 392 | try expect(@atomicRmw(T, &x, .Xchg, b, .seq_cst) == a); |
| 393 | try expect(@cmpxchgStrong(T, &x, b, a, .seq_cst, .seq_cst) == null); |
| 394 | if (@sizeOf(T) != 0) |
| 395 | try expect(@cmpxchgStrong(T, &x, b, a, .seq_cst, .seq_cst).? == a); |
| 396 | } |
| 397 | |
| 398 | fn testAtomicsWithPackedStruct(comptime T: type, a: T, b: T) !void { |
| 399 | const BackingInt = @typeInfo(T).@"struct".backing_integer.?; |
| 400 | var x: T = b; |
| 401 | @atomicStore(T, &x, a, .seq_cst); |
| 402 | try expect(@as(BackingInt, @bitCast(x)) == @as(BackingInt, @bitCast(a))); |
| 403 | try expect(@as(BackingInt, @bitCast(@atomicLoad(T, &x, .seq_cst))) == @as(BackingInt, @bitCast(a))); |
| 404 | try expect(@as(BackingInt, @bitCast(@atomicRmw(T, &x, .Xchg, b, .seq_cst))) == @as(BackingInt, @bitCast(a))); |
| 405 | try expect(@cmpxchgStrong(T, &x, b, a, .seq_cst, .seq_cst) == null); |
| 406 | if (@sizeOf(T) != 0) |
| 407 | try expect(@as(BackingInt, @bitCast(@cmpxchgStrong(T, &x, b, a, .seq_cst, .seq_cst).?)) == @as(BackingInt, @bitCast(a))); |
| 408 | } |
| 409 | |
| 410 | test "return @atomicStore, using it as a void value" { |
| 411 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 412 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 413 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 414 | |
| 415 | const S = struct { |
| 416 | const A = struct { |
| 417 | value: usize, |
| 418 | |
| 419 | pub fn store(self: *A, value: usize) void { |
| 420 | return @atomicStore(usize, &self.value, value, .unordered); |
| 421 | } |
| 422 | |
| 423 | pub fn store2(self: *A, value: usize) void { |
| 424 | return switch (value) { |
| 425 | else => @atomicStore(usize, &self.value, value, .unordered), |
| 426 | }; |
| 427 | } |
| 428 | }; |
| 429 | |
| 430 | fn doTheTest() !void { |
| 431 | var x: A = .{ .value = 5 }; |
| 432 | x.store(10); |
| 433 | try expect(x.value == 10); |
| 434 | x.store(100); |
| 435 | try expect(x.value == 100); |
| 436 | } |
| 437 | }; |
| 438 | try S.doTheTest(); |
| 439 | try comptime S.doTheTest(); |
| 440 | } |