473,408 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Sliding window and wrap-around

Hello,

Consider the following scenario.
'A' produces data which are sent in "packets" to 'B'.
Each "packet" carries a sequence number, so that 'B' can
insert the packet in the "right place" in a sorted list.

The sequence number of the 1st packet is 0.
The sequence number of the 65536th packet is 65535.
The sequence number of the 65537th packet "wraps around" back to 0.

Because of the wrap-around, I can't just use the normal relational
operators. For example, with very high probability, seqno 0 is newer
than seqno 65535, which translates to : 0 "is greater than" 65535.

Given a sequence number 'u'
u+1, u+2, ..., u+32767 (mod 65536) are considered greater than u.
u-1, u-2, ..., u-32768 (mod 65536) are considered lower than u.

I have implemented this as :

#include <stdint.h>
int greater_than(uint16_t u, uint16_t v)
{
return (int16_t)(u-v) 0;
}

This works on my platform (GCC, Linux, x86) but AFAIU, conversion
from unsigned to signed integer is undefined?

Is there a way to implement the comparison function in a
portable manner?

Regards.
Jun 27 '08 #1
8 2666
In article <48**********************@news.free.fr>,
Noob <root@localhostwrote:
>Given a sequence number 'u'
u+1, u+2, ..., u+32767 (mod 65536) are considered greater than u.
u-1, u-2, ..., u-32768 (mod 65536) are considered lower than u.

I have implemented this as :

#include <stdint.h>
int greater_than(uint16_t u, uint16_t v)
{
return (int16_t)(u-v) 0;
}

This works on my platform (GCC, Linux, x86) but AFAIU, conversion
from unsigned to signed integer is undefined?
Yes, or rather implementation-defined.

The unsigned arithmetic is defined to work mod 65536, so I suggest

return (u - v) <= 32767;

-- Richard
--
:wq
Jun 27 '08 #2
Richard Tobin wrote:
Noob wrote:
>Given a sequence number 'u'
u+1, u+2, ..., u+32767 (mod 65536) are considered greater than u.
u-1, u-2, ..., u-32768 (mod 65536) are considered lower than u.

I have implemented this as :

#include <stdint.h>
int greater_than(uint16_t u, uint16_t v)
{
return (int16_t)(u-v) 0;
}

This works on my platform (GCC, Linux, x86) but AFAIU, conversion
from unsigned to signed integer is undefined?

Yes, or rather implementation-defined.
Right. The standard states

<quote>
When an unsigned integer is converted to its corresponding
signed integer, if the value cannot be represented the result
is implementation-defined.
</quote>

Implementation-defined means it is documented somewhere, right?
But where? In the compiler's documentation?

(e.g. GCC runs on many platforms; would the documentation have
to specify the behavior for every supported architecture?)
The unsigned arithmetic is defined to work mod 65536, so I suggest

return (u - v) <= 32767;
Are u and v of type uint16_t in this expression?
What if stdint.h is not available?

If uint16_t is a typedef for unsigned short, aren't u and v
promoted to int before performing the subtraction?

Is there a solution using only unsigned or unsigned long,
without assuming any specific width for these types?

Regards.
Jun 27 '08 #3

"Noob" <root@localhostwrote in message
news:48**********************@news.free.fr...
Hello,

Consider the following scenario.
'A' produces data which are sent in "packets" to 'B'.
Each "packet" carries a sequence number, so that 'B' can
insert the packet in the "right place" in a sorted list.

The sequence number of the 1st packet is 0.
The sequence number of the 65536th packet is 65535.
The sequence number of the 65537th packet "wraps around" back to 0.

Because of the wrap-around, I can't just use the normal relational
operators. For example, with very high probability, seqno 0 is newer
than seqno 65535, which translates to : 0 "is greater than" 65535.

Given a sequence number 'u'
u+1, u+2, ..., u+32767 (mod 65536) are considered greater than u.
u-1, u-2, ..., u-32768 (mod 65536) are considered lower than u.

I have implemented this as :

#include <stdint.h>
int greater_than(uint16_t u, uint16_t v)
{
return (int16_t)(u-v) 0;
}

