preprocessor macro for repeating? 
Author Message
 preprocessor macro for repeating?

Hi!

I have a c program where I want to repeat a block of statements 10000
times in real, not by a loop. Is there a preprocessor macro to do this
or any other trick? I don't want to write it that often...

Thanks in advance.
--
Martin
--



Mon, 26 Jul 2004 01:07:01 GMT  
 preprocessor macro for repeating?

Quote:

> I have a c program where I want to repeat a block of statements 10000
> times in real, not by a loop. Is there a preprocessor macro to do this
> or any other trick? I don't want to write it that often...

You could write a macro to repeat a statement 10 times, then
apply that macro to itself; e.g.,

        #define REPEAT10(A) A;A;A;A;A;A;A;A;A;A
        #define REPEAT100(A) REPEAT10(REPEAT10(A))
        #define REPEAT10000(A) REPEAT100(REPEAT100(A))

But the minimum allowed limit on line length is only 4095
characters, so this isn't guaranteed to work everywhere
(especially if I have a thinko above).
--
"Welcome to the Slippery Slope. Here is your handbasket.
 Say, can you work 70 hours this week?"
--Ron Mansolino
--



Mon, 26 Jul 2004 05:42:34 GMT  
 preprocessor macro for repeating?

Quote:
> I have a c program where I want to repeat a block of statements 10000
> times in real, not by a loop. Is there a preprocessor macro to do this
> or any other trick? I don't want to write it that often...

Can you elaborate on _why_ you want to unravel this loop? Duffs'
device comes into mind and (shudder) m4 too ...

For one thing: cpp can't help you out here.

kind regards,


--



Mon, 26 Jul 2004 05:42:39 GMT  
 preprocessor macro for repeating?

Quote:

> I have a c program where I want to repeat a block of statements 10000
> times in real, not by a loop.

Not with the repeat count as an argument, I think.  You could start
with

#define REP10(a) a a a a a a a a a a
#define REP100(a) REP10(a) REP10(a) REP10(a) REP10(a) REP10(a) \
                  REP10(a) REP10(a) REP10(a) REP10(a) REP10(a)

and so on.  But sooner or later, you'ld be bound to hit the source
line length limit of your C preprocessor or compiler.

Anyway: the possible gains of this kind of manual loop unrolling
dwindle away quickly towards higher number of iterations. I.e. it
really doesn't help much to unroll all of your 10000 iterations.
Unrolling 10 and putting a 1..1000 loop around that would be faster,
on just about every platform.  Unless the loop body is a single NOP,
that is ;-)

--

Even if all the snow were burnt, ashes would remain.
--



Mon, 26 Jul 2004 05:42:49 GMT  
 preprocessor macro for repeating?

Quote:

> I have a c program where I want to repeat a block of statements 10000
> times in real, not by a loop. Is there a preprocessor macro to do this
> or any other trick? I don't want to write it that often...

You could do something like:

  #define A your statement;     /* 1 */
  #define B A A A A A A A A A A /* 10 */
  #define C B B B B B B B B B B /* 100 */
  #define D C C C C C C C C C C /* 1000 */
  #define E D D D D D D D D D D /* 10000 */

Or write a program that outputs a file that contains the statements
repeated 10000 times, or use a suitably advanced editor.

Why do you need ten thousand statements?  That's going to be a lot of
machine code, which may be hard to keep in a cache.
--



Mon, 26 Jul 2004 05:42:56 GMT  
 preprocessor macro for repeating?

Quote:

>I have a c program where I want to repeat a block of statements 10000
>times in real, not by a loop. Is there a preprocessor macro to do this
>or any other trick? I don't want to write it that often...

It would be helpful if you could provide a more specific example of what
exactly you want.  I'm guessing that something along these lines might
be what you were trying to ask for:

#define ten(a) a a a a a a a a a a
#define hundred(a) ten(a) ten(a) ten(a) ten(a) ten(a) ten(a) ten(a) \
        ten(a) ten(a) ten(a)
#define thousand(a) hundred(a) hundred(a) hundred(a) hundred(a) \
        hundred(a) hundred(a) hundred(a) hundred(a) hundred(a) hundred(a)

int i;

