Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a code written in pure C that uses the __asm__ keyword. I need to translate the assembly statements from something the gcc recognises into something the Visual C++ compiler will recognise. In Microsoft's C++ compiler, assembly instructions are invoked using the __asm keyword (not __asm__) and the instructions should be enclosed in curly braces {} not parentheses.
I'm not an assembler coder, please help :)

Here is the code I need to translate to Visual C++ :

#    define FASTDIV(a,b) \
    ({\
        int ret,dmy;\
        __asm__ volatile(\
            "mull %3"\
            :"=d"(ret),"=a"(dmy)\
            :"1"(a),"g"(ff_inverse[b])\
            );\
        ret;\
    })


#define MASK_ABS(mask, level)\
            __asm__ volatile(\
                "cltd                   \n\t"\
                "xorl %1, %0            \n\t"\
                "subl %1, %0            \n\t"\
                : "+a" (level), "=&d" (mask)\
            );


uint32_t a, d;
__asm__ volatile("rdtsc\n\t" : "=a" (a), "=d" (d));
return ((uint64_t)d << 32) + a;


I'm sure it will be an easy answer for someone who has more experience in assembly coding. Thanks in advance!
Posted

Confused of England here, how can you have "pure C" with things written in assembler?

The first thing I'd suggest doing is binning the assembler entirely and letting the compiler generate the code. For example the second one looks like a xor and subtract abs function. VC++ implements abs using exactly the same instruction stream (if I've got the slightly bent gcc assembler syntax correct).

Cheers,

Ash

PS: Whenever you see inline assembler in a piece of code try rewriting it in C. For micro-optimisations like that the compiler can often do as good a job as a human programmer. So make sure what you're using is better than the compiler generated code.

PPS: One interesting fact is that hand optimised assembly injected in a compiler's code stream usually turns off the compiler's optimiser so the generated code is often slower. I saw this a couple (er, 10 actually) years ago when an "optimised" strcpy was actually 20% slower than the compilers. While the copy was optimised everything that used it wasn't. How I would have laughed had I not been the programmer.
 
Share this answer
 
Comments
Sauro Viti 7-Oct-10 8:27am    
PPPS: Today optimizing compilers are able to produce a so deeply optimized code, that is very hard to do better by hand. Then, in the most of cases, is too much better to forget the __asm keyword, write everything in C/C++, and let the compiler do the job!
Aescleal 7-Oct-10 9:12am    
Hi Sauro,

You can quite often do the job better, but you don't do it by using micro-optimisations like those the original poster displayed. You'll never (okay, you might rarely - compilers are still crap at size optimisation on a complicated processor like an x86) beat a compiler (for speed) these days by peephole optimising individual statements.

When you've got time and mental capacity for a problem you'll often be able to beat the compiler. And if you don't you can try again. It'll take you far longer though as you won't be able to partition your understanding of the problem into different levels, you have to pay attention to all the details at the same time. As soon as you start abstracting you start moving back into the area the compiler can do better than you.

Cheers,

Ash
I know that but unfortunately this code is not mine. That is why it is in such condition. I really need this code so I can use it in MSVC environment.

Looking forward for an answer that contains MSVC code. :)
 
Share this answer
 
Comments
Aescleal 8-Oct-10 4:11am    
- Don't answer your own questions
- Just rewrite the code in C, it'll be as fast and actually readable

The only (vaguely) tricky one but a quick google for rdtsc and C++ sorts that one out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900