aboutsummaryrefslogtreecommitdiff
path: root/lib/compiler_rt/muldi3.zig
blob: a51c6c7b76f0e697c162db37c2a1db0b1da8ec3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Ported from
//! https://github.com/llvm/llvm-project/blob/llvmorg-9.0.0/compiler-rt/lib/builtins/muldi3.c

const std = @import("std");
const builtin = @import("builtin");
const native_endian = builtin.cpu.arch.endian();
const common = @import("common.zig");

pub const panic = common.panic;

comptime {
    if (common.want_aeabi) {
        @export(__aeabi_lmul, .{ .name = "__aeabi_lmul", .linkage = common.linkage });
    } else {
        @export(__muldi3, .{ .name = "__muldi3", .linkage = common.linkage });
    }
}

pub fn __muldi3(a: i64, b: i64) callconv(.C) i64 {
    return mul(a, b);
}

fn __aeabi_lmul(a: i64, b: i64) callconv(.AAPCS) i64 {
    return mul(a, b);
}

inline fn mul(a: i64, b: i64) i64 {
    const x = dwords{ .all = a };
    const y = dwords{ .all = b };
    var r = dwords{ .all = muldsi3(x.s.low, y.s.low) };
    r.s.high +%= x.s.high *% y.s.low +% x.s.low *% y.s.high;
    return r.all;
}

const dwords = extern union {
    all: i64,
    s: switch (native_endian) {
        .Little => extern struct {
            low: u32,
            high: u32,
        },
        .Big => extern struct {
            high: u32,
            low: u32,
        },
    },
};

fn muldsi3(a: u32, b: u32) i64 {
    const bits_in_word_2 = @sizeOf(i32) * 8 / 2;
    const lower_mask = (~@as(u32, 0)) >> bits_in_word_2;

    var r: dwords = undefined;
    r.s.low = (a & lower_mask) *% (b & lower_mask);
    var t: u32 = r.s.low >> bits_in_word_2;
    r.s.low &= lower_mask;
    t += (a >> bits_in_word_2) *% (b & lower_mask);
    r.s.low +%= (t & lower_mask) << bits_in_word_2;
    r.s.high = t >> bits_in_word_2;
    t = r.s.low >> bits_in_word_2;
    r.s.low &= lower_mask;
    t +%= (b >> bits_in_word_2) *% (a & lower_mask);
    r.s.low +%= (t & lower_mask) << bits_in_word_2;
    r.s.high +%= t >> bits_in_word_2;
    r.s.high +%= (a >> bits_in_word_2) *% (b >> bits_in_word_2);
    return r.all;
}

test {
    _ = @import("muldi3_test.zig");
}