From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22019 invoked by alias); 12 Jun 2009 18:47:05 -0000 Received: (qmail 22009 invoked by uid 22791); 12 Jun 2009 18:47:04 -0000 X-SWARE-Spam-Status: No, hits=-2.0 required=5.0 tests=AWL,BAYES_00,J_CHICKENPOX_41,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: sourceware.org Received: from mx2.redhat.com (HELO mx2.redhat.com) (66.187.237.31) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 12 Jun 2009 18:46:54 +0000 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n5CIkriE016362; Fri, 12 Jun 2009 14:46:53 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n5CIkpZt013216; Fri, 12 Jun 2009 14:46:52 -0400 Received: from zebedee.pink (vpn-12-83.rdu.redhat.com [10.11.12.83]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n5CIkoJa026640; Fri, 12 Jun 2009 14:46:50 -0400 Message-ID: <4A32A299.9050009@redhat.com> Date: Fri, 12 Jun 2009 18:47:00 -0000 From: Andrew Haley User-Agent: Thunderbird 2.0.0.17 (X11/20081009) MIME-Version: 1.0 To: Zachary Turner CC: gcc@gcc.gnu.org Subject: Re: naked functions on x86 architecture References: <478231340906120920m74bd9c0i88861d5e759a8fb1@mail.gmail.com> <4A328311.2020803@gmail.com> <478231340906121025g57c31c71o8bf9d7fffaa8b0e8@mail.gmail.com> <4A3292C6.5020708@redhat.com> <478231340906121056p46e2cb33gcfefe0ca633a2e01@mail.gmail.com> In-Reply-To: <478231340906121056p46e2cb33gcfefe0ca633a2e01@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org X-SW-Source: 2009-06/txt/msg00278.txt.bz2 Zachary Turner wrote: > 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. I still don't get it. A gcc asm version of this is ------------------------------------------------------------------------- template intptr_t scas(T *a, T val, int len); template<> intptr_t scas(uint8_t *a, uint8_t val, int len) { intptr_t result; __asm__ ("rep scasb" : "=D"(result): "a"(val), "D"(a), "c"(len)); return result; } template int find_first_nonzero_scas(T* x, int cnt) { intptr_t result = 0; result = scas(x, 0, cnt); result -= reinterpret_cast(x); result /= sizeof(T); return --result; } ------------------------------------------------------------------------- which, when instantiated, generates int find_first_nonzero_scas(unsigned char*, int): movq %rdi, %rdx xorl %eax, %eax movl %esi, %ecx notq %rdx rep scasb leaq (%rdx,%rdi), %rax ret How is this not better in every way ? I can understand that you want something compatible with your source. But you said "I don't think anyone has ever presented a good example of where [naked asms are] really really useful on x86 architectures." Baffled, Andrew.