33#ifndef __SIM_BYTE_SWAP_HH__
34#define __SIM_BYTE_SWAP_HH__
44#include <sys/isa_defs.h>
46#include <machine/endian.h>
50#include <libkern/OSByteOrder.h>
56#include "enums/ByteOrder.hh"
72#elif defined(__APPLE__)
73 return OSSwapInt64(
x);
75 return (uint64_t)((((uint64_t)(
x) & 0xff) << 56) |
76 ((uint64_t)(
x) & 0xff00ULL) << 40 |
77 ((uint64_t)(
x) & 0xff0000ULL) << 24 |
78 ((uint64_t)(
x) & 0xff000000ULL) << 8 |
79 ((uint64_t)(
x) & 0xff00000000ULL) >> 8 |
80 ((uint64_t)(
x) & 0xff0000000000ULL) >> 24 |
81 ((uint64_t)(
x) & 0xff000000000000ULL) >> 40 |
82 ((uint64_t)(
x) & 0xff00000000000000ULL) >> 56) ;
91#elif defined(__APPLE__)
92 return OSSwapInt32(
x);
94 return (uint32_t)(((uint32_t)(
x) & 0xff) << 24 |
95 ((uint32_t)(
x) & 0xff00) << 8 | ((uint32_t)(
x) & 0xff0000) >> 8 |
96 ((uint32_t)(
x) & 0xff000000) >> 24);
103#if defined(__linux__)
105#elif defined(__APPLE__)
106 return OSSwapInt16(
x);
108 return (uint16_t)(((uint16_t)(
x) & 0xff) << 8 |
109 ((uint16_t)(
x) & 0xff00) >> 8);
114inline std::enable_if_t<
115 sizeof(T) == 8 && std::is_convertible_v<T, uint64_t>, T>
122inline std::enable_if_t<
123 sizeof(T) == 4 && std::is_convertible_v<T, uint32_t>, T>
130inline std::enable_if_t<
131 sizeof(T) == 2 && std::is_convertible_v<T, uint16_t>, T>
138inline std::enable_if_t<
139 sizeof(T) == 1 && std::is_convertible_v<T, uint8_t>, T>
148std::enable_if_t<std::is_same_v<T, vring_used_elem>, T>
151std::enable_if_t<std::is_same_v<T, vring_desc>, T>
154template <
typename T,
size_t N>
155inline std::array<T, N>
170#if (defined(_BIG_ENDIAN) || !defined(_LITTLE_ENDIAN)) && BYTE_ORDER == BIG_ENDIAN
174template <
typename T>
inline T
htobe(T value) {
return value;}
175template <
typename T>
inline T
betoh(T value) {
return value;}
176#elif defined(_LITTLE_ENDIAN) || BYTE_ORDER == LITTLE_ENDIAN
178template <
typename T>
inline T
htole(T value) {
return value;}
179template <
typename T>
inline T
letoh(T value) {
return value;}
180template <
typename T>
inline T
htobe(T value) {
return swap_byte(value);}
181template <
typename T>
inline T
betoh(T value) {
return swap_byte(value);}
183 #error Invalid Endianess
187inline T
htog(T value, ByteOrder guest_byte_order)
189 return guest_byte_order == ByteOrder::big ?
194inline T
gtoh(T value, ByteOrder guest_byte_order)
196 return guest_byte_order == ByteOrder::big ?
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
uint32_t swap_byte32(uint32_t x)
std::enable_if_t< std::is_same_v< T, vring_used_elem >, T > swap_byte(T v)
T gtoh(T value, ByteOrder guest_byte_order)
const ByteOrder HostByteOrder
uint16_t swap_byte16(uint16_t x)
T htog(T value, ByteOrder guest_byte_order)
uint64_t swap_byte64(uint64_t x)