Re: very simple questions about integer division

  • From: Mike Pall <mike-1208@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Fri, 31 Aug 2012 11:57:47 +0200

Francesco Abbate wrote:
> c = math.floor(a / b)
> but I was wondering what was the better way to do it with LuaJIT2 to
> be sure that the code is properly optimized.

The latter is definitely easier to handle. However it's not
optimized at all right now.

Sure, I could turn this into an integer division with some effort.
But then all architecture-specific backends need to be changed,
too. I don't know whether the limited use cases and the limited
performance gain for integer division over FP division makes this
worth the bother.

> For me it is also quite annoying that there isn't simple way to do
> integer division.

You can do it with FFI int64_t/uint64_t: print(42LL/5LL) --> 8LL
But these are 64 bit divisions, which are more expensive than FP
divisions or 32 bit divisions.

> I'm also missing the
> divmod operator that returns the quotient *and* the remainder. I've
> added a divmod function myself in the math module but I'm afraid that
> this could be not well optimised by LuaJIT2.

Only adding a function to the library doesn't help. You'll need to
teach the compiler how to handle this function and change all of
the backends to implement this function (unless it can be constructed
from existing IR instructions).


Other related posts: