MySQL 9.7.0
Source Code Documentation
bit.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2026, 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#include <type_traits> // std::has_unique_object_representations_v
32
33namespace stdx {
34
35// implementation 'byteswap()' from std c++23
36//
37// see:
38// - http://wg21.link/P1272
39
40// byteswap() functions translate into
41//
42// - `bswap` on x86 with
43// - clang -O1 since 3.5
44// - gcc -O2 since 5.1
45// - msvc /O1 for all but 8-byte. _byteswap_uint64() exists, but isn't
46// constexpr
47// - `rev` on armv6 and later with -O1
48// - clang
49// - gcc
50// - msvc /O1 for 4 byte
51
52template <std::integral T>
53constexpr T byteswap(T num) noexcept {
54 static_assert(std::has_unique_object_representations_v<decltype(num)>,
55 "T may not have padding bits");
56
57 if constexpr (sizeof(T) == 1) {
58 return num;
59#if defined(__GNUC__)
60 } else if constexpr (sizeof(T) == 2) {
61 return __builtin_bswap16(num);
62 } else if constexpr (sizeof(T) == 4) {
63 return __builtin_bswap32(num);
64 } else if constexpr (sizeof(T) == 8) {
65 return __builtin_bswap64(num);
66#endif
67 } else if constexpr (sizeof(T) == 2) {
68 return (num & UINT16_C(0x00ff)) << (1 * 8) |
69 (num & UINT16_C(0xff00)) >> (1 * 8);
70 } else if constexpr (sizeof(T) == 4) {
71 return (num & UINT32_C(0x0000'00ff)) << (3 * 8) |
72 (num & UINT32_C(0x0000'ff00)) << (1 * 8) |
73 (num & UINT32_C(0x00ff'0000)) >> (1 * 8) |
74 (num & UINT32_C(0xff00'0000)) >> (3 * 8);
75 } else if constexpr (sizeof(T) == 8) {
76 return (num & UINT64_C(0x0000'0000'0000'00ff)) << (7 * 8) |
77 (num & UINT64_C(0x0000'0000'0000'ff00)) << (5 * 8) |
78 (num & UINT64_C(0x0000'0000'00ff'0000)) << (3 * 8) |
79 (num & UINT64_C(0x0000'0000'ff00'0000)) << (1 * 8) |
80 (num & UINT64_C(0x0000'00ff'0000'0000)) >> (1 * 8) |
81 (num & UINT64_C(0x0000'ff00'0000'0000)) >> (3 * 8) |
82 (num & UINT64_C(0x00ff'0000'0000'0000)) >> (5 * 8) |
83 (num & UINT64_C(0xff00'0000'0000'0000)) >> (7 * 8);
84 } else {
85 static_assert(sizeof(num) == 0,
86 "byteswap not implemented for integral types of this size");
87 }
88}
89
90} // namespace stdx
91
92#endif
#define T
Definition: jit_executor_value.cc:373
Definition: bit.h:33
constexpr T byteswap(T num) noexcept
Definition: bit.h:53