65 uword c, d, result, n_bits;
66 uword explicit_end, implicit_end;
73 implicit_end = c == coding;
74 explicit_end = (zdata & 1) &~ implicit_end;
75 d = (zdata >> explicit_end) & (c - 1);
76 if (explicit_end | implicit_end)
79 n_bits +=
min_log2 (c) + explicit_end;
91 *n_zdata_bits = n_bits;
98 uword * n_result_bits)
100 uword c, shift, result;
101 uword explicit_end, implicit_end;
105 ASSERT (data <= coding - 1);
111 implicit_end = c == coding;
112 explicit_end = ((data & (c - 1)) == data);
113 if (explicit_end | implicit_end)
115 uword t = explicit_end &~ implicit_end;
116 result = ((data << t) | t) << shift;
137 else if (data_bytes == 2)
139 else if (data_bytes == 4)
141 else if (data_bytes == 8)
160 else if (data_bytes == 2)
167 else if (data_bytes == 4)
174 else if (data_bytes == 8)
204 d0 =
get_data (data + 0*data_stride, data_bytes, is_signed);
205 data += 1*data_stride;
217 #define _(TYPE,IS_SIGNED) \ 218 uword * zvec_encode_##TYPE (uword * zvec, \ 219 uword * zvec_n_bits, \ 225 return zvec_encode_inline (zvec, zvec_n_bits, \ 227 data, data_stride, n_data, \ 272 put_data (data + 0*data_stride, data_bytes, is_signed, d0);
273 data += 1*data_stride;
279 #define _(TYPE,IS_SIGNED) \ 280 void zvec_decode_##TYPE (uword * zvec, \ 281 uword * zvec_n_bits, \ 287 return zvec_decode_inline (zvec, zvec_n_bits, \ 289 data, data_stride, n_data, \ 307 uword * histogram_counts,
310 uword n_type_bits, n_bits;
311 uword this_count, last_count, max_count_index;
317 max_count_index =
vec_len (histogram_counts) - 1;
320 if (coding <= max_count_index)
330 this_count = histogram_counts[i > max_count_index ? max_count_index : i-1];
333 if (this_count == last_count)
340 n_bits += (this_count - last_count) * (n_type_bits + l);
343 if (n_bits >= min_bits)
346 last_count = this_count;
355 _zvec_coding_from_histogram (
void * histogram,
357 uword histogram_elt_count_offset,
358 uword histogram_elt_bytes,
359 uword max_value_to_encode,
362 uword coding, min_coding;
363 uword min_coding_bits, coding_bits;
364 uword i, n_bits_set, total_count;
368 if (histogram_len < 1)
370 coding_return->
coding = 0;
372 coding_return->
n_data = 0;
380 for (i = 0; i < histogram_len; i++)
383 total_count += this_count;
384 counts[
i] = total_count;
389 min_coding_bits = ~0;
392 uword base_coding = max_value_to_encode != ~0 ? (1 + max_value_to_encode) :
vec_len (counts);
395 for (n_bits_set = 1; n_bits_set <= 8; n_bits_set++)
402 if (coding_bits >= min_coding_bits)
404 min_coding_bits = coding_bits;
412 coding_return->
coding = min_coding;
414 coding_return->
n_data = total_count;
427 return format (s,
"zvec coding 0x%x, %d elts, %d codes, %d bits total, %.4f ave bits/code",
always_inline uword max_pow2(uword x)
sll srl srl sll sra u16x4 i
static void(BVT(clib_bihash)*h, BVT(clib_bihash_value)*v)
always_inline void put_data(void *data, uword data_bytes, uword is_signed, uword x)
always_inline uword zvec_signed_to_unsigned(word s)
always_inline uword first_set(uword x)
#define vec_new(T, N)
Create new vector of given type and length (unspecified alignment, no header).
always_inline uword coding_max_n_bits(uword coding)
uword zvec_decode(uword coding, uword zdata, uword *n_zdata_bits)
uword zvec_encode(uword coding, uword data, uword *n_result_bits)
always_inline uword next_with_same_number_of_set_bits(uword x)
always_inline void zvec_decode_inline(uword *zvec, uword *zvec_n_bits, uword coding, void *data, uword data_stride, uword n_data, uword data_bytes, uword is_signed)
u32 zvec_histogram_count_t
#define vec_free(V)
Free vector's memory (no header).
always_inline uword * clib_bitmap_set_multiple(uword *bitmap, uword i, uword value, uword n_bits)
always_inline uword get_data(void *data, uword data_bytes, uword is_signed)
always_inline uword * zvec_encode_inline(uword *zvec, uword *zvec_n_bits, uword coding, void *data, uword data_stride, uword n_data, uword data_bytes, uword is_signed)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static uword zvec_coding_bits(uword coding, uword *histogram_counts, uword min_bits)
always_inline uword clib_bitmap_get_multiple(uword *bitmap, uword i, uword n_bits)
always_inline uword min_log2(uword x)
always_inline uword pow2_mask(uword x)
u8 * format_zvec_coding(u8 *s, va_list *args)
always_inline word zvec_unsigned_to_signed(uword u)
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".