int ten_k(void)
{
        thousand(i++;) thousand(i++;) thousand(i++;) thousand(i++;)
        thousand(i++;) thousand(i++;) thousand(i++;) thousand(i++;)
        thousand(i++;) thousand(i++;)

Quote:
}

(Note the odd semicolon placement!)

-andy
--



Mon, 26 Jul 2004 05:43:23 GMT  
 preprocessor macro for repeating?

)Hi!
)
)I have a c program where I want to repeat a block of statements 10000
)times in real, not by a loop. Is there a preprocessor macro to do this
)or any other trick? I don't want to write it that often...
)
)Thanks in advance.

There are some possibilities. One is this:

============================================
file incl10a.h
--------------------------------------------
#include "incl10b.h"
#include "incl10b.h"
#include "incl10b.h"
#include "incl10b.h"
#include "incl10b.h"
#include "incl10b.h"
#include "incl10b.h"
#include "incl10b.h"
#include "incl10b.h"
#include "incl10b.h"
============================================
file incl10b.h
--------------------------------------------
#include "incl10c.h"
#include "incl10c.h"
#include "incl10c.h"
#include "incl10c.h"
#include "incl10c.h"
#include "incl10c.h"
#include "incl10c.h"
#include "incl10c.h"
#include "incl10c.h"
#include "incl10c.h"
============================================
file incl10c.h
--------------------------------------------
#include "incl10d.h"
#include "incl10d.h"
#include "incl10d.h"
#include "incl10d.h"
#include "incl10d.h"
#include "incl10d.h"
#include "incl10d.h"
#include "incl10d.h"
#include "incl10d.h"
#include "incl10d.h"
============================================
file incl10d.h
--------------------------------------------
#include "incl10e.h"
#include "incl10e.h"
#include "incl10e.h"
#include "incl10e.h"
#include "incl10e.h"
#include "incl10e.h"
#include "incl10e.h"
#include "incl10e.h"
#include "incl10e.h"
#include "incl10e.h"
============================================
file incl10e.h
/* lines you want repeated */
============================================

Another is cut'n'paste using your handy dandy editor.

You can also make macros like this:

#define REPEAT10A(x)     \
    x                    \
    x                    \
    x                    \
    x                    \
    x                    \
    x                    \
    x                    \
    x                    \
    x                    \
    x

And so on.

Mike
--
char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
This message made from 100% recycled bits.
I can explain it for you, but I can't understand it for you.
I don't speak for Alcatel      <- They make me say that.
--



Mon, 26 Jul 2004 05:43:31 GMT  
 preprocessor macro for repeating?

Quote:

> I have a c program where I want to repeat a block of statements 10000
> times in real, not by a loop. Is there a preprocessor macro to do this
> or any other trick? I don't want to write it that often...

I can't recall a C preprocessor directive that does this. My memory
may be faulty, but you usually don't need to do this with C.

If you _really_ want to do this with macros, there are macro languages
such as m4 available, which could be used to pre-pre-process your
source. I don't remember if m4 actually has the loop, but, if it
doesn't, you can trick it into looping.

There are a few cases where you want to build a large table of
constants where the entries can be algorithmically derived, but those
cases tend to require run-time evaluation. (One example might be to
generate a table of prime numbers.) In thoses cases, there is nothing
wrong with writing a separate program to generate the source for the
table, then cutting and pasting the generated source into the program
that needs the table.

(If you are really careful, you can simply pipe the generated source
to the compiler, but that takes some experience to get it to work
right.)

Joel
--



Mon, 26 Jul 2004 23:25:44 GMT  
 preprocessor macro for repeating?

Quote:
>Why do you need ten thousand statements?  That's going to be a lot of
>machine code, which may be hard to keep in a cache.

I'm coding on an m68k emulator core.

I now saw, that I need not 10000 times the same statement, but maybe
100 times - that in a loop. This could be coded by hand.

This part is for to get the next instruction of a virtual 68000
processor, increase the program counter, strip the last 3 bits, then
use the value for to jump into the instruction jumptable:

        inst = GetInst ();
        pc += 2;
        (*jmp_table[inst >> 3]) (inst);

