gnu.gcc.help
[Top] [All Lists]

Re: guaranteed no-op function pointer?

Subject: Re: guaranteed no-op function pointer?
From: Paul Pluzhnikov <ppluzhnikov-nsp@xxxxxxxxxxx>
Date: Tue, 13 Dec 2005 22:32:25 -0800
Newsgroups: gnu.gcc.help
"bluejack" <bluujack@xxxxxxxxx> writes:

> I want a function pointer that, when called, can be a guaranteed to act
> as a no-op:

You can't have it (well, not portably).

The closest you can come is to have self-modifying code that writes
proper (for a given architecture) "mov $0,&z" instruction 

> So: does the reference to noop_funcs.func_b(x) actually make a function
> call? 

Yes, using gcc-4.1-20051022 and gcc-3.3.3 with -O6 on Linux/x86.

> So, to those of you with deep understanding of gcc optimizations,

You can *see* what gcc optimizations do:

  gcc -S -O6 junk.c && cat junk.s

For example, gcc-3.3.3 on Linux/x86 generates this:

main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        subl    $12, %esp
           /* everything above is function prolog */
        pushl   $10
        call    *does_stuff
        movl    $10, (%esp)
        call    *noop_funcs+4
        leave
        ret

Let me know if you need help understanding the assembly above.

> what's the right answer here? (Including: "You dummy, you're going
> about this all wrong." -- steer me right!)

If you are going to call noop functions billions of times, and
*measure* significant difference when these calls are commented
out, and don't care much about portability, then binary code
rewriting techniques (just-in-time compilation) may be called for.

Otherwize, you are guilty of premature optimization: function
call/return do not consume many cycles on modern CPUs.

Cheers,
-- 
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.

<Prev in Thread] Current Thread [Next in Thread>