This works on my platform (GCC, Linux, x86) but AFAIU, conversion
from unsigned to signed integer is undefined?

Is there a way to implement the comparison function in a
portable manner?
that will not work right in general...
what you would need to do instead is to keep track of the rover, and so, for
example:
if(u<rov)u+=65536;
if(v<rov)v+=65536;
return(u>v);

note that all this works so long as one does not do absolute comparrisons,
or retains sequence numbers out of range of the window.

or such...

Regards.

Jun 27 '08 #4
In article <48***********************@news.free.fr>,
Noob <root@localhostwrote:
>Implementation-defined means it is documented somewhere, right?
But where? In the compiler's documentation?
Yes, in theory.
>The unsigned arithmetic is defined to work mod 65536, so I suggest

return (u - v) <= 32767;
>Are u and v of type uint16_t in this expression?
Yes.
>What if stdint.h is not available?
Then it won't compile.
>If uint16_t is a typedef for unsigned short, aren't u and v
promoted to int before performing the subtraction?
Yes, I should have said

return (uint16_t)(u - v) <= 32767;
>Is there a solution using only unsigned or unsigned long,
without assuming any specific width for these types?
The OP's problem assumed the existence of uint16_t. You could operate
on unsigned and reduce the result mod 65536 if you thought it might
not be available. But the fact that the sequence numbers wrap around
at 65536 strongly suggests that 16-bit integers are available!

-- Richard
--
:wq
Jun 27 '08 #5
On Apr 23, 5:20 am, Noob <root@localhostwrote:
Hello,

Consider the following scenario.
'A' produces data which are sent in "packets" to 'B'.
Each "packet" carries a sequence number, so that 'B' can
insert the packet in the "right place" in a sorted list.

The sequence number of the 1st packet is 0.
The sequence number of the 65536th packet is 65535.
The sequence number of the 65537th packet "wraps around" back to 0.

Because of the wrap-around, I can't just use the normal relational
operators. For example, with very high probability, seqno 0 is newer
than seqno 65535, which translates to : 0 "is greater than" 65535.

Given a sequence number 'u'
u+1, u+2, ..., u+32767 (mod 65536) are considered greater than u.
u-1, u-2, ..., u-32768 (mod 65536) are considered lower than u.

I have implemented this as :

#include <stdint.h>
int greater_than(uint16_t u, uint16_t v)
{
return (int16_t)(u-v) 0;

}

This works on my platform (GCC, Linux, x86) but AFAIU, conversion
from unsigned to signed integer is undefined?

Is there a way to implement the comparison function in a
portable manner?

Regards.
Outside the C types question, It isn't clear to me what the problem
is. Do you really expect to get >65535 packets? You do not have any
other way of knowing the difference between packet #00000 and #65536.
So on the receiving end you must have already gotten packet #00000.
Can you not simply try to insert in the list and seeing the collision,
you determine if this is a duplicate of packet #00000 or a new packet
#65536. This is less a C question in my mind than a hash collision
algorithm question.

Does that help?
Ed
Jun 27 '08 #6
cr88192 wrote:
Noob wrote:
>Consider the following scenario.
'A' produces data which are sent in "packets" to 'B'.
Each "packet" carries a sequence number, so that 'B' can
insert the packet in the "right place" in a sorted list.

The sequence number of the 1st packet is 0.
The sequence number of the 65536th packet is 65535.
The sequence number of the 65537th packet "wraps around" back to 0.

Because of the wrap-around, I can't just use the normal relational
operators. For example, with very high probability, seqno 0 is newer
than seqno 65535, which translates to : 0 "is greater than" 65535.

Given a sequence number 'u'
u+1, u+2, ..., u+32767 (mod 65536) are considered greater than u.
u-1, u-2, ..., u-32768 (mod 65536) are considered lower than u.

I have implemented this as :

#include <stdint.h>
int greater_than(uint16_t u, uint16_t v)
{
return (int16_t)(u-v) 0;
}

This works on my platform (GCC, Linux, x86) but AFAIU, conversion
from unsigned to signed integer is undefined?

Is there a way to implement the comparison function in a
portable manner?

