xref: /aosp_15_r20/external/libaom/test/log2_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2018, Alliance for Open Media. All rights reserved.
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <limits.h>
13 #include <math.h>
14 
15 #include "aom_ports/bitops.h"
16 #include "av1/common/entropymode.h"
17 #include "gtest/gtest.h"
18 
TEST(Log2Test,GetMsb)19 TEST(Log2Test, GetMsb) {
20   // Test small numbers exhaustively.
21   for (unsigned int n = 1; n < 10000; n++) {
22     EXPECT_EQ(get_msb(n), static_cast<int>(floor(log2(n))));
23   }
24 
25   // Test every power of 2 and the two adjacent numbers.
26   for (int exponent = 2; exponent < 32; exponent++) {
27     const unsigned int power_of_2 = 1U << exponent;
28     EXPECT_EQ(get_msb(power_of_2 - 1), exponent - 1);
29     EXPECT_EQ(get_msb(power_of_2), exponent);
30     EXPECT_EQ(get_msb(power_of_2 + 1), exponent);
31   }
32 }
33 
TEST(Log2Test,Av1CeilLog2)34 TEST(Log2Test, Av1CeilLog2) {
35   // Test small numbers exhaustively.
36   EXPECT_EQ(av1_ceil_log2(0), 0);
37   for (int n = 1; n < 10000; n++) {
38     EXPECT_EQ(av1_ceil_log2(n), static_cast<int>(ceil(log2(n))));
39   }
40 
41   // Test every power of 2 and the two adjacent numbers.
42   for (int exponent = 2; exponent < 31; exponent++) {
43     const int power_of_2 = 1 << exponent;
44     EXPECT_EQ(av1_ceil_log2(power_of_2 - 1), exponent);
45     EXPECT_EQ(av1_ceil_log2(power_of_2), exponent);
46     EXPECT_EQ(av1_ceil_log2(power_of_2 + 1), exponent + 1);
47   }
48 
49   // INT_MAX = 2^31 - 1
50   EXPECT_EQ(av1_ceil_log2(INT_MAX), 31);
51 }
52