S
Skybuck Flying
Hi,
Right now I am trying to implement some udp checksum code.
The idea which I am trying out is located on this web page:
http://www.netfor2.com/udpsum.htm
The idea seems to be to use a 32 bit integer to do the summing with.
Therefore all the carries are collected into the high order word.
Apperently these carries need to be added to the summation.
(
I am not exactly sure why these carries need to be added ?
Does it have something to do with one complement ?
Or does it have to do something with the way one complement computers work
?
)
Anyway the following code on the website seems dangerous to me:
// keep only the last 16 bits of the 32 bit calculated sum and add the
carries
while (sum>>16)
sum = (sum & 0xFFFF)+(sum >> 16);
Especially the last line.
If the compiler translates this into the following instruction sequence:
1. sum = sum & 0xFFFF;
and then:
2. sum = sum + (sum >> 16);
then the carries will have been lost ?!
According to wikipedia the instruction order is not specified ?
http://en.wikipedia.org/wiki/C_(programming_language)
It reads:
"
Expressions can use a variety of built-in operators (see below) and may
contain function calls. The order in which operands to most operators, as
well as the arguments to functions, are evaluated is unspecified;
"
What you make of this... dangerous or not dangerous ?
(Also I am translating this code to Delphi... but now I am getting some
serious doubts !
)
Bye,
Skybuck.
Right now I am trying to implement some udp checksum code.
The idea which I am trying out is located on this web page:
http://www.netfor2.com/udpsum.htm
The idea seems to be to use a 32 bit integer to do the summing with.
Therefore all the carries are collected into the high order word.
Apperently these carries need to be added to the summation.
(
I am not exactly sure why these carries need to be added ?
Does it have something to do with one complement ?
Or does it have to do something with the way one complement computers work
?
)
Anyway the following code on the website seems dangerous to me:
// keep only the last 16 bits of the 32 bit calculated sum and add the
carries
while (sum>>16)
sum = (sum & 0xFFFF)+(sum >> 16);
Especially the last line.
If the compiler translates this into the following instruction sequence:
1. sum = sum & 0xFFFF;
and then:
2. sum = sum + (sum >> 16);
then the carries will have been lost ?!
According to wikipedia the instruction order is not specified ?
http://en.wikipedia.org/wiki/C_(programming_language)
It reads:
"
Expressions can use a variety of built-in operators (see below) and may
contain function calls. The order in which operands to most operators, as
well as the arguments to functions, are evaluated is unspecified;
"
What you make of this... dangerous or not dangerous ?
(Also I am translating this code to Delphi... but now I am getting some
serious doubts !
Bye,
Skybuck.