2 * Copyright (c) 2011, 2012 Nicira, Inc.
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.
24 #include "byte-order.h"
25 #include "command-line.h"
30 check_log_2_floor(uint32_t x, int n)
32 if (log_2_floor(x) != n) {
33 fprintf(stderr, "log_2_floor(%"PRIu32") is %d but should be %d\n",
34 x, log_2_floor(x), n);
40 test_log_2_floor(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
44 for (n = 0; n < 32; n++) {
45 /* Check minimum x such that f(x) == n. */
46 check_log_2_floor(1 << n, n);
48 /* Check maximum x such that f(x) == n. */
49 check_log_2_floor((1 << n) | ((1 << n) - 1), n);
51 /* Check a random value in the middle. */
52 check_log_2_floor((random_uint32() & ((1 << n) - 1)) | (1 << n), n);
55 /* log_2_floor(0) is undefined, so don't check it. */
59 check_ctz(uint32_t x, int n)
62 fprintf(stderr, "ctz(%"PRIu32") is %d but should be %d\n",
69 test_ctz(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
73 for (n = 0; n < 32; n++) {
74 /* Check minimum x such that f(x) == n. */
77 /* Check maximum x such that f(x) == n. */
78 check_ctz(UINT32_MAX << n, n);
80 /* Check a random value in the middle. */
81 check_ctz((random_uint32() | 1) << n, n);
88 /* Returns the sum of the squares of the first 'n' positive integers. */
92 return n * (n + 1) * (2 * n + 1) / 6;
96 test_bitwise_copy(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
104 for (n_bits = 0; n_bits <= 64; n_bits++) {
105 for (src_ofs = 0; src_ofs < 64 - n_bits; src_ofs++) {
106 for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) {
107 ovs_be64 src = htonll(random_uint64());
108 ovs_be64 dst = htonll(random_uint64());
109 ovs_be64 orig_dst = dst;
115 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
116 expect = orig_dst & ~htonll(mask << dst_ofs);
117 expect |= htonll(((ntohll(src) >> src_ofs) & mask)
121 bitwise_copy(&src, sizeof src, src_ofs,
122 &dst, sizeof dst, dst_ofs,
125 fprintf(stderr,"copy_bits(0x%016"PRIx64",8,%d, "
126 "0x%016"PRIx64",8,%d, %d) yielded 0x%016"PRIx64" "
127 "instead of the expected 0x%016"PRIx64"\n",
128 ntohll(src), src_ofs,
129 ntohll(orig_dst), dst_ofs,
131 ntohll(dst), ntohll(expect));
140 if (n_loops != sum_of_squares(64)) {
146 test_bitwise_zero(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
148 unsigned int n_loops;
153 for (n_bits = 0; n_bits <= 64; n_bits++) {
154 for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) {
155 ovs_be64 dst = htonll(random_uint64());
156 ovs_be64 orig_dst = dst;
162 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
163 expect = orig_dst & ~htonll(mask << dst_ofs);
166 bitwise_zero(&dst, sizeof dst, dst_ofs, n_bits);
168 fprintf(stderr,"bitwise_zero(0x%016"PRIx64",8,%d, %d) "
169 "yielded 0x%016"PRIx64" "
170 "instead of the expected 0x%016"PRIx64"\n",
171 ntohll(orig_dst), dst_ofs,
173 ntohll(dst), ntohll(expect));
181 if (n_loops != 64 * (64 + 1) / 2) {
187 test_bitwise_one(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
189 unsigned int n_loops;
194 for (n_bits = 0; n_bits <= 64; n_bits++) {
195 for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) {
196 ovs_be64 dst = htonll(random_uint64());
197 ovs_be64 orig_dst = dst;
201 expect = htonll(UINT64_MAX);
203 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
204 expect = orig_dst | htonll(mask << dst_ofs);
207 bitwise_one(&dst, sizeof dst, dst_ofs, n_bits);
209 fprintf(stderr,"bitwise_one(0x%016"PRIx64",8,%d, %d) "
210 "yielded 0x%016"PRIx64" "
211 "instead of the expected 0x%016"PRIx64"\n",
212 ntohll(orig_dst), dst_ofs,
214 ntohll(dst), ntohll(expect));
222 if (n_loops != 64 * (64 + 1) / 2) {
228 test_bitwise_is_all_zeros(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
232 for (n_loops = 0; n_loops < 100; n_loops++) {
233 ovs_be64 x = htonll(0);
236 for (i = 0; i < 64; i++) {
240 /* Change a random 0-bit into a 1-bit. */
242 bit = htonll(UINT64_C(1) << (random_uint32() % 64));
246 for (ofs = 0; ofs < 64; ofs++) {
247 for (n = 0; n <= 64 - ofs; n++) {
253 : !(x & htonll(((UINT64_C(1) << n) - 1)
255 answer = bitwise_is_all_zeros(&x, sizeof x, ofs, n);
256 if (expect != answer) {
258 "bitwise_is_all_zeros(0x%016"PRIx64",8,%d,%d "
259 "returned %s instead of %s\n",
261 answer ? "true" : "false",
262 expect ? "true" : "false");
272 test_follow_symlinks(int argc, char *argv[])
276 for (i = 1; i < argc; i++) {
277 char *target = follow_symlinks(argv[i]);
283 static const struct command commands[] = {
284 {"ctz", 0, 0, test_ctz},
285 {"log_2_floor", 0, 0, test_log_2_floor},
286 {"bitwise_copy", 0, 0, test_bitwise_copy},
287 {"bitwise_zero", 0, 0, test_bitwise_zero},
288 {"bitwise_one", 0, 0, test_bitwise_one},
289 {"bitwise_is_all_zeros", 0, 0, test_bitwise_is_all_zeros},
290 {"follow-symlinks", 1, INT_MAX, test_follow_symlinks},
295 main(int argc, char *argv[])
297 set_program_name(argv[0]);
298 run_command(argc - 1, argv + 1, commands);