A smaller loop around this would mean another unneeded instruction and
a branch, where I don't know, how this would have influence on the
prefetching of the host processor. Ok, you may say, that prefetching
thoughts do not matter, if I do so many things for each instruction
before...

--
Martin
--



Mon, 26 Jul 2004 23:26:19 GMT  
 preprocessor macro for repeating?

Quote:

> I have a c program where I want to repeat a block of statements 10000
> times in real, not by a loop. Is there a preprocessor macro to do this
> or any other trick? I don't want to write it that often...

It sounds like the wrong solution to whatever the problem is.
Anyway, the C preprocessor doesn't have that kind of programmability,
although as other posters have said, there are ways to kludge it.
Usually, if one really had to do this, he'd write a program to
generate the actual program (or at least an #includable chunk of it).
--



Mon, 26 Jul 2004 23:25:55 GMT  
 preprocessor macro for repeating?

Quote:


> >Why do you need ten thousand statements?  That's going to be a lot of
> >machine code, which may be hard to keep in a cache.

> I'm coding on an m68k emulator core.

> I now saw, that I need not 10000 times the same statement, but maybe
> 100 times - that in a loop. This could be coded by hand.

> This part is for to get the next instruction of a virtual 68000
> processor, increase the program counter, strip the last 3 bits, then
> use the value for to jump into the instruction jumptable:

>         inst = GetInst ();
>         pc += 2;
>         (*jmp_table[inst >> 3]) (inst);

> A smaller loop around this would mean another unneeded instruction and
> a branch, where I don't know, how this would have influence on the
> prefetching of the host processor.

Designs involve tradeoffs.  For the application you give, I would expect
that the overhead of the loop would be negligible compared to the
execution time of the two function calls.  Here is a suggestion: try it
with a single loop, loop of 10 copies, and maybe a loop of 50 copies.
Compare the execution time.  My expectation is that the last version
will either take longer or be virtually the same as the 10 copy loop.

If you are very concerned about execution time, consider putting
GetInst() inline, using an inline attribute (if available), macro, or
plain text insertion.  That will probably give a much bigger performance
improvement that loop unrolling.

Thad
--



Tue, 27 Jul 2004 04:09:56 GMT  
 preprocessor macro for repeating?
Have you looked into using.... D_DAH....
the much feared goto statement..?

Have you tried writing or using a program that will 'cut-n-paste' the code?
I'd recommend perl for that, or some unix tools eg awk, etc...


Quote:
> Hi!

> I have a c program where I want to repeat a block of statements 10000
> times in real, not by a loop. Is there a preprocessor macro to do this
> or any other trick? I don't want to write it that often...

> Thanks in advance.
> --
> Martin
> --


--



Tue, 27 Jul 2004 04:19:58 GMT  
 preprocessor macro for repeating?

Quote:
>Designs involve tradeoffs.  For the application you give, I would expect
>that the overhead of the loop would be negligible compared to the
>execution time of the two function calls.  Here is a suggestion: try it
>with a single loop, loop of 10 copies, and maybe a loop of 50 copies.
>Compare the execution time.  My expectation is that the last version
>will either take longer or be virtually the same as the 10 copy loop.

You are right. It performs better, but just till about a few repeats.
After that I feel no better performance. So my idea was bad. Maybe a
small lopp does fit better in the processors cache...

Quote:
>If you are very concerned about execution time, consider putting
>GetInst() inline, using an inline attribute (if available), macro, or
>plain text insertion.  That will probably give a much bigger performance
>improvement that loop unrolling.

Already did that.

Thanks
--
Martin
--



Wed, 28 Jul 2004 02:42:09 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Preprocessor: Textual value of a macro

2. C Macros textually substituting for preprocessor directives.

3. Non-recursive preprocessor macros?

4. Preprocessor / Macro substitution

5. C preprocessor - Macros

6. Deciphering a regexp preprocessor macros

7. Non-recursive preprocessor macros?

8. Preprocessor: defining a macro inside a #define directive

9. Question about preprocessor evaluation of expressions in macro expansions

10. Preprocessor macros

11. Preprocessor havoc: Pasting values of macros rather than names

12. Use of ellipsis in preprocessor macros

 

 
Powered by phpBB® Forum Software