aboutsummaryrefslogtreecommitdiffstats
path: root/extras/deprecated/vppinfra/zvec.h
blob: 7d35a3fe41fe6aeff804899294fcab8f34995acf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
  Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef included_zvec_h
#define included_zvec_h

#include <vppinfra/clib.h>
#include <vppinfra/error.h>	/* for ASSERT */
#include <vppinfra/format.h>

/* zvec: compressed vectors.

   Data is entropy coded with 32 bit "codings".

   Consider coding as bitmap, coding = 2^c_0 + 2^c_1 + ... + 2^c_n
   With c_0 < c_1 < ... < c_n.  coding == 0 represents c_n = BITS (uword).

   Unsigned integers i = 0 ... are represented as follows:

       0 <= i < 2^c_0       	(i << 1) | (1 << 0) binary:   i 1
   2^c_0 <= i < 2^c_0 + 2^c_1   (i << 2) | (1 << 1) binary: i 1 0
   ...                                              binary: i 0 ... 0

   Smaller numbers use less bits.  Coding is chosen so that encoding
   of given histogram of typical values gives smallest number of bits.
   The number and position of coding bits c_i are used to best fit the
   histogram of typical values.
*/

typedef struct
{
  /* Smallest coding for given histogram of typical data. */
  u32 coding;

  /* Number of data in histogram. */
  u32 n_data;

  /* Number of codes (unique values) in histogram. */
  u32 n_codes;

  /* Number of bits in smallest coding of data. */
  u32 min_coding_bits;

  /* Average number of bits per code. */
  f64 ave_coding_bits;
} zvec_coding_info_t;

/* Encode/decode data. */
uword zvec_encode (uword coding, uword data, uword * n_result_bits);
uword zvec_decode (uword coding, uword zdata, uword * n_zdata_bits);

format_function_t format_zvec_coding;

typedef u32 zvec_histogram_count_t;

#define zvec_coding_from_histogram(h,count_field,len,max_value_to_encode,zc) \
  _zvec_coding_from_histogram ((h), (len),				\
			       STRUCT_OFFSET_OF_VAR (h, count_field),	\
			       sizeof (h[0]),				\
			       max_value_to_encode,			\
			       (zc))

uword
_zvec_coding_from_histogram (void *_histogram,
			     uword histogram_len,
			     uword histogram_elt_count_offset,
			     uword histogram_elt_bytes,
			     uword max_value_to_encode,
			     zvec_coding_info_t * coding_info_return);

#define _(TYPE,IS_SIGNED)						\
  uword * zvec_encode_##TYPE (uword * zvec, uword * zvec_n_bits, uword coding, \
			   void * data, uword data_stride, uword n_data);

_(u8, /* is_signed */ 0);
_(u16, /* is_signed */ 0);
_(u32, /* is_signed */ 0);
_(u64, /* is_signed */ 0);
_(i8, /* is_signed */ 1);
_(i16, /* is_signed */ 1);
_(i32, /* is_signed */ 1);
_(i64, /* is_signed */ 1);

#undef _

#define _(TYPE,IS_SIGNED)			\
  void zvec_decode_##TYPE (uword * zvec,	\
			   uword * zvec_n_bits,	\
			   uword coding,	\
			   void * data,		\
			   uword data_stride,	\
			   uword n_data)

_(u8, /* is_signed */ 0);
_(u16, /* is_signed */ 0);
_(u32, /* is_signed */ 0);
_(u64, /* is_signed */ 0);
_(i8, /* is_signed */ 1);
_(i16, /* is_signed */ 1);
_(i32, /* is_signed */ 1);
_(i64, /* is_signed */ 1);

#undef _

/* Signed <=> unsigned conversion.
      -1, -2, -3, ... =>    1, 3, 5, ... odds
   0, +1, +2, +3, ... => 0, 2, 4, 6, ... evens */
always_inline uword
zvec_signed_to_unsigned (word s)
{
  uword a = s < 0;
  s = 2 * s + a;
  return a ? -s : s;
}

always_inline word
zvec_unsigned_to_signed (uword u)
{
  uword a = u & 1;
  u >>= 1;
  return a ? -u : u;
}

#endif /* included_zvec_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */