summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/asm_x86.c
AgeCommit message (Expand)AuthorFilesLines
2018-10-23c11 safe string handling supportDave Barach1-1/+1
2016-12-28Reorganize source tree to use single autotools instanceDamjan Marion1-0/+1947
ref='#n74'>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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
/*
 * 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) 2010 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.
*/

#include <vppinfra/vhash.h>

#ifdef CLIB_HAVE_VEC128

/* Overflow search buckets have an extra u32x4 for saving key_hash data.
   This makes it easier to refill main search bucket from overflow vector. */
typedef struct
{
  /* 4 results for this bucket. */
  u32x4_union_t result;

  /* 4 hash codes for this bucket.  These are used to refill main
     search buckets from overflow buckets when space becomes available. */
  u32x4_union_t key_hash;

  /* n_key_u32s u32x4s of key data follow. */
  u32x4_union_t key[0];
} vhash_overflow_search_bucket_t;

always_inline void
set_overflow_result (vhash_overflow_search_bucket_t * b,
		     u32 i, u32 result, u32 key_hash)
{
  b->result.as_u32[i] = result;
  b->key_hash.as_u32[i] = key_hash;
}

always_inline void
free_overflow_bucket (vhash_overflow_buckets_t * ob,
		      vhash_overflow_search_bucket_t * b, u32 i)
{
  u32 o = (u32x4_union_t *) b - ob->search_buckets;
  ASSERT (o < vec_len (ob->search_buckets));
  vec_add1 (ob->free_indices, 4 * o + i);
}

always_inline vhash_overflow_search_bucket_t *
get_overflow_search_bucket (vhash_overflow_buckets_t * obs, u32 i,
			    u32 n_key_u32s)
{
  return ((vhash_overflow_search_bucket_t *)
	  vec_elt_at_index (obs->search_buckets, i));
}

always_inline vhash_overflow_search_bucket_t *
next_overflow_bucket (vhash_overflow_search_bucket_t * b, u32 n_key_u32s)
{
  return (vhash_overflow_search_bucket_t *) & b->key[n_key_u32s];
}

#define foreach_vhash_overflow_bucket(b,ob,n_key_u32s)			\
  for ((b) = (vhash_overflow_search_bucket_t *) ob->search_buckets;	\
       (u32x4_union_t *) (b) < vec_end (ob->search_buckets);		\
       b = next_overflow_bucket (b, n_key_u32s))

u32
vhash_get_overflow (vhash_t * h, u32 key_hash, u32 vi, u32 n_key_u32s)
{
  vhash_overflow_buckets_t *ob = vhash_get_overflow_buckets (h, key_hash);
  vhash_overflow_search_bucket_t *b;
  u32 i, result = 0;

  foreach_vhash_overflow_bucket (b, ob, n_key_u32s)
  {
    u32x4 r = b->result.as_u32x4;

    for (i = 0; i < n_key_u32s; i++)
      r &= vhash_bucket_compare (h, &b->key[0], i, vi);

    result = vhash_merge_results (r);
    if (result)
      break;
  }

  return result;
}

u32
vhash_set_overflow (vhash_t * h,
		    u32 key_hash, u32 vi, u32 new_result, u32 n_key_u32s)
{
  vhash_overflow_buckets_t *ob = vhash_get_overflow_buckets (h, key_hash);
  vhash_overflow_search_bucket_t *b;
  u32 i_set, i, old_result;

  foreach_vhash_overflow_bucket (b, ob, n_key_u32s)
  {
    u32x4 r;

    r = b->result.as_u32x4;
    for (i = 0; i < n_key_u32s; i++)
      r &= vhash_bucket_compare (h, &b->key[0], i, vi);

    old_result = vhash_merge_results (r);
    if (old_result)
      {
	i_set = vhash_non_empty_result_index (r);
	set_overflow_result (b, i_set, new_result, key_hash);
	return old_result;
      }
  }

  /* Check free list. */
  if (vec_len (ob->free_indices) == 0)
    {
      /* Out of free overflow buckets.  Resize. */
      u32 j, *p;
      i = vec_len (ob->search_buckets);
      vec_resize_aligned (ob->search_buckets,
			  sizeof (b[0]) / sizeof (u32x4) + n_key_u32s,
			  CLIB_CACHE_LINE_BYTES);
      vec_add2 (ob->free_indices, p, 4);
      for (j = 0; j < 4; j++)
	p[j] = 4 * i + j;
    }

  i = vec_pop (ob->free_indices);

  i_set = i & 3;
  b = ((vhash_overflow_search_bucket_t *)
       vec_elt_at_index (ob->search_buckets, i / 4));

  /* Insert result. */
  set_overflow_result (b, i_set, new_result, key_hash);

  /* Insert key. */
  for (i = 0; i < n_key_u32s; i++)
    b->key[i].as_u32[i_set] = vhash_get_key_word (h, i, vi);

  ob->n_overflow++;
  h->n_elts++;

  return /* old result was invalid */ 0;
}

