aboutsummaryrefslogtreecommitdiff
path: root/examples/redis-unstable/src/endianconv.h
diff options
context:
space:
mode:
Diffstat (limited to 'examples/redis-unstable/src/endianconv.h')
-rw-r--r--examples/redis-unstable/src/endianconv.h69
1 files changed, 69 insertions, 0 deletions
diff --git a/examples/redis-unstable/src/endianconv.h b/examples/redis-unstable/src/endianconv.h
new file mode 100644
index 0000000..ca63cd9
--- /dev/null
+++ b/examples/redis-unstable/src/endianconv.h
@@ -0,0 +1,69 @@
1/* See endianconv.c top comments for more information
2 *
3 * ----------------------------------------------------------------------------
4 *
5 * Copyright (c) 2011-Present, Redis Ltd.
6 * All rights reserved.
7 *
8 * Licensed under your choice of (a) the Redis Source Available License 2.0
9 * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
10 * GNU Affero General Public License v3 (AGPLv3).
11 */
12
13#ifndef __ENDIANCONV_H
14#define __ENDIANCONV_H
15
16#include "config.h"
17#include <stdint.h>
18
19/* --------------------------------------------------------------------------
20 * Optimized endian conversion helpers
21 * -------------------------------------------------------------------------- */
22
23/* For GCC, Clang — use builtins that compile to a single instruction */
24#if defined(__GNUC__) || defined(__clang__)
25#define REDIS_BSWAP64(v) __builtin_bswap64(v)
26#else
27#define REDIS_BSWAP64(v) intrev64(v)
28#endif
29
30void memrev16(void *p);
31void memrev32(void *p);
32void memrev64(void *p);
33uint16_t intrev16(uint16_t v);
34uint32_t intrev32(uint32_t v);
35uint64_t intrev64(uint64_t v);
36
37/* variants of the function doing the actual conversion only if the target
38 * host is big endian */
39#if (BYTE_ORDER == LITTLE_ENDIAN)
40#define memrev16ifbe(p) ((void)(0))
41#define memrev32ifbe(p) ((void)(0))
42#define memrev64ifbe(p) ((void)(0))
43#define intrev16ifbe(v) (v)
44#define intrev32ifbe(v) (v)
45#define intrev64ifbe(v) (v)
46#else
47#define memrev16ifbe(p) memrev16(p)
48#define memrev32ifbe(p) memrev32(p)
49#define memrev64ifbe(p) memrev64(p)
50#define intrev16ifbe(v) intrev16(v)
51#define intrev32ifbe(v) intrev32(v)
52#define intrev64ifbe(v) intrev64(v)
53#endif
54
55/* The functions htonu64() and ntohu64() convert the specified value to
56 * network byte ordering and back. In big endian systems they are no-ops. */
57#if (BYTE_ORDER == BIG_ENDIAN)
58#define htonu64(v) (v)
59#define ntohu64(v) (v)
60#else
61#define htonu64(v) REDIS_BSWAP64(v)
62#define ntohu64(v) REDIS_BSWAP64(v)
63#endif
64
65#ifdef REDIS_TEST
66int endianconvTest(int argc, char *argv[], int flags);
67#endif
68
69#endif