RetroArch
utils.h
Go to the documentation of this file.
1 #pragma once
2 #include <cstring>
3 #include <type_traits>
4 
5 #if defined(WIN32) || defined(_WIN32) || defined(_MSC_VER)
6 #define PLATFORM_WINDOWS
7 #elif __APPLE__
8 #define PLATFORM_APPLE
9 #define PLATFORM_POSIX
10 #elif __linux__
11 #define PLATFORM_LINUX
12 #define PLATFORM_POSIX
13 #endif
14 
15 #ifdef PLATFORM_LINUX
16 #include <byteswap.h>
17 #endif
18 
19 /* reinterpret_cast for value types */
20 template<typename DstType, typename SrcType>
21 inline DstType
22 bit_cast(const SrcType& src)
23 {
24  static_assert(sizeof(SrcType) == sizeof(DstType), "bit_cast must be between same sized types");
25  static_assert(std::is_trivially_copyable<SrcType>::value, "SrcType is not trivially copyable.");
26  static_assert(std::is_trivially_copyable<DstType>::value, "DstType is not trivially copyable.");
27 
28  DstType dst;
29  std::memcpy(&dst, &src, sizeof(SrcType));
30  return dst;
31 }
32 
33 /* Utility class to swap endian for types of size 1, 2, 4, 8 */
34 /* other type sizes are not supported */
35 template<typename Type, unsigned Size = sizeof(Type)>
36 struct byte_swap_t;
37 
38 template<typename Type>
39 struct byte_swap_t<Type, 1>
40 {
41  static Type swap(Type src)
42  {
43  return src;
44  }
45 };
46 
47 template<typename Type>
48 struct byte_swap_t<Type, 2>
49 {
50  static Type swap(Type src)
51  {
52 #ifdef PLATFORM_WINDOWS
53  return bit_cast<Type>(_byteswap_ushort(bit_cast<uint16_t>(src)));
54 #elif defined(PLATFORM_APPLE)
55  /* Apple has no 16-bit byteswap intrinsic */
56  const uint16_t data = bit_cast<uint16_t>(src);
57  return bit_cast<Type>((uint16_t)((data >> 8) | (data << 8)));
58 #elif defined(PLATFORM_LINUX)
59  return bit_cast<Type>(bswap_16(bit_cast<uint16_t>(src)));
60 #endif
61  }
62 };
63 
64 template<typename Type>
65 struct byte_swap_t<Type, 4>
66 {
67  static Type swap(Type src)
68  {
69 #ifdef PLATFORM_WINDOWS
70  return bit_cast<Type>(_byteswap_ulong(bit_cast<uint32_t>(src)));
71 #elif defined(PLATFORM_APPLE)
72  return bit_cast<Type>(__builtin_bswap32(bit_cast<uint32_t>(src)));
73 #elif defined(PLATFORM_LINUX)
74  return bit_cast<Type>(bswap_32(bit_cast<uint32_t>(src)));
75 #endif
76  }
77 };
78 
79 template<typename Type>
80 struct byte_swap_t<Type, 8>
81 {
82  static Type swap(Type src)
83  {
84 #ifdef PLATFORM_WINDOWS
85  return bit_cast<Type>(_byteswap_uint64(bit_cast<uint64_t>(src)));
86 #elif defined(PLATFORM_APPLE)
87  return bit_cast<Type>(__builtin_bswap64(bit_cast<uint64_t>(src)));
88 #elif defined(PLATFORM_LINUX)
89  return bit_cast<Type>(bswap_64(bit_cast<uint64_t>(src)));
90 #endif
91  }
92 };
93 
94 /* Swaps endian of src */
95 template<typename Type>
96 inline Type
98 {
100 }
101 
102 /* Alignment helpers */
103 template<typename Type>
104 constexpr inline Type
105 align_up(Type value, size_t alignment)
106 {
107  return static_cast<Type>((static_cast<size_t>(value) + (alignment - 1)) & ~(alignment - 1));
108 }
109 
110 template<typename Type>
111 constexpr inline Type
112 align_down(Type value, size_t alignment)
113 {
114  return static_cast<Type>(static_cast<size_t>(value) & ~(alignment - 1));
115 }
116 
117 #define CHECK_SIZE(Type, Size) \
118  static_assert(sizeof(Type) == Size, \
119  #Type " must be " #Size " bytes")
constexpr Type align_down(Type value, size_t alignment)
Definition: utils.h:112
static INLINE uint32_t bswap_32(uint32_t val)
Definition: xenon360_audio.c:54
Definition: ibxm.h:9
static Type swap(Type src)
Definition: utils.h:41
VULKAN_HPP_INLINE void swap(UniqueHandle< Type > &lhs, UniqueHandle< Type > &rhs)
Definition: vulkan.hpp:441
static Type swap(Type src)
Definition: utils.h:67
constexpr Type align_up(Type value, size_t alignment)
Definition: utils.h:105
GLenum src
Definition: glext.h:6980
Type byte_swap(Type src)
Definition: utils.h:97
static Type swap(Type src)
Definition: utils.h:50
static Type swap(Type src)
Definition: utils.h:82
GLsizei const GLfloat * value
Definition: glext.h:6709
Definition: utils.h:36
Type
Type of JSON value.
Definition: rapidjson.h:603
GLenum GLenum dst
Definition: glext.h:6980
unsigned short uint16_t
Definition: stdint.h:125
DstType bit_cast(const SrcType &src)
Definition: utils.h:22
void * memcpy(void *dst, const void *src, size_t len)
Definition: string.c:26