MySQL 8.4.0
Source Code Documentation
bit.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_STDX_BIT_H_
27#define MYSQL_HARNESS_STDX_BIT_H_
28
29#include <concepts> // integral
30#include <cstdint> // UINT64_C
31
32namespace stdx {
33
34// implementation 'byteswap()' from std c++23
35//
36// see:
37// - http://wg21.link/P1272
38
39// byteswap() functions translate into
40//
41// - `bswap` on x86 with
42// - clang -O1 since 3.5
43// - gcc -O2 since 5.1
44// - msvc /O1 for all but 8-byte. _byteswap_uint64() exists, but isn't
45// constexpr
46// - `rev` on armv6 and later with -O1
47// - clang
48// - gcc
49// - msvc /O1 for 4 byte
50
51template <std::integral T>
52constexpr T byteswap(T num) noexcept {
53 static_assert(std::has_unique_object_representations_v<decltype(num)>,
54 "T may not have padding bits");
55
56 if constexpr (sizeof(T) == 1) {
57 return num;
58#if defined(__GNUC__)
59 } else if constexpr (sizeof(T) == 2) {
60 return __builtin_bswap16(num);
61 } else if constexpr (sizeof(T) == 4) {
62 return __builtin_bswap32(num);
63 } else if constexpr (sizeof(T) == 8) {
64 return __builtin_bswap64(num);
65#endif
66 } else if constexpr (sizeof(T) == 2) {
67 return (num & UINT16_C(0x00ff)) << (1 * 8) |
68 (num & UINT16_C(0xff00)) >> (1 * 8);
69 } else if constexpr (sizeof(T) == 4) {
70 return (num & UINT32_C(0x0000'00ff)) << (3 * 8) |
71 (num & UINT32_C(0x0000'ff00)) << (1 * 8) |
72 (num & UINT32_C(0x00ff'0000)) >> (1 * 8) |
73 (num & UINT32_C(0xff00'0000)) >> (3 * 8);
74 } else if constexpr (sizeof(T) == 8) {
75 return (num & UINT64_C(0x0000'0000'0000'00ff)) << (7 * 8) |
76 (num & UINT64_C(0x0000'0000'0000'ff00)) << (5 * 8) |
77 (num & UINT64_C(0x0000'0000'00ff'0000)) << (3 * 8) |
78 (num & UINT64_C(0x0000'0000'ff00'0000)) << (1 * 8) |
79 (num & UINT64_C(0x0000'00ff'0000'0000)) >> (1 * 8) |
80 (num & UINT64_C(0x0000'ff00'0000'0000)) >> (3 * 8) |
81 (num & UINT64_C(0x00ff'0000'0000'0000)) >> (5 * 8) |
82 (num & UINT64_C(0xff00'0000'0000'0000)) >> (7 * 8);
83 } else {
84 static_assert(sizeof(num) == 0,
85 "byteswap not implemented for integral types of this size");
86 }
87}
88
89} // namespace stdx
90
91#endif
Definition: bit.h:32
constexpr T byteswap(T num) noexcept
Definition: bit.h:52