mirror of
https://github.com/openvswitch/ovs
synced 2025-09-01 14:55:18 +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:
@@ -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 *p = mask_values;
|
||||
uint32_t hash;
|
||||
uint64_t map;
|
||||
int idx;
|
||||
|
||||
hash = basis;
|
||||
for (map = mask->masks.map; map; map = zero_rightmost_1bit(map)) {
|
||||
hash = hash_add(hash, flow_u32[raw_ctz(map)] & *p++);
|
||||
MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
|
||||
hash = hash_add(hash, flow_u32[idx] & *p++);
|
||||
}
|
||||
|
||||
return hash_finish(hash, (p - mask_values) * 4);
|
||||
@@ -180,9 +180,10 @@ flow_hash_in_minimask_range(const struct flow *flow,
|
||||
&offset);
|
||||
const uint32_t *p = mask_values + offset;
|
||||
uint32_t hash = *basis;
|
||||
int idx;
|
||||
|
||||
for (; map; map = zero_rightmost_1bit(map)) {
|
||||
hash = hash_add(hash, flow_u32[raw_ctz(map)] & *p++);
|
||||
MAP_FOR_EACH_INDEX(idx, map) {
|
||||
hash = hash_add(hash, flow_u32[idx] & *p++);
|
||||
}
|
||||
|
||||
*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,
|
||||
&offset);
|
||||
const uint32_t *p = miniflow_get_u32_values(&mask->masks) + offset;
|
||||
int idx;
|
||||
|
||||
for (; map; map = zero_rightmost_1bit(map)) {
|
||||
dst_u32[raw_ctz(map)] |= *p++;
|
||||
MAP_FOR_EACH_INDEX(idx, map) {
|
||||
dst_u32[idx] |= *p++;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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 *maskp = miniflow_get_u32_values(&mask->masks);
|
||||
uint32_t idx;
|
||||
int idx;
|
||||
|
||||
MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
|
||||
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 *maskp = miniflow_get_u32_values(&mask->masks);
|
||||
uint32_t idx;
|
||||
int idx;
|
||||
|
||||
MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
|
||||
uint32_t mask = *maskp++;
|
||||
|
44
lib/flow.c
44
lib/flow.c
@@ -1853,10 +1853,10 @@ miniflow_init__(struct miniflow *dst, const struct flow *src, int n)
|
||||
{
|
||||
const uint32_t *src_u32 = (const uint32_t *) src;
|
||||
uint32_t *dst_u32 = miniflow_alloc_values(dst, n);
|
||||
uint64_t map;
|
||||
int idx;
|
||||
|
||||
for (map = dst->map; map; map = zero_rightmost_1bit(map)) {
|
||||
*dst_u32++ = src_u32[raw_ctz(map)];
|
||||
MAP_FOR_EACH_INDEX(idx, dst->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 *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);
|
||||
|
||||
return !memcmp(ap, bp, count * sizeof *ap);
|
||||
} else {
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
@@ -2017,12 +2013,10 @@ miniflow_equal_in_minimask(const struct miniflow *a, const struct miniflow *b,
|
||||
const struct minimask *mask)
|
||||
{
|
||||
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)) {
|
||||
int ofs = raw_ctz(map);
|
||||
|
||||
if ((miniflow_get(a, ofs) ^ miniflow_get(b, ofs)) & *p++) {
|
||||
MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
|
||||
if ((miniflow_get(a, idx) ^ miniflow_get(b, idx)) & *p++) {
|
||||
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 *p = miniflow_get_u32_values(&mask->masks);
|
||||
uint64_t map;
|
||||
int idx;
|
||||
|
||||
for (map = mask->masks.map; map; map = zero_rightmost_1bit(map)) {
|
||||
int ofs = raw_ctz(map);
|
||||
|
||||
if ((miniflow_get(a, ofs) ^ b_u32[ofs]) & *p++) {
|
||||
MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
|
||||
if ((miniflow_get(a, idx) ^ b_u32[idx]) & *p++) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -2089,21 +2081,19 @@ minimask_combine(struct minimask *dst_,
|
||||
uint32_t *dst_values = storage;
|
||||
const struct miniflow *a = &a_->masks;
|
||||
const struct miniflow *b = &b_->masks;
|
||||
uint64_t map;
|
||||
int n = 0;
|
||||
int idx;
|
||||
|
||||
dst->values_inline = false;
|
||||
dst->offline_values = storage;
|
||||
|
||||
dst->map = 0;
|
||||
for (map = a->map & b->map; map; map = zero_rightmost_1bit(map)) {
|
||||
int ofs = raw_ctz(map);
|
||||
MAP_FOR_EACH_INDEX(idx, a->map & b->map) {
|
||||
/* 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) {
|
||||
dst->map |= rightmost_1bit(map);
|
||||
dst_values[n++] = mask;
|
||||
dst->map |= UINT64_C(1) << idx;
|
||||
*dst_values++ = mask;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -706,10 +706,10 @@ flow_union_with_miniflow(struct flow *dst, const struct miniflow *src)
|
||||
{
|
||||
uint32_t *dst_u32 = (uint32_t *) dst;
|
||||
const uint32_t *p = miniflow_get_u32_values(src);
|
||||
uint64_t map;
|
||||
int idx;
|
||||
|
||||
for (map = src->map; map; map = zero_rightmost_1bit(map)) {
|
||||
dst_u32[raw_ctz(map)] |= *p++;
|
||||
MAP_FOR_EACH_INDEX(idx, src->map) {
|
||||
dst_u32[idx] |= *p++;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1203,10 +1203,10 @@ minimatch_matches_flow(const struct minimatch *match,
|
||||
const uint32_t *target_u32 = (const uint32_t *) target;
|
||||
const uint32_t *flowp = miniflow_get_u32_values(&match->flow);
|
||||
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)) {
|
||||
if ((*flowp++ ^ target_u32[raw_ctz(map)]) & *maskp++) {
|
||||
MAP_FOR_EACH_INDEX(idx, match->flow.map) {
|
||||
if ((*flowp++ ^ target_u32[idx]) & *maskp++) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user