Path: blob/master/test/hotspot/gtest/utilities/test_count_leading_zeros.cpp
41144 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "metaprogramming/isSigned.hpp"26#include "utilities/count_leading_zeros.hpp"27#include "utilities/globalDefinitions.hpp"28#include "unittest.hpp"2930#include <limits>3132template <typename T> void one_or_two_set_bits() {33uint32_t bit1_pos = 0;34uint32_t bits = sizeof(T) * BitsPerByte;35uint32_t limit = bits - (IsSigned<T>::value ? 1 : 0);36for (uint64_t ix = 1; bit1_pos < limit; ix = ix * 2, ++bit1_pos) {37uint32_t bit2_pos = 0;38for (uint64_t jx = 1; bit2_pos < limit; jx = jx * 2, ++bit2_pos) {39T value = T(ix | jx);40EXPECT_EQ((uint32_t)(bits - 1u - MAX2(bit1_pos, bit2_pos)), count_leading_zeros(value))41<< "value = " << value;42}43}44}4546TEST(count_leading_zeros, one_or_two_set_bits) {47one_or_two_set_bits<int8_t>();48one_or_two_set_bits<int16_t>();49one_or_two_set_bits<int32_t>();50one_or_two_set_bits<int64_t>();51one_or_two_set_bits<uint8_t>();52one_or_two_set_bits<uint16_t>();53one_or_two_set_bits<uint32_t>();54one_or_two_set_bits<uint64_t>();55}5657template <typename T> void high_zeros_low_ones() {58uint32_t number_of_leading_zeros = (IsSigned<T>::value ? 1 : 0);59T value = std::numeric_limits<T>::max();60for ( ; value != 0; value >>= 1, ++number_of_leading_zeros) {61EXPECT_EQ(number_of_leading_zeros, count_leading_zeros(value))62<< "value = " << value;63}64}6566TEST(count_leading_zeros, high_zeros_low_ones) {67high_zeros_low_ones<int8_t>();68high_zeros_low_ones<int16_t>();69high_zeros_low_ones<int32_t>();70high_zeros_low_ones<int64_t>();71high_zeros_low_ones<uint8_t>();72high_zeros_low_ones<uint16_t>();73high_zeros_low_ones<uint32_t>();74high_zeros_low_ones<uint64_t>();75}7677template <typename T> void high_ones_low_zeros() {78T value = std::numeric_limits<T>::max();7980uint32_t number_of_leading_zeros = (IsSigned<T>::value ? 1 : 0);81for (uint64_t i = 1; value != 0; value -= i, i <<= 1) {82EXPECT_EQ(number_of_leading_zeros, count_leading_zeros(value))83<< "value = " << value;84}85value = (T)(~((uint64_t)0)); // all ones86EXPECT_EQ(0u, count_leading_zeros(value))87<< "value = " << value;88}8990TEST(count_leading_zeros, high_ones_low_zeros) {91high_ones_low_zeros<int8_t>();92high_ones_low_zeros<int16_t>();93high_ones_low_zeros<int32_t>();94high_ones_low_zeros<int64_t>();95high_ones_low_zeros<uint8_t>();96high_ones_low_zeros<uint16_t>();97high_ones_low_zeros<uint32_t>();98high_ones_low_zeros<uint64_t>();99}100101