LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   right tool for the job ? / code expander / auto C loop unrolling (https://www.linuxquestions.org/questions/programming-9/right-tool-for-the-job-code-expander-auto-c-loop-unrolling-609395/)

demosuzki 12-27-2007 08:48 AM

right tool for the job ? / code expander / auto C loop unrolling
 
While I'd normally program in C++
I've a small task that I think might be better solved in Perl or
similar. I'm looking for advice.

I need to make a code expander.

What i have to do is parse a set of C/C++ files and for all occurrences
of a function call.
(MADD192(a,b,c,rp); (or MADD256(a,b,c,rp); or MADD512(a,b,c,rp); etc.)

I need to swap in a block of code (using the the 'N' ).
This code body being an large unwound loop based on the function
parameters and the N (192, 256 etc.).


so


madd192(a,b,c,rp);
is expanded to something like

rp[1] = a[1] * b[1] + c[1];
rp[2] = a[2] * b[2] + c[2];
.
.
rp[192] = a[192] * b[192] + c[192];


its actually a bit more complicated than my example so the compiler can't do it automatically.


Is a job for perl or is there anything else I should look at ?
I'll be doing this a lot for other, similar, functions.

anyone care to to have a go ?

-ds

Dox Systems - Brian 12-27-2007 09:08 AM

Have you looked at the Sun Studio C compiler? It has many more capabilities in that area than gcc, etc...

David1357 12-27-2007 10:05 AM

Quote:

Originally Posted by demosuzki (Post 3002876)
its actually a bit more complicated than my example so the compiler can't do it automatically.

Unless I am missing something, you can do it with macros like the rest of us.

Code:

#define MEGAFUNC(a, b, c, d) \
    {            \
        a[b] = c; \
        b[c] = d; \
        ...      \
        d[a] = b; \
    }

This is a bogus example, but it should illustrate the idea.

demosuzki 12-28-2007 10:51 AM

Quote:

Originally Posted by Dox Systems - Brian (Post 3002898)
Have you looked at the Sun Studio C compiler? It has many more capabilities in that area than gcc, etc...

thank you for your response

Its actually for an architecture (the cell) not supported by Sun.

Its because of the immaturity (non optimal code generation) of the compilers that i am hand optimising

/ds

demosuzki 12-28-2007 10:57 AM

[QUOTE=David1357;3002946]Unless I am missing something, you can do it with macros like the rest of us.

thank you for your response

My example was small to illustrate the idea the real problem is a little more complicated.

I considered macros and have used them to prototype the idea
what I need to do will introduce 1000s of lines.

I'm thinking that 'something else' would ease maintenance and hopefully reduce the possibility of introducing subtle but hard to find bugs.

/ds

David1357 12-28-2007 03:04 PM

Quote:

Originally Posted by demosuzki (Post 3003960)
I considered macros and have used them to prototype the idea
what I need to do will introduce 1000s of lines.

Use nested macros. That is how most cryptographic algorithms are implemented. You can use small, well-tested macros to build larger ones. The compiler does not care how many lines of code you eventually generate.

demosuzki 01-02-2008 08:55 AM

I've been looking a bit more at this

found a book on code generation (http://www.manning.com/herrington/)

and

settled on Ruby (http://www.ruby-lang.org/en/)
to do parsing and text manipulation.
got powerful regexp .


I'm pretty happy with the results.
One run made a c source file with 67,000 lines

One of the advantages over the preprocessor is that you get more power to name variables in unrolling which helps in debugging compared to my experience with macros


-ds


All times are GMT -5. The time now is 10:35 PM.