summaryrefslogtreecommitdiffstats
path: root/vendor/compiler_builtins/src/int/sdiv.rs
blob: f1822f0f86e87e1bb5bea2ec9f307873c8782045 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use int::udiv::*;

macro_rules! sdivmod {
    (
        $unsigned_fn:ident, // name of the unsigned division function
        $signed_fn:ident, // name of the signed division function
        $uX:ident, // unsigned integer type for the inputs and outputs of `$unsigned_name`
        $iX:ident, // signed integer type for the inputs and outputs of `$signed_name`
        $($attr:tt),* // attributes
    ) => {
        intrinsics! {
            #[avr_skip]
            $(
                #[$attr]
            )*
            /// Returns `n / d` and sets `*rem = n % d`
            pub extern "C" fn $signed_fn(a: $iX, b: $iX, rem: &mut $iX) -> $iX {
                let a_neg = a < 0;
                let b_neg = b < 0;
                let mut a = a;
                let mut b = b;
                if a_neg {
                    a = a.wrapping_neg();
                }
                if b_neg {
                    b = b.wrapping_neg();
                }
                let mut r = *rem as $uX;
                let t = $unsigned_fn(a as $uX, b as $uX, Some(&mut r)) as $iX;
                let mut r = r as $iX;
                if a_neg {
                    r = r.wrapping_neg();
                }
                *rem = r;
                if a_neg != b_neg {
                    t.wrapping_neg()
                } else {
                    t
                }
            }
        }
    }
}

macro_rules! sdiv {
    (
        $unsigned_fn:ident, // name of the unsigned division function
        $signed_fn:ident, // name of the signed division function
        $uX:ident, // unsigned integer type for the inputs and outputs of `$unsigned_name`
        $iX:ident, // signed integer type for the inputs and outputs of `$signed_name`
        $($attr:tt),* // attributes
    ) => {
        intrinsics! {
            #[avr_skip]
            $(
                #[$attr]
            )*
            /// Returns `n / d`
            pub extern "C" fn $signed_fn(a: $iX, b: $iX) -> $iX {
                let a_neg = a < 0;
                let b_neg = b < 0;
                let mut a = a;
                let mut b = b;
                if a_neg {
                    a = a.wrapping_neg();
                }
                if b_neg {
                    b = b.wrapping_neg();
                }
                let t = $unsigned_fn(a as $uX, b as $uX) as $iX;
                if a_neg != b_neg {
                    t.wrapping_neg()
                } else {
                    t
                }
            }
        }
    }
}

macro_rules! smod {
    (
        $unsigned_fn:ident, // name of the unsigned division function
        $signed_fn:ident, // name of the signed division function
        $uX:ident, // unsigned integer type for the inputs and outputs of `$unsigned_name`
        $iX:ident, // signed integer type for the inputs and outputs of `$signed_name`
        $($attr:tt),* // attributes
    ) => {
        intrinsics! {
            #[avr_skip]
            $(
                #[$attr]
            )*
            /// Returns `n % d`
            pub extern "C" fn $signed_fn(a: $iX, b: $iX) -> $iX {
                let a_neg = a < 0;
                let b_neg = b < 0;
                let mut a = a;
                let mut b = b;
                if a_neg {
                    a = a.wrapping_neg();
                }
                if b_neg {
                    b = b.wrapping_neg();
                }
                let r = $unsigned_fn(a as $uX, b as $uX) as $iX;
                if a_neg {
                    r.wrapping_neg()
                } else {
                    r
                }
            }
        }
    }
}

sdivmod!(
    __udivmodsi4,
    __divmodsi4,
    u32,
    i32,
    maybe_use_optimized_c_shim
);
// The `#[arm_aeabi_alias = __aeabi_idiv]` attribute cannot be made to work with `intrinsics!` in macros
intrinsics! {
    #[maybe_use_optimized_c_shim]
    #[arm_aeabi_alias = __aeabi_idiv]
    /// Returns `n / d`
    pub extern "C" fn __divsi3(a: i32, b: i32) -> i32 {
        let a_neg = a < 0;
        let b_neg = b < 0;
        let mut a = a;
        let mut b = b;
        if a_neg {
            a = a.wrapping_neg();
        }
        if b_neg {
            b = b.wrapping_neg();
        }
        let t = __udivsi3(a as u32, b as u32) as i32;
        if a_neg != b_neg {
            t.wrapping_neg()
        } else {
            t
        }
    }
}
smod!(__umodsi3, __modsi3, u32, i32, maybe_use_optimized_c_shim);

sdivmod!(
    __udivmoddi4,
    __divmoddi4,
    u64,
    i64,
    maybe_use_optimized_c_shim
);
sdiv!(__udivdi3, __divdi3, u64, i64, maybe_use_optimized_c_shim);
smod!(__umoddi3, __moddi3, u64, i64, maybe_use_optimized_c_shim);

// LLVM does not currently have a `__divmodti4` function, but GCC does
sdivmod!(
    __udivmodti4,
    __divmodti4,
    u128,
    i128,
    maybe_use_optimized_c_shim
);
sdiv!(__udivti3, __divti3, u128, i128, win64_128bit_abi_hack);
smod!(__umodti3, __modti3, u128, i128, win64_128bit_abi_hack);