# Details

- Reviewers
rjmccall leonardchan bjope

# Diff Detail

- Repository
- rG LLVM Github Monorepo
- Build Status
**Buildable 44680**Build 46087: arc lint + arc unit

### Event Timeline

clang/lib/Basic/FixedPoint.cpp | ||
---|---|---|

242 | If the maximum expressible value is *k*, and the fully-precise multiplication yields *k+e* for some epsilon *e* that isn't representable in the result semantics, is that considered an overflow? If so, I think you need to do the shift after these bound checks, since the shift destroys the difference between *k* and *k+e*. That is, unless there's a compelling mathematical argument that it's not possible to overflow only in the fully-precision multiplication — but while I think that's possibly true of Would this algorithm be simpler if you took advantage of the fact that |

clang/lib/Basic/FixedPoint.cpp | ||
---|---|---|

242 |
I don't think I would consider that to be overflow; that's precision loss. E-C considers these to be different:
There is also no value of
It's likely possible to use APFixedPoint in the calculations here, but I used APInt to make the behavior explicit and not accidentally be dependent on the behavior of APFixedPoint's conversions or operations. |

clang/lib/Basic/FixedPoint.cpp | ||
---|---|---|

242 | Although.,. I guess I see your point in that an intermediate result of k+e technically "does not fit within the range of the fixed-point type"... but I wonder if treating such cases as overflow is particularly meaningful. I don't find there to be much of a distinction between such a case and the case where the exact result lands inbetween two representable values. We just end up with a less precise result. |

clang/lib/Basic/FixedPoint.cpp | ||
---|---|---|

242 | Right, I was wondering if there was an accepted answer here. For saturating arithmetic, it's equivalent to truncate this extra precision down to *k* or to saturate to the maximum representable value, since by assumption that was just *k*; but for non-saturating arithmetic, it affects whether the operation has UB. All else being the same, it's better to have fewer corner-case sources of UB. My read is that Embedded C is saying there's a sequence here: compute the exact mathematical result; round that to the precision of the result type; the operation overflows if the rounded result is not representable in the result type. Is the rounding direction completely unspecified, down to being potentially operand-specific? If so, we could just say that we always round to avoid overflow if possible. The main consideration here is that we need to give the operation the same semantics statically and dynamically, and I don't know if there's any situation where those semantics would affect the performance of the operation when done dynamically. |

clang/lib/Basic/FixedPoint.cpp | ||
---|---|---|

242 |
I'm fairly sure that the conclusions here about k and e only hold if k truly is the maximum representable value. If k is anything else (even epsilon-of-the-representable-range less), k+e can never be greater than the maximum. And actually, crunching the numbers on this... If we have integers a and b of width N, sign extended to the double bitwidth A and B, there can be no values for a and b for which A*B is greater than N_Max<<N ( I'm unsure if this reasoning applies in the minimum case as well.
I wonder if it's intended to be a sequence. It's starting to feel like it can't actually be both cases at the same time. |

clang/lib/Basic/FixedPoint.cpp | ||
---|---|---|

242 |
I think you mean the scale instead of 01.11 (1.75) x 10.01 (2.25) ------------- 11.1111 (3.9375) -> shr 2 -> 11.11 (3.75) where the our My interpretation of the spec (which could be wrong) is whenever they refer to "source value", they mean the exact mathematical result ( |

clang/lib/Basic/FixedPoint.cpp | ||
---|---|---|

242 | Your logic only works if the entire integer is scaled, i.e. for Here's a program to brute-force search for counter-examples for an arbitrary unsigned fixed-point type: https://gist.github.com/rjmccall/562c2c7c9d289edd8cdf034edd6c1f17 |

clang/lib/Basic/FixedPoint.cpp | ||
---|---|---|

242 |
Yes, you're absolutely correct, big mistake on my part. Realized that I'd made the mistake the same day but stuff got in the way of responding :)
I agree with the interpretation of "source value". This is still a bit uncertain for me, though. Can they really occur simultaneously? Aren't we just considering the overflow case first rather than the precision loss/rounding case first? If we instead rounded down first (the shift) and then checked overflow, it wouldn't be UB. It feels like a common case to get this kind of result. All that happened during the operation was that we lost precision. Is it really worth considering it to be UB? |