that will not work right in general...
What will not work in general?
what you would need to do instead is to keep track of the rover,
and so, for example:
if(u<rov)u+=65536;
if(v<rov)v+=65536;
return(u>v);
What are rover and rov?
Could you detail the algorithm?
note that all this works so long as one does not do absolute comparisons,
What do you consider an absolute comparison?
or retains sequence numbers out of range of the window.

or such...
Or such?
Jun 27 '08 #7
Ed Prochak wrote:
Outside the C types question, It isn't clear to me what the problem
is. Do you really expect to get >65535 packets?
Yes.

'A' sends between 100 and 5000 packets per second.

Therefore, wrap-around might occur as often as every 13 seconds.
You do not have any
other way of knowing the difference between packet #00000 and #65536.
(Is it a question?) No, I have no way to tell apart two packets with
the same sequence number; they might be the same (duplicated) packet,
or they might be different packets, the sequence numbers of which are
spaced 65536*k apart.
So on the receiving end you must have already gotten packet #00000.
Can you not simply try to insert in the list and seeing the collision,
I don't keep the packets forever, they are sent "downstream".
you determine if this is a duplicate of packet #00000 or a new packet
#65536. This is less a C question in my mind than a hash collision
algorithm question.

Does that help?
I'll think about it.

Regards.
Jun 27 '08 #8
Richard Tobin wrote:
Noob wrote:
>Implementation-defined means it is documented somewhere, right?
But where? In the compiler's documentation?

Yes, in theory.
OK. I'll scour the documentation.
Yes, I should have said

return (uint16_t)(u - v) <= 32767;
>Is there a solution using only unsigned or unsigned long,
without assuming any specific width for these types?

The OP's problem assumed the existence of uint16_t. You could operate
on unsigned and reduce the result mod 65536 if you thought it might
not be available. But the fact that the sequence numbers wrap around
at 65536 strongly suggests that 16-bit integers are available!
It is the protocol that mandates storing sequence numbers in
a 16-bit wide field. This protocol might be implemented on
platforms that do not provide exact width integers.

As far as I can tell, the original implementation...

#include <stdint.h>
int greater_than(uint16_t u, uint16_t v)
{
return (int16_t)(u-v) 0;
}

is equivalent to...

int greater_than2(unsigned long u, unsigned long v)
{
return ((u-v-1UL) & 0xffffUL) < 0x7fffUL;
}

The second implementation should (??) be portable to any platform.

Regards.
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Developwebsites | last post by:
Hi all, I've made a sliding puzzle game in shockwave which works just fine, except I dont know how to have it solve itself. the URL is: http://members.aol.com/rglukov/games/selfsolve.htm ...
8
by: Jakej | last post by:
I've been using a javascript in an html file for a banner slider, and it works as desired. But I'd like to use it on more than one page and it would be great if I could transfer the code to a .js...
1
by: Ryan | last post by:
What I want to do is run tcpdump continuously in the background and dump to a file. Then, while tcpdump is working, I'd like to grab arbitrary chunks of the open dump file and parse pieces at the...
2
by: Mohammad Parwez | last post by:
Hi, Can any one help me creating a control, which slides on any particular event. The scenario is something like this: - I have a form, which loads a user control. On load of the form this...
1
by: Guadala Harry | last post by:
AFAIK, when placing an object into the Cache with no special instructions (no dependencies, sliding expirations, hard expirations, etc), it will just sit there in the Cache until the system decides...
2
by: Lonewolf | last post by:
Hi, does anyone know of a way to do sliding window similar to WMP 9's slider bar which can slide up when the mouse gets to the bottom of the screen in fullscreen mode during playback. Basically...
11
by: epidemiologist | last post by:
Hi; I am new to programming. I want to make a sliding window through colomns to claculate its average. For example A B C D E F 2 4 5 6 9 0 4 5 6 6 6 7 5 3 4 4 4 4 7 ...
12
by: rhino | last post by:
I'm having some problems with Sliding Doors, as described in an article in A List Apart (http://www.alistapart.com/articles/slidingdoors/) and as detailed in the CSS Tab Designer v2.0 program. ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.