codemod 2010-2016 to 2010-present
[hiphop-php.git] / hphp / util / htonll.h
blobbca7c1f9a15148a40ea211c26f8ae71bf59959bb
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_HTONLL_H_
18 #define incl_HPHP_HTONLL_H_
21 * Tries to find a suitable implementation of htonll/ntohll if it doesn't
22 * already exist. This could go into portability.h, but seemed specific enough
23 * to be worth pulling out.
26 #include <folly/portability/Sockets.h>
27 #if defined(__FreeBSD__)
28 # include <sys/endian.h>
29 #elif defined(__APPLE__)
30 # include <machine/endian.h>
31 # include <libkern/OSByteOrder.h>
32 #elif defined(_MSC_VER)
33 # include <stdlib.h>
34 #else
35 # include <byteswap.h>
36 #endif
38 #if __BYTE_ORDER == __LITTLE_ENDIAN
39 # define htolell(x) (x)
40 # define letohll(x) (x)
41 # if !defined(htonll) && !defined(ntohll)
42 # if defined(__FreeBSD__)
43 # define htonll(x) bswap64(x)
44 # define ntohll(x) bswap64(x)
45 # elif defined(__APPLE__)
46 # define htonll(x) OSSwapInt64(x)
47 # define ntohll(x) OSSwapInt64(x)
48 # elif defined(_MSC_VER)
49 # define htonll(x) _byteswap_uint64(x)
50 # define ntohll(x) _byteswap_uint64(x)
51 # else
52 # define htonll(x) bswap_64(x)
53 # define ntohll(x) bswap_64(x)
54 # endif
55 # endif
56 #else
57 # if defined(__FreeBSD__)
58 # define htolell(x) bswap64(x)
59 # define letohll(x) bswap64(x)
60 # elif defined(__APPLE__)
61 # define htolell(x) OSSwapInt64(x)
62 # define letohll(x) OSSwapInt64(x)
63 # elif defined(_MSC_VER)
64 # define htolell(x) _byteswap_uint64(x)
65 # define letohll(x) _byteswap_uint64(x)
66 # else
67 # define htolell(x) bswap_64(x)
68 # define letohll(x) bswap_64(x)
69 # endif
70 # if !defined(htonll) && !defined(ntohll)
71 # define htonll(x) (x)
72 # define ntohll(x) (x)
73 # endif
74 #endif
76 #endif