diff options
author | apatrick@google.com <apatrick@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-18 23:08:39 +0000 |
---|---|---|
committer | apatrick@google.com <apatrick@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-18 23:08:39 +0000 |
commit | 6d1729ee957d70b80195a701c0532ffb99e064ec (patch) | |
tree | 2dc6c71a9f366df537df700df9d205e9a87f3bc1 /base/bits.h | |
parent | 20d296ddc770d1bdb547bab4485ad7cb8c124085 (diff) | |
download | chromium_src-6d1729ee957d70b80195a701c0532ffb99e064ec.zip chromium_src-6d1729ee957d70b80195a701c0532ffb99e064ec.tar.gz chromium_src-6d1729ee957d70b80195a701c0532ffb99e064ec.tar.bz2 |
Moved bits.h from O3D to Chrome base.
TEST=none
BUG=none
Review URL: http://codereview.chromium.org/373001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@32428 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/bits.h')
-rw-r--r-- | base/bits.h | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/base/bits.h b/base/bits.h new file mode 100644 index 0000000..b2209e8 --- /dev/null +++ b/base/bits.h @@ -0,0 +1,47 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This file defines some bit utilities. + +#ifndef BASE_BITS_H_ +#define BASE_BITS_H_ + +#include "base/basictypes.h" +#include "base/logging.h" + +namespace base { +namespace bits { + +// Returns the integer i such as 2^i <= n < 2^(i+1) +inline int Log2Floor(uint32 n) { + if (n == 0) + return -1; + int log = 0; + uint32 value = n; + for (int i = 4; i >= 0; --i) { + int shift = (1 << i); + uint32 x = value >> shift; + if (x != 0) { + value = x; + log += shift; + } + } + DCHECK_EQ(value, 1u); + return log; +} + +// Returns the integer i such as 2^(i-1) < n <= 2^i +inline int Log2Ceiling(uint32 n) { + if (n == 0) { + return -1; + } else { + // Log2Floor returns -1 for 0, so the following works correctly for n=1. + return 1 + Log2Floor(n - 1); + } +} + +} // namespace bits +} // namespace base + +#endif // BASE_BITS_H_ |