u32
vhash_unset_overflow (vhash_t * h, u32 key_hash, u32 vi, u32 n_key_u32s)
{
  vhash_overflow_buckets_t *ob = vhash_get_overflow_buckets (h, key_hash);
  vhash_overflow_search_bucket_t *b;
  u32 i_set, i, old_result;

  foreach_vhash_overflow_bucket (b, ob, n_key_u32s)
  {
    u32x4 r;

    r = b->result.as_u32x4;
    for (i = 0; i < n_key_u32s; i++)
      r &= vhash_bucket_compare (h, &b->key[0], i, vi);

    old_result = vhash_merge_results (r);
    if (old_result)
      {
	i_set = vhash_non_empty_result_index (r);

	/* Invalidate result and invert key hash so that this will
	   never match since all keys in this overflow bucket have
	   matching key hashs. */
	set_overflow_result (b, i_set, 0, ~key_hash);

	free_overflow_bucket (ob, b, i_set);

	ASSERT (ob->n_overflow > 0);
	ob->n_overflow--;
	h->n_elts--;
	return old_result;
      }
  }

  /* Could not find key. */
  return 0;
}

void
vhash_unset_refill_from_overflow (vhash_t * h,
				  vhash_search_bucket_t * sb,
				  u32 key_hash, u32 n_key_u32s)
{
  vhash_overflow_buckets_t *obs = vhash_get_overflow_buckets (h, key_hash);
  vhash_overflow_search_bucket_t *ob;
  u32 i, j, i_refill, bucket_mask = h->bucket_mask.as_u32[0];

  /* Find overflow element with matching key hash. */
  foreach_vhash_overflow_bucket (ob, obs, n_key_u32s)
  {
    for (i = 0; i < 4; i++)
      {
	if (!ob->result.as_u32[i])
	  continue;
	if ((ob->key_hash.as_u32[i] & bucket_mask)
	    != (key_hash & bucket_mask))
	  continue;

	i_refill = vhash_empty_result_index (sb->result.as_u32x4);
	sb->result.as_u32[i_refill] = ob->result.as_u32[i];
	for (j = 0; j < n_key_u32s; j++)
	  sb->key[j].as_u32[i_refill] = ob->key[j].as_u32[i];
	set_overflow_result (ob, i, 0, ~key_hash);
	free_overflow_bucket (obs, ob, i);
	return;
      }
  }
}

void
vhash_init (vhash_t * h, u32 log2_n_keys, u32 n_key_u32, u32 * hash_seeds)
{
  uword i, j, m;
  vhash_search_bucket_t *b;

  memset (h, 0, sizeof (h[0]));

  /* Must have at least 4 keys (e.g. one search bucket). */
  log2_n_keys = clib_max (log2_n_keys, 2);

  h->log2_n_keys = log2_n_keys;
  h->n_key_u32 = n_key_u32;
  m = pow2_mask (h->log2_n_keys) & ~3;
  for (i = 0; i < VECTOR_WORD_TYPE_LEN (u32); i++)
    h->bucket_mask.as_u32[i] = m;

  /* Allocate and zero search buckets. */
  i = (sizeof (b[0]) / sizeof (u32x4) + n_key_u32) << (log2_n_keys - 2);
  vec_validate_aligned (h->search_buckets, i - 1, CLIB_CACHE_LINE_BYTES);

  for (i = 0; i < ARRAY_LEN (h->find_first_zero_table); i++)
    h->find_first_zero_table[i] = min_log2 (first_set (~i));

  for (i = 0; i < ARRAY_LEN (h->hash_seeds); i++)
    for (j = 0; j < VECTOR_WORD_TYPE_LEN (u32); j++)
      h->hash_seeds[i].as_u32[j] = hash_seeds[i];
}

