WPILibC++ 2024.3.2
bit.h
Go to the documentation of this file.
1//===-- llvm/ADT/bit.h - C++20 <bit> ----------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements the C++20 <bit> header.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef WPIUTIL_WPI_BIT_H
15#define WPIUTIL_WPI_BIT_H
16
17#include "wpi/Compiler.h"
18#include <cstdint>
19#include <limits>
20#include <type_traits>
21
22#if !__has_builtin(__builtin_bit_cast)
23#include <cstring>
24#endif
25
26#if defined(_MSC_VER) && !defined(_DEBUG)
27#include <cstdlib> // for _byteswap_{ushort,ulong,uint64}
28#endif
29
30namespace wpi {
31
32// This implementation of bit_cast is different from the C++20 one in two ways:
33// - It isn't constexpr because that requires compiler support.
34// - It requires trivially-constructible To, to avoid UB in the implementation.
35template <
36 typename To, typename From,
37 typename = std::enable_if_t<sizeof(To) == sizeof(From)>,
38 typename = std::enable_if_t<std::is_trivially_constructible<To>::value>,
39 typename = std::enable_if_t<std::is_trivially_copyable<To>::value>,
40 typename = std::enable_if_t<std::is_trivially_copyable<From>::value>>
41[[nodiscard]] inline To bit_cast(const From &from) noexcept {
42#if __has_builtin(__builtin_bit_cast)
43 return __builtin_bit_cast(To, from);
44#else
45 To to;
46 std::memcpy(&to, &from, sizeof(To));
47 return to;
48#endif
49}
50
51/// Reverses the bytes in the given integer value V.
52template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
53[[nodiscard]] constexpr T byteswap(T V) noexcept {
54 if constexpr (sizeof(T) == 1) {
55 return V;
56 } else if constexpr (sizeof(T) == 2) {
57 uint16_t UV = V;
58#if defined(_MSC_VER) && !defined(_DEBUG)
59 // The DLL version of the runtime lacks these functions (bug!?), but in a
60 // release build they're replaced with BSWAP instructions anyway.
61 return _byteswap_ushort(UV);
62#else
63 uint16_t Hi = UV << 8;
64 uint16_t Lo = UV >> 8;
65 return Hi | Lo;
66#endif
67 } else if constexpr (sizeof(T) == 4) {
68 uint32_t UV = V;
69#if __has_builtin(__builtin_bswap32)
70 return __builtin_bswap32(UV);
71#elif defined(_MSC_VER) && !defined(_DEBUG)
72 return _byteswap_ulong(UV);
73#else
74 uint32_t Byte0 = UV & 0x000000FF;
75 uint32_t Byte1 = UV & 0x0000FF00;
76 uint32_t Byte2 = UV & 0x00FF0000;
77 uint32_t Byte3 = UV & 0xFF000000;
78 return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
79#endif
80 } else if constexpr (sizeof(T) == 8) {
81 uint64_t UV = V;
82#if __has_builtin(__builtin_bswap64)
83 return __builtin_bswap64(UV);
84#elif defined(_MSC_VER) && !defined(_DEBUG)
85 return _byteswap_uint64(UV);
86#else
87 uint64_t Hi = wpi::byteswap<uint32_t>(UV);
88 uint32_t Lo = wpi::byteswap<uint32_t>(UV >> 32);
89 return (Hi << 32) | Lo;
90#endif
91 } else {
92 static_assert(!sizeof(T *), "Don't know how to handle the given type.");
93 return 0;
94 }
95}
96
97} // namespace wpi
98
99#endif
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable from
Definition: ThirdPartyNotices.txt:128
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:256
Definition: ntcore_cpp.h:26
constexpr T byteswap(T V) noexcept
Reverses the bytes in the given integer value V.
Definition: bit.h:53
To bit_cast(const From &from) noexcept
Definition: bit.h:41