"arithmetic if":: Re: Feature for the next C version

William Davidsen davidsen at sungod.crd.ge.com
Wed Aug 2 03:46:10 AEST 1989


  Thanks to all the people who took the time to write about the lack of
something filling the same function as the FORTRAN arithmetic IF. As
someone pointed out, I'm not looking for arithmetic IF, but a way to do
three separate things based on a value, usually the compare of two
values in a sort or tree search.

  Some interesting ideas:
	ifcase
	  (a < b) code; code;
	  (a == b) more(code);
	  (a > b) still(more);
	endcase;

If I were going to make a really readable program, I could do this (and
did, just to see how it looked and worked).

#define LESS		-1
#define EQUAL		0
#define GREATER		1
float T_tmp;
#define COMPARE(a,b) ((T_tmp=a-b)==0 ? 0 : ((T_tmp>0)<<1)-1)

  For the person who said his FORTRAN didn't uses the flags set on a
single test, that's poor compilation. I ran a program into the SunOS
compiler (v3.5) and her's the program and partial output.

	m = 4
	if (m) 1,2,3
    1	print 5, m
	goto 4
    2	print 10, m
	goto 4
    3	print 15, m
    4	stop

    5	format(2H <, i5)
   10	format(2h =, i5)
   15	format(2H >, i5)
	end
 _______________
|
| Partial generated code, comments are mine
|________________

L11:
	movl	#0x4,a5@(-0x8000)
	movl	a5@(-0x8000),a6@(-0xc)
	tstl	a6@(-0xc)		; one test
	jge	L20			;  first jump
	jra	L16			;  second jump
L20:					; Fall into inline code
	tstl	a6@(-0xc)
	jne	L21
	jra	L17
L21:
	jra	L18
L16:


  Again thanks for all the input, hopefully any new standard will come
up with a reasonable way to write this, to (a) make it easy to read, (b)
make it easy for compilers to generate good code (ie. recognize the
special case). Obviously no one wants to have labels, but perhaps
something like:
	n = (a-b $ -1 : 0 : 1);
- or -
	return (a-ptr->val $ getv(ptr->prev : ptr->val : getv(ptr->next));

where there would be a "quadratic operator." No one could argue that
this isn't in the spirit of the language, and an operator might be more
easily added to the language than a control structure.
	bill davidsen		(davidsen at crdos1.crd.GE.COM)
  {uunet | philabs}!crdgw1!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me



More information about the Comp.lang.c mailing list