fennec
Loading...
Searching...
No Matches
bits.h
Go to the documentation of this file.
1// =====================================================================================================================
2// fennec, a free and open source game engine
3// Copyright © 2025 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
30
31#ifndef FENNEC_LANG_BITS_H
32#define FENNEC_LANG_BITS_H
33
81
84#include <fennec/lang/detail/_bits.h>
85
86namespace fennec
87{
88
97template<typename ToT, typename FromT> requires(sizeof(ToT) == sizeof(FromT))
98constexpr ToT bit_cast(const FromT& from) {
99 if constexpr(FENNEC_HAS_BUILTIN_BIT_CAST) {
100 return FENNEC_BUILTIN_BIT_CAST(ToT, from);
101 } else {
102 ToT to;
103 fennec::memcpy(&to, &from, sizeof(ToT));
104 return to;
105 }
106}
107
108
109
110
111
120constexpr void* bit_and(void* arr, const void* mask, size_t n) {
121 if (arr == mask) {
122 return arr;
123 }
124
125 uint8_t* d = static_cast<uint8_t*>(arr);
126 const uint8_t* s = static_cast<const uint8_t*>(mask);
127
128 while (n > 0) {
129 const size_t step = detail::_bit_and(d, s, n);
130 d += step; s += step; n -= step;
131 }
132
133 return arr;
134}
135
145constexpr void* bit_and_s(void* arr, size_t n0, const void* mask, size_t n1) {
146 return bit_and(arr, mask, n0 < n1 ? n0 : n1);
147}
148
149
150
151
152
161constexpr void* bit_or(void* arr, const void* mask, size_t n) {
162 if (arr == mask) {
163 return arr;
164 }
165
166 uint8_t* d = static_cast<uint8_t*>(arr);
167 const uint8_t* s = static_cast<const uint8_t*>(mask);
168 while (n > 0)
169 {
170 const size_t step = detail::_bit_or(d, s, n);
171 d += step; s += step; n -= step;
172 }
173
174 return arr;
175}
176
186constexpr void* bit_or_s(void* arr, size_t n0, const void* mask, size_t n1) {
187 return bit_or(arr, mask, n0 < n1 ? n0 : n1);
188}
189
190
191
192
193
202constexpr void* bit_xor(void* arr, const void* mask, size_t n) {
203 if (arr == mask) {
204 return arr;
205 }
206
207 uint8_t* d = static_cast<uint8_t*>(arr);
208 const uint8_t* s = static_cast<const uint8_t*>(mask);
209 while (n > 0) {
210 const size_t step = detail::_bit_xor(d, s, n);
211 d += step; s += step; n -= step;
212 }
213
214 return arr;
215}
216
226constexpr void* bit_xor_s(void* arr, size_t n0, const void* mask, size_t n1) {
227 return bit_xor(arr, mask, n0 < n1 ? n0 : n1);
228}
229
230}
231
232#endif // FENNEC_LANG_BITS_H
constexpr void * bit_xor(void *arr, const void *mask, size_t n)
Perform a bit-wise or over an array of bytes.
Definition bits.h:202
constexpr void * bit_and(void *arr, const void *mask, size_t n)
Perform a bit-wise and over an array of bytes.
Definition bits.h:120
constexpr void * bit_or(void *arr, const void *mask, size_t n)
Perform a bit-wise or over an array of bytes.
Definition bits.h:161
constexpr void * bit_or_s(void *arr, size_t n0, const void *mask, size_t n1)
Safe version of fennec::bit_or.
Definition bits.h:186
constexpr ToT bit_cast(const FromT &from)
Perform a bitcast of FromT to ToT.
Definition bits.h:98
constexpr void * bit_xor_s(void *arr, size_t n0, const void *mask, size_t n1)
Safe version of fennec::bit_xor.
Definition bits.h:226
constexpr void * bit_and_s(void *arr, size_t n0, const void *mask, size_t n1)
Safe version of fennec::bit_and.
Definition bits.h:145
Intrinsics
constexpr genType step(genType edge, genType x)
Returns if , otherwise, it returns .
Definition common.h:750
This header contains functions related to analyzing, modifying or copying buffers interpreted as byte...
::uint8_t uint8_t
Unsigned 8-bit integer.
Definition types.h:272