Vicent Marti | e127310 | 2013-11-14 07:43:51 -0500 | [diff] [blame] | 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, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | #ifndef __EWOK_RLW_H__ |
| 21 | #define __EWOK_RLW_H__ |
| 22 | |
| 23 | #define RLW_RUNNING_BITS (sizeof(eword_t) * 4) |
| 24 | #define RLW_LITERAL_BITS (sizeof(eword_t) * 8 - 1 - RLW_RUNNING_BITS) |
| 25 | |
| 26 | #define RLW_LARGEST_RUNNING_COUNT (((eword_t)1 << RLW_RUNNING_BITS) - 1) |
| 27 | #define RLW_LARGEST_LITERAL_COUNT (((eword_t)1 << RLW_LITERAL_BITS) - 1) |
| 28 | |
| 29 | #define RLW_LARGEST_RUNNING_COUNT_SHIFT (RLW_LARGEST_RUNNING_COUNT << 1) |
| 30 | |
| 31 | #define RLW_RUNNING_LEN_PLUS_BIT (((eword_t)1 << (RLW_RUNNING_BITS + 1)) - 1) |
| 32 | |
| 33 | static int rlw_get_run_bit(const eword_t *word) |
| 34 | { |
| 35 | return *word & (eword_t)1; |
| 36 | } |
| 37 | |
| 38 | static inline void rlw_set_run_bit(eword_t *word, int b) |
| 39 | { |
| 40 | if (b) { |
| 41 | *word |= (eword_t)1; |
| 42 | } else { |
| 43 | *word &= (eword_t)(~1); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | static inline void rlw_xor_run_bit(eword_t *word) |
| 48 | { |
| 49 | if (*word & 1) { |
| 50 | *word &= (eword_t)(~1); |
| 51 | } else { |
| 52 | *word |= (eword_t)1; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static inline void rlw_set_running_len(eword_t *word, eword_t l) |
| 57 | { |
| 58 | *word |= RLW_LARGEST_RUNNING_COUNT_SHIFT; |
| 59 | *word &= (l << 1) | (~RLW_LARGEST_RUNNING_COUNT_SHIFT); |
| 60 | } |
| 61 | |
| 62 | static inline eword_t rlw_get_running_len(const eword_t *word) |
| 63 | { |
| 64 | return (*word >> 1) & RLW_LARGEST_RUNNING_COUNT; |
| 65 | } |
| 66 | |
| 67 | static inline eword_t rlw_get_literal_words(const eword_t *word) |
| 68 | { |
| 69 | return *word >> (1 + RLW_RUNNING_BITS); |
| 70 | } |
| 71 | |
| 72 | static inline void rlw_set_literal_words(eword_t *word, eword_t l) |
| 73 | { |
| 74 | *word |= ~RLW_RUNNING_LEN_PLUS_BIT; |
| 75 | *word &= (l << (RLW_RUNNING_BITS + 1)) | RLW_RUNNING_LEN_PLUS_BIT; |
| 76 | } |
| 77 | |
| 78 | static inline eword_t rlw_size(const eword_t *self) |
| 79 | { |
| 80 | return rlw_get_running_len(self) + rlw_get_literal_words(self); |
| 81 | } |
| 82 | |
| 83 | struct rlw_iterator { |
| 84 | const eword_t *buffer; |
| 85 | size_t size; |
| 86 | size_t pointer; |
| 87 | size_t literal_word_start; |
| 88 | |
| 89 | struct { |
| 90 | const eword_t *word; |
| 91 | int literal_words; |
| 92 | int running_len; |
| 93 | int literal_word_offset; |
| 94 | int running_bit; |
| 95 | } rlw; |
| 96 | }; |
| 97 | |
| 98 | void rlwit_init(struct rlw_iterator *it, struct ewah_bitmap *bitmap); |
| 99 | void rlwit_discard_first_words(struct rlw_iterator *it, size_t x); |
| 100 | size_t rlwit_discharge( |
| 101 | struct rlw_iterator *it, struct ewah_bitmap *out, size_t max, int negate); |
| 102 | void rlwit_discharge_empty(struct rlw_iterator *it, struct ewah_bitmap *out); |
| 103 | |
| 104 | static inline size_t rlwit_word_size(struct rlw_iterator *it) |
| 105 | { |
| 106 | return it->rlw.running_len + it->rlw.literal_words; |
| 107 | } |
| 108 | |
| 109 | static inline size_t rlwit_literal_words(struct rlw_iterator *it) |
| 110 | { |
| 111 | return it->pointer - it->rlw.literal_words; |
| 112 | } |
| 113 | |
| 114 | #endif |