File: bitwiseOperations.cpp

package info (click to toggle)
faust 2.79.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 397,496 kB
  • sloc: cpp: 278,433; ansic: 116,164; javascript: 18,529; vhdl: 14,052; sh: 13,884; java: 5,900; objc: 3,852; python: 3,222; makefile: 2,655; cs: 1,672; lisp: 1,146; ruby: 954; yacc: 586; xml: 471; lex: 247; awk: 110; tcl: 26
file content (276 lines) | stat: -rw-r--r-- 7,712 bytes parent folder | download | duplicates (2)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* Copyright 2023 Yann ORLAREY
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <cassert>
#include <tuple>
#include <utility>

#include "bitwiseOperations.hh"

namespace itv {

/**
 * Split a signed interval into two unsigned intervals, for the negative and the positive part
 */
std::pair<UInterval, UInterval> signSplit(const SInterval& x)
{
    if (isEmpty(x)) {
        return {UEMPTY, UEMPTY};
    }
    if (x.hi < 0) {
        return {{(unsigned int)(x.lo), (unsigned int)(x.hi)}, UEMPTY};
    }
    if (x.lo >= 0) {
        return {UEMPTY, {(unsigned int)(x.lo), (unsigned int)(x.hi)}};
    }
    return {{(unsigned int)(x.lo), (unsigned int)(-1)}, {(unsigned int)(0), (unsigned int)(x.hi)}};
}

/**
 * Merge two unsigned intervals to form a signed interval
 */
SInterval signMerge(const UInterval& np, const UInterval& pp)
{
    if (isEmpty(np)) {
        if (isEmpty(pp)) {
            return SEMPTY;
        }
        return {(int)(pp.lo), (int)(pp.hi)};
    }
    if (isEmpty(pp)) {
        return {(int)(np.lo), (int)(np.hi)};
    }

    return {(int)(np.lo), (int)(pp.hi)};
}

UInterval bitwiseUnsignedNot(const UInterval& a)
{
    return UInterval{(unsigned int)(~a.hi), (unsigned int)(~a.lo)};
}

SInterval bitwiseSignedNot(const SInterval& a)
{
    return SInterval{(int)(~a.hi), (int)(~a.lo)};
}

//==============================================================================
// main algorithm
UInterval                                      operator+(const UInterval& a, unsigned int offset);
UInterval                                      operator-(const UInterval& a, unsigned int offset);
unsigned int                                   loOr2(UInterval a, UInterval b);
unsigned int                                   hiOr2(UInterval a, UInterval b);
std::tuple<unsigned int, UInterval, UInterval> splitInterval(UInterval x);
static bool                                    contains(const UInterval& i, unsigned int x);

UInterval bitwiseUnsignedOr(const UInterval& a, const UInterval& b)
{
    if (a == UInterval{0, 0}) {
        return b;
    }
    if (b == UInterval{0, 0}) {
        return a;
    }
    if (isEmpty(a)) {
        return a;
    }
    if (isEmpty(b)) {
        return b;
    }
    UInterval r{loOr2(a, b), hiOr2(a, b)};
    return r;
}

//==============================================================================
static bool contains(const UInterval& i, unsigned int x)
{
    return (i.lo <= x) && (x <= i.hi);
}

unsigned int hiOr2(UInterval a, UInterval b)
{
    // simple cases
    if (a.lo == 0 && a.hi == 0) {
        return b.hi;
    }
    if (b.lo == 0 && b.hi == 0) {
        return a.hi;
    }

    // analyze and split the intervals
    auto [ma, a0, a1] = splitInterval(a);
    auto [mb, b0, b1] = splitInterval(b);

    // mask rule
    if ((a.hi == 2 * ma - 1) || (b.hi == 2 * mb - 1)) {
        return a.hi | b.hi;
    }

    if (mb > ma) {
        if (contains(a, mb - 1)) {
            return 2 * mb - 1;
        }
        return hiOr2(b1 - mb, a) + mb;
    }
    if (ma > mb) {
        if (contains(b, ma - 1)) {
            return 2 * ma - 1;
        }
        return hiOr2(a1 - ma, b) + ma;
    }
    // ma == mb != 0
    if (isEmpty(a0) && isEmpty(b0)) {
        return hiOr2(a1 - ma, b1 - ma) + ma;
    }
    if (isEmpty(a0)) {
        return std::max(hiOr2(a1 - ma, b1 - ma), hiOr2(a1 - ma, b0)) + ma;
    }
    if (isEmpty(b0)) {
        return std::max(hiOr2(a1 - ma, b1 - ma), hiOr2(a0, b1 - ma)) + ma;
    }
    return std::max(hiOr2(a1 - ma, b1 - ma), std::max(hiOr2(a1 - ma, b0), hiOr2(a0, b1 - ma))) + ma;
}

unsigned int loOr2(UInterval a, UInterval b)
{
    // isEmpty case
    if (isEmpty(a) || isEmpty(b)) {
        return 0;
    }

    // zero cases
    if (a.lo == 0) {
        return b.lo;
    }
    if (b.lo == 0) {
        return a.lo;
    }

    // non zero cases
    auto [ma, a0, a1] = splitInterval(a);
    auto [mb, b0, b1] = splitInterval(b);
    assert(ma != 0 && mb != 0);

    // obvious cases
    if (ma > mb) {
        if (isEmpty(a0)) {
            return loOr2(a1 - ma, b) | ma;  // ma bit unavoidable !
        }
        return loOr2(a0, b);
    }
    if (mb > ma) {
        if (isEmpty(b0)) {
            return loOr2(a, b1 - mb) | mb;
        }
        return loOr2(a, b0);
    }
    // ma == mb != 0
    if (!isEmpty(a0) && !isEmpty(b0)) {
        return loOr2(a0, b0);  // obvious case !
    }
    if (isEmpty(a0) && isEmpty(b0)) {
        return loOr2(a1 - ma, b1 - ma) | ma;  // ma bit unavoidable !
    }
    if (isEmpty(a0)) {
        return std::min(loOr2(a1 - ma, b0) | ma, loOr2(a1 - ma, b1 - ma) | ma);
    }
    return std::min(loOr2(a0, b1 - mb) | mb, loOr2(a1 - ma, b1 - ma) | ma);
}

//==============================================================================
// details

UInterval operator+(const UInterval& a, unsigned int offset)
{
    return {(unsigned int)(a.lo + offset), (unsigned int)(a.hi + offset)};
}

UInterval operator-(const UInterval& a, unsigned int offset)
{
    return {(unsigned int)(a.lo - offset), (unsigned int)(a.hi - offset)};
}

unsigned int msb32(unsigned int x)
{
    x |= (x >> 1);
    x |= (x >> 2);
    x |= (x >> 4);
    x |= (x >> 8);
    x |= (x >> 16);
    return (x & ~(x >> 1));
}

// split interval according to its msb
std::tuple<unsigned int, UInterval, UInterval> splitInterval(UInterval x)
{
    if (x.lo == 0 && x.hi == 0) {
        return {0, {1, 0}, x};  // special case, no msb
    }
    unsigned int m = msb32(x.hi);
    assert(m > 0);

    if (m <= x.lo) {
        return {m, {1, 0}, x};  // no msb in the interval
    }
    return {m, {x.lo, (unsigned int)(m - 1)}, {m, x.hi}};
}

SInterval bitwiseSignedOr(const SInterval& a, const SInterval& b)
{
    auto [an, ap] = signSplit(a);
    auto [bn, bp] = signSplit(b);
    UInterval pp  = bitwiseUnsignedOr(ap, bp);
    UInterval nn  = bitwiseUnsignedOr(an, bn);
    UInterval pn  = bitwiseUnsignedOr(ap, bn);
    UInterval np  = bitwiseUnsignedOr(an, bp);
    return signMerge(np + nn + pn, pp);
}

//==============================================================================
// main algorithm

UInterval bitwiseUnsignedAnd(const UInterval& a, const UInterval& b)
{
    return bitwiseUnsignedNot(bitwiseUnsignedOr(bitwiseUnsignedNot(a), bitwiseUnsignedNot(b)));
}

SInterval bitwiseSignedAnd(const SInterval& a, const SInterval& b)
{
    return bitwiseSignedNot(bitwiseSignedOr(bitwiseSignedNot(a), bitwiseSignedNot(b)));
}

//==============================================================================
// main algorithm
// p^q = p|q & non(p&q)
//

// p&q = non(non(p)|non(q))

UInterval bitwiseUnsignedXOr(const UInterval& a, const UInterval& b)
{
    return bitwiseUnsignedAnd(bitwiseUnsignedOr(a, b),
                              bitwiseUnsignedNot(bitwiseUnsignedAnd(a, b)));
}

SInterval bitwiseSignedXOr(const SInterval& a, const SInterval& b)
{
    auto [an, ap] = signSplit(a);
    auto [bn, bp] = signSplit(b);
    UInterval pp  = bitwiseUnsignedXOr(ap, bp);
    UInterval nn  = bitwiseUnsignedXOr(an, bn);
    UInterval pn  = bitwiseUnsignedXOr(ap, bn);
    UInterval np  = bitwiseUnsignedXOr(an, bp);
    return signMerge(np + pn, pp + nn);
}
}  // namespace itv