From: antirez Date: Wed, 9 Mar 2011 15:24:18 +0000 (+0100) Subject: endianess conversion API, to be applied to specially encoded data types for arch... X-Git-Url: https://git.saurik.com/redis.git/commitdiff_plain/e12cb14308ab2719b506762e662ab179f31aceb9 endianess conversion API, to be applied to specially encoded data types for arch agnostic encoding. --- diff --git a/src/endian.c b/src/endian.c new file mode 100644 index 00000000..aff2425a --- /dev/null +++ b/src/endian.c @@ -0,0 +1,63 @@ +/* Toggle the 16 bit unsigned integer pointed by *p from little endian to + * big endian */ +void memrev16(void *p) { + unsigned char *x = p, t; + + t = x[0]; + x[0] = x[1]; + x[1] = t; +} + +/* Toggle the 32 bit unsigned integer pointed by *p from little endian to + * big endian */ +void memrev32(void *p) { + unsigned char *x = p, t; + + t = x[0]; + x[0] = x[3]; + x[3] = t; + t = x[1]; + x[1] = x[2]; + x[2] = t; +} + +/* Toggle the 64 bit unsigned integer pointed by *p from little endian to + * big endian */ +void memrev64(void *p) { + unsigned char *x = p, t; + + t = x[0]; + x[0] = x[7]; + x[7] = t; + t = x[1]; + x[1] = x[6]; + x[6] = t; + t = x[2]; + x[2] = x[5]; + x[5] = t; + t = x[3]; + x[3] = x[4]; + x[4] = t; +} + +#ifdef TESTMAIN +#include + +int main(void) { + char buf[32]; + + sprintf(buf,"ciaoroma"); + memrev16(buf); + printf("%s\n", buf); + + sprintf(buf,"ciaoroma"); + memrev32(buf); + printf("%s\n", buf); + + sprintf(buf,"ciaoroma"); + memrev64(buf); + printf("%s\n", buf); + + return 0; +} +#endif diff --git a/src/endian.h b/src/endian.h new file mode 100644 index 00000000..ea295ee5 --- /dev/null +++ b/src/endian.h @@ -0,0 +1,8 @@ +#ifndef __ENDIAN_H +#define __ENDIAN_H + +void memrev16(void *p); +void memrev32(void *p); +void memrev64(void *p); + +#endif