Widening multiplication, but no narrowing division [i386/AMD64]

LIU Hao lh_mouse@126.com
Mon Jan 9 13:10:53 GMT 2023


在 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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 840 bytes
Desc: OpenPGP digital signature
URL: <https://gcc.gnu.org/pipermail/gcc/attachments/20230109/46a74172/attachment.sig>


More information about the Gcc mailing list