| @@ -9,8 +9,10 @@ |
| 9 | 9 | //! limbs, and a small int never equals a big one. |
| 10 | 10 | //! |
| 11 | 11 | //! The inline fast paths in `tilrt.h` (`tl_int_add`, `tl_int_sub`, |
| 12 | | −//! `tl_int_mul`) handle two small ints that do not overflow; everything |
| 13 | | −//! else comes here. Big arithmetic goes through `num-bigint`, as in the |
| 12 | +//! `tl_int_mul`, `tl_int_div`, `tl_int_rem`, `tl_int_eq`, |
| 13 | +//! `tl_int_compare`) handle two small ints whose result is small; |
| 14 | +//! everything else (and a division by zero) comes here, to the `_slow` |
| 15 | +//! functions. Big arithmetic goes through `num-bigint`, as in the |
| 14 | 16 | //! interpreter (`tilc_core::int`), so both backends compute, print and |
| 15 | 17 | //! hash alike. Like every `@extern` function these borrow their arguments. |
| 16 | 18 | |
| @@ -99,8 +101,10 @@ pub(crate) unsafe fn saturating(v: Value) -> i64 { |
| 99 | 101 | } |
| 100 | 102 | } |
| 101 | 103 | |
| 102 | | −/// A binary operation on two `Int`s: `small` when both are small (in 128 |
| 103 | | −/// bits, where it cannot overflow), `big` otherwise. |
| 104 | +/// A binary operation on two `Int`s: `small` when both are small, `big` |
| 105 | +/// otherwise. `small` computes in 64 bits, where 63-bit operands only |
| 106 | +/// overflow a product (it returns `None` then). Not in 128 bits: a |
| 107 | +/// 128-bit division is a library call, several times slower. |
| 104 | 108 | /// |
| 105 | 109 | /// # Safety |
| 106 | 110 | /// `a` and `b` must be live `Int`s. |
| @@ -108,11 +112,14 @@ unsafe fn binary( |
| 108 | 112 | ctx: *mut Ctx, |
| 109 | 113 | a: Value, |
| 110 | 114 | b: Value, |
| 111 | | − small: impl Fn(i128, i128) -> i128, |
| 115 | + small: impl Fn(i64, i64) -> Option<i64>, |
| 112 | 116 | big: impl Fn(BigInt, BigInt) -> BigInt, |
| 113 | 117 | ) -> Value { |
| 114 | | − if !is_ptr(a) && !is_ptr(b) { |
| 115 | | − return from_i128(ctx, small(i128::from(int_get(a)), i128::from(int_get(b)))); |
| 118 | + if !is_ptr(a) |
| 119 | + && !is_ptr(b) |
| 120 | + && let Some(r) = small(int_get(a), int_get(b)) |
| 121 | + { |
| 122 | + return from_i128(ctx, i128::from(r)); |
| 116 | 123 | } |
| 117 | 124 | // SAFETY: forwarded caller guarantee. |
| 118 | 125 | let (x, y) = unsafe { (to_big(a), to_big(b)) }; |
| @@ -127,7 +134,7 @@ unsafe fn binary( |
| 127 | 134 | #[unsafe(no_mangle)] |
| 128 | 135 | pub unsafe extern "C" fn tl_int_add_slow(ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 129 | 136 | // SAFETY: forwarded caller guarantee. |
| 130 | | − unsafe { binary(ctx, a, b, |x, y| x + y, |x, y| x + y) } |
| 137 | + unsafe { binary(ctx, a, b, |x, y| Some(x + y), |x, y| x + y) } |
| 131 | 138 | } |
| 132 | 139 | |
| 133 | 140 | /// # Safety |
| @@ -135,7 +142,7 @@ pub unsafe extern "C" fn tl_int_add_slow(ctx: *mut Ctx, a: Value, b: Value) -> V |
| 135 | 142 | #[unsafe(no_mangle)] |
| 136 | 143 | pub unsafe extern "C" fn tl_int_sub_slow(ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 137 | 144 | // SAFETY: forwarded caller guarantee. |
| 138 | | − unsafe { binary(ctx, a, b, |x, y| x - y, |x, y| x - y) } |
| 145 | + unsafe { binary(ctx, a, b, |x, y| Some(x - y), |x, y| x - y) } |
| 139 | 146 | } |
| 140 | 147 | |
| 141 | 148 | /// # Safety |
| @@ -143,7 +150,7 @@ pub unsafe extern "C" fn tl_int_sub_slow(ctx: *mut Ctx, a: Value, b: Value) -> V |
| 143 | 150 | #[unsafe(no_mangle)] |
| 144 | 151 | pub unsafe extern "C" fn tl_int_mul_slow(ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 145 | 152 | // SAFETY: forwarded caller guarantee. |
| 146 | | − unsafe { binary(ctx, a, b, |x, y| x * y, |x, y| x * y) } |
| 153 | + unsafe { binary(ctx, a, b, i64::checked_mul, |x, y| x * y) } |
| 147 | 154 | } |
| 148 | 155 | |
| 149 | 156 | /// Truncating division; panics on division by zero. |
| @@ -151,12 +158,12 @@ pub unsafe extern "C" fn tl_int_mul_slow(ctx: *mut Ctx, a: Value, b: Value) -> V |
| 151 | 158 | /// # Safety |
| 152 | 159 | /// `ctx` must be the running actor's context; `a` and `b` live `Int`s. |
| 153 | 160 | #[unsafe(no_mangle)] |
| 154 | | −pub unsafe extern "C" fn tl_int_div(ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 161 | +pub unsafe extern "C" fn tl_int_div_slow(ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 155 | 162 | if b == int(0) { |
| 156 | 163 | return panic_with(ctx, "division by zero"); |
| 157 | 164 | } |
| 158 | 165 | // SAFETY: forwarded caller guarantee. |
| 159 | | − unsafe { binary(ctx, a, b, |x, y| x / y, |x, y| x / y) } |
| 166 | + unsafe { binary(ctx, a, b, |x, y| Some(x / y), |x, y| x / y) } |
| 160 | 167 | } |
| 161 | 168 | |
| 162 | 169 | /// The remainder of the truncating division (the sign of `a`); panics on |
| @@ -165,18 +172,18 @@ pub unsafe extern "C" fn tl_int_div(ctx: *mut Ctx, a: Value, b: Value) -> Value |
| 165 | 172 | /// # Safety |
| 166 | 173 | /// `ctx` must be the running actor's context; `a` and `b` live `Int`s. |
| 167 | 174 | #[unsafe(no_mangle)] |
| 168 | | −pub unsafe extern "C" fn tl_int_rem(ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 175 | +pub unsafe extern "C" fn tl_int_rem_slow(ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 169 | 176 | if b == int(0) { |
| 170 | 177 | return panic_with(ctx, "division by zero"); |
| 171 | 178 | } |
| 172 | 179 | // SAFETY: forwarded caller guarantee. |
| 173 | | − unsafe { binary(ctx, a, b, |x, y| x % y, |x, y| x % y) } |
| 180 | + unsafe { binary(ctx, a, b, |x, y| Some(x % y), |x, y| x % y) } |
| 174 | 181 | } |
| 175 | 182 | |
| 176 | 183 | /// # Safety |
| 177 | 184 | /// `a` and `b` must be live `Int`s. |
| 178 | 185 | #[unsafe(no_mangle)] |
| 179 | | −pub unsafe extern "C" fn tl_int_eq(_ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 186 | +pub unsafe extern "C" fn tl_int_eq_slow(_ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 180 | 187 | // Canonical forms: a small int equals only the same word. |
| 181 | 188 | if a == b || !is_ptr(a) || !is_ptr(b) { |
| 182 | 189 | return boolean(a == b); |
| @@ -188,7 +195,7 @@ pub unsafe extern "C" fn tl_int_eq(_ctx: *mut Ctx, a: Value, b: Value) -> Value |
| 188 | 195 | /// # Safety |
| 189 | 196 | /// `a` and `b` must be live `Int`s. |
| 190 | 197 | #[unsafe(no_mangle)] |
| 191 | | −pub unsafe extern "C" fn tl_int_compare(_ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 198 | +pub unsafe extern "C" fn tl_int_compare_slow(_ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 192 | 199 | if !is_ptr(a) && !is_ptr(b) { |
| 193 | 200 | return order(int_get(a).cmp(&int_get(b))); |
| 194 | 201 | } |