blob: 7d35a3fe41fe6aeff804899294fcab8f34995acf [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Barachc3799992016-08-15 11:12:27 -040042#include <vppinfra/error.h> /* for ASSERT */
Ed Warnickecb9cada2015-12-08 15:45:58 -070043#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 Barachc3799992016-08-15 11:12:27 -040064typedef struct
65{
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 /* 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. */
83uword zvec_encode (uword coding, uword data, uword * n_result_bits);
84uword zvec_decode (uword coding, uword zdata, uword * n_zdata_bits);
85
86format_function_t format_zvec_coding;
87
88typedef 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
97uword
Dave Barachc3799992016-08-15 11:12:27 -040098_zvec_coding_from_histogram (void *_histogram,
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 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 Barachc3799992016-08-15 11:12:27 -0400109_(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 Warnickecb9cada2015-12-08 15:45:58 -0700117
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 Barachc3799992016-08-15 11:12:27 -0400128_(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 Warnickecb9cada2015-12-08 15:45:58 -0700136
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 */
142always_inline uword
143zvec_signed_to_unsigned (word s)
144{
145 uword a = s < 0;
Dave Barachc3799992016-08-15 11:12:27 -0400146 s = 2 * s + a;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 return a ? -s : s;
148}
149
150always_inline word
151zvec_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 Barachc3799992016-08-15 11:12:27 -0400159
160/*
161 * fd.io coding-style-patch-verification: ON
162 *
163 * Local Variables:
164 * eval: (c-set-style "gnu")
165 * End:
166 */