public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: LIU Hao <lh_mouse@126.com>
To: Stefan Kanthak <stefan.kanthak@nexgo.de>, gcc@gnu.org
Subject: Re: Widening multiplication, but no narrowing division [i386/AMD64]
Date: Mon, 9 Jan 2023 21:10:53 +0800	[thread overview]
Message-ID: <dd434f54-975d-7cd3-5609-3bd22509ae14@126.com> (raw)
In-Reply-To: <554A1354252F43BB8915A74129C41BE3@H270>


[-- Attachment #1.1: Type: text/plain, Size: 874 bytes --]

在 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 #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

  reply	other threads:[~2023-01-09 13:11 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-09 12:20 Stefan Kanthak
2023-01-09 13:10 ` LIU Hao [this message]
2023-01-09 13:19   ` Stefan Kanthak
2023-01-09 14:23 ` Paul Koning
2023-01-09 15:20   ` Stefan Kanthak
2023-01-09 15:30     ` Paul Koning
2023-01-09 16:27       ` Stefan Kanthak
2023-01-10 16:49         ` Paul Koning

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=dd434f54-975d-7cd3-5609-3bd22509ae14@126.com \
    --to=lh_mouse@126.com \
    --cc=gcc@gnu.org \
    --cc=stefan.kanthak@nexgo.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).