2
* Copyright (c) 2011, 2012 Nicira, Inc.
2
* Copyright (c) 2011, 2012, 2013 Nicira, Inc.
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
93
/* Returns a random number in the range 'min'...'max' inclusive. */
95
random_in_range(uint32_t min, uint32_t max)
97
return min == max ? min : min + random_range(max - min + 1);
101
check_rup2(uint32_t x, int n)
103
uint32_t rup2 = ROUND_UP_POW2(x);
105
fprintf(stderr, "ROUND_UP_POW2(%#"PRIx32") is %#"PRIx32" "
106
"but should be %#"PRIx32"\n", x, rup2, n);
112
test_round_up_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
116
for (n = 0; n < 32; n++) {
117
/* Min, max value for which ROUND_UP_POW2 should yield (1 << n). */
118
uint32_t min = ((1u << n) >> 1) + 1;
119
uint32_t max = 1u << n;
121
check_rup2(min, 1u << n);
122
check_rup2(max, 1u << n);
123
check_rup2(random_in_range(min, max), 1u << n);
129
check_rdp2(uint32_t x, int n)
131
uint32_t rdp2 = ROUND_DOWN_POW2(x);
133
fprintf(stderr, "ROUND_DOWN_POW2(%#"PRIx32") is %#"PRIx32" "
134
"but should be %#"PRIx32"\n", x, rdp2, n);
140
test_round_down_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
144
for (n = 0; n < 32; n++) {
145
/* Min, max value for which ROUND_DOWN_POW2 should yield (1 << n). */
146
uint32_t min = 1u << n;
147
uint32_t max = ((1u << n) << 1) - 1;
149
check_rdp2(min, 1u << n);
150
check_rdp2(max, 1u << n);
151
check_rdp2(random_in_range(min, max), 1u << n);
94
157
shuffle(unsigned int *p, size_t n)
96
159
for (; n > 1; n--, p++) {
97
unsigned int *q = &p[rand() % n];
160
unsigned int *q = &p[random_range(n)];
98
161
unsigned int tmp = *p;
347
410
static const struct command commands[] = {
348
411
{"ctz", 0, 0, test_ctz},
412
{"round_up_pow2", 0, 0, test_round_up_pow2},
413
{"round_down_pow2", 0, 0, test_round_down_pow2},
349
414
{"popcount", 0, 0, test_popcount},
350
415
{"log_2_floor", 0, 0, test_log_2_floor},
351
416
{"bitwise_copy", 0, 0, test_bitwise_copy},