2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
20 #include <rte_cycles.h>
22 #include "prox_malloc.h"
25 static uint32_t round_pow2(uint32_t val)
30 while ((s & val) == 0)
32 if (s == 1U << 31 && s != val)
42 static uint32_t get_r_max(struct cdf *cdf, uint32_t cur)
44 uint32_t right_child = cur;
48 right_child = cur * 2 + 1;
49 } while (right_child < cdf->elems[0]);
51 return cdf->elems[cur];
54 struct cdf *cdf_create(uint32_t n_vals, int socket_id)
58 uint32_t n_vals_round = round_pow2(n_vals);
60 if (0 == n_vals_round)
63 mem_size += sizeof(struct cdf);
64 mem_size += sizeof(((struct cdf *)(0))->elems[0]) * n_vals_round * 2;
65 ret = prox_zmalloc(mem_size, socket_id);
66 ret->elems[0] = n_vals;
68 /* leafs are [n_vals, 2 * n_vals[. During cdf_add() and
69 cdf_setup(), rand_max refers to the index of the next leaf
71 ret->rand_max = n_vals_round;
72 ret->first_child = n_vals_round;
73 ret->seed = rte_rdtsc();
78 void cdf_add(struct cdf *cdf, uint32_t len)
80 cdf->elems[cdf->rand_max++] = len;
83 int cdf_setup(struct cdf *cdf)
85 uint32_t last_leaf, first_leaf;
86 uint32_t first_parent, last_parent;
87 uint32_t total, multiplier, cur, end;
89 if (cdf->elems[0] == 1) {
90 cdf->rand_max = RAND_MAX;
91 cdf->elems[1] = RAND_MAX;
96 last_leaf = cdf->rand_max;
97 first_leaf = round_pow2(cdf->elems[0]);
98 /* Failed to add all elements through cdf_add() */
99 if (last_leaf - first_leaf != cdf->elems[0])
103 for (uint32_t i = first_leaf; i < last_leaf; ++i) {
104 total += cdf->elems[i];
107 multiplier = RAND_MAX / total;
108 if (multiplier * total == RAND_MAX)
110 cdf->rand_max = multiplier * total;
112 for (uint32_t i = first_leaf; i < last_leaf; ++i) {
113 uint32_t cur = cdf->elems[i];
115 /* Each element represents the range between previous
116 total (non-inclusive) and new total (inclusive). */
117 total += cur * multiplier - 1;
118 cdf->elems[i] = total;
121 end = round_pow2(first_leaf) << 1;
122 for (uint32_t i = last_leaf; i < end; ++i) {
123 cdf->elems[i] = RAND_MAX;
125 cdf->first_child = first_leaf;
128 /* Build the binary tree used at run-time. */
131 first_parent = first_leaf/2;
132 last_parent = last_leaf/2;
134 for (uint32_t i = first_parent; i <= last_parent; ++i) {
135 /* The current nodes value should be the
136 biggest value accessible through its left
137 child. This value is stored in the right
138 most child of the left child. The left most
139 child of the right child is the first value
140 that can not be accessed through the left
142 cdf->elems[i] = get_r_max(cdf, i * 2);
144 first_leaf = first_parent;
145 last_leaf = last_parent;
146 } while (first_parent != last_parent);