/* * 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 #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; } }
/*
 * 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.
 */
/*
  ------------------------------------------------------------------------------
  By Bob Jenkins, 1996, Public Domain
  MODIFIED:
  960327: Creation (addition of randinit, really)
  970719: use context, not global variables, for internal state
  980324: renamed seed to flag
  980605: recommend ISAAC_LOG2_SIZE=4 for noncryptography.
  010626: note this is public domain
  ------------------------------------------------------------------------------

  Modified for CLIB by Eliot Dresselhaus.
  Dear Bob, Thanks for all the great work. - Eliot

  modifications copyright (c) 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_random_isaac_h
#define included_random_isaac_h

#include <vppinfra/clib.h>	/* for u32/u64 */
#include <vppinfra/format.h>	/* for unformat_input_t */

/* Bob recommends 8 for crypto, 4 for simulations */
#define ISAAC_LOG2_SIZE   (4)
#define ISAAC_SIZE (1 << ISAAC_LOG2_SIZE)

typedef struct
{
  uword memory[ISAAC_SIZE];
  uword a, b, c;
} isaac_t;

void isaac (isaa