static_always_inline u32
vhash_main_key_gather (void *_vm, u32 vi, u32 wi, u32 n_key_u32)
{
  vhash_main_t *vm = _vm;
  return vec_elt (vm->keys, vi * n_key_u32 + wi);
}

static_always_inline u32x4
vhash_main_4key_gather (void *_vm, u32 vi, u32 wi, u32 n_key_u32s)
{
  vhash_main_t *vm = _vm;
  u32x4_union_t x;

  ASSERT (n_key_u32s == vm->n_key_u32);
  ASSERT (wi < n_key_u32s);

  x.as_u32[0] = vec_elt (vm->keys, (vi + 0) * n_key_u32s + wi);
  x.as_u32[1] = vec_elt (vm->keys, (vi + 1) * n_key_u32s + wi);
  x.as_u32[2] = vec_elt (vm->keys, (vi + 2) * n_key_u32s + wi);
  x.as_u32[3] = vec_elt (vm->keys, (vi + 3) * n_key_u32s + wi);
  return x.as_u32x4;
}

static_always_inline u32
vhash_main_set_result (void *_vm, u32 vi, u32 old_result, u32 n_key_u32)
{
  vhash_main_t *vm = _vm;
  u32 *p = vec_elt_at_index (vm->results, vi);
  u32 new_result = p[0];
  p[0] = old_result;
  return new_result;
}

static_always_inline u32
vhash_main_get_result (void *_vm, u32 vi, u32 old_result, u32 n_key_u32)
{
  vhash_main_t *vm = _vm;
  vec_elt (vm->results, vi) = old_result;
  return old_result;
}

static_always_inline u32x4
vhash_main_get_4result (void *_vm, u32 vi, u32x4 old_result, u32 n_key_u32)
{
  vhash_main_t *vm = _vm;
  u32x4 *p = (u32x4 *) vec_elt_at_index (vm->results, vi);
  p[0] = old_result;
  return old_result;
}

