-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverflow.cs
More file actions
142 lines (122 loc) · 4.48 KB
/
Overflow.cs
File metadata and controls
142 lines (122 loc) · 4.48 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
using System.Collections;
using System.Collections.Generic;
namespace Kaminari
{
public static class Overflow
{
// TODO(gpascualg): Templatize
public static bool le(ushort x, ushort y, ushort threshold = ushort.MaxValue / 2)
{
return
(x < y && (y - x) < threshold) || // Standard case, 200 < 255 && (255 - 200) < thr
(x > y && (x - y) > threshold); // Overflow case, 255 > 1 && (255 - 1) > thr
}
public static bool leq(ushort x, ushort y, ushort threshold = ushort.MaxValue / 2)
{
return
(x <= y && (y - x) < threshold) || // Standard case, 200 < 255 && (255 - 200) < thr
(x > y && (x - y) > threshold); // Overflow case, 255 > 1 && (255 - 1) > thr
}
public static bool ge(ushort x, ushort y, ushort threshold = ushort.MaxValue / 2)
{
return
(x > y && (x - y) < threshold) || // Standard case, 255 > 200 && (255 - 200) < thr
(y > x && (y - x) > threshold); // Overflow case, 255 > 1 && (255 - 1) > thr
}
public static bool geq(ushort x, ushort y, ushort threshold = ushort.MaxValue / 2)
{
return
(x >= y && (x - y) < threshold) || // Standard case, 255 > 200 && (255 - 200) < thr
(y > x && (y - x) > threshold); // Overflow case, 255 > 1 && (255 - 1) > thr
}
public static ushort max(ushort x, ushort y, byte threshold = byte.MaxValue / 2)
{
return geq(x, y, threshold) ? x : y;
}
public static bool le(byte x, byte y, byte threshold = byte.MaxValue / 2)
{
return
(x < y && (y - x) < threshold) || // Standard case, 200 < 255 && (255 - 200) < thr
(x > y && (x - y) > threshold); // Overflow case, 255 > 1 && (255 - 1) > thr
}
public static bool leq(byte x, byte y, byte threshold = byte.MaxValue / 2)
{
return
(x <= y && (y - x) < threshold) || // Standard case, 200 < 255 && (255 - 200) < thr
(x > y && (x - y) > threshold); // Overflow case, 255 > 1 && (255 - 1) > thr
}
public static bool ge(byte x, byte y, byte threshold = byte.MaxValue / 2)
{
return
(x > y && (x - y) < threshold) || // Standard case, 255 > 200 && (255 - 200) < thr
(y > x && (y - x) > threshold); // Overflow case, 255 > 1 && (255 - 1) > thr
}
public static bool geq(byte x, byte y, byte threshold = byte.MaxValue / 2)
{
return
(x >= y && (x - y) < threshold) || // Standard case, 255 > 200 && (255 - 200) < thr
(y > x && (y - x) > threshold); // Overflow case, 255 > 1 && (255 - 1) > thr
}
public static ushort sub(ushort x, ushort y)
{
return
(x >= y) ? (ushort)(x - y) : (ushort)(ushort.MaxValue - y + x);
}
public static ushort submod(ushort x, ushort y, ushort m)
{
return
(x >= y) ? mod((ushort)(x - y), m) : (ushort)(m - y + x);
}
public static ushort add(ushort x, ushort y)
{
return (ushort)(x + y);
}
public static ushort abs_diff(ushort x, ushort y)
{
return
(x >= y) ? (ushort)(x - y) : (ushort)(y - x);
}
public static int signed_diff(ushort x, ushort y)
{
if (ge(x, y))
{
return sub(x, y);
}
return -sub(y, x);
}
public static ushort sub0(ushort x, ushort y)
{
ushort z = sub(x, y);
if (z != 0)
{
return z;
}
return ushort.MaxValue;
}
public static ushort inc(ushort x)
{
return (ushort)(++x);
}
public static byte inc(byte x)
{
return (byte)(++x);
}
public static ushort inc_max(ushort x, ushort max)
{
return (ushort)((ushort)(++x) % max);
}
public static ushort inc0(ushort x)
{
ushort z = inc(x);
if (z != 0)
{
return z;
}
return 1;
}
public static ushort mod(ushort x, ushort y)
{
return (ushort)(x % y);
}
}
}