diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-21 22:40:55 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-21 22:40:55 +0100 |
| commit | 5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda (patch) | |
| tree | 1acdfa5220cd13b7be43a2a01368e80d306473ca /examples/redis-unstable/src/endianconv.h | |
| parent | c7ab12bba64d9c20ccd79b132dac475f7bc3923e (diff) | |
| download | crep-5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda.tar.gz | |
Add Redis source code for testing
Diffstat (limited to 'examples/redis-unstable/src/endianconv.h')
| -rw-r--r-- | examples/redis-unstable/src/endianconv.h | 69 |
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 @@ +/* See endianconv.c top comments for more information + * + * ---------------------------------------------------------------------------- + * + * Copyright (c) 2011-Present, Redis Ltd. + * All rights reserved. + * + * Licensed under your choice of (a) the Redis Source Available License 2.0 + * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the + * GNU Affero General Public License v3 (AGPLv3). + */ + +#ifndef __ENDIANCONV_H +#define __ENDIANCONV_H + +#include "config.h" +#include <stdint.h> + +/* -------------------------------------------------------------------------- + * Optimized endian conversion helpers + * -------------------------------------------------------------------------- */ + +/* For GCC, Clang — use builtins that compile to a single instruction */ +#if defined(__GNUC__) || defined(__clang__) +#define REDIS_BSWAP64(v) __builtin_bswap64(v) +#else +#define REDIS_BSWAP64(v) intrev64(v) +#endif + +void memrev16(void *p); +void memrev32(void *p); +void memrev64(void *p); +uint16_t intrev16(uint16_t v); +uint32_t intrev32(uint32_t v); +uint64_t intrev64(uint64_t v); + +/* variants of the function doing the actual conversion only if the target + * host is big endian */ +#if (BYTE_ORDER == LITTLE_ENDIAN) +#define memrev16ifbe(p) ((void)(0)) +#define memrev32ifbe(p) ((void)(0)) +#define memrev64ifbe(p) ((void)(0)) +#define intrev16ifbe(v) (v) +#define intrev32ifbe(v) (v) +#define intrev64ifbe(v) (v) +#else +#define memrev16ifbe(p) memrev16(p) +#define memrev32ifbe(p) memrev32(p) +#define memrev64ifbe(p) memrev64(p) +#define intrev16ifbe(v) intrev16(v) +#define intrev32ifbe(v) intrev32(v) +#define intrev64ifbe(v) intrev64(v) +#endif + +/* The functions htonu64() and ntohu64() convert the specified value to + * network byte ordering and back. In big endian systems they are no-ops. */ +#if (BYTE_ORDER == BIG_ENDIAN) +#define htonu64(v) (v) +#define ntohu64(v) (v) +#else +#define htonu64(v) REDIS_BSWAP64(v) +#define ntohu64(v) REDIS_BSWAP64(v) +#endif + +#ifdef REDIS_TEST +int endianconvTest(int argc, char *argv[], int flags); +#endif + +#endif |
