public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Zachary Turner <divisortheory@gmail.com>
To: Andrew Haley <aph@redhat.com>
Cc: gcc@gcc.gnu.org
Subject: Re: naked functions on x86 architecture
Date: Fri, 12 Jun 2009 17:56:00 -0000	[thread overview]
Message-ID: <478231340906121056p46e2cb33gcfefe0ca633a2e01@mail.gmail.com> (raw)
In-Reply-To: <4A3292C6.5020708@redhat.com>

On Fri, Jun 12, 2009 at 12:39 PM, Andrew Haley<aph@redhat.com> wrote:
> Zachary Turner wrote:
>> On Fri, Jun 12, 2009 at 11:32 AM, Paolo Bonzini<paolo.bonzini@gmail.com> wrote:
>>>> This is one example, but it illustrates a general concept that I think
>>>> is really useful and I personally have used numerous times for lots of
>>>> other instructions than SCAS.  If there is a way to achieve this
>>>> without using a naked function then please advise.
>>> Keeping the __asm syntax, I'd be surprised if this did not work:
>>>
>>> template<typename T>
>>> int find_first_nonzero_scas(T* x, int cnt)
>>> {
>>>    int result = 0;
>>>    __asm {
>>>        xor eax, eax
>>>        mov edi, x
>>>        mov ecx, cnt
>>>    }
>>>    if (sizeof (T) == 1)
>>>      __asm { rep scasb; mov result, edi }
>>>    if (sizeof (T) == 2)
>>>      __asm { rep scasw; mov result, edi }
>>>    if (sizeof (T) == 4)
>>>      __asm { rep scasl; mov result, edi }
>>>    result -= reinterpret_cast<int>(x);
>>>    result /= sizeof(T);
>>>    return --result;
>>> }
>
>> Sorry about the asm syntax, I still haven't used inline assembly in
>> gcc so I haven't looked at the syntax yet.  I was just going to start
>> porting over some code to work on gcc when I started looking into the
>> naked issue.
>>
>> That being said, what you suggest will indeed work, and be optimized
>> to be as efficient as the template method.  It's what I'll probably
>> end up doing as a fallback.  But it's very ugly, and there are a
>> couple of cases where I have much more inline assembly than in this
>> particular example.  So I have to litter segments of code like that
>> all throughout the function.  I suppose I could wrap it in a macro for
>> readability, but its' nicer if it's just integrated with C++ like
>> everything else.  Its supported for many other platforms, it just
>> seems a little odd to explicitly not support on the most common
>> platform.
>
> I've never quite understood why anyone would want naked asm functions in
> C source code.  You have an assembler, and it's trivial to write the
> functions you want in assembly language.  Well, apart from the name
> mangling, but that's pretty simple.
>
> And even then, surely you'd be much better off with an inline asm than
> calling a naked function.
>
> Andrew.
>
>
>

I guess the same reason people would want any asm functions in C
source code.  Sometimes it's just the best way to express something.
Like in the example I mentioned, I could write 4 different functions
in assembly, one for each size suffix, wrap them all up in a separate
assembly language file but IMHO it's more readable, quicker to code,
and more expressive to use a template switch like I've done.  C++ is
built on the philosophy of giving you enough rope to hang yourself
with.

I don't think there's a better way to express the selection of an
instruction based on operand size than through a naked template
specialization.

Using a .s file is more difficult to port across different compilers.
Many compilers provide support for naked functions and it's easy to
just use a #ifdef to check which compiler you're running on and define
the appropriate naked declaration string.

Besides, it's supported for embedded architectures, it's frustrating
because it feels like back in the days of a 386SX's where the
processors had working FPUs on them but they were switched off "just
because".  All the investment has already been done to add support for
naked functions, so I think people should be "permitted" to use it,
even if other people feel like they should be using something else.

  reply	other threads:[~2009-06-12 17:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-12 16:20 Zachary Turner
2009-06-12 16:32 ` Paolo Bonzini
2009-06-12 17:25   ` Zachary Turner
2009-06-12 17:39     ` Andrew Haley
2009-06-12 17:56       ` Zachary Turner [this message]
2009-06-12 18:47         ` Andrew Haley

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=478231340906121056p46e2cb33gcfefe0ca633a2e01@mail.gmail.com \
    --to=divisortheory@gmail.com \
    --cc=aph@redhat.com \
    --cc=gcc@gcc.gnu.org \
    /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).