14#ifndef WPIUTIL_WPI_BIT_H
15#define WPIUTIL_WPI_BIT_H
22#if !__has_builtin(__builtin_bit_cast)
26#if defined(_MSC_VER) && !defined(_DEBUG)
36 typename To,
typename 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>>
42#if __has_builtin(__builtin_bit_cast)
43 return __builtin_bit_cast(To,
from);
46 std::memcpy(&to, &
from,
sizeof(To));
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) {
56 }
else if constexpr (
sizeof(T) == 2) {
58#if defined(_MSC_VER) && !defined(_DEBUG)
61 return _byteswap_ushort(UV);
63 uint16_t Hi = UV << 8;
64 uint16_t Lo = UV >> 8;
67 }
else if constexpr (
sizeof(T) == 4) {
69#if __has_builtin(__builtin_bswap32)
70 return __builtin_bswap32(UV);
71#elif defined(_MSC_VER) && !defined(_DEBUG)
72 return _byteswap_ulong(UV);
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);
80 }
else if constexpr (
sizeof(T) == 8) {
82#if __has_builtin(__builtin_bswap64)
83 return __builtin_bswap64(UV);
84#elif defined(_MSC_VER) && !defined(_DEBUG)
85 return _byteswap_uint64(UV);
87 uint64_t Hi = wpi::byteswap<uint32_t>(UV);
88 uint32_t Lo = wpi::byteswap<uint32_t>(UV >> 32);
89 return (Hi << 32) | Lo;
92 static_assert(!
sizeof(T *),
"Don't know how to handle the given type.");
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