• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <cstdint>
17 #include "libpandabase/utils/utils.h"
18 
19 namespace panda {
20 
CountDigits(uint64_t v)21 uint32_t CountDigits(uint64_t v)
22 {
23     constexpr uint64_t POW10_1 = 10ULL;
24     constexpr uint64_t POW10_2 = 100ULL;
25     constexpr uint64_t POW10_3 = 1000ULL;
26     constexpr uint64_t POW10_4 = 10000ULL;
27     uint32_t count = 1U;
28     while (v >= POW10_1) {
29         if (v < POW10_2) {
30             return count + 1U;
31         }
32         if (v < POW10_3) {
33             return count + 2U;
34         }
35         if (v < POW10_4) {
36             return count + 3U;
37         }
38         count += 4U;
39         v /= POW10_4;
40     }
41     return count;
42 }
43 
44 }  // namespace panda
45