1*103e46e4SHarish Mahendrakar // Copyright (c) 2016 The WebM project authors. All Rights Reserved. 2*103e46e4SHarish Mahendrakar // 3*103e46e4SHarish Mahendrakar // Use of this source code is governed by a BSD-style license 4*103e46e4SHarish Mahendrakar // that can be found in the LICENSE file in the root of the source 5*103e46e4SHarish Mahendrakar // tree. An additional intellectual property rights grant can be found 6*103e46e4SHarish Mahendrakar // in the file PATENTS. All contributing project authors may 7*103e46e4SHarish Mahendrakar // be found in the AUTHORS file in the root of the source tree. 8*103e46e4SHarish Mahendrakar #include "src/bit_utils.h" 9*103e46e4SHarish Mahendrakar 10*103e46e4SHarish Mahendrakar #include <cstdint> 11*103e46e4SHarish Mahendrakar 12*103e46e4SHarish Mahendrakar namespace webm { 13*103e46e4SHarish Mahendrakar CountLeadingZeros(std::uint8_t value)14*103e46e4SHarish Mahendrakarstd::uint8_t CountLeadingZeros(std::uint8_t value) { 15*103e46e4SHarish Mahendrakar // Special case for 0 since we can't shift by sizeof(T) * 8 bytes. 16*103e46e4SHarish Mahendrakar if (value == 0) 17*103e46e4SHarish Mahendrakar return 8; 18*103e46e4SHarish Mahendrakar 19*103e46e4SHarish Mahendrakar std::uint8_t count = 0; 20*103e46e4SHarish Mahendrakar while (!(value & (0x80 >> count))) { 21*103e46e4SHarish Mahendrakar ++count; 22*103e46e4SHarish Mahendrakar } 23*103e46e4SHarish Mahendrakar return count; 24*103e46e4SHarish Mahendrakar } 25*103e46e4SHarish Mahendrakar 26*103e46e4SHarish Mahendrakar } // namespace webm 27