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


##########
arrow-cast/src/cast/decimal.rs:
##########
@@ -226,11 +257,12 @@ where
         // div is >= 10 and so this cannot overflow
         let (d, r) = x.div_rem_wrapping(div);
 
-        // Round result
-        let adjusted = match x >= I::Native::ZERO {
-            true if r >= half => d.add_wrapping(I::Native::ONE),
-            false if r <= half_neg => d.sub_wrapping(I::Native::ONE),
-            _ => d,
+        // Round half away from zero. Whether a value rounds depends on the
+        // data, so select the result instead of branching on it.
+        let adjusted = if x >= I::Native::ZERO {
+            std::hint::select_unpredictable(r >= half, 
d.add_wrapping(I::Native::ONE), d)
+        } else {
+            std::hint::select_unpredictable(r <= half_neg, 
d.sub_wrapping(I::Native::ONE), d)
         };

Review Comment:
   The benchmark data for these casts is all non-negative. 
[`create_decimal256_array`](https://github.com/apache/arrow-rs/blob/ec32e61a6b52e26c4d8c077b9b4b21c4dc12ad6c/arrow/src/util/bench_util.rs#L205-L215)
 and `create_decimal128_array` draw from `0..1_000_000_000`, so the `if x >= 
I::Native::ZERO` branch is always predicted. The only data-dependent test the 
benchmarks exercise is the remainder comparison.
   
   The sign test is still a branch at this commit. On aarch64 (rustc 1.100, 
release profile, no LTO), the per-row body of the `Decimal256` to `Decimal128` 
`unary_opt` cast tests the sign of `x` right after the `div_rem` call:
   
   ```
   tbnz  x21, #63, LBB1047_2   ; x21 = high word of x
   ```
   
   The `Decimal128` to `Decimal256` downscale has the same `tbnz x21, #63`. 
With mixed-sign values, that branch mispredicts on about half the rows, which 
is the problem `select_unpredictable` fixes for the remainder.
   
   Could we add a `cast_kernels` case with mixed-sign values, and one with 
well-predicted remainders (for example, every value a multiple of 
10^delta_scale), and report both? 
[AGENTS.md](https://github.com/apache/arrow-rs/blob/ec32e61a6b52e26c4d8c077b9b4b21c4dc12ad6c/AGENTS.md?plain=1#L61-L63)
 asks performance PRs to cover both kinds of input and to name the inputs that 
get slower. On well-predicted remainders the old branch may beat the select.
   
   If the mixed-sign case shows the sign branch, selecting on the sign as well 
removes it. With the change below, both instances choose on the sign with a 
`csel` (`cmn x21, #1` and `csel ..., gt` for `i256`, `cmp x21, #0` and `csel 
..., mi` for `i128`), and the rounding has no branch left. The cost is that 
both comparisons run on every row, so the all-positive cases would show whether 
that matters.
   
   ```suggestion
           let adjusted = std::hint::select_unpredictable(
               x >= I::Native::ZERO,
               std::hint::select_unpredictable(r >= half, 
d.add_wrapping(I::Native::ONE), d),
               std::hint::select_unpredictable(r <= half_neg, 
d.sub_wrapping(I::Native::ONE), d),
           );
   ```



-- 
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