2
0
mirror of https://github.com/openvswitch/ovs synced 2025-09-02 15:25:22 +00:00

lib: Use MAP_FOR_EACH_INDEX to improve readability.

Use MAP_FOR_EACH_INDEX to improve readability when there is no
apparent cost for it.

Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
This commit is contained in:
Jarno Rajahalme
2014-11-26 15:30:33 -08:00
parent f4d335e985
commit 1cea007c92
5 changed files with 34 additions and 42 deletions

View File

@@ -132,11 +132,11 @@ flow_hash_in_minimask(const struct flow *flow, const struct minimask *mask,
const uint32_t *flow_u32 = (const uint32_t *)flow; const uint32_t *flow_u32 = (const uint32_t *)flow;
const uint32_t *p = mask_values; const uint32_t *p = mask_values;
uint32_t hash; uint32_t hash;
uint64_t map; int idx;
hash = basis; hash = basis;
for (map = mask->masks.map; map; map = zero_rightmost_1bit(map)) { MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
hash = hash_add(hash, flow_u32[raw_ctz(map)] & *p++); hash = hash_add(hash, flow_u32[idx] & *p++);
} }
return hash_finish(hash, (p - mask_values) * 4); return hash_finish(hash, (p - mask_values) * 4);
@@ -180,9 +180,10 @@ flow_hash_in_minimask_range(const struct flow *flow,
&offset); &offset);
const uint32_t *p = mask_values + offset; const uint32_t *p = mask_values + offset;
uint32_t hash = *basis; uint32_t hash = *basis;
int idx;
for (; map; map = zero_rightmost_1bit(map)) { MAP_FOR_EACH_INDEX(idx, map) {
hash = hash_add(hash, flow_u32[raw_ctz(map)] & *p++); hash = hash_add(hash, flow_u32[idx] & *p++);
} }
*basis = hash; /* Allow continuation from the unfinished value. */ *basis = hash; /* Allow continuation from the unfinished value. */
@@ -209,9 +210,10 @@ flow_wildcards_fold_minimask_range(struct flow_wildcards *wc,
uint64_t map = miniflow_get_map_in_range(&mask->masks, start, end, uint64_t map = miniflow_get_map_in_range(&mask->masks, start, end,
&offset); &offset);
const uint32_t *p = miniflow_get_u32_values(&mask->masks) + offset; const uint32_t *p = miniflow_get_u32_values(&mask->masks) + offset;
int idx;
for (; map; map = zero_rightmost_1bit(map)) { MAP_FOR_EACH_INDEX(idx, map) {
dst_u32[raw_ctz(map)] |= *p++; dst_u32[idx] |= *p++;
} }
} }

View File