#define _(N_KEY_U32)							\
  static_always_inline u32						\
  vhash_main_key_gather_##N_KEY_U32 (void * _vm, u32 vi, u32 i)		\
  { return vhash_main_key_gather (_vm, vi, i, N_KEY_U32); }		\
									\
  static_always_inline u32x4						\
  vhash_main_4key_gather_##N_KEY_U32 (void * _vm, u32 vi, u32 i)	\
  { return vhash_main_4key_gather (_vm, vi, i, N_KEY_U32); }		\
									\
  clib_pipeline_stage_static						\
  (vhash_main_gather_keys_stage_##N_KEY_U32,				\
   vhash_main_t *, vm, i,						\
   {									\
     vhash_gather_4key_stage						\
       (vm->vhash,							\
	/* vector_index */ i,						\
	vhash_main_4key_gather_##N_KEY_U32,				\
	vm,								\
	N_KEY_U32);							\
   })									\
									\
  clib_pipeline_stage_no_inline						\
  (vhash_main_gather_keys_mod_stage_##N_KEY_U32,			\
   vhash_main_t *, vm, i,						\
   {									\
     vhash_gather_key_stage						\
       (vm->vhash,							\
	/* vector_index */ vm->n_vectors_div_4,				\
	/* n_vectors */ vm->n_vectors_mod_4,				\
	vhash_main_key_gather_##N_KEY_U32,				\
	vm,								\
	N_KEY_U32);							\
   })									\
									\
  clib_pipeline_stage							\
  (vhash_main_hash_finalize_stage_##N_KEY_U32,				\
   vhash_main_t *, vm, i,						\
   {									\
     vhash_finalize_stage (vm->vhash, i, N_KEY_U32);			\
   })									\
									\
  clib_pipeline_stage_no_inline						\
  (vhash_main_hash_finalize_mod_stage_##N_KEY_U32,			\
   vhash_main_t *, vm, i,						\
   {									\
     vhash_finalize_stage (vm->vhash, vm->n_vectors_div_4, N_KEY_U32);	\
   })									\
									\
  clib_pipeline_stage_static						\
  (vhash_main_get_stage_##N_KEY_U32,					\
   vhash_main_t *, vm, i,						\
   {									\
     vhash_get_4_stage (vm->vhash,					\
			/* vector_index */ i,				\
			vhash_main_get_4result,				\
			vm, N_KEY_U32);					\
   })									\
									\
  clib_pipeline_stage_no_inline						\
  (vhash_main_get_mod_stage_##N_KEY_U32,				\
   vhash_main_t *, vm, i,						\
   {									\
     vhash_get_stage (vm->vhash,					\
		      /* vector_index */ vm->n_vectors_div_4,		\
		      /* n_vectors */ vm->n_vectors_mod_4,		\
		      vhash_main_get_result,				\
		      vm, N_KEY_U32);					\
   })									\
									\
  clib_pipeline_stage_static						\
  (vhash_main_set_stage_##N_KEY_U32,					\
   vhash_main_t *, vm, i,						\
   {									\
     vhash_set_stage (vm->vhash,					\
		      /* vector_index */ i,				\
		      /* n_vectors */ VECTOR_WORD_TYPE_LEN (u32),	\
		      vhash_main_set_result,				\
		      vm, N_KEY_U32);					\
   })									\
									\
  clib_pipeline_stage_no_inline						\
  (vhash_main_set_mod_stage_##N_KEY_U32,				\
   vhash_main_t *, vm, i,						\
   {									\
     vhash_set_stage (vm->vhash,					\
		      /* vector_index */ vm->n_vectors_div_4,		\
		      /* n_vectors */ vm->n_vectors_mod_4,		\
		      vhash_main_set_result,				\
		      vm, N_KEY_U32);					\
   })									\
									\
  clib_pipeline_stage_static						\
  (vhash_main_unset_stage_##N_KEY_U32,					\
   vhash_main_t *, vm, i,						\
   {									\
     vhash_unset_stage (vm->vhash,					\
		      /* vector_index */ i,				\
		      /* n_vectors */ VECTOR_WORD_TYPE_LEN (u32),	\
		      vhash_main_get_result,				\
		      vm, N_KEY_U32);					\
   })									\
									\
  clib_pipeline_stage_no_inline						\
  (vhash_main_unset_mod_stage_##N_KEY_U32,				\
   vhash_main_t *, vm, i,						\
   {									\
     vhash_unset_stage (vm->vhash,					\
		      /* vector_index */ vm->n_vectors_div_4,		\
		      /* n_vectors */ vm->n_vectors_mod_4,		\
		      vhash_main_get_result,				\
		      vm, N_KEY_U32);					\
   })

_(1);
_(2);
_(3);
_(4);
_(5);
_(6);

#undef _

#define _(N_KEY_U32)							\
  clib_pipeline_stage							\
  (vhash_main_hash_mix_stage_##N_KEY_U32,				\
   vhash_main_t *, vm, i,						\
   {									\
     vhash_mix_stage (vm->vhash, i, N_KEY_U32);				\
   })									\
									\
  clib_pipeline_stage_no_inline						\
  (vhash_main_hash_mix_mod_stage_##N_KEY_U32,				\
   vhash_main_t *, vm, i,						\
   {									\
     vhash_mix_stage (vm->vhash, vm->n_vectors_div_4, N_KEY_U32);	\
   })

_(4);
_(5);
_(6);

#undef _

typedef enum
{
  GET, SET, UNSET,
} vhash_main_op_t;

