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.
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.