mbutrovich commented on code in PR #12232:
URL: https://github.com/apache/arrow-rs/pull/12232#discussion_r4588435954


##########
arrow-buffer/src/divisor.rs:
##########
@@ -470,20 +513,389 @@ unsigned_divisor!(
     u64,
     u128
 );
-signed_divisor!(
+/// Expands to the arguments of `signed_divisor!` that compute with `$wide`, a 
type twice as wide
+/// as `$t`
+macro_rules! signed_divisor_wide {
+    ($name:ident, $power_of_two:ident, $add:ident, $t:ty, $wide:ty) => {
+        signed_divisor!(
+            $name,
+            $power_of_two,
+            $add,
+            $t,
+            [const],
+            // m = 1 + floor(2^(N + l - 1) / |divisor|) is between 2^(N - 1) 
and 2^N, so m - 2^N
+            // fits in the narrow type
+            |magnitude, l| {
+                let m = 1 + (1 << (<$t>::BITS + l - 1)) / magnitude as $wide;
+                (m - (1 << <$t>::BITS)) as $t
+            },
+            // `n + MULSH(m', n)` in figure 5.1
+            |m, n| n + ((m as $wide * n as $wide) >> <$t>::BITS) as $t,
+            []
+        );
+    };
+}
+
+signed_divisor_wide!(
     DivisorI32,
     PowerOfTwoDivisorI32,
     AddIndicatorDivisorI32,
     i32,
     i64
 );
-signed_divisor!(
+signed_divisor_wide!(
     DivisorI64,
     PowerOfTwoDivisorI64,
     AddIndicatorDivisorI64,
     i64,
     i128
 );
+signed_divisor!(

Review Comment:
   > Could these be `pub(crate)` until the decimal kernels in #11421 use them?
   
   The kernels in #11421 live in `arrow-arith` and `arrow-cast`, which can't 
see a `pub(crate)` item in `arrow-buffer`. The first kernel PR would have to 
make these types `pub`, so the API would be settled there instead. #11467 added 
the 32-bit and 64-bit divisors the same way, before any kernel used them, and 
#12076 then used them unchanged.
   
   > If those kernels end up wanting a different shape (a narrow variant, a 
`const` constructor, etc.) we'd be free to change it.
   
   We can add both of those later without a breaking change. The enums are 
[`#[non_exhaustive]`](https://github.com/apache/arrow-rs/blob/cb2844e35fbebf73f045966c9f66c3d47e997d47/arrow-buffer/src/divisor.rs#L81),
 so a new variant is additive. Making `new` a `const fn` doesn't break callers 
either.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to