@@ -1283,7 +1283,7 @@ miniflow_and_mask_matches_flow(const struct miniflow *flow,
{ {
const uint32_t *flowp = miniflow_get_u32_values(flow); const uint32_t *flowp = miniflow_get_u32_values(flow);
const uint32_t *maskp = miniflow_get_u32_values(&mask->masks); const uint32_t *maskp = miniflow_get_u32_values(&mask->masks);
uint32_t idx; int idx;
MAP_FOR_EACH_INDEX(idx, mask->masks.map) { MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
uint32_t diff = (*flowp++ ^ flow_u32_value(target, idx)) & *maskp++; uint32_t diff = (*flowp++ ^ flow_u32_value(target, idx)) & *maskp++;
@@ -1326,7 +1326,7 @@ miniflow_and_mask_matches_flow_wc(const struct miniflow *flow,
{ {
const uint32_t *flowp = miniflow_get_u32_values(flow); const uint32_t *flowp = miniflow_get_u32_values(flow);
const uint32_t *maskp = miniflow_get_u32_values(&mask->masks); const uint32_t *maskp = miniflow_get_u32_values(&mask->masks);
uint32_t idx; int idx;
MAP_FOR_EACH_INDEX(idx, mask->masks.map) { MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
uint32_t mask = *maskp++; uint32_t mask = *maskp++;

View File

@@ -1853,10 +1853,10 @@ miniflow_init__(struct miniflow *dst, const struct flow *src, int n)
{ {
const uint32_t *src_u32 = (const uint32_t *) src; const uint32_t *src_u32 = (const uint32_t *) src;
uint32_t *dst_u32 = miniflow_alloc_values(dst, n); uint32_t *dst_u32 = miniflow_alloc_values(dst, n);
uint64_t map; int idx;
for (map = dst->map; map; map = zero_rightmost_1bit(map)) { MAP_FOR_EACH_INDEX(idx, dst->map) {
*dst_u32++ = src_u32[raw_ctz(map)]; *dst_u32++ = src_u32[idx];
} }
} }
@@ -1986,22 +1986,18 @@ miniflow_equal(const struct miniflow *a, const struct miniflow *b)
{ {
const uint32_t *ap = miniflow_get_u32_values(a); const uint32_t *ap = miniflow_get_u32_values(a);
const uint32_t *bp = miniflow_get_u32_values(b); const uint32_t *bp = miniflow_get_u32_values(b);
const uint64_t a_map = a->map;
const uint64_t b_map = b->map;
if (OVS_LIKELY(a_map == b_map)) { if (OVS_LIKELY(a->map == b->map)) {
int count = miniflow_n_values(a); int count = miniflow_n_values(a);
return !memcmp(ap, bp, count * sizeof *ap); return !memcmp(ap, bp, count * sizeof *ap);
} else { } else {
uint64_t map; uint64_t map;
for (map = a_map | b_map; map; map = zero_rightmost_1bit(map)) { for (map = a->map | b->map; map; map = zero_rightmost_1bit(map)) {
uint64_t bit = rightmost_1bit(map); uint64_t bit = rightmost_1bit(map);
uint64_t a_value = a_map & bit ? *ap++ : 0;
uint64_t b_value = b_map & bit ? *bp++ : 0;
if (a_value != b_value) { if ((a->map & bit ? *ap++ : 0) != (b->map & bit ? *bp++ : 0)) {
return false; return false;
} }
} }
@@ -2017,12 +2013,10 @@ miniflow_equal_in_minimask(const struct miniflow *a, const struct miniflow *b,
const struct minimask *mask) const struct minimask *mask)
{ {
const uint32_t *p = miniflow_get_u32_values(&mask->masks); const uint32_t *p = miniflow_get_u32_values(&mask->masks);
uint64_t map; int idx;
for (map = mask->masks.map; map; map = zero_rightmost_1bit(map)) { MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
int ofs = raw_ctz(map); if ((miniflow_get(a, idx) ^ miniflow_get(b, idx)) & *p++) {
if ((miniflow_get(a, ofs) ^ miniflow_get(b, ofs)) & *p++) {
return false; return false;
} }
} }
@@ -2038,12 +2032,10 @@ miniflow_equal_flow_in_minimask(const struct miniflow *a, const struct flow *b,
{ {
const uint32_t *b_u32 = (const uint32_t *) b; const uint32_t *b_u32 = (const uint32_t *) b;
const uint32_t *p = miniflow_get_u32_values(&mask->masks); const uint32_t *p = miniflow_get_u32_values(&mask->masks);
uint64_t map; int idx;
for (map = mask->masks.map; map; map = zero_rightmost_1bit(map)) { MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
int ofs = raw_ctz(map); if ((miniflow_get(a, idx) ^ b_u32[idx]) & *p++) {
if ((miniflow_get(a, ofs) ^ b_u32[ofs]) & *p++) {
return false; return false;
} }
} }
@@ -2089,21 +2081,19 @@ minimask_combine(struct minimask *dst_,
uint32_t *dst_values = storage; uint32_t *dst_values = storage;
const struct miniflow *a = &a_->masks; const struct miniflow *a = &a_->masks;
const struct miniflow *b = &b_->masks; const struct miniflow *b = &b_->masks;
uint64_t map; int idx;
int n = 0;
dst->values_inline = false; dst->values_inline = false;
dst->offline_values = storage; dst->offline_values = storage;
dst->map = 0; dst->map = 0;
for (map = a->map & b->map; map; map = zero_rightmost_1bit(map)) { MAP_FOR_EACH_INDEX(idx, a->map & b->map) {
int ofs = raw_ctz(map);
/* Both 'a' and 'b' have non-zero data at 'idx'. */ /* Both 'a' and 'b' have non-zero data at 'idx'. */
uint32_t mask = miniflow_get__(a, ofs) & miniflow_get__(b, ofs); uint32_t mask = miniflow_get__(a, idx) & miniflow_get__(b, idx);
if (mask) { if (mask) {
dst->map |= rightmost_1bit(map); dst->map |= UINT64_C(1) << idx;
dst_values[n++] = mask; *dst_values++ = mask;
} }
} }
} }

View File

@@ -706,10 +706,10 @@ flow_union_with_miniflow(struct flow *dst, const struct miniflow *src)
{ {
uint32_t *dst_u32 = (uint32_t *) dst; uint32_t *dst_u32 = (uint32_t *) dst;
const uint32_t *p = miniflow_get_u32_values(src); const uint32_t *p = miniflow_get_u32_values(src);
uint64_t map; int idx;
for (map = src->map; map; map = zero_rightmost_1bit(map)) { MAP_FOR_EACH_INDEX(idx, src->map) {
dst_u32[raw_ctz(map)] |= *p++; dst_u32[idx] |= *p++;
} }
} }

View File

@@ -1203,10 +1203,10 @@ minimatch_matches_flow(const struct minimatch *match,
const uint32_t *target_u32 = (const uint32_t *) target; const uint32_t *target_u32 = (const uint32_t *) target;
const uint32_t *flowp = miniflow_get_u32_values(&match->flow); const uint32_t *flowp = miniflow_get_u32_values(&match->flow);
const uint32_t *maskp = miniflow_get_u32_values(&match->mask.masks); const uint32_t *maskp = miniflow_get_u32_values(&match->mask.masks);
uint64_t map; int idx;
for (map = match->flow.map; map; map = zero_rightmost_1bit(map)) { MAP_FOR_EACH_INDEX(idx, match->flow.map) {
if ((*flowp++ ^ target_u32[raw_ctz(map)]) & *maskp++) { if ((*flowp++ ^ target_u32[idx]) & *maskp++) {
return false; return false;
} }
} }