seq.c
3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright (C) 2015 Cenk Gündoğan <cnkgndgn@gmail.com>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup sys_seq
* @{
*
* @file
* @brief Serial Number Arithmetic (RFC 1982)
*
* @author Cenk Gündoğan <cnkgndgn@gmail.com>
*
* @}
*/
#include <assert.h>
#include "seq.h"
seq8_t seq8_adds(seq8_t s, uint8_t n, uint8_t space)
{
/* check if space is a power of 2 minus 1 */
assert((space != 0) && ((space & (space + 1)) == 0));
if (n > SEQ_LIMIT(space)) {
return s;
}
return (space == UINT8_MAX) ? (s + n) : ((s + n) & space);
}
int seq8_compares(seq8_t s1, seq8_t s2, uint8_t space)
{
/* check if space is a power of 2 minus 1 */
assert((space != 0) && ((space & (space + 1)) == 0));
if (s1 == s2) {
return 0;
}
if (((s1 < s2) && ((uint8_t)(s2 - s1) < (SEQ_LIMIT(space) + 1))) ||
((s1 > s2) && ((uint8_t)(s1 - s2) > (SEQ_LIMIT(space) + 1)))) {
return -1;
}
if (((s1 < s2) && ((uint8_t)(s2 - s1) > (SEQ_LIMIT(space) + 1))) ||
((s1 > s2) && ((uint8_t)(s1 - s2) < (SEQ_LIMIT(space) + 1)))) {
return 1;
}
return -EINVAL;
}
seq16_t seq16_adds(seq16_t s, uint16_t n, uint16_t space)
{
/* check if space is a power of 2 minus 1 */
assert((space != 0) && ((space & (space + 1)) == 0));
if (n > SEQ_LIMIT(space)) {
return s;
}
return (space == UINT16_MAX) ? (s + n) : ((s + n) & space);
}
int seq16_compares(seq16_t s1, seq16_t s2, uint16_t space)
{
/* check if space is a power of 2 minus 1 */
assert((space != 0) && ((space & (space + 1)) == 0));
if (s1 == s2) {
return 0;
}
if (((s1 < s2) && ((uint16_t)(s2 - s1) < (SEQ_LIMIT(space) + 1))) ||
((s1 > s2) && ((uint16_t)(s1 - s2) > (SEQ_LIMIT(space) + 1)))) {
return -1;
}
if (((s1 < s2) && ((uint16_t)(s2 - s1) > (SEQ_LIMIT(space) + 1))) ||
((s1 > s2) && ((uint16_t)(s1 - s2) < (SEQ_LIMIT(space) + 1)))) {
return 1;
}
return -EINVAL;
}
seq32_t seq32_adds(seq32_t s, uint32_t n, uint32_t space)
{
/* check if space is a power of 2 minus 1 */
assert((space != 0) && ((space & (space + 1)) == 0));
if (n > SEQ_LIMIT(space)) {
return s;
}
return (space == UINT32_MAX) ? (s + n) : ((s + n) & space);
}
int seq32_compares(seq32_t s1, seq32_t s2, uint32_t space)
{
/* check if space is a power of 2 minus 1 */
assert((space != 0) && ((space & (space + 1)) == 0));
if (s1 == s2) {
return 0;
}
if (((s1 < s2) && ((uint32_t)(s2 - s1) < (SEQ_LIMIT(space) + 1))) ||
((s1 > s2) && ((uint32_t)(s1 - s2) > (SEQ_LIMIT(space) + 1)))) {
return -1;
}
if (((s1 < s2) && ((uint32_t)(s2 - s1) > (SEQ_LIMIT(space) + 1))) ||
((s1 > s2) && ((uint32_t)(s1 - s2) < (SEQ_LIMIT(space) + 1)))) {
return 1;
}
return -EINVAL;
}
seq64_t seq64_adds(seq64_t s, uint64_t n, uint64_t space)
{
/* check if space is a power of 2 minus 1 */
assert((space != 0) && ((space & (space + 1)) == 0));
if (n > SEQ_LIMIT(space)) {
return s;
}
return (space == UINT64_MAX) ? (s + n) : ((s + n) & space);
}
int seq64_compares(seq64_t s1, seq64_t s2, uint64_t space)
{
/* check if space is a power of 2 minus 1 */
assert((space != 0) && ((space & (space + 1)) == 0));
if (s1 == s2) {
return 0;
}
if (((s1 < s2) && ((uint64_t)(s2 - s1) < (SEQ_LIMIT(space) + 1))) ||
((s1 > s2) && ((uint64_t)(s1 - s2) > (SEQ_LIMIT(space) + 1)))) {
return -1;
}
if (((s1 < s2) && ((uint64_t)(s2 - s1) > (SEQ_LIMIT(space) + 1))) ||
((s1 > s2) && ((uint64_t)(s1 - s2) < (SEQ_LIMIT(space) + 1)))) {
return 1;
}
return -EINVAL;
}