Grok 10.0.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#ifndef _MSC_VER
35#pragma GCC diagnostic push
36#pragma GCC diagnostic ignored "-Wconversion"
37#pragma GCC diagnostic ignored "-Wsign-conversion"
38#pragma GCC diagnostic ignored "-Wunused-parameter"
39#pragma GCC diagnostic ignored "-Wunused-variable"
40#pragma GCC diagnostic ignored "-Wsign-compare"
41#pragma GCC diagnostic ignored "-Wparentheses"
42#endif
43
44#define round_up(x, n) (((x) + (n)-1) & (-n))
45#define round_down(x, n) ((x) & (-n))
46#define ceil_int(a, b) ((a) + ((b)-1)) / (b)
47
48#if defined(__arm64__) || defined(__arm__) || defined(__aarch64__)
49 #include <arm_acle.h>
50 #if defined(__ARM_NEON__)
51 #include <arm_neon.h>
52 #endif
53#elif defined(_MSC_VER) || defined(__MINGW64__)
54 #include <intrin.h>
55#else
56 #include <x86intrin.h>
57#endif
58
59static inline size_t popcount32(uintmax_t num) {
60 size_t precision = 0;
61#if defined(_MSC_VER)
62 precision = __popcnt(static_cast<uint32_t>(num));
63#elif defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
64 precision = _popcnt32(num);
65#else
66 while (num != 0) {
67 if (1 == (num & 1)) {
68 precision++;
69 }
70 num >>= 1;
71 }
72#endif
73 return precision;
74}
75
76static inline uint32_t int_log2(const uint32_t x) {
77 uint32_t y;
78#if defined(_MSC_VER)
79 unsigned long tmp;
80 _BitScanReverse(&tmp, x);
81 y = tmp;
82#else
83 y = 31 - __builtin_clz(x);
84#endif
85 return (x == 0) ? 0 : y;
86}
87
88static inline uint32_t count_leading_zeros(const uint32_t x) {
89 uint32_t y;
90#if defined(_MSC_VER)
91 y = __lzcnt(x);
92#elif defined(__AVX2__)
93 y = _lzcnt_u32(x);
94#elif defined(__MINGW32__) || defined(__MINGW64__)
95 y = __builtin_clz(x);
96#elif defined(__ARM_FEATURE_CLZ)
97 y = __builtin_clz(x);
98#else
99 y = 31 - int_log2(x);
100#endif
101 return (x == 0) ? 31 : y;
102}
103
104#ifndef _MSC_VER
105#pragma GCC diagnostic pop
106#endif
uint32_t y
Definition BlockExec.h:39
uint32_t x
Definition BlockExec.h:38
static uint32_t int_log2(const uint32_t x)
Definition utils.hpp:76
static uint32_t count_leading_zeros(const uint32_t x)
Definition utils.hpp:88
static size_t popcount32(uintmax_t num)
Definition utils.hpp:59