| ... | ... | @@ -251,31 +251,61 @@ test "atomicrmw with ints" { |
| 251 | 251 | return error.SkipZigTest; |
| 252 | 252 | } |
| 253 | 253 | |
| 254 | | try testAtomicRmwInt(); |
| 255 | | comptime try testAtomicRmwInt(); |
| 254 | // TODO: https://github.com/ziglang/zig/issues/13989 |
| 255 | const bit_values = [_]usize{ 8, 16, 32, 64 };// ++ if (builtin.zig_backend != .stage2_c) [_]usize{ } else [_]usize{ 128 }; |
| 256 | inline for (bit_values) |bits| { |
| 257 | try testAtomicRmwInt(.unsigned, bits); |
| 258 | comptime try testAtomicRmwInt(.unsigned, bits); |
| 259 | } |
| 256 | 260 | } |
| 257 | 261 | |
| 258 | | fn testAtomicRmwInt() !void { |
| 259 | | var x: u8 = 1; |
| 260 | | var res = @atomicRmw(u8, &x, .Xchg, 3, .SeqCst); |
| 262 | fn testAtomicRmwInt(comptime signedness: std.builtin.Signedness, comptime N: usize) !void { |
| 263 | const int = std.meta.Int(signedness, N); |
| 264 | |
| 265 | var x: int = 1; |
| 266 | var res = @atomicRmw(int, &x, .Xchg, 3, .SeqCst); |
| 261 | 267 | try expect(x == 3 and res == 1); |
| 262 | | _ = @atomicRmw(u8, &x, .Add, 3, .SeqCst); |
| 263 | | try expect(x == 6); |
| 264 | | _ = @atomicRmw(u8, &x, .Sub, 1, .SeqCst); |
| 265 | | try expect(x == 5); |
| 266 | | _ = @atomicRmw(u8, &x, .And, 4, .SeqCst); |
| 267 | | try expect(x == 4); |
| 268 | | _ = @atomicRmw(u8, &x, .Nand, 4, .SeqCst); |
| 269 | | try expect(x == 0xfb); |
| 270 | | _ = @atomicRmw(u8, &x, .Or, 6, .SeqCst); |
| 271 | | try expect(x == 0xff); |
| 272 | | _ = @atomicRmw(u8, &x, .Xor, 2, .SeqCst); |
| 273 | | try expect(x == 0xfd); |
| 274 | | |
| 275 | | _ = @atomicRmw(u8, &x, .Max, 1, .SeqCst); |
| 276 | | try expect(x == 0xfd); |
| 277 | | _ = @atomicRmw(u8, &x, .Min, 1, .SeqCst); |
| 278 | | try expect(x == 1); |
| 268 | |
| 269 | res = @atomicRmw(int, &x, .Add, 3, .SeqCst); |
| 270 | var y: int = 3; |
| 271 | try expect(res == y); |
| 272 | y = y + 3; |
| 273 | try expect(x == y); |
| 274 | |
| 275 | res = @atomicRmw(int, &x, .Sub, 1, .SeqCst); |
| 276 | try expect(res == y); |
| 277 | y = y - 1; |
| 278 | try expect(x == y); |
| 279 | |
| 280 | res = @atomicRmw(int, &x, .And, 4, .SeqCst); |
| 281 | try expect(res == y); |
| 282 | y = y & 4; |
| 283 | try expect(x == y); |
| 284 | |
| 285 | res = @atomicRmw(int, &x, .Nand, 4, .SeqCst); |
| 286 | try expect(res == y); |
| 287 | y = ~(y & 4); |
| 288 | try expect(x == y); |
| 289 | |
| 290 | res = @atomicRmw(int, &x, .Or, 6, .SeqCst); |
| 291 | try expect(res == y); |
| 292 | y = y | 6; |
| 293 | try expect(x == y); |
| 294 | |
| 295 | res = @atomicRmw(int, &x, .Xor, 2, .SeqCst); |
| 296 | try expect(res == y); |
| 297 | y = y ^ 2; |
| 298 | try expect(x == y); |
| 299 | |
| 300 | res = @atomicRmw(int, &x, .Max, 1, .SeqCst); |
| 301 | try expect(res == y); |
| 302 | y = @max(y, 1); |
| 303 | try expect(x == y); |
| 304 | |
| 305 | res = @atomicRmw(int, &x, .Min, 1, .SeqCst); |
| 306 | try expect(res == y); |
| 307 | y = @min(y, 1); |
| 308 | try expect(x == y); |
| 279 | 309 | } |
| 280 | 310 | |
| 281 | 311 | test "atomics with different types" { |