Grok  9.7.5
utils.hpp
Go to the documentation of this file.
1 // Copyright (c) 2019 - 2021, Osamu Watanabe
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include <cstdint>
32 #include <cstdlib>
33 
34 #define round_up(x, n) (((x) + (n)-1) & (-n))
35 #define round_down(x, n) ((x) & (-n))
36 #define ceil_int(a, b) ((a) + ((b)-1)) / (b)
37 
38 #if defined(__arm64__) || defined(__arm__) || defined(__aarch64__)
39  #include <arm_acle.h>
40  #if defined(__ARM_NEON__)
41  #include <arm_neon.h>
42  #endif
43 #elif defined(_MSC_VER) || defined(__MINGW64__)
44  #include <intrin.h>
45 #else
46  #include <x86intrin.h>
47 #endif
48 
49 static inline size_t popcount32(uintmax_t num) {
50  size_t precision = 0;
51 #if defined(_MSC_VER)
52  precision = __popcnt(static_cast<uint32_t>(num));
53 #elif defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
54  precision = _popcnt32(num);
55 #else
56  while (num != 0) {
57  if (1 == (num & 1)) {
58  precision++;
59  }
60  num >>= 1;
61  }
62 #endif
63  return precision;
64 }
65 
66 static inline uint32_t int_log2(const uint32_t x) {
67  uint32_t y;
68 #if defined(_MSC_VER)
69  unsigned long tmp;
70  _BitScanReverse(&tmp, x);
71  y = tmp;
72 #else
73  y = 31 - __builtin_clz(x);
74 #endif
75  return (x == 0) ? 0 : y;
76 }
77 
78 static inline uint32_t count_leading_zeros(const uint32_t x) {
79  uint32_t y;
80 #if defined(_MSC_VER)
81  y = __lzcnt(x);
82 #elif defined(__AVX2__)
83  y = _lzcnt_u32(x);
84 #elif defined(__MINGW32__) || defined(__MINGW64__)
85  y = __builtin_clz(x);
86 #elif defined(__ARM_FEATURE_CLZ)
87  y = __builtin_clz(x);
88 #else
89  y = 31 - int_log2(x);
90 #endif
91  return (x == 0) ? 31 : y;
92 }
static uint32_t int_log2(const uint32_t x)
Definition: utils.hpp:66
static uint32_t count_leading_zeros(const uint32_t x)
Definition: utils.hpp:78
static size_t popcount32(uintmax_t num)
Definition: utils.hpp:49