clang/lib/Basic/FixedPoint.cpp | ||
---|---|---|

242 | Well, like I said up-thread, since the spec doesn't seem to impose any constraints on rounding at all, I think we can just define it such that we always round to avoid overflow if possible. For saturating math, it's the same either way, since we either (1) "round to avoid overflow" and thus only see a maximal/minimal value or (2) we detect overflow and thus substitute the maximal/minimal value. For non-saturating math, it changes whether UB formally occurs, which I think affects three areas: - C++
`constexpr`, which isn't allowed to invoke UB. Abstractly, it's better to accept more programs here instead of emitting really pedantic errors about unrepresentable overflows.
- Fixed-point intrinsics which check whether UB occurred dynamically. I don't think we have any of these today, but we might add them someday — among other things, I think the UBSan people would say that UBSan should have a check for this, which would require such an intrinsic. It's not unlikely that this will complicate the implementation because we won't be able to simply consider whether the underlying integer operation overflowed.
- High-level optimizations that exploit the UB-ness of non-saturating overflow. For example, it is abstractly true that
`x * C > x`when`x`is known to be strictly positive and`C`is a constant greater than 1, but if we define rounding as avoiding overflow, there might be corner cases where this isn't true for some`C`. I'm not sure we would ever do any optimizations like this, but if we did, they'd probably have to be more conservative in some cases.
So it's really a judgment call for you folks, one that you need to make with an understanding of where you want to take this feature. |

clang/lib/Basic/FixedPoint.cpp | ||
---|---|---|

242 | Okay, these are good points. I think I'm starting to agree with the idea of avoiding overflow if possible. I was a bit concerned that it might be a bit too strong of a claim to make, for example if there were cases where it would be more natural for a particular calculation to detect an overflow rather than round and avoid it. But I'm starting to wonder if there really are any such cases. I would also be fine with simply defining the order in which we perform the operations; round first, then check for overflow. That would be in line with the order it's written in the spec, but I don't know if that was how it was intended to work. We'll see what Leonard has to say. |

clang/lib/Basic/FixedPoint.cpp | ||
---|---|---|

242 | I think for simplicity and since this doesn't seem to actively go against the spec, it would be good to do rounding then overflow check in that sense. Going on a tangent (I don't remember if this was brought up before, but do remind me if there was a consensus on this): let's say we have a target that defines rounding to always be towards positive infinity for their multiplication intrinsics. Currently in this patch, I believe the default is always going to be rounding towards negative infinity from right shifting after the multiplication. To match the static calculation behavior against the dynamic intrinsics, would it be better to add a field in Something that's been bothering me with this is that if we wanted to do something like |

clang/lib/Basic/FixedPoint.cpp | ||
---|---|---|

242 | You're right that if we have targets with divergent rounding semantics, we'll probably need to represent that in the FixedPointSemantics — and yeah, the results could then be target-specific. |

clang/lib/Basic/FixedPoint.cpp | ||
---|---|---|

242 |
Well, rounding direction isn't just a problem in consteval. If a target's legal fixed-point multiplication instructions all round up, but we somehow get an illegal intrinsic and the expansion code is invoked, that particular runtime operation will round down instead since the default expansion lowering rounds down. It's really hard to balance the complexity of this without running wires through the entire compilation pipeline for managing rounding direction. That's why it's a bit more straightforward to state that the rounding is indeterminate, but keeping it consistent in the general case, say, if a target doesn't have any opinion on fixed-point operation rounding (which most targets probably don't). Rounding down just happens to be simpler than rounding up (for multiplication, anyway) so that's a fair choice. Maybe rounding is something we should look into in the future, though. |

Since we've settled on not considering that to be overflow, yeah, I think the patch is fine. Might be worth being explicit about that at the point you do the shift: that it's known that this discards precision that could leave the true mathematical value outside of the expressible range, and that we are interpreting the spec as allowing us to round to avoid this formal overflow in order to avoid unnecessary UB.