Project
Loading...
Searching...
No Matches
Endian.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#ifndef O2_FRAMEWORK_ENDIAN_H_
13#define O2_FRAMEWORK_ENDIAN_H_
14
15#include <cstdint>
16#include <concepts>
17#include <cstring>
18// Lookup file for __BYTE_ORDER
19#ifdef __APPLE__
20#include <machine/endian.h>
21#define swap16_ ntohs
22#define swap32_ ntohl
23#define swap64_ ntohll
24#else
25#include <endian.h>
26#define swap16_ be16toh
27#define swap32_ be32toh
28#define ntohll be64toh
29#define htonll htobe64
30#define swap64_ ntohll
31#endif
32#define O2_HOST_BYTE_ORDER __BYTE_ORDER
33#define O2_BIG_ENDIAN __BIG_ENDIAN
34#define O2_LITTLE_ENDIAN __LITTLE_ENDIAN
35
36inline uint16_t doSwap(std::same_as<uint16_t> auto x)
37{
38 return swap16_(x);
39}
40
41inline uint32_t doSwap(std::same_as<uint32_t> auto x)
42{
43 return swap32_(x);
44}
45
46inline uint64_t doSwap(std::same_as<uint64_t> auto x)
47{
48 return swap64_(x);
49}
50
51template <typename T>
52inline void doSwapCopy_(void* dest, void* source, int size) noexcept
53{
54 auto tdest = static_cast<T*>(dest);
55 auto tsrc = static_cast<T*>(source);
56 for (auto i = 0; i < size; ++i) {
57 tdest[i] = doSwap<T>(tsrc[i]);
58 }
59}
60
61inline void swapCopy(unsigned char* dest, char* source, int size, int typeSize) noexcept
62{
63 switch (typeSize) {
64 case 1:
65 return (void)std::memcpy(dest, source, size);
66 case 2:
67 return doSwapCopy_<uint16_t>(dest, source, size);
68 case 4:
69 return doSwapCopy_<uint32_t>(dest, source, size);
70 case 8:
71 return doSwapCopy_<uint64_t>(dest, source, size);
72 }
73}
74#endif // O2_FRAMEWORK_ENDIAN_H_
#define swap64_
Definition Endian.h:30
#define swap32_
Definition Endian.h:27
#define swap16_
Definition Endian.h:26
void doSwapCopy_(void *dest, void *source, int size) noexcept
Definition Endian.h:52
void swapCopy(unsigned char *dest, char *source, int size, int typeSize) noexcept
Definition Endian.h:61
uint16_t doSwap(std::same_as< uint16_t > auto x)
Definition Endian.h:36
int32_t i
GLint GLenum GLint x
Definition glcorearb.h:403
GLsizeiptr size
Definition glcorearb.h:659
GLsizei GLsizei GLchar * source
Definition glcorearb.h:798