]> git.saurik.com Git - redis.git/blob - src/endian.c
ziplist.c fixes for bigendian
[redis.git] / src / endian.c
1 #include <stdint.h>
2
3 /* Toggle the 16 bit unsigned integer pointed by *p from little endian to
4 * big endian */
5 void memrev16(void *p) {
6 unsigned char *x = p, t;
7
8 t = x[0];
9 x[0] = x[1];
10 x[1] = t;
11 }
12
13 /* Toggle the 32 bit unsigned integer pointed by *p from little endian to
14 * big endian */
15 void memrev32(void *p) {
16 unsigned char *x = p, t;
17
18 t = x[0];
19 x[0] = x[3];
20 x[3] = t;
21 t = x[1];
22 x[1] = x[2];
23 x[2] = t;
24 }
25
26 /* Toggle the 64 bit unsigned integer pointed by *p from little endian to
27 * big endian */
28 void memrev64(void *p) {
29 unsigned char *x = p, t;
30
31 t = x[0];
32 x[0] = x[7];
33 x[7] = t;
34 t = x[1];
35 x[1] = x[6];
36 x[6] = t;
37 t = x[2];
38 x[2] = x[5];
39 x[5] = t;
40 t = x[3];
41 x[3] = x[4];
42 x[4] = t;
43 }
44
45 uint16_t intrev16(uint16_t v) {
46 memrev16(&v);
47 return v;
48 }
49
50 uint32_t intrev32(uint32_t v) {
51 memrev32(&v);
52 return v;
53 }
54
55 uint64_t intrev64(uint64_t v) {
56 memrev64(&v);
57 return v;
58 }
59
60 #ifdef TESTMAIN
61 #include <stdio.h>
62
63 int main(void) {
64 char buf[32];
65
66 sprintf(buf,"ciaoroma");
67 memrev16(buf);
68 printf("%s\n", buf);
69
70 sprintf(buf,"ciaoroma");
71 memrev32(buf);
72 printf("%s\n", buf);
73
74 sprintf(buf,"ciaoroma");
75 memrev64(buf);
76 printf("%s\n", buf);
77
78 return 0;
79 }
80 #endif