Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | Copyright (c) 2004 Eliot Dresselhaus |
| 17 | |
| 18 | Permission is hereby granted, free of charge, to any person obtaining |
| 19 | a copy of this software and associated documentation files (the |
| 20 | "Software"), to deal in the Software without restriction, including |
| 21 | without limitation the rights to use, copy, modify, merge, publish, |
| 22 | distribute, sublicense, and/or sell copies of the Software, and to |
| 23 | permit persons to whom the Software is furnished to do so, subject to |
| 24 | the following conditions: |
| 25 | |
| 26 | The above copyright notice and this permission notice shall be |
| 27 | included in all copies or substantial portions of the Software. |
| 28 | |
| 29 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 32 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 33 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 34 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 | */ |
| 37 | |
| 38 | #ifndef included_clib_byte_order_h |
| 39 | #define included_clib_byte_order_h |
| 40 | |
| 41 | #include <vppinfra/clib.h> |
| 42 | |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 43 | #if (__BYTE_ORDER__)==( __ORDER_LITTLE_ENDIAN__) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | #define CLIB_ARCH_IS_BIG_ENDIAN (0) |
| 45 | #define CLIB_ARCH_IS_LITTLE_ENDIAN (1) |
| 46 | #else |
| 47 | /* Default is big endian. */ |
| 48 | #define CLIB_ARCH_IS_BIG_ENDIAN (1) |
| 49 | #define CLIB_ARCH_IS_LITTLE_ENDIAN (0) |
| 50 | #endif |
| 51 | |
| 52 | /* Big/little endian. */ |
| 53 | #define clib_arch_is_big_endian CLIB_ARCH_IS_BIG_ENDIAN |
| 54 | #define clib_arch_is_little_endian CLIB_ARCH_IS_LITTLE_ENDIAN |
| 55 | |
| 56 | always_inline u16 |
| 57 | clib_byte_swap_u16 (u16 x) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 58 | { |
| 59 | return (x >> 8) | (x << 8); |
| 60 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | |
| 62 | always_inline i16 |
| 63 | clib_byte_swap_i16 (i16 x) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 64 | { |
| 65 | return clib_byte_swap_u16 (x); |
| 66 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | |
| 68 | always_inline u32 |
| 69 | clib_byte_swap_u32 (u32 x) |
| 70 | { |
| 71 | #if defined (i386) || defined (__x86_64__) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 72 | if (!__builtin_constant_p (x)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 74 | asm volatile ("bswap %0":"=r" (x):"0" (x)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | return x; |
| 76 | } |
| 77 | #endif |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 78 | return ((x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | always_inline i32 |
| 82 | clib_byte_swap_i32 (i32 x) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 83 | { |
| 84 | return clib_byte_swap_u32 (x); |
| 85 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | |
| 87 | always_inline u64 |
| 88 | clib_byte_swap_u64 (u64 x) |
| 89 | { |
| 90 | #if defined (__x86_64__) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 91 | if (!__builtin_constant_p (x)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 93 | asm volatile ("bswapq %0":"=r" (x):"0" (x)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | return x; |
| 95 | } |
| 96 | #endif |
| 97 | #define _(x,n,i) \ |
| 98 | ((((x) >> (8*(i))) & 0xff) << (8*((n)-(i)-1))) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 99 | return (_(x, 8, 0) | _(x, 8, 1) |
| 100 | | _(x, 8, 2) | _(x, 8, 3) |
| 101 | | _(x, 8, 4) | _(x, 8, 5) | _(x, 8, 6) | _(x, 8, 7)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | #undef _ |
| 103 | } |
| 104 | |
| 105 | always_inline i64 |
| 106 | clib_byte_swap_i64 (i64 x) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 107 | { |
| 108 | return clib_byte_swap_u64 (x); |
| 109 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | |
| 111 | #define _(sex,type) \ |
| 112 | /* HOST -> SEX */ \ |
| 113 | always_inline type \ |
| 114 | clib_host_to_##sex##_##type (type x) \ |
| 115 | { \ |
| 116 | if (! clib_arch_is_##sex##_endian) \ |
| 117 | x = clib_byte_swap_##type (x); \ |
| 118 | return x; \ |
| 119 | } \ |
| 120 | \ |
| 121 | always_inline type \ |
| 122 | clib_host_to_##sex##_mem_##type (type * x) \ |
| 123 | { \ |
| 124 | type v = x[0]; \ |
| 125 | return clib_host_to_##sex##_##type (v); \ |
| 126 | } \ |
| 127 | \ |
| 128 | always_inline type \ |
| 129 | clib_host_to_##sex##_unaligned_mem_##type (type * x) \ |
| 130 | { \ |
| 131 | type v = clib_mem_unaligned (x, type); \ |
| 132 | return clib_host_to_##sex##_##type (v); \ |
| 133 | } \ |
| 134 | \ |
| 135 | /* SEX -> HOST */ \ |
| 136 | always_inline type \ |
| 137 | clib_##sex##_to_host_##type (type x) \ |
| 138 | { return clib_host_to_##sex##_##type (x); } \ |
| 139 | \ |
| 140 | always_inline type \ |
| 141 | clib_##sex##_to_host_mem_##type (type * x) \ |
| 142 | { return clib_host_to_##sex##_mem_##type (x); } \ |
| 143 | \ |
| 144 | always_inline type \ |
| 145 | clib_##sex##_to_host_unaligned_mem_##type (type * x) \ |
| 146 | { return clib_host_to_##sex##_unaligned_mem_##type (x); } |
| 147 | |
| 148 | #ifndef __cplusplus |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 149 | _(little, u16) |
| 150 | _(little, u32) |
| 151 | _(little, u64) |
| 152 | _(little, i16) |
| 153 | _(little, i32) |
| 154 | _(little, i64) |
| 155 | _(big, u16) _(big, u32) _(big, u64) _(big, i16) _(big, i32) _(big, i64) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | #undef _ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | /* Network "net" alias for "big". */ |
| 159 | #define _(type) \ |
| 160 | always_inline type \ |
| 161 | clib_net_to_host_##type (type x) \ |
| 162 | { return clib_big_to_host_##type (x); } \ |
| 163 | \ |
| 164 | always_inline type \ |
| 165 | clib_net_to_host_mem_##type (type * x) \ |
| 166 | { return clib_big_to_host_mem_##type (x); } \ |
| 167 | \ |
| 168 | always_inline type \ |
| 169 | clib_net_to_host_unaligned_mem_##type (type * x) \ |
| 170 | { return clib_big_to_host_unaligned_mem_##type (x); } \ |
| 171 | \ |
| 172 | always_inline type \ |
| 173 | clib_host_to_net_##type (type x) \ |
| 174 | { return clib_host_to_big_##type (x); } \ |
| 175 | \ |
| 176 | always_inline type \ |
| 177 | clib_host_to_net_mem_##type (type * x) \ |
| 178 | { return clib_host_to_big_mem_##type (x); } \ |
| 179 | \ |
| 180 | always_inline type \ |
| 181 | clib_host_to_net_unaligned_mem_##type (type * x) \ |
| 182 | { return clib_host_to_big_unaligned_mem_##type (x); } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | #ifndef __cplusplus |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 184 | _(u16); |
| 185 | _(i16); |
| 186 | _(u32); |
| 187 | _(i32); |
| 188 | _(u64); |
| 189 | _(i64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | #endif |
| 191 | |
| 192 | #undef _ |
| 193 | |
| 194 | #endif /* included_clib_byte_order_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 195 | |
| 196 | /* |
| 197 | * fd.io coding-style-patch-verification: ON |
| 198 | * |
| 199 | * Local Variables: |
| 200 | * eval: (c-set-style "gnu") |
| 201 | * End: |
| 202 | */ |