FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
zvec.h
Go to the documentation of this file.
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>
42 #include <vppinfra/error.h> /* for ASSERT */
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 
64 typedef struct
65 {
66  /* Smallest coding for given histogram of typical data. */
68 
69  /* Number of data in histogram. */
71 
72  /* Number of codes (unique values) in histogram. */
74 
75  /* Number of bits in smallest coding of data. */
77 
78  /* Average number of bits per code. */
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 
87 
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
98 _zvec_coding_from_histogram (void *_histogram,
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 
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);
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 
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);
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 */
144 {
145  uword a = s < 0;
146  s = 2 * s + a;
147  return a ? -s : s;
148 }
149 
152 {
153  uword a = u & 1;
154  u >>= 1;
155  return a ? -u : u;
156 }
157 
158 #endif /* included_zvec_h */
159 
160 /*
161  * fd.io coding-style-patch-verification: ON
162  *
163  * Local Variables:
164  * eval: (c-set-style "gnu")
165  * End:
166  */
a
Definition: bitmap.h:516
f64 ave_coding_bits
Definition: zvec.h:79
uword zvec_encode(uword coding, uword data, uword *n_result_bits)
Definition: zvec.c:97
#define always_inline
Definition: clib.h:84
int i32
Definition: types.h:81
char i8
Definition: types.h:45
unsigned long u64
Definition: types.h:89
uword zvec_decode(uword coding, uword zdata, uword *n_zdata_bits)
Definition: zvec.c:64
static uword zvec_signed_to_unsigned(word s)
Definition: zvec.h:143
long i64
Definition: types.h:82
u32 zvec_histogram_count_t
Definition: zvec.h:88
format_function_t format_zvec_coding
Definition: zvec.h:86
unsigned int u32
Definition: types.h:88
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
static word zvec_unsigned_to_signed(uword u)
Definition: zvec.h:151
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
i64 word
Definition: types.h:111
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
short i16
Definition: types.h:46
u32 min_coding_bits
Definition: zvec.h:76