JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
Bits.h
Go to the documentation of this file.
1//
2// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
3// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPLSpatial **
4// ██║██████╔╝ ██║ ██║██████╔╝███████╗
5// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatial
6// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
7// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
8//
9// Copyright 2024 Jaroslav Pevno, JPLSpatial is offered under the terms of the ISC license:
10//
11// Permission to use, copy, modify, and/or distribute this software for any purpose with or
12// without fee is hereby granted, provided that the above copyright notice and this permission
13// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
14// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
16// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#pragma once
21
22#include <algorithm>
23#include <concepts>
24#include <bit>
25#include <limits>
26#include <climits>
27
28namespace JPL
29{
30 template<std::integral T>
31 [[nodiscard]] constexpr T Byteswap(T value) noexcept
32 {
33#if defined(__cpp_lib_byteswap) && __cpp_lib_byteswap >= 202110L
34 return std::byteswap(value);
35#else
36 static_assert(std::has_unique_object_representations_v<T>, "T may not have padding bits");
37 auto value_representation = std::bit_cast<std::array<std::byte, sizeof(T)>>(value);
38 std::ranges::reverse(value_representation);
39 return std::bit_cast<T>(value_representation);
40#endif
41 }
42
43 template <class T> requires (std::unsigned_integral<T>)
44 [[nodiscard]] constexpr int MSBIndex(T n) noexcept
45 {
46 if (n == T(0))
47 return -1;
48 return std::numeric_limits<T>::digits - std::countl_zero(n) - 1;
49 }
50
51 template<std::integral T>
52 [[nodiscard]] constexpr T RoundUpBy4(T n) noexcept
53 {
54 return (n + T(3)) & ~T(3);
55 }
56
57 template<std::integral T>
58 [[nodiscard]] constexpr std::size_t BitWidthOf() noexcept { return sizeof(T) * CHAR_BIT; }
59
60} // namespace JPL
Definition AcousticMaterial.h:36
constexpr int MSBIndex(T n) noexcept
Definition Bits.h:44
constexpr T Byteswap(T value) noexcept
Definition Bits.h:31
constexpr T RoundUpBy4(T n) noexcept
Definition Bits.h:52
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813
constexpr std::size_t BitWidthOf() noexcept
Definition Bits.h:58