Determine if two integers have different sign

T

thomas.mertes

I use the following condition, to determine, if a and b have
different signs:

(a>0&&b<0)||(a<0&&b>0)

Cases were a=0 or b=0 hold are handled separate. When twos
complement integers are used, something like

(a^b)<0

would do the same. The second solution is probably faster (but maybe
this assumption is wrong).

Can I assume that a C compiler like gcc does this optimization?

The code above is generated in the backend of the Seed7 compiler.
The type of integer representation is known, so the Seed7 compiler
could decide, which code should be generated.

Does it pay off, to do such low level optimizations, or is it better
to leave them to the C compiler?

The example code above is used by the Seed7 compiler as part of the
modulo (mod) operator. The whole modulo function is implemented
with:

c=a%b,((a>0&&b<0)||(a<0&&b>0))&&c!=0?c+b:c

With the "optimization" above it would be:

c=a%b,(a^b)<0&&c!=0?c+b:c

The mod operator is explained in this chapter:

http://seed7.sourceforge.net/manual/types.htm#integer


Greetings Thomas Mertes

--
Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.
 
S

Stefan Ram


What should be correct would be the quite literal
translation of the subject

a<0!=b<0

or

a<0^b<0

. I do not know, if you want to avoid this, because you want
to write a more »runtime-efficient« expression; but a
compiler is free to recognize these patterns and compile
them using hand-crafted assembly for this calculation.
Tuning for a specific compiler might not be on-topic in a
general C newsgroup.
 
G

Gene

I use the following condition, to determine, if a and b have
different signs:

  (a>0&&b<0)||(a<0&&b>0)

Cases were a=0 or b=0 hold are handled separate. When twos
complement integers are used, something like

  (a^b)<0

would do the same. The second solution is probably faster (but maybe
this assumption is wrong).

Can I assume that a C compiler like gcc does this optimization?

Not really. At this level gcc optimizations are IME quite grainy.
The code above is generated in the backend of the Seed7 compiler.
The type of integer representation is known, so the Seed7 compiler
could decide, which code should be generated.

Does it pay off, to do such low level optimizations, or is it better
to leave them to the C compiler?

A quick test showed that (a<0&&b>0)||(a>0&&b<0) compiled to complex
jumping code while (a<0)^(b<0) compiled to two instructions--an xor
and a right shift. So you can be the judge. Micro-optimization might
pay today, but you can't tell what will happen tomorrow.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top