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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
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:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
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.
38 #ifndef included_zvec_h
39 #define included_zvec_h
41 #include <vppinfra/clib.h>
42 #include <vppinfra/error.h> /* for ASSERT */
43 #include <vppinfra/format.h>
45 /* zvec: compressed vectors.
47 Data is entropy coded with 32 bit "codings".
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).
52 Unsigned integers i = 0 ... are represented as follows:
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
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.
66 /* Smallest coding for given histogram of typical data. */
69 /* Number of data in histogram. */
72 /* Number of codes (unique values) in histogram. */
75 /* Number of bits in smallest coding of data. */
78 /* Average number of bits per code. */
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);
86 format_function_t format_zvec_coding;
88 typedef u32 zvec_histogram_count_t;
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), \
94 max_value_to_encode, \
98 _zvec_coding_from_histogram (void *_histogram,
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);
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);
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);
120 #define _(TYPE,IS_SIGNED) \
121 void zvec_decode_##TYPE (uword * zvec, \
122 uword * zvec_n_bits, \
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);
139 /* Signed <=> unsigned conversion.
140 -1, -2, -3, ... => 1, 3, 5, ... odds
141 0, +1, +2, +3, ... => 0, 2, 4, 6, ... evens */
143 zvec_signed_to_unsigned (word s)
151 zvec_unsigned_to_signed (uword u)
158 #endif /* included_zvec_h */
161 * fd.io coding-style-patch-verification: ON
164 * eval: (c-set-style "gnu")