bits_test.cc
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/base/internal/bits.h"
00016 
00017 #include "gtest/gtest.h"
00018 
00019 namespace {
00020 
00021 int CLZ64(uint64_t n) {
00022   int fast = absl::base_internal::CountLeadingZeros64(n);
00023   int slow = absl::base_internal::CountLeadingZeros64Slow(n);
00024   EXPECT_EQ(fast, slow) << n;
00025   return fast;
00026 }
00027 
00028 TEST(BitsTest, CountLeadingZeros64) {
00029   EXPECT_EQ(64, CLZ64(uint64_t{}));
00030   EXPECT_EQ(0, CLZ64(~uint64_t{}));
00031 
00032   for (int index = 0; index < 64; index++) {
00033     uint64_t x = static_cast<uint64_t>(1) << index;
00034     const auto cnt = 63 - index;
00035     ASSERT_EQ(cnt, CLZ64(x)) << index;
00036     ASSERT_EQ(cnt, CLZ64(x + x - 1)) << index;
00037   }
00038 }
00039 
00040 int CLZ32(uint32_t n) {
00041   int fast = absl::base_internal::CountLeadingZeros32(n);
00042   int slow = absl::base_internal::CountLeadingZeros32Slow(n);
00043   EXPECT_EQ(fast, slow) << n;
00044   return fast;
00045 }
00046 
00047 TEST(BitsTest, CountLeadingZeros32) {
00048   EXPECT_EQ(32, CLZ32(uint32_t{}));
00049   EXPECT_EQ(0, CLZ32(~uint32_t{}));
00050 
00051   for (int index = 0; index < 32; index++) {
00052     uint32_t x = static_cast<uint32_t>(1) << index;
00053     const auto cnt = 31 - index;
00054     ASSERT_EQ(cnt, CLZ32(x)) << index;
00055     ASSERT_EQ(cnt, CLZ32(x + x - 1)) << index;
00056     ASSERT_EQ(CLZ64(x), CLZ32(x) + 32);
00057   }
00058 }
00059 
00060 int CTZ64(uint64_t n) {
00061   int fast = absl::base_internal::CountTrailingZerosNonZero64(n);
00062   int slow = absl::base_internal::CountTrailingZerosNonZero64Slow(n);
00063   EXPECT_EQ(fast, slow) << n;
00064   return fast;
00065 }
00066 
00067 TEST(BitsTest, CountTrailingZerosNonZero64) {
00068   EXPECT_EQ(0, CTZ64(~uint64_t{}));
00069 
00070   for (int index = 0; index < 64; index++) {
00071     uint64_t x = static_cast<uint64_t>(1) << index;
00072     const auto cnt = index;
00073     ASSERT_EQ(cnt, CTZ64(x)) << index;
00074     ASSERT_EQ(cnt, CTZ64(~(x - 1))) << index;
00075   }
00076 }
00077 
00078 int CTZ32(uint32_t n) {
00079   int fast = absl::base_internal::CountTrailingZerosNonZero32(n);
00080   int slow = absl::base_internal::CountTrailingZerosNonZero32Slow(n);
00081   EXPECT_EQ(fast, slow) << n;
00082   return fast;
00083 }
00084 
00085 TEST(BitsTest, CountTrailingZerosNonZero32) {
00086   EXPECT_EQ(0, CTZ32(~uint32_t{}));
00087 
00088   for (int index = 0; index < 32; index++) {
00089     uint32_t x = static_cast<uint32_t>(1) << index;
00090     const auto cnt = index;
00091     ASSERT_EQ(cnt, CTZ32(x)) << index;
00092     ASSERT_EQ(cnt, CTZ32(~(x - 1))) << index;
00093   }
00094 }
00095 
00096 
00097 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14