Why x86_64 divq is not used for 128-bit by 64-bit division?

Hi there,

Let’s have this C code:

unsigned long div(unsigned __int128 n, unsigned long d)
return n / d;

I would assume that the divq is the perfect match here. But the compiler generates the
code that calls the __udivti3 procedure which performs 128-bit by 128-bit division.

Why is divq not used here?

  • Paweł

divq only produces a 64-bit result. There’s way for the compiler to know you didn’t divide a greater than 64-bit number by 1 or some other value that required a large result.

In my example, I’m truncating the result.

But anyway, are you saying that when the result of divq does not fit in 64-bit the result stored in the register is undefined?

Actually, it generates a #DE exception if the result doesn't fit.