nndeploy C++ API  0.2.0
nndeploy C++ API
endian.h
Go to the documentation of this file.
1 
5 #ifndef _NNDEPLOY_BASE_ENDIAN_H_
6 #define _NNDEPLOY_BASE_ENDIAN_H_
7 
8 #ifdef NNDEPLOY_CMAKE_LITTLE_ENDIAN
9 // If compiled with CMake, use CMake's endian detection logic
10 #define NNDEPLOY_LITTLE_ENDIAN NNDEPLOY_CMAKE_LITTLE_ENDIAN
11 #else
12 #if defined(__APPLE__) || defined(_WIN32)
13 #define NNDEPLOY_LITTLE_ENDIAN 1
14 #elif defined(__GLIBC__) || defined(__GNU_LIBRARY__) || \
15  defined(__ANDROID__) || defined(__RISCV__)
16 #include <endian.h>
17 #define NNDEPLOY_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
18 #elif defined(__FreeBSD__) || defined(__OpenBSD__)
19 #include <sys/endian.h>
20 #define NNDEPLOY_LITTLE_ENDIAN (_BYTE_ORDER == _LITTLE_ENDIAN)
21 #elif defined(__QNX__)
22 #include <sys/param.h>
23 #define NNDEPLOY_LITTLE_ENDIAN (BYTE_ORDER == LITTLE_ENDIAN)
24 #elif defined(__EMSCRIPTEN__) || defined(__hexagon__)
25 #define NNDEPLOY_LITTLE_ENDIAN 1
26 #elif defined(__sun) || defined(sun)
27 #include <sys/isa_defs.h>
28 #if defined(_LITTLE_ENDIAN)
29 #define NNDEPLOY_LITTLE_ENDIAN 1
30 #else
31 #define NNDEPLOY_LITTLE_ENDIAN 0
32 #endif
33 #else
34 #error "Unable to determine endianness of your machine; use CMake to compile"
35 #endif
36 #endif
37 
42 #ifndef NNDEPLOY_IO_USE_LITTLE_ENDIAN
43 #define NNDEPLOY_IO_USE_LITTLE_ENDIAN 1
44 #endif
45 
47 #define NNDEPLOY_IO_NO_ENDIAN_SWAP \
48  (NNDEPLOY_LITTLE_ENDIAN == NNDEPLOY_IO_USE_LITTLE_ENDIAN)
49 
50 namespace nndeploy {
51 namespace base {
52 
61 inline void byteSwap(void* data, size_t elem_bytes, size_t num_elems) {
62  for (size_t i = 0; i < num_elems; ++i) {
63  uint8_t* bptr = reinterpret_cast<uint8_t*>(data) + elem_bytes * i;
64  for (size_t j = 0; j < elem_bytes / 2; ++j) {
65  uint8_t v = bptr[elem_bytes - 1 - j];
66  bptr[elem_bytes - 1 - j] = bptr[j];
67  bptr[j] = v;
68  }
69  }
70 }
71 
72 } // namespace base
73 } // namespace nndeploy
74 
75 #endif // _NNDEPLOY_BASE_ENDIAN_H_
ref dmlc-core/include/dmlc/endian.h
void byteSwap(void *data, size_t elem_bytes, size_t num_elems)
A generic inplace byte swapping function.
Definition: endian.h:61