1/**
2 * Copyright 2013, GitHub, Inc
3 * Copyright 2009-2013, Daniel Lemire, Cliff Moon,
4 * David McIntosh, Robert Becho, Google Inc. and Veronika Zenz
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19#ifndef __EWOK_RLW_H__
20#define __EWOK_RLW_H__
21
22#define RLW_RUNNING_BITS (sizeof(eword_t) * 4)
23#define RLW_LITERAL_BITS (sizeof(eword_t) * 8 - 1 - RLW_RUNNING_BITS)
24
25#define RLW_LARGEST_RUNNING_COUNT (((eword_t)1 << RLW_RUNNING_BITS) - 1)
26#define RLW_LARGEST_LITERAL_COUNT (((eword_t)1 << RLW_LITERAL_BITS) - 1)
27
28#define RLW_LARGEST_RUNNING_COUNT_SHIFT (RLW_LARGEST_RUNNING_COUNT << 1)
29
30#define RLW_RUNNING_LEN_PLUS_BIT (((eword_t)1 << (RLW_RUNNING_BITS + 1)) - 1)
31
32static int rlw_get_run_bit(const eword_t *word)
33{
34 return *word & (eword_t)1;
35}
36
37static inline void rlw_set_run_bit(eword_t *word, int b)
38{
39 if (b) {
40 *word |= (eword_t)1;
41 } else {
42 *word &= (eword_t)(~1);
43 }
44}
45
46static inline void rlw_xor_run_bit(eword_t *word)
47{
48 if (*word & 1) {
49 *word &= (eword_t)(~1);
50 } else {
51 *word |= (eword_t)1;
52 }
53}
54
55static inline void rlw_set_running_len(eword_t *word, eword_t l)
56{
57 *word |= RLW_LARGEST_RUNNING_COUNT_SHIFT;
58 *word &= (l << 1) | (~RLW_LARGEST_RUNNING_COUNT_SHIFT);
59}
60
61static inline eword_t rlw_get_running_len(const eword_t *word)
62{
63 return (*word >> 1) & RLW_LARGEST_RUNNING_COUNT;
64}
65
66static inline eword_t rlw_get_literal_words(const eword_t *word)
67{
68 return *word >> (1 + RLW_RUNNING_BITS);
69}
70
71static inline void rlw_set_literal_words(eword_t *word, eword_t l)
72{
73 *word |= ~RLW_RUNNING_LEN_PLUS_BIT;
74 *word &= (l << (RLW_RUNNING_BITS + 1)) | RLW_RUNNING_LEN_PLUS_BIT;
75}
76
77static inline eword_t rlw_size(const eword_t *self)
78{
79 return rlw_get_running_len(self) + rlw_get_literal_words(self);
80}
81
82struct rlw_iterator {
83 const eword_t *buffer;
84 size_t size;
85 size_t pointer;
86 size_t literal_word_start;
87
88 struct {
89 const eword_t *word;
90 int literal_words;
91 int running_len;
92 int literal_word_offset;
93 int running_bit;
94 } rlw;
95};
96
97void rlwit_init(struct rlw_iterator *it, struct ewah_bitmap *bitmap);
98void rlwit_discard_first_words(struct rlw_iterator *it, size_t x);
99size_t rlwit_discharge(
100 struct rlw_iterator *it, struct ewah_bitmap *out, size_t max, int negate);
101
102static inline size_t rlwit_word_size(struct rlw_iterator *it)
103{
104 return it->rlw.running_len + it->rlw.literal_words;
105}
106
107static inline size_t rlwit_literal_words(struct rlw_iterator *it)
108{
109 return it->pointer - it->rlw.literal_words;
110}
111
112#endif