[comp.lang.c] using !

heroux@cemmva.cem.msu.edu (Brett Heroux) (12/21/90)

Is !(a > b) portable? slower or faster than  a <= b?

bjh

Brett Heroux
7350 Oliver Woods Dr. SE
Grand Rapids, MI 49546

internet: heroux@cemvax.cem.msu.edu

brandis@inf.ethz.ch (Marc Brandis) (12/21/90)

In article <009417DC.37A9DCA0@cemmva.cem.msu.edu> heroux@cemmva.cem.msu.edu (Brett Heroux) writes:
>
>Is !(a > b) portable? 

Yes!

> slower or faster than  a <= b?

Exactly the same for every compiler that I know of. However, if could be both
ways round, but it is very likely that you will even get the same code for both
statements. When you should ever find a place where you have to care about it,
it is probably time to use assembly for this piece of code -:) 


Marc-Michael Brandis
Computer Systems Laboratory, ETH-Zentrum (Swiss Federal Institute of Technology)
CH-8092 Zurich, Switzerland
email: brandis@inf.ethz.ch

steve@taumet.com (Stephen Clamage) (12/22/90)

heroux@cemmva.cem.msu.edu (Brett Heroux) writes:


>Is !(a > b) portable?

Yes.

>slower or faster than  a <= b?

Depends on the compiler and machine.  Many compilers will generate
identical code for both constructs.  You can look at the code your
compiler generates for each construct, and if they are different,
measure the time taken by each version.  If you choose one construct
on this basis, you must realize that the next compiler or machine you
use may not produce the same result.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

rober@jetsun.weitek.COM (Don Rober) (12/22/90)

In article <009417DC.37A9DCA0@cemmva.cem.msu.edu> heroux@cemmva.cem.msu.edu (Brett Heroux) writes:
>
>Is !(a > b) portable? slower or faster than  a <= b?

I am leaving for vacation, so I won't consider the integer case. :>)

If you are using IEEE floating point numbers, the two cases aren't
even equivalent. If one or both have a vane of NaN, both relations are
false. The negation of false in the first example will then produce true.

Just another interesting thing to watch out for in IEEE floating point.

-- 
----------------------------------------------------------------------------
Don Rober				       UUCP: {pyramid}!weitek!rober

jak@sactoh0.SAC.CA.US (Jay A. Konigsberg) (12/22/90)

In article <009417DC.37A9DCA0@cemmva.cem.msu.edu> heroux@cemmva.cem.msu.edu (Brett Heroux) writes:
>
>Is !(a > b) portable? slower or faster than  a <= b?
>
I compiled the following two programs on a 3B2-400 Sys V.3.2

main()               main()
{ int a,b;           { int a,b;
  !(a>b);              a<=b;
}                    }

cc -S foo.c          cc -S foo2.c

This is the diff:

1c1
< 	.file	"foo.c"
---
> 	.file	"foo2.c"

Accucally, I was a bit surprised. Perhaps other compliers produce
something different?

-- 
-------------------------------------------------------------
Jay @ SAC-UNIX, Sacramento, Ca.   UUCP=...pacbell!sactoh0!jak
If something is worth doing, it's worth doing correctly.

dgil@pa.reuter.COM (Dave Gillett) (12/28/90)

In <544@taumet.com> steve@taumet.com (Stephen Clamage) writes:

>heroux@cemmva.cem.msu.edu (Brett Heroux) writes:
>>Is !(a > b) portable?
>>slower or faster than  a <= b?

>Depends on the compiler and machine.  Many compilers will generate
>identical code for both constructs.  You can look at the code your
>compiler generates for each construct, and if they are different,
>measure the time taken by each version.  If you choose one construct
>on this basis, you must realize that the next compiler or machine you
>use may not produce the same result.


     If you find a compiler that generates different code for the two cases,
it is most likely to have crudely generated code for the !(a > b) case, where
two different operators appear, rather than for the single operator in the
a <= b case.  So while a good compiler will generate the same code for both
cases, a bad compiler is likely to generate better code for the a <= b case.

                                                   Dave

henry@zoo.toronto.edu (Henry Spencer) (01/03/91)

In article <639@saxony.pa.reuter.COM> dgil@pa.reuter.COM (Dave Gillett) writes:
>>>Is !(a > b) portable?
>>>slower or faster than  a <= b?
>
>     If you find a compiler that generates different code for the two cases,
>it is most likely to have crudely generated code for the !(a > b) case...

While I agree with Dave's comments in general, there is a subtle issue here
which may be worth mentioning:  `!(a > b)' and `a <= b' are *NOT NECESSARILY
EQUIVALENT* for floating point.  In some floating-point representations,
notably the IEEE standard which is everywhere now, there are floating-point
values which compare "unordered" against normal numbers.  That is, `a < b',
`a == b', and `a > b' may all be false, in which case `!(a > b)' and `a <= b'
have different values.
-- 
"The average pointer, statistically,    |Henry Spencer at U of Toronto Zoology
points somewhere in X." -Hugh Redelmeier| henry@zoo.toronto.edu   utzoo!henry

merriman@camb.com (01/08/91)

In article <009417DC.37A9DCA0@cemmva.cem.msu.edu>, 
    heroux@cemmva.cem.msu.edu (Brett Heroux) writes:
> Is !(a > b) portable? 
It better be!

>slower or faster than  a <= b?
VAX C produces identical object code for both forms, with or without 
optimization.

> 
> bjh
> 
> Brett Heroux
> 7350 Oliver Woods Dr. SE
> Grand Rapids, MI 49546
> 
> internet: heroux@cemvax.cem.msu.edu