4b6eeb07acbeafaf2ea54107f15e824dcf47ed73
[awesomized/libmemcached] / libmemcached / byteorder.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 #include "byteorder.h"
13
14 /* Byte swap a 64-bit number. */
15 static inline uint64_t swap64(uint64_t in)
16 {
17 #ifndef WORDS_BIGENDIAN
18 /* Little endian, flip the bytes around until someone makes a faster/better
19 * way to do this. */
20 uint64_t rv= 0;
21 uint8_t x= 0;
22 for(x= 0; x < 8; x++)
23 {
24 rv= (rv << 8) | (in & 0xff);
25 in >>= 8;
26 }
27 return rv;
28 #else
29 /* big-endian machines don't need byte swapping */
30 return in;
31 #endif
32 }
33
34 uint64_t ntohll(uint64_t value)
35 {
36 return swap64(value);
37 }
38
39 uint64_t htonll(uint64_t value)
40 {
41 return swap64(value);
42 }