FD.io VPP  v16.06
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  /* Smallest coding for given histogram of typical data. */
67 
68  /* Number of data in histogram. */
70 
71  /* Number of codes (unique values) in histogram. */
73 
74  /* Number of bits in smallest coding of data. */
76 
77  /* Average number of bits per code. */
80 
81 /* Encode/decode data. */
82 uword zvec_encode (uword coding, uword data, uword * n_result_bits);
83 uword zvec_decode (uword coding, uword zdata, uword * n_zdata_bits);
84 
86 
88 
89 #define zvec_coding_from_histogram(h,count_field,len,max_value_to_encode,zc) \
90  _zvec_coding_from_histogram ((h), (len), \
91  STRUCT_OFFSET_OF_VAR (h, count_field), \
92  sizeof (h[0]), \
93  max_value_to_encode, \
94  (zc))
95 
96 uword
97 _zvec_coding_from_histogram (void * _histogram,
98  uword histogram_len,
99  uword histogram_elt_count_offset,
100  uword histogram_elt_bytes,
101  uword max_value_to_encode,
102  zvec_coding_info_t * coding_info_return);
103 
104 #define _(TYPE,IS_SIGNED) \
105  uword * zvec_encode_##TYPE (uword * zvec, uword * zvec_n_bits, uword coding, \
106  void * data, uword data_stride, uword n_data);
107 
108 _ (u8, /* is_signed */ 0);
109 _ (u16, /* is_signed */ 0);
110 _ (u32, /* is_signed */ 0);
111 _ (u64, /* is_signed */ 0);
112 _ (i8, /* is_signed */ 1);
113 _ (i16, /* is_signed */ 1);
114 _ (i32, /* is_signed */ 1);
115 _ (i64, /* is_signed */ 1);
116 
117 #undef _
118 
119 #define _(TYPE,IS_SIGNED) \
120  void zvec_decode_##TYPE (uword * zvec, \
121  uword * zvec_n_bits, \
122  uword coding, \
123  void * data, \
124  uword data_stride, \
125  uword n_data)
126 
127 _ (u8, /* is_signed */ 0);
128 _ (u16, /* is_signed */ 0);
129 _ (u32, /* is_signed */ 0);
130 _ (u64, /* is_signed */ 0);
131 _ (i8, /* is_signed */ 1);
132 _ (i16, /* is_signed */ 1);
133 _ (i32, /* is_signed */ 1);
134 _ (i64, /* is_signed */ 1);
135 
136 #undef _
137 
138 /* Signed <=> unsigned conversion.
139  -1, -2, -3, ... => 1, 3, 5, ... odds
140  0, +1, +2, +3, ... => 0, 2, 4, 6, ... evens */
143 {
144  uword a = s < 0;
145  s = 2*s + a;
146  return a ? -s : s;
147 }
148 
151 {
152  uword a = u & 1;
153  u >>= 1;
154  return a ? -u : u;
155 }
156 
157 #endif /* included_zvec_h */
a
Definition: bitmap.h:393
always_inline uword zvec_signed_to_unsigned(word s)
Definition: zvec.h:142
f64 ave_coding_bits
Definition: zvec.h:78
uword zvec_encode(uword coding, uword data, uword *n_result_bits)
Definition: zvec.c:96
#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:63
long i64
Definition: types.h:82
u32 zvec_histogram_count_t
Definition: zvec.h:87
format_function_t format_zvec_coding
Definition: zvec.h:85
unsigned int u32
Definition: types.h:88
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
i64 word
Definition: types.h:111
double f64
Definition: types.h:140
unsigned char u8
Definition: types.h:56
short i16
Definition: types.h:46
u32 min_coding_bits
Definition: zvec.h:75
always_inline word zvec_unsigned_to_signed(uword u)
Definition: zvec.h:150