Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus |
| 17 | |
| 18 | Permission is hereby granted, free of charge, to any person obtaining |
| 19 | a copy of this software and associated documentation files (the |
| 20 | "Software"), to deal in the Software without restriction, including |
| 21 | without limitation the rights to use, copy, modify, merge, publish, |
| 22 | distribute, sublicense, and/or sell copies of the Software, and to |
| 23 | permit persons to whom the Software is furnished to do so, subject to |
| 24 | the following conditions: |
| 25 | |
| 26 | The above copyright notice and this permission notice shall be |
| 27 | included in all copies or substantial portions of the Software. |
| 28 | |
| 29 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 32 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 33 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 34 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 | */ |
| 37 | |
| 38 | #ifndef included_zvec_h |
| 39 | #define included_zvec_h |
| 40 | |
| 41 | #include <vppinfra/clib.h> |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 42 | #include <vppinfra/error.h> /* for ASSERT */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | #include <vppinfra/format.h> |
| 44 | |
| 45 | /* zvec: compressed vectors. |
| 46 | |
| 47 | Data is entropy coded with 32 bit "codings". |
| 48 | |
| 49 | Consider coding as bitmap, coding = 2^c_0 + 2^c_1 + ... + 2^c_n |
| 50 | With c_0 < c_1 < ... < c_n. coding == 0 represents c_n = BITS (uword). |
| 51 | |
| 52 | Unsigned integers i = 0 ... are represented as follows: |
| 53 | |
| 54 | 0 <= i < 2^c_0 (i << 1) | (1 << 0) binary: i 1 |
| 55 | 2^c_0 <= i < 2^c_0 + 2^c_1 (i << 2) | (1 << 1) binary: i 1 0 |
| 56 | ... binary: i 0 ... 0 |
| 57 | |
| 58 | Smaller numbers use less bits. Coding is chosen so that encoding |
| 59 | of given histogram of typical values gives smallest number of bits. |
| 60 | The number and position of coding bits c_i are used to best fit the |
| 61 | histogram of typical values. |
| 62 | */ |
| 63 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 64 | typedef struct |
| 65 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | /* Smallest coding for given histogram of typical data. */ |
| 67 | u32 coding; |
| 68 | |
| 69 | /* Number of data in histogram. */ |
| 70 | u32 n_data; |
| 71 | |
| 72 | /* Number of codes (unique values) in histogram. */ |
| 73 | u32 n_codes; |
| 74 | |
| 75 | /* Number of bits in smallest coding of data. */ |
| 76 | u32 min_coding_bits; |
| 77 | |
| 78 | /* Average number of bits per code. */ |
| 79 | f64 ave_coding_bits; |
| 80 | } zvec_coding_info_t; |
| 81 | |
| 82 | /* Encode/decode data. */ |
| 83 | uword zvec_encode (uword coding, uword data, uword * n_result_bits); |
| 84 | uword zvec_decode (uword coding, uword zdata, uword * n_zdata_bits); |
| 85 | |
| 86 | format_function_t format_zvec_coding; |
| 87 | |
| 88 | typedef u32 zvec_histogram_count_t; |
| 89 | |
| 90 | #define zvec_coding_from_histogram(h,count_field,len,max_value_to_encode,zc) \ |
| 91 | _zvec_coding_from_histogram ((h), (len), \ |
| 92 | STRUCT_OFFSET_OF_VAR (h, count_field), \ |
| 93 | sizeof (h[0]), \ |
| 94 | max_value_to_encode, \ |
| 95 | (zc)) |
| 96 | |
| 97 | uword |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 98 | _zvec_coding_from_histogram (void *_histogram, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | uword histogram_len, |
| 100 | uword histogram_elt_count_offset, |
| 101 | uword histogram_elt_bytes, |
| 102 | uword max_value_to_encode, |
| 103 | zvec_coding_info_t * coding_info_return); |
| 104 | |
| 105 | #define _(TYPE,IS_SIGNED) \ |
| 106 | uword * zvec_encode_##TYPE (uword * zvec, uword * zvec_n_bits, uword coding, \ |
| 107 | void * data, uword data_stride, uword n_data); |
| 108 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 109 | _(u8, /* is_signed */ 0); |
| 110 | _(u16, /* is_signed */ 0); |
| 111 | _(u32, /* is_signed */ 0); |
| 112 | _(u64, /* is_signed */ 0); |
| 113 | _(i8, /* is_signed */ 1); |
| 114 | _(i16, /* is_signed */ 1); |
| 115 | _(i32, /* is_signed */ 1); |
| 116 | _(i64, /* is_signed */ 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | |
| 118 | #undef _ |
| 119 | |
| 120 | #define _(TYPE,IS_SIGNED) \ |
| 121 | void zvec_decode_##TYPE (uword * zvec, \ |
| 122 | uword * zvec_n_bits, \ |
| 123 | uword coding, \ |
| 124 | void * data, \ |
| 125 | uword data_stride, \ |
| 126 | uword n_data) |
| 127 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 128 | _(u8, /* is_signed */ 0); |
| 129 | _(u16, /* is_signed */ 0); |
| 130 | _(u32, /* is_signed */ 0); |
| 131 | _(u64, /* is_signed */ 0); |
| 132 | _(i8, /* is_signed */ 1); |
| 133 | _(i16, /* is_signed */ 1); |
| 134 | _(i32, /* is_signed */ 1); |
| 135 | _(i64, /* is_signed */ 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | |
| 137 | #undef _ |
| 138 | |
| 139 | /* Signed <=> unsigned conversion. |
| 140 | -1, -2, -3, ... => 1, 3, 5, ... odds |
| 141 | 0, +1, +2, +3, ... => 0, 2, 4, 6, ... evens */ |
| 142 | always_inline uword |
| 143 | zvec_signed_to_unsigned (word s) |
| 144 | { |
| 145 | uword a = s < 0; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 146 | s = 2 * s + a; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | return a ? -s : s; |
| 148 | } |
| 149 | |
| 150 | always_inline word |
| 151 | zvec_unsigned_to_signed (uword u) |
| 152 | { |
| 153 | uword a = u & 1; |
| 154 | u >>= 1; |
| 155 | return a ? -u : u; |
| 156 | } |
| 157 | |
| 158 | #endif /* included_zvec_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * fd.io coding-style-patch-verification: ON |
| 162 | * |
| 163 | * Local Variables: |
| 164 | * eval: (c-set-style "gnu") |
| 165 | * End: |
| 166 | */ |