Adding integer ceil and floor in Std Dialect

Right now, there is an implementation of integer ceil and floor “hidden” in AffineToStandard.cpp, which is used to lower the affine expressions (which has the floordiv and ceildiv).

This is not avail for regular computations (only for affine lowering), and only support positive denominator. In ONNX, we have need for regular computations with negative nominator and/or denominator

Would there be objections to add regular floordiv and ceildiv in the std dialect? I have worked an algorithm that works for positive/negative nominator/denominator, and uses only “integer divide” and “select” operations.

floorDiv(n, m) {
  x = (n<0) ? -1 : 1
  return (n*m<0) ? - ((-n+x) / m) -1 : n / m

ceilDiv(n, m) {
  x = (n > 0) ? -1 : 1
  return (n*m>0) ? ((n+x) / m) + 1 : - (-n / m)

where the “/” is the normal c++ divide integer operation.

1 Like