| ... | ... | @@ -4,6 +4,14 @@ const minInt = std.math.minInt; |
| 4 | 4 | const maxInt = std.math.maxInt; |
| 5 | 5 | const expect = std.testing.expect; |
| 6 | 6 | |
| 7 | fn testSatAdd(comptime T: type, lhs: T, rhs: T, expected: T) !void { |
| 8 | try expect((lhs +| rhs) == expected); |
| 9 | |
| 10 | var x = lhs; |
| 11 | x +|= rhs; |
| 12 | try expect(x == expected); |
| 13 | } |
| 14 | |
| 7 | 15 | test "saturating add" { |
| 8 | 16 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 9 | 17 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| ... | ... | @@ -28,32 +36,23 @@ test "saturating add" { |
| 28 | 36 | try testSatAdd(u2, 3, 2, 3); |
| 29 | 37 | try testSatAdd(u3, 7, 1, 7); |
| 30 | 38 | } |
| 31 | | |
| 32 | | fn testSatAdd(comptime T: type, lhs: T, rhs: T, expected: T) !void { |
| 33 | | try expect((lhs +| rhs) == expected); |
| 34 | | |
| 35 | | var x = lhs; |
| 36 | | x +|= rhs; |
| 37 | | try expect(x == expected); |
| 38 | | } |
| 39 | 39 | }; |
| 40 | 40 | |
| 41 | 41 | try S.doTheTest(); |
| 42 | 42 | try comptime S.doTheTest(); |
| 43 | 43 | |
| 44 | | try comptime S.testSatAdd(comptime_int, 0, 0, 0); |
| 45 | | try comptime S.testSatAdd(comptime_int, -1, 1, 0); |
| 46 | | try comptime S.testSatAdd(comptime_int, 3, 2, 5); |
| 47 | | try comptime S.testSatAdd(comptime_int, -3, -2, -5); |
| 48 | | try comptime S.testSatAdd(comptime_int, 3, -2, 1); |
| 49 | | try comptime S.testSatAdd(comptime_int, -3, 2, -1); |
| 50 | | try comptime S.testSatAdd(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 1119305249183743626545271163355074748512); |
| 51 | | try comptime S.testSatAdd(comptime_int, 7, -593423721213448152027139550640105366508, -593423721213448152027139550640105366501); |
| 44 | try comptime testSatAdd(comptime_int, 0, 0, 0); |
| 45 | try comptime testSatAdd(comptime_int, -1, 1, 0); |
| 46 | try comptime testSatAdd(comptime_int, 3, 2, 5); |
| 47 | try comptime testSatAdd(comptime_int, -3, -2, -5); |
| 48 | try comptime testSatAdd(comptime_int, 3, -2, 1); |
| 49 | try comptime testSatAdd(comptime_int, -3, 2, -1); |
| 50 | try comptime testSatAdd(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 1119305249183743626545271163355074748512); |
| 51 | try comptime testSatAdd(comptime_int, 7, -593423721213448152027139550640105366508, -593423721213448152027139550640105366501); |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | test "saturating add 128bit" { |
| 55 | 55 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 56 | | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO |
| 57 | 56 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 58 | 57 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 59 | 58 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| ... | ... | @@ -65,19 +64,20 @@ test "saturating add 128bit" { |
| 65 | 64 | try testSatAdd(i128, minInt(i128), maxInt(i128), -1); |
| 66 | 65 | try testSatAdd(u128, maxInt(u128), 1, maxInt(u128)); |
| 67 | 66 | } |
| 68 | | fn testSatAdd(comptime T: type, lhs: T, rhs: T, expected: T) !void { |
| 69 | | try expect((lhs +| rhs) == expected); |
| 70 | | |
| 71 | | var x = lhs; |
| 72 | | x +|= rhs; |
| 73 | | try expect(x == expected); |
| 74 | | } |
| 75 | 67 | }; |
| 76 | 68 | |
| 77 | 69 | try S.doTheTest(); |
| 78 | 70 | try comptime S.doTheTest(); |
| 79 | 71 | } |
| 80 | 72 | |
| 73 | fn testSatSub(comptime T: type, lhs: T, rhs: T, expected: T) !void { |
| 74 | try expect((lhs -| rhs) == expected); |
| 75 | |
| 76 | var x = lhs; |
| 77 | x -|= rhs; |
| 78 | try expect(x == expected); |
| 79 | } |
| 80 | |
| 81 | 81 | test "saturating subtraction" { |
| 82 | 82 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 83 | 83 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| ... | ... | @@ -101,32 +101,23 @@ test "saturating subtraction" { |
| 101 | 101 | try testSatSub(u8, 10, 3, 7); |
| 102 | 102 | try testSatSub(u8, 0, 255, 0); |
| 103 | 103 | } |
| 104 | | |
| 105 | | fn testSatSub(comptime T: type, lhs: T, rhs: T, expected: T) !void { |
| 106 | | try expect((lhs -| rhs) == expected); |
| 107 | | |
| 108 | | var x = lhs; |
| 109 | | x -|= rhs; |
| 110 | | try expect(x == expected); |
| 111 | | } |
| 112 | 104 | }; |
| 113 | 105 | |
| 114 | 106 | try S.doTheTest(); |
| 115 | 107 | try comptime S.doTheTest(); |
| 116 | 108 | |
| 117 | | try comptime S.testSatSub(comptime_int, 0, 0, 0); |
| 118 | | try comptime S.testSatSub(comptime_int, 1, 1, 0); |
| 119 | | try comptime S.testSatSub(comptime_int, 3, 2, 1); |
| 120 | | try comptime S.testSatSub(comptime_int, -3, -2, -1); |
| 121 | | try comptime S.testSatSub(comptime_int, 3, -2, 5); |
| 122 | | try comptime S.testSatSub(comptime_int, -3, 2, -5); |
| 123 | | try comptime S.testSatSub(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 182846383813587550256162760261375991602); |
| 124 | | try comptime S.testSatSub(comptime_int, 7, -593423721213448152027139550640105366508, 593423721213448152027139550640105366515); |
| 109 | try comptime testSatSub(comptime_int, 0, 0, 0); |
| 110 | try comptime testSatSub(comptime_int, 1, 1, 0); |
| 111 | try comptime testSatSub(comptime_int, 3, 2, 1); |
| 112 | try comptime testSatSub(comptime_int, -3, -2, -1); |
| 113 | try comptime testSatSub(comptime_int, 3, -2, 5); |
| 114 | try comptime testSatSub(comptime_int, -3, 2, -5); |
| 115 | try comptime testSatSub(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 182846383813587550256162760261375991602); |
| 116 | try comptime testSatSub(comptime_int, 7, -593423721213448152027139550640105366508, 593423721213448152027139550640105366515); |
| 125 | 117 | } |
| 126 | 118 | |
| 127 | 119 | test "saturating subtraction 128bit" { |
| 128 | 120 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 129 | | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO |
| 130 | 121 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 131 | 122 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 132 | 123 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| ... | ... | @@ -138,14 +129,6 @@ test "saturating subtraction 128bit" { |
| 138 | 129 | try testSatSub(i128, minInt(i128), -maxInt(i128), -1); |
| 139 | 130 | try testSatSub(u128, 0, maxInt(u128), 0); |
| 140 | 131 | } |
| 141 | | |
| 142 | | fn testSatSub(comptime T: type, lhs: T, rhs: T, expected: T) !void { |
| 143 | | try expect((lhs -| rhs) == expected); |
| 144 | | |
| 145 | | var x = lhs; |
| 146 | | x -|= rhs; |
| 147 | | try expect(x == expected); |
| 148 | | } |
| 149 | 132 | }; |
| 150 | 133 | |
| 151 | 134 | try S.doTheTest(); |
| ... | ... | @@ -257,7 +240,6 @@ test "saturating mul i64, i128" { |
| 257 | 240 | |
| 258 | 241 | test "saturating multiplication" { |
| 259 | 242 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 260 | | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO |
| 261 | 243 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 262 | 244 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 263 | 245 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| ... | ... | @@ -294,6 +276,14 @@ test "saturating multiplication" { |
| 294 | 276 | try comptime testSatMul(comptime_int, 7, -593423721213448152027139550640105366508, -4153966048494137064189976854480737565556); |
| 295 | 277 | } |
| 296 | 278 | |
| 279 | fn testSatShl(comptime Lhs: type, lhs: Lhs, comptime Rhs: type, rhs: Rhs, expected: Lhs) !void { |
| 280 | try expect((lhs <<| rhs) == expected); |
| 281 | |
| 282 | var x = lhs; |
| 283 | x <<|= rhs; |
| 284 | try expect(x == expected); |
| 285 | } |
| 286 | |
| 297 | 287 | test "saturating shift-left" { |
| 298 | 288 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 299 | 289 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| ... | ... | @@ -320,23 +310,15 @@ test "saturating shift-left" { |
| 320 | 310 | try testSatShl(u8, 0, u4, 8, 0); |
| 321 | 311 | try testSatShl(u8, 3, u4, 8, maxInt(u8)); |
| 322 | 312 | } |
| 323 | | |
| 324 | | fn testSatShl(comptime Lhs: type, lhs: Lhs, comptime Rhs: type, rhs: Rhs, expected: Lhs) !void { |
| 325 | | try expect((lhs <<| rhs) == expected); |
| 326 | | |
| 327 | | var x = lhs; |
| 328 | | x <<|= rhs; |
| 329 | | try expect(x == expected); |
| 330 | | } |
| 331 | 313 | }; |
| 332 | 314 | |
| 333 | 315 | try S.doTheTest(); |
| 334 | 316 | try comptime S.doTheTest(); |
| 335 | 317 | |
| 336 | | try comptime S.testSatShl(comptime_int, 0, comptime_int, 0, 0); |
| 337 | | try comptime S.testSatShl(comptime_int, 1, comptime_int, 2, 4); |
| 338 | | try comptime S.testSatShl(comptime_int, 13, comptime_int, 150, 18554220005177478453757717602843436772975706112); |
| 339 | | try comptime S.testSatShl(comptime_int, -582769, comptime_int, 180, -893090893854873184096635538665358532628308979495815656505344); |
| 318 | try comptime testSatShl(comptime_int, 0, comptime_int, 0, 0); |
| 319 | try comptime testSatShl(comptime_int, 1, comptime_int, 2, 4); |
| 320 | try comptime testSatShl(comptime_int, 13, comptime_int, 150, 18554220005177478453757717602843436772975706112); |
| 321 | try comptime testSatShl(comptime_int, -582769, comptime_int, 180, -893090893854873184096635538665358532628308979495815656505344); |
| 340 | 322 | } |
| 341 | 323 | |
| 342 | 324 | test "saturating shift-left large rhs" { |
| ... | ... | @@ -344,7 +326,6 @@ test "saturating shift-left large rhs" { |
| 344 | 326 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; |
| 345 | 327 | if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; |
| 346 | 328 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 347 | | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; |
| 348 | 329 | |
| 349 | 330 | { |
| 350 | 331 | var lhs: u8 = undefined; |
| ... | ... | @@ -386,3 +367,59 @@ test "saturating shl uses the LHS type" { |
| 386 | 367 | |
| 387 | 368 | try expect((1 <<| @as(u8, 200)) == 1606938044258990275541962092341162602522202993782792835301376); |
| 388 | 369 | } |
| 370 | |
| 371 | test "sat add > 128 bits" { |
| 372 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 373 | |
| 374 | try testSatAdd(u140, 0, 0, 0); |
| 375 | try testSatAdd(u140, maxInt(u140), 1, maxInt(u140)); |
| 376 | try testSatAdd(u200, 1 << 150, 1 << 20, (1 << 150) + (1 << 20)); |
| 377 | try testSatAdd(u200, maxInt(u200), maxInt(u200), maxInt(u200)); |
| 378 | |
| 379 | try testSatAdd(i140, minInt(i140), -1, minInt(i140)); |
| 380 | try testSatAdd(i140, maxInt(i140), 1, maxInt(i140)); |
| 381 | try testSatAdd(i200, -1 << 150, 1 << 149, -1 << 149); |
| 382 | try testSatAdd(i200, maxInt(i200), maxInt(i200), maxInt(i200)); |
| 383 | } |
| 384 | |
| 385 | test "sat sub > 128 bits" { |
| 386 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 387 | |
| 388 | try testSatSub(u140, 0, 1, 0); |
| 389 | try testSatSub(u140, maxInt(u140), maxInt(u140), 0); |
| 390 | try testSatSub(u200, 1 << 150, 1 << 20, (1 << 150) - (1 << 20)); |
| 391 | try testSatSub(u200, maxInt(u200), 0, maxInt(u200)); |
| 392 | |
| 393 | try testSatSub(i140, minInt(i140), 1, minInt(i140)); |
| 394 | try testSatSub(i140, maxInt(i140), -1, maxInt(i140)); |
| 395 | try testSatSub(i200, -1 << 150, 1 << 149, -3 << 149); |
| 396 | try testSatSub(i200, 0, minInt(i200), maxInt(i200)); |
| 397 | } |
| 398 | |
| 399 | test "sat mul > 128 bits" { |
| 400 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 401 | |
| 402 | try testSatMul(u140, 0, maxInt(u140), 0); |
| 403 | try testSatMul(u140, 1 << 70, 1 << 69, 1 << 139); |
| 404 | try testSatMul(u200, maxInt(u200), 2, maxInt(u200)); |
| 405 | try testSatMul(u200, maxInt(u200) - 1, 1, maxInt(u200) - 1); |
| 406 | |
| 407 | try testSatMul(i140, -1, maxInt(i140), -maxInt(i140)); |
| 408 | try testSatMul(i140, minInt(i140), -1, maxInt(i140)); |
| 409 | try testSatMul(i200, 1 << 100, 1 << 99, maxInt(i200)); |
| 410 | try testSatMul(i200, -1 << 150, 1 << 30, -1 << 180); |
| 411 | } |
| 412 | |
| 413 | test "sat shl > 128 bits" { |
| 414 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 415 | |
| 416 | try testSatShl(u140, 0, u8, 17, 0); |
| 417 | try testSatShl(u140, 1 << 100, u8, 20, 1 << 120); |
| 418 | try testSatShl(u200, maxInt(u200), u8, 1, maxInt(u200)); |
| 419 | try testSatShl(u200, 1 << 199, u8, 1, maxInt(u200)); |
| 420 | |
| 421 | try testSatShl(i140, 0, u8, 17, 0); |
| 422 | try testSatShl(i140, 1 << 100, u8, 38, 1 << 138); |
| 423 | try testSatShl(i140, 1 << 100, u8, 39, maxInt(i140)); |
| 424 | try testSatShl(i200, minInt(i200) + 1, u8, 1, minInt(i200)); |
| 425 | } |