| ... | ... | @@ -1,6 +1,8 @@ |
| 1 | 1 | // Builtin functions that operate on integer types |
| 2 | 2 | const builtin = @import("builtin"); |
| 3 | 3 | const testing = @import("std").testing; |
| 4 | const maxInt = @import("std").math.maxInt; |
| 5 | const minInt = @import("std").math.maxInt; |
| 4 | 6 | |
| 5 | 7 | const udivmod = @import("udivmod.zig").udivmod; |
| 6 | 8 | |
| ... | ... | @@ -578,3 +580,61 @@ fn test_one_umodsi3(a: u32, b: u32, expected_r: u32) void { |
| 578 | 580 | const r: u32 = __umodsi3(a, b); |
| 579 | 581 | testing.expect(r == expected_r); |
| 580 | 582 | } |
| 583 | |
| 584 | pub fn __mulsi3(a: i32, b: i32) callconv(.C) i32 { |
| 585 | @setRuntimeSafety(builtin.is_test); |
| 586 | |
| 587 | var ua = @bitCast(u32, a); |
| 588 | var ub = @bitCast(u32, b); |
| 589 | var r: u32 = 0; |
| 590 | |
| 591 | while (ua > 0) { |
| 592 | if ((ua & 1) != 0) r +%= ub; |
| 593 | ua >>= 1; |
| 594 | ub <<= 1; |
| 595 | } |
| 596 | |
| 597 | return @bitCast(i32, r); |
| 598 | } |
| 599 | |
| 600 | fn test_one_mulsi3(a: i32, b: i32, result: i32) void { |
| 601 | testing.expectEqual(result, __mulsi3(a, b)); |
| 602 | } |
| 603 | |
| 604 | test "mulsi3" { |
| 605 | test_one_mulsi3(0, 0, 0); |
| 606 | test_one_mulsi3(0, 1, 0); |
| 607 | test_one_mulsi3(1, 0, 0); |
| 608 | test_one_mulsi3(0, 10, 0); |
| 609 | test_one_mulsi3(10, 0, 0); |
| 610 | test_one_mulsi3(0, maxInt(i32), 0); |
| 611 | test_one_mulsi3(maxInt(i32), 0, 0); |
| 612 | test_one_mulsi3(0, -1, 0); |
| 613 | test_one_mulsi3(-1, 0, 0); |
| 614 | test_one_mulsi3(0, -10, 0); |
| 615 | test_one_mulsi3(-10, 0, 0); |
| 616 | test_one_mulsi3(0, minInt(i32), 0); |
| 617 | test_one_mulsi3(minInt(i32), 0, 0); |
| 618 | test_one_mulsi3(1, 1, 1); |
| 619 | test_one_mulsi3(1, 10, 10); |
| 620 | test_one_mulsi3(10, 1, 10); |
| 621 | test_one_mulsi3(1, maxInt(i32), maxInt(i32)); |
| 622 | test_one_mulsi3(maxInt(i32), 1, maxInt(i32)); |
| 623 | test_one_mulsi3(1, -1, -1); |
| 624 | test_one_mulsi3(1, -10, -10); |
| 625 | test_one_mulsi3(-10, 1, -10); |
| 626 | test_one_mulsi3(1, minInt(i32), minInt(i32)); |
| 627 | test_one_mulsi3(minInt(i32), 1, minInt(i32)); |
| 628 | test_one_mulsi3(46340, 46340, 2147395600); |
| 629 | test_one_mulsi3(-46340, 46340, -2147395600); |
| 630 | test_one_mulsi3(46340, -46340, -2147395600); |
| 631 | test_one_mulsi3(-46340, -46340, 2147395600); |
| 632 | test_one_mulsi3(4194303, 8192, @truncate(i32, 34359730176)); |
| 633 | test_one_mulsi3(-4194303, 8192, @truncate(i32, -34359730176)); |
| 634 | test_one_mulsi3(4194303, -8192, @truncate(i32, -34359730176)); |
| 635 | test_one_mulsi3(-4194303, -8192, @truncate(i32, 34359730176)); |
| 636 | test_one_mulsi3(8192, 4194303, @truncate(i32, 34359730176)); |
| 637 | test_one_mulsi3(-8192, 4194303, @truncate(i32, -34359730176)); |
| 638 | test_one_mulsi3(8192, -4194303, @truncate(i32, -34359730176)); |
| 639 | test_one_mulsi3(-8192, -4194303, @truncate(i32, 34359730176)); |
| 640 | } |