static void
vhash_main_op (vhash_main_t * vm, vhash_main_op_t op)
{
  u32 n_keys = vec_len (vm->results);

  vm->n_key_u32 = vm->vhash->n_key_u32;

  vhash_validate_sizes (vm->vhash, vm->n_key_u32, n_keys);

  vm->n_vectors_div_4 = n_keys / 4;
  vm->n_vectors_mod_4 = n_keys % 4;

  if (vm->n_vectors_div_4 > 0)
    {
      switch (vm->n_key_u32)
	{
	default:
	  ASSERT (0);
	  break;

#define _(N_KEY_U32)						\
	case N_KEY_U32:						\
	  if (op == GET)					\
	    clib_pipeline_run_3_stage				\
	      (vm->n_vectors_div_4,				\
	       vm,						\
	       vhash_main_gather_keys_stage_##N_KEY_U32,	\
	       vhash_main_hash_finalize_stage_##N_KEY_U32,	\
	       vhash_main_get_stage_##N_KEY_U32);		\
	  else if (op == SET)					\
	    clib_pipeline_run_3_stage				\
	      (vm->n_vectors_div_4,				\
	       vm,						\
	       vhash_main_gather_keys_stage_##N_KEY_U32,	\
	       vhash_main_hash_finalize_stage_##N_KEY_U32,	\
	       vhash_main_set_stage_##N_KEY_U32);		\
	  else							\
	    clib_pipeline_run_3_stage				\
	      (vm->n_vectors_div_4,				\
	       vm,						\
	       vhash_main_gather_keys_stage_##N_KEY_U32,	\
	       vhash_main_hash_finalize_stage_##N_KEY_U32,	\
	       vhash_main_unset_stage_##N_KEY_U32);		\
	  break;

	  _(1);
	  _(2);
	  _(3);

#undef _

#define _(N_KEY_U32)						\
	case N_KEY_U32:						\
	  if (op == GET)					\
	    clib_pipeline_run_4_stage				\
	      (vm->n_vectors_div_4,				\
	       vm,						\
	       vhash_main_gather_keys_stage_##N_KEY_U32,	\
	       vhash_main_hash_mix_stage_##N_KEY_U32,		\
	       vhash_main_hash_finalize_stage_##N_KEY_U32,	\
	       vhash_main_get_stage_##N_KEY_U32);		\
	  else if (op == SET)					\
	    clib_pipeline_run_4_stage				\
	      (vm->n_vectors_div_4,				\
	       vm,						\
	       vhash_main_gather_keys_stage_##N_KEY_U32,	\
	       vhash_main_hash_mix_stage_##N_KEY_U32,		\
	       vhash_main_hash_finalize_stage_##N_KEY_U32,	\
	       vhash_main_set_stage_##N_KEY_U32);		\
	  else							\
	    clib_pipeline_run_4_stage				\
	      (vm->n_vectors_div_4,				\
	       vm,						\
	       vhash_main_gather_keys_stage_##N_KEY_U32,	\
	       vhash_main_hash_mix_stage_##N_KEY_U32,		\
	       vhash_main_hash_finalize_stage_##N_KEY_U32,	\
	       vhash_main_unset_stage_##N_KEY_U32);		\
	  break;

	  _(4);
	  _(5);
	  _(6);

#undef _
	}
    }


  if (vm->n_vectors_mod_4 > 0)
    {
      switch (vm->n_key_u32)
	{
	default:
	  ASSERT (0);
	  break;

#define _(N_KEY_U32)						\
	case N_KEY_U32:						\
	  if (op == GET)					\
	    clib_pipeline_run_3_stage				\
	      (1,						\
	       vm,						\
	       vhash_main_gather_keys_mod_stage_##N_KEY_U32,	\
	       vhash_main_hash_finalize_mod_stage_##N_KEY_U32,	\
	       vhash_main_get_mod_stage_##N_KEY_U32);		\
	  else if (op == SET)					\
	    clib_pipeline_run_3_stage				\
	      (1,						\
	       vm,						\
	       vhash_main_gather_keys_mod_stage_##N_KEY_U32,	\
	       vhash_main_hash_finalize_mod_stage_##N_KEY_U32,	\
	       vhash_main_set_mod_stage_##N_KEY_U32);		\
	  else							\
	    clib_pipeline_run_3_stage				\
	      (1,						\
	       vm,						\
	       vhash_main_gather_keys_mod_stage_##N_KEY_U32,	\
	       vhash_main_hash_finalize_mod_stage_##N_KEY_U32,	\
	       vhash_main_unset_mod_stage_##N_KEY_U32);		\
	break;

	  _(1);
	  _(2);
	  _(3);

#undef _

#define _(N_KEY_U32)						\
	case N_KEY_U32:						\
	  if (op == GET)					\
	    clib_pipeline_run_4_stage				\
	      (1,						\
	       vm,						\
	       vhash_main_gather_keys_mod_stage_##N_KEY_U32,	\
	       vhash_main_hash_mix_mod_stage_##N_KEY_U32,	\
	       vhash_main_hash_finalize_mod_stage_##N_KEY_U32,	\
	       vhash_main_get_mod_stage_##N_KEY_U32);		\
	  else if (op == SET)					\
	    clib_pipeline_run_4_stage				\
	      (1,						\
	       vm,						\
	       vhash_main_gather_keys_mod_stage_##N_KEY_U32,	\
	       vhash_main_hash_mix_mod_stage_##N_KEY_U32,	\
	       vhash_main_hash_finalize_mod_stage_##N_KEY_U32,	\
	       vhash_main_set_mod_stage_##N_KEY_U32);		\
	  else							\
	    clib_pipeline_run_4_stage				\
	      (1,						\
	       vm,						\
	       vhash_main_gather_keys_mod_stage_##N_KEY_U32,	\
	       vhash_main_hash_mix_mod_stage_##N_KEY_U32,	\
	       vhash_main_hash_finalize_mod_stage_##N_KEY_U32,	\
	       vhash_main_unset_mod_stage_##N_KEY_U32);		\
	  break;

	  _(4);
	  _(5);
	  _(6);

#undef _
	}
    }
}

void
vhash_main_get (vhash_main_t * vm)
{
  vhash_main_op (vm, GET);
}

void
vhash_main_set (vhash_main_t * vm)
{
  vhash_main_op (vm, SET);
}

void
vhash_main_unset (vhash_main_t * vm)
{
  vhash_main_op (vm, UNSET);
}

u32
vhash_resize_incremental (vhash_resize_t * vr, u32 vector_index,
			  u32 n_keys_this_call)
{
  vhash_t *old = vr->old;
  vhash_main_t *vm = &vr->new;
  vhash_t *new = vm->vhash;
  uword i, j, n_key_u32;

  n_key_u32 = old->n_key_u32;

  if (vector_index == 0)
    {
      u32 hash_seeds[3];
      hash_seeds[0] = old->hash_seeds[0].as_u32[0];
      hash_seeds[1] = old->hash_seeds[1].as_u32[0];
      hash_seeds[2] = old->hash_seeds[2].as_u32[0];
      vhash_init (new, old->log2_n_keys + 1, n_key_u32, hash_seeds);
    }

  vec_reset_length (vm->keys);
  vec_reset_length (vm->results);

  if (0 == (vector_index >> old->log2_n_keys))
    {
      for (i = vector_index; 0 == (i >> (old->log2_n_keys - 2)); i++)
	{
	  vhash_search_bucket_t *b =
	    vhash_get_search_bucket_with_index (old, 4 * i, n_key_u32);
	  u32 r, *k;

#define _(I)					\
  if ((r = b->result.as_u32[I]) != 0)		\
    {						\
      vec_add1 (vm->results, r - 1);		\
      vec_add2 (vm->keys, k, n_key_u32);	\
      for (j = 0; j < n_key_u32; j++)		\
	k[j] = b->key[j].as_u32[I];		\
    }

	  _(0);
	  _(1);
	  _(2);
	  _(3);

#undef _

	  if (vec_len (vm->results) >= n_keys_this_call)
	    {
	      vhash_main_op (vm, SET);
	      return i;
	    }
	}
    }

  /* Add overflow buckets. */
  {
    vhash_overflow_buckets_t *ob;
    vhash_overflow_search_bucket_t *b;

    for (ob = old->overflow_buckets;
	 ob < old->overflow_buckets + ARRAY_LEN (old->overflow_buckets); ob++)
      {
	foreach_vhash_overflow_bucket (b, ob, old->n_key_u32)
	{
	  u32 r, *k;

#define _(I)					\
  if ((r = b->result.as_u32[I]) != 0)		\
    {						\
      vec_add1 (vm->results, r - 1);		\
      vec_add2 (vm->keys, k, n_key_u32);	\
      for (j = 0; j < n_key_u32; j++)		\
	k[j] = b->key[j].as_u32[I];		\
    }

	  _(0);
	  _(1);
	  _(2);
	  _(3);

#undef _
	}
      }
  }

  vhash_main_op (vm, SET);

  /* Let caller know we are done. */
  return ~0;
}

void
vhash_resize (vhash_t * old, u32 log2_n_keys)
{
  static vhash_resize_t vr;
  vhash_t new;
  u32 i = 0;

  vr.old = old;
  vr.new.vhash = &new;

  while (1)
    {
      i = vhash_resize_incremental (&vr, i, 1024);
      if (i == ~0)
	break;
    }

  vhash_free (old);
  *old = new;
}

#endif /* CLIB_HAVE_VEC128 */

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