在 2023/1/9 20:20, Stefan Kanthak 写道:
Hi,

GCC (and other C compilers too) support the widening multiplication
of i386/AMD64 processors, but DON'T support their narrowing division:



QWORD-DWORD division would change the behavior of your program.


Given:

   ```
   uint32_t xdiv(uint64_t x, uint32_t y) { return x / y;  }
   ```

then `xdiv(0x200000002, 2)` should first convert both operands to `uint64_t`, perform the division which yields `0x100000001`, then truncate the quotient to 32-bit which gives `1`. The result is exact.


If DIV was used, it would effect an exception:

   ```
   mov edx, 2
   mov eax, edx   # edx:eax = 0x200000002

   mov ecx, edx
   div ecx        # division overflows because the quotient
                  # can't stored into EAX
   ```





--
Best regards,
LIU Hao

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

Reply via email to