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 | { |
Gabriel Ganne | 70d44d3 | 2017-11-21 11:33:45 +0100 | [diff] [blame] | 59 | #if defined (__aarch64__) |
| 60 | if (!__builtin_constant_p (x)) |
| 61 | { |
| 62 | __asm__ ("rev16 %w0, %w0":"+r" (x)); |
| 63 | return x; |
| 64 | } |
| 65 | #endif |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 66 | return (x >> 8) | (x << 8); |
| 67 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | |
| 69 | always_inline i16 |
| 70 | clib_byte_swap_i16 (i16 x) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 71 | { |
| 72 | return clib_byte_swap_u16 (x); |
| 73 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | |
| 75 | always_inline u32 |
| 76 | clib_byte_swap_u32 (u32 x) |
| 77 | { |
| 78 | #if defined (i386) || defined (__x86_64__) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 79 | if (!__builtin_constant_p (x)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 81 | asm volatile ("bswap %0":"=r" (x):"0" (x)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | return x; |
| 83 | } |
Gabriel Ganne | 70d44d3 | 2017-11-21 11:33:45 +0100 | [diff] [blame] | 84 | #elif defined (__aarch64__) |
| 85 | if (!__builtin_constant_p (x)) |
| 86 | { |
| 87 | __asm__ ("rev %w0, %w0":"+r" (x)); |
| 88 | return x; |
| 89 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | #endif |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 91 | return ((x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | always_inline i32 |
| 95 | clib_byte_swap_i32 (i32 x) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 96 | { |
| 97 | return clib_byte_swap_u32 (x); |
| 98 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | |
| 100 | always_inline u64 |
| 101 | clib_byte_swap_u64 (u64 x) |
| 102 | { |
| 103 | #if defined (__x86_64__) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 104 | if (!__builtin_constant_p (x)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 106 | asm volatile ("bswapq %0":"=r" (x):"0" (x)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | return x; |
| 108 | } |
Gabriel Ganne | 70d44d3 | 2017-11-21 11:33:45 +0100 | [diff] [blame] | 109 | #elif defined (__aarch64__) |
| 110 | if (!__builtin_constant_p (x)) |
| 111 | { |
| 112 | __asm__ ("rev %0, %0":"+r" (x)); |
| 113 | return x; |
| 114 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | #endif |
| 116 | #define _(x,n,i) \ |
| 117 | ((((x) >> (8*(i))) & 0xff) << (8*((n)-(i)-1))) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 118 | return (_(x, 8, 0) | _(x, 8, 1) |
| 119 | | _(x, 8, 2) | _(x, 8, 3) |
| 120 | | _(x, 8, 4) | _(x, 8, 5) | _(x, 8, 6) | _(x, 8, 7)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | #undef _ |
| 122 | } |
| 123 | |
| 124 | always_inline i64 |
| 125 | clib_byte_swap_i64 (i64 x) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 126 | { |
| 127 | return clib_byte_swap_u64 (x); |
| 128 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
| 130 | #define _(sex,type) \ |
| 131 | /* HOST -> SEX */ \ |
| 132 | always_inline type \ |
| 133 | clib_host_to_##sex##_##type (type x) \ |
| 134 | { \ |
| 135 | if (! clib_arch_is_##sex##_endian) \ |
| 136 | x = clib_byte_swap_##type (x); \ |
| 137 | return x; \ |
| 138 | } \ |
| 139 | \ |
| 140 | always_inline type \ |
| 141 | clib_host_to_##sex##_mem_##type (type * x) \ |
| 142 | { \ |
| 143 | type v = x[0]; \ |
| 144 | return clib_host_to_##sex##_##type (v); \ |
| 145 | } \ |
| 146 | \ |
| 147 | always_inline type \ |
| 148 | clib_host_to_##sex##_unaligned_mem_##type (type * x) \ |
| 149 | { \ |
| 150 | type v = clib_mem_unaligned (x, type); \ |
| 151 | return clib_host_to_##sex##_##type (v); \ |
| 152 | } \ |
| 153 | \ |
| 154 | /* SEX -> HOST */ \ |
| 155 | always_inline type \ |
| 156 | clib_##sex##_to_host_##type (type x) \ |
| 157 | { return clib_host_to_##sex##_##type (x); } \ |
| 158 | \ |
| 159 | always_inline type \ |
| 160 | clib_##sex##_to_host_mem_##type (type * x) \ |
| 161 | { return clib_host_to_##sex##_mem_##type (x); } \ |
| 162 | \ |
| 163 | always_inline type \ |
| 164 | clib_##sex##_to_host_unaligned_mem_##type (type * x) \ |
| 165 | { return clib_host_to_##sex##_unaligned_mem_##type (x); } |
| 166 | |
| 167 | #ifndef __cplusplus |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 168 | _(little, u16) |
| 169 | _(little, u32) |
| 170 | _(little, u64) |
| 171 | _(little, i16) |
| 172 | _(little, i32) |
| 173 | _(little, i64) |
| 174 | _(big, u16) _(big, u32) _(big, u64) _(big, i16) _(big, i32) _(big, i64) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | #undef _ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 177 | /* Network "net" alias for "big". */ |
| 178 | #define _(type) \ |
| 179 | always_inline type \ |
| 180 | clib_net_to_host_##type (type x) \ |
| 181 | { return clib_big_to_host_##type (x); } \ |
| 182 | \ |
| 183 | always_inline type \ |
| 184 | clib_net_to_host_mem_##type (type * x) \ |
| 185 | { return clib_big_to_host_mem_##type (x); } \ |
| 186 | \ |
| 187 | always_inline type \ |
| 188 | clib_net_to_host_unaligned_mem_##type (type * x) \ |
| 189 | { return clib_big_to_host_unaligned_mem_##type (x); } \ |
| 190 | \ |
| 191 | always_inline type \ |
| 192 | clib_host_to_net_##type (type x) \ |
| 193 | { return clib_host_to_big_##type (x); } \ |
| 194 | \ |
| 195 | always_inline type \ |
| 196 | clib_host_to_net_mem_##type (type * x) \ |
| 197 | { return clib_host_to_big_mem_##type (x); } \ |
| 198 | \ |
| 199 | always_inline type \ |
| 200 | clib_host_to_net_unaligned_mem_##type (type * x) \ |
| 201 | { return clib_host_to_big_unaligned_mem_##type (x); } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | #ifndef __cplusplus |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 203 | _(u16); |
| 204 | _(i16); |
| 205 | _(u32); |
| 206 | _(i32); |
| 207 | _(u64); |
| 208 | _(i64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | #endif |
| 210 | |
| 211 | #undef _ |
| 212 | |
Dave Barach | d0853d7 | 2019-06-28 15:08:49 -0400 | [diff] [blame^] | 213 | /* Dummy endian swap functions for IEEE floating-point numbers */ |
| 214 | /* *INDENT-OFF* */ |
| 215 | always_inline f64 clib_net_to_host_f64 (f64 x) { return x; } |
| 216 | always_inline f64 clib_host_to_net_f64 (f64 x) { return x; } |
| 217 | always_inline f32 clib_net_to_host_f32 (f32 x) { return x; } |
| 218 | always_inline f32 clib_host_to_net_f32 (f32 x) { return x; } |
| 219 | /* *INDENT-ON* */ |
| 220 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | #endif /* included_clib_byte_order_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 222 | |
| 223 | /* |
| 224 | * fd.io coding-style-patch-verification: ON |
| 225 | * |
| 226 | * Local Variables: |
| 227 | * eval: (c-set-style "gnu") |
| 228 | * End: |
| 229 | */ |