[comp.lang.c] Davidsen's device

davidsen@sungod.crd.ge.com (William Davidsen) (08/02/89)

  I recently discovered the following technique. While I can't swear
that it's new and unique to me, I can claim independent discovery. I
make no claims that it is suited to any problem you have, will have, or
would admit to having.

  I was calculating a function and had two algorithms, one of which was
13 times faster than the other but unreliable for input values less than
1.0, the other very accurate but slow. I had to evaluate the function
6E8 times on a slow machine.

Here's the code:

	double hacs1(), hacs2();
	double asect;

	(*(asect < 1.0 ? hacs2 : hacs1)(asect);

  Since the name of a function is typed "pointer to function
returning..." this was treated as "(*ptr)()" and generated the desired
code. It was slightly faster than the obvious:

	if (asect < 1.0) hasc2(asect);
	else hasc1(asect);
	bill davidsen		(davidsen@crdos1.crd.GE.COM)
  {uunet | philabs}!crdgw1!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

net@tub.UUCP (Oliver Laumann) (08/04/89)

In article <1430@crdgw1.crd.ge.com> davidsen@crdos1.UUCP (bill davidsen) writes:
>   I recently discovered the following technique. While I can't swear
> that it's new and unique to me, I can claim independent discovery. I
> make no claims that it is suited to any problem you have, will have, or
> would admit to having.
> 
> 	(*(asect < 1.0 ? hacs2 : hacs1)(asect);

This is quite a common idiom, I have used it lots of times (without the
syntax error in your example, of course).

You can even omit the * since it's superfluous.

Or is it?  Our K&R (the old edition) doesn't seem to say anything about
this, but "gnucc -pedantic -ansi" doesn't complain about the following
program (a good heuristic for checking whether it's valid C):

int g() { return 5; }

int foo() {
    int (*f)() = g;
    return f();
}

--
Oliver Laumann              net@TUB.BITNET              net@tub.UUCP

scjones@sdrc.UUCP (Larry Jones) (08/08/89)

In article <888@tub.UUCP>, net@tub.UUCP (Oliver Laumann) writes:
> In article <1430@crdgw1.crd.ge.com> davidsen@crdos1.UUCP (bill davidsen) writes:
> > 	(*(asect < 1.0 ? hacs2 : hacs1)(asect);
> 
> You can even omit the * since it's superfluous.
> 
> Or is it?  Our K&R (the old edition) doesn't seem to say anything about
> this, but "gnucc -pedantic -ansi" doesn't complain about the following
> program (a good heuristic for checking whether it's valid C):

dpANS C allows the star to be omitted, K&R C requires it.
----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones@SDRC.UU.NET
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150-2789             AT&T: (513) 576-2070
"You can't get a body like mine in a bottle --
unless you push REAL HARD." - Judy Tenuta / Dr. Pepper

karl@haddock.ima.isc.com (Karl Heuer) (08/08/89)

In article <888@tub.UUCP> net@tub.UUCP (Oliver Laumann) writes:
>You can even omit the * since it's superfluous.  Or is it?

In ANSI C, it is.  (I consider this unfortunate; C already has too much DWIM.)
But in pre-ANSI C, you do need the `*' when invoking a function via a pointer
(though some compilers silently did what ANSI now requires).

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

davidsen@sungod.crd.ge.com (ody) (08/08/89)

In article <888@tub.UUCP> net@tub.UUCP (Oliver Laumann) writes:

| This is quite a common idiom, I have used it lots of times (without the
| syntax error in your example, of course).

  I'm not sure that everyone would agree with that statement... the mail
I got runs about 7:1 with people who found it new and disgusting as
opposed to those who use it all the time (that's scarey, I wouldn't use
it unless there was a good reason). I never claimed more than
independent discovery, so I can ffel hurt that it's not a totally new
idea. There aren't many of those.

| 
| You can even omit the * since it's superfluous.

  Not in portable code it isn't. ANSI made it optional, K&R 1st ed
required it. I personally (opinion here) feel that it makes the code
more readable to include it, so that the reader knows what's going on.
The omission will also break a lot of tools which scan the code and
provide cross index, etc.
	bill davidsen		(davidsen@crdos1.crd.GE.COM)
  {uunet | philabs}!crdgw1!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me