On Fri, Jun 12, 2009 at 12:39 PM, Andrew Haley<a...@redhat.com> wrote: > Zachary Turner wrote: >> On Fri, Jun 12, 2009 at 11:32 AM, Paolo Bonzini<paolo.bonz...@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.