| 1 | /* |
| 2 | * Copyright 2008-2018 Aerospike, Inc. |
| 3 | * |
| 4 | * Portions may be licensed to Aerospike, Inc. under one or more contributor |
| 5 | * license agreements. |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 8 | * use this file except in compliance with the License. You may obtain a copy of |
| 9 | * the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations under |
| 15 | * the License. |
| 16 | */ |
| 17 | #pragma once |
| 18 | |
| 19 | #include <aerospike/as_std.h> |
| 20 | |
| 21 | #if defined(__linux__) || defined(__FreeBSD__) |
| 22 | |
| 23 | #if defined(__linux__) |
| 24 | #include <endian.h> |
| 25 | #else |
| 26 | #include <sys/endian.h> |
| 27 | #endif |
| 28 | |
| 29 | #define cf_swap_to_be16(_n) htobe16(_n) |
| 30 | #define cf_swap_to_le16(_n) htole16(_n) |
| 31 | #define cf_swap_from_be16(_n) be16toh(_n) |
| 32 | #define cf_swap_from_le16(_n) le16toh(_n) |
| 33 | |
| 34 | #define cf_swap_to_be32(_n) htobe32(_n) |
| 35 | #define cf_swap_to_le32(_n) htole32(_n) |
| 36 | #define cf_swap_from_be32(_n) be32toh(_n) |
| 37 | #define cf_swap_from_le32(_n) le32toh(_n) |
| 38 | |
| 39 | #define cf_swap_to_be64(_n) htobe64(_n) |
| 40 | #define cf_swap_to_le64(_n) htole64(_n) |
| 41 | #define cf_swap_from_be64(_n) be64toh(_n) |
| 42 | #define cf_swap_from_le64(_n) le64toh(_n) |
| 43 | #endif // __linux__ || __FreeBSD__ |
| 44 | |
| 45 | #if defined(__APPLE__) |
| 46 | #include <libkern/OSByteOrder.h> |
| 47 | #include <arpa/inet.h> |
| 48 | |
| 49 | #define cf_swap_to_be16(_n) OSSwapHostToBigInt16(_n) |
| 50 | #define cf_swap_to_le16(_n) OSSwapHostToLittleInt16(_n) |
| 51 | #define cf_swap_from_be16(_n) OSSwapBigToHostInt16(_n) |
| 52 | #define cf_swap_from_le16(_n) OSSwapLittleToHostInt16(_n) |
| 53 | |
| 54 | #define cf_swap_to_be32(_n) OSSwapHostToBigInt32(_n) |
| 55 | #define cf_swap_to_le32(_n) OSSwapHostToLittleInt32(_n) |
| 56 | #define cf_swap_from_be32(_n) OSSwapBigToHostInt32(_n) |
| 57 | #define cf_swap_from_le32(_n) OSSwapLittleToHostInt32(_n) |
| 58 | |
| 59 | #define cf_swap_to_be64(_n) OSSwapHostToBigInt64(_n) |
| 60 | #define cf_swap_to_le64(_n) OSSwapHostToLittleInt64(_n) |
| 61 | #define cf_swap_from_be64(_n) OSSwapBigToHostInt64(_n) |
| 62 | #define cf_swap_from_le64(_n) OSSwapLittleToHostInt64(_n) |
| 63 | #endif // __APPLE__ |
| 64 | |
| 65 | #if defined(_MSC_VER) |
| 66 | #include <WinSock2.h> |
| 67 | |
| 68 | #define cf_swap_to_be16(_n) _byteswap_ushort(_n) |
| 69 | #define cf_swap_to_le16(_n) (_n) |
| 70 | #define cf_swap_from_be16(_n) _byteswap_ushort(_n) |
| 71 | #define cf_swap_from_le16(_n) (_n) |
| 72 | |
| 73 | #define cf_swap_to_be32(_n) _byteswap_ulong(_n) |
| 74 | #define cf_swap_to_le32(_n) (_n) |
| 75 | #define cf_swap_from_be32(_n) _byteswap_ulong(_n) |
| 76 | #define cf_swap_from_le32(_n) (_n) |
| 77 | |
| 78 | #define cf_swap_to_be64(_n) _byteswap_uint64(_n) |
| 79 | #define cf_swap_to_le64(_n) (_n) |
| 80 | #define cf_swap_from_be64(_n) _byteswap_uint64(_n) |
| 81 | #define cf_swap_from_le64(_n) (_n) |
| 82 | #endif // _MSC_VER |
| 83 | |
| 84 | static inline double |
| 85 | cf_swap_to_big_float64(double d) |
| 86 | { |
| 87 | uint64_t i = cf_swap_to_be64(*(uint64_t*)&d); |
| 88 | return *(double*)&i; |
| 89 | } |
| 90 | |
| 91 | static inline double |
| 92 | cf_swap_to_little_float64(double d) |
| 93 | { |
| 94 | uint64_t i = cf_swap_to_le64(*(uint64_t*)&d); |
| 95 | return *(double*)&i; |
| 96 | } |
| 97 | |
| 98 | static inline double |
| 99 | cf_swap_from_big_float64(double d) |
| 100 | { |
| 101 | uint64_t i = cf_swap_from_be64(*(uint64_t*)&d); |
| 102 | return *(double*)&i; |
| 103 | } |
| 104 | |
| 105 | static inline double |
| 106 | cf_swap_from_little_float64(double d) |
| 107 | { |
| 108 | uint64_t i = cf_swap_from_le64(*(uint64_t*)&d); |
| 109 | return *(double*)&i; |
| 110 | } |
| 111 | |