public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Stefan Kanthak" <stefan.kanthak@nexgo.de>
To: "Paul Koning" <paulkoning@comcast.net>
Cc: <gcc@gnu.org>
Subject: Re: Widening multiplication, but no narrowing division [i386/AMD64]
Date: Mon, 9 Jan 2023 17:27:12 +0100	[thread overview]
Message-ID: <78300C8F5F0D4B7B96FB1EFB713C5575@H270> (raw)
In-Reply-To: <46F8B35E-93C9-4B76-90C1-217291D6509A@comcast.net>

"Paul Koning" <paulkoning@comcast.net> wrote:

>> On Jan 9, 2023, at 10:20 AM, Stefan Kanthak <stefan.kanthak@nexgo.de> wrote:
>> 
>> "Paul Koning" <paulkoning@comcast.net> wrote:
>> 
>>>> On Jan 9, 2023, at 7:20 AM, Stefan Kanthak <stefan.kanthak@nexgo.de> wrote:
>>>> 
>>>> Hi,
>>>> 
>>>> GCC (and other C compilers too) support the widening multiplication
>>>> of i386/AMD64 processors, but DON'T support their narrowing division:
>>> 
>>> I wonder if this changed in the recent past.
>>> I have a pattern for this type of thing in pdp11.md:
>> [...]
>>> and I'm pretty sure this worked at some point in the past.  
>> 
>> Unfortunately the C standard defines that the smaller operand (of lesser
>> conversion rank), here divisor, has to undergo a conversion to the "real
>> common type", i.e. the broader operand (of higher conversion rank), here
>> dividend. Unless the information about promotion/conversion is handed over
>> to the code generator it can't apply such patterns -- as demonstrated by
>> the demo code.

> Yes, I was thinking the same.  But I spent a while on that pattern -- I
> wanted to support div/mod as a single operation because the machine has
> that primitive.  And I'm pretty sure I saw it work before I committed
> that change.  That's why I'm wondering if something changed.

I can't tell from the past how GCC once worked, but today it can't
(or doesn't) use such patterns, at least not on i386/AMD64 processors.
To give another example where the necessary information is most
obviously NOT propagated from front end to back end:

--- clmul.c ---
// widening carry-less multiplication

unsigned long long clmul(unsigned long p, unsigned long q)
{
    unsigned long long r = 0;
    unsigned long      s = 1UL << 31;

    do {
        r <<= 1;
        if (q & s)
#ifdef _MSC_VER
            (unsigned long) r ^= p;
#else
            r ^= p; // no need to promote/convert p here!
#endif
    } while (s >>= 1);

    return r;
}
--- EOF ---

# https://gcc.godbolt.org/z/E99v7fEP3
clmul(unsigned long, unsigned long):
        push    ebp
        mov     ecx, -2147483648
        xor     eax, eax
        xor     edx, edx
        push    edi        # OOPS: superfluous
        xor     edi, edi   # OOPS: superfluous
        push    esi
        push    ebx        # OUCH: WTF?
        mov     ebp, DWORD PTR [esp+24]
        mov     ebx, 32    # OUCH: WTF?
        mov     esi, DWORD PTR [esp+20]
.L3:
        shld    edx, eax, 1
        add     eax, eax
        test    ebp, ecx
        je      .L2
        xor     eax, esi
        xor     edx, edi   # OOPS: superfluous
.L2:
        shr     ecx, 1
        sub     ebx, 1     # OUCH: WTF?
        jne     .L3
        pop     ebx        # OUCH: WTF?
        pop     esi
        pop     edi        # OOPS: superfluous
        pop     ebp
        ret

8 superfluous instructions out of the total 25 instructions!

NOT AMUSED
Stefan

  reply	other threads:[~2023-01-09 16:32 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
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 [this message]
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=78300C8F5F0D4B7B96FB1EFB713C5575@H270 \
    --to=stefan.kanthak@nexgo.de \
    --cc=gcc@gnu.org \
    --cc=paulkoning@comcast.net \
    /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).