Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
integer.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
21#include <type_traits>
22
23namespace shambase {
24
35 template<class T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
36 inline constexpr bool is_pow_of_two_fast(T v) noexcept {
37 return (v & (v - 1)) == 0;
38 }
39
48 template<class T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
49 inline constexpr bool is_pow_of_two(T v) noexcept {
50 return v && !(v & (v - 1));
51 }
52
63 template<class T, std::enable_if_t<std::is_integral_v<T> || std::is_signed_v<T>, int> = 0>
64 inline constexpr bool sign_differ(T a, T b) noexcept {
65 return ((a ^ b) < 0);
66 }
67
76 template<class T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
77 inline void xor_swap(T &a, T &b) {
78 a ^= b;
79 b ^= a;
80 a ^= b;
81 };
82
91 template<class T, std::enable_if_t<std::is_integral_v<T> || (!std::is_signed_v<T>), int> = 0>
92 inline constexpr T roundup_pow2(T v) noexcept {
93
94 if constexpr (has_bitlen_v<T, 32>) {
95 v--;
96 v |= v >> 1;
97 v |= v >> 2;
98 v |= v >> 4;
99 v |= v >> 8;
100 v |= v >> 16;
101 v++;
102 return v;
103 }
104
105 if constexpr (has_bitlen_v<T, 64>) {
106 v--;
107 v |= v >> 1;
108 v |= v >> 2;
109 v |= v >> 4;
110 v |= v >> 8;
111 v |= v >> 16;
112 v |= v >> 32;
113 v++;
114 return v;
115 }
116 };
117
125 inline constexpr u32 group_count(u32 len, u32 group_size) {
126 return (len + group_size - 1) / group_size;
127 }
128
137 template<class T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
138 inline T select_bit(T value, T bitnum) {
139 return (value >> bitnum) & 1;
140 }
141
148 template<class T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
149 inline constexpr T most_sig_bit_mask() noexcept {
150 return T(std::make_unsigned_t<T>(1) << (sizeof(T) * 8 - 1));
151 }
152
160 template<class T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
161 inline constexpr bool is_most_sig_bit_set(const T x) noexcept {
162 return (x & most_sig_bit_mask<T>());
163 }
164
173 template<i32 power, class T>
174 inline constexpr T pow_constexpr(T a) noexcept {
175
176 if constexpr (power == 0) {
177 return T{1};
178 } else if constexpr (power % 2 == 0) {
179 T tmp = pow_constexpr<power / 2>(a);
180 return tmp * tmp;
181 } else if constexpr (power % 2 == 1) {
182 T tmp = pow_constexpr<(power - 1) / 2>(a);
183 return tmp * tmp * a;
184 }
185 }
186
187 template<u32 flag>
188 inline constexpr bool is_flag_on(u32 val) {
189 return (val & (u32) flag) == (u32) flag;
190 }
191
192} // namespace shambase
std::uint32_t u32
32 bit unsigned integer
namespace for basic c++ utilities
constexpr u32 group_count(u32 len, u32 group_size)
Calculates the number of groups based on the length and group size.
Definition integer.hpp:125
T select_bit(T value, T bitnum)
Selects and returns the bit at a specific position in the given value.
Definition integer.hpp:138
constexpr bool is_pow_of_two(T v) noexcept
determine if v is a power of two and check if v==0 Source : https://graphics.stanford....
Definition integer.hpp:49
constexpr T pow_constexpr(T a) noexcept
Calculates the power of a number at compile time.
Definition integer.hpp:174
constexpr bool sign_differ(T a, T b) noexcept
check if the sign of the two integers differs Source : https://graphics.stanford.edu/~seander/bithack...
Definition integer.hpp:64
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
constexpr T most_sig_bit_mask() noexcept
Generates a mask with only the most significant bit set.
Definition integer.hpp:149
constexpr T roundup_pow2(T v) noexcept
round up to the next power of two Source : https://graphics.stanford.edu/~seander/bithacks....
Definition integer.hpp:92
void xor_swap(T &a, T &b)
swap two values using xor Source : https://graphics.stanford.edu/~seander/bithacks....
Definition integer.hpp:77
constexpr bool is_pow_of_two_fast(T v) noexcept
determine if v is a power of two Warning : this function return true if v == 0 Source : https://graph...
Definition integer.hpp:36
constexpr bool is_most_sig_bit_set(const T x) noexcept
Checks if the most significant bit is set in the given value.
Definition integer.hpp:161
Traits for C++ types.