Ruby 4.1.0dev (2026-10-20 revision 957a081892437c842a94cfd9443b0bdc569d1ef2)
bswap.h
Go to the documentation of this file.
1
4#ifndef PRISM_COMPILER_BSWAP_H
5#define PRISM_COMPILER_BSWAP_H
6
10#if defined(__GNUC__) || defined(__clang__)
11#define pm_bswap32(x_) __builtin_bswap32((x_))
12#define pm_bswap64(x_) __builtin_bswap64((x_))
13#elif defined(_MSC_VER)
14#include <stdlib.h>
15
16#define pm_bswap32(x_) _byteswap_ulong((x_))
17#define pm_bswap64(x_) _byteswap_uint64((x_))
18#else
19#include <stdint.h>
20
21static uint32_t
22pm_bswap32(uint32_t x) {
23 return (
24 ((x >> 24) & 0x000000FFUL) |
25 ((x << 24) & 0xFF000000UL) |
26 ((x >> 8) & 0x0000FF00UL) |
27 ((x << 8) & 0x00FF0000UL)
28 );
29}
30
31static uint64_t
32pm_bswap64(uint64_t x) {
33 return (
34 ((x >> 56) & 0x00000000000000FFULL) |
35 ((x << 56) & 0xFF00000000000000ULL) |
36 ((x >> 40) & 0x000000000000FF00ULL) |
37 ((x << 40) & 0x00FF000000000000ULL) |
38 ((x >> 24) & 0x0000000000FF0000ULL) |
39 ((x << 24) & 0x0000FF0000000000ULL) |
40 ((x >> 8) & 0x00000000FF000000ULL) |
41 ((x << 8) & 0x000000FF00000000ULL)
42 );
43}
44#endif
45
46#endif
#define pm_bswap32(x_)
Byte swap utilities for 32-bit and 64-bit integers.
Definition bswap.h:11