Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 1 | /* |
2 | * Portions of this file are copyright Rebirth contributors and licensed as |
||
3 | * described in COPYING.txt. |
||
4 | * Portions of this file are copyright Parallax Software and licensed |
||
5 | * according to the Parallax license below. |
||
6 | * See COPYING.txt for license details. |
||
7 | |||
8 | THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
||
9 | SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
||
10 | END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
||
11 | ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
||
12 | IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
||
13 | SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
||
14 | FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
||
15 | CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
||
16 | AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
||
17 | COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
||
18 | */ |
||
19 | |||
20 | /* |
||
21 | * |
||
22 | * code to swap bytes because of big/little endian problems. |
||
23 | * contains the macros: |
||
24 | * SWAP{INT64,INT,SHORT}(x): returns a swapped version of x |
||
25 | * INTEL_{INT64,INT,SHORT}(x): returns x after conversion to/from little endian |
||
26 | * GET_INTEL_{INT64,INT,SHORT}(src): gets value from little-endian buffer src |
||
27 | * PUT_INTEL_{INT64,INT,SHORT}(dest, src): puts src into little-endian buffer dest |
||
28 | * |
||
29 | * the GET/PUT macros are safe to use on platforms which segfault on unaligned word access |
||
30 | * |
||
31 | */ |
||
32 | |||
33 | #pragma once |
||
34 | |||
35 | #include <cstdint> |
||
36 | #include <string.h> // for memcpy |
||
37 | #include <type_traits> |
||
38 | #include "dxxsconf.h" |
||
39 | #include "pstypes.h" |
||
40 | |||
41 | static constexpr uint16_t SWAPSHORT(const uint16_t &x) |
||
42 | { |
||
43 | #ifdef DXX_HAVE_BUILTIN_BSWAP16 |
||
44 | return __builtin_bswap16(x); |
||
45 | #else |
||
46 | return (x << 8) | (x >> 8); |
||
47 | #endif |
||
48 | } |
||
49 | |||
50 | static constexpr int16_t SWAPSHORT(const int16_t &i) |
||
51 | { |
||
52 | return SWAPSHORT(static_cast<uint16_t>(i)); |
||
53 | } |
||
54 | |||
55 | static constexpr uint32_t SWAPINT(const uint32_t &x) |
||
56 | { |
||
57 | #ifdef DXX_HAVE_BUILTIN_BSWAP |
||
58 | return __builtin_bswap32(x); |
||
59 | #else |
||
60 | return (x << 24) | (x >> 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00); |
||
61 | #endif |
||
62 | } |
||
63 | |||
64 | static constexpr int32_t SWAPINT(const int32_t &i) |
||
65 | { |
||
66 | return SWAPINT(static_cast<uint32_t>(i)); |
||
67 | } |
||
68 | |||
69 | #if !DXX_WORDS_BIGENDIAN |
||
70 | #define byteutil_choose_endian(F,a) (a) |
||
71 | #else // ! WORDS_BIGENDIAN |
||
72 | #define byteutil_choose_endian(F,a) (F(a)) |
||
73 | #endif // ! WORDS_BIGENDIAN |
||
74 | constexpr std::integral_constant<int, DXX_WORDS_BIGENDIAN> words_bigendian{}; |
||
75 | |||
76 | #if DXX_WORDS_NEED_ALIGNMENT |
||
77 | #define byteutil_unaligned_copy(dt, d, s) ( DXX_BEGIN_COMPOUND_STATEMENT { dt &destination_reference = d; memcpy(&destination_reference, (s), sizeof(d)); } DXX_END_COMPOUND_STATEMENT ) |
||
78 | #else // WORDS_NEED_ALIGNMENT |
||
79 | #define byteutil_unaligned_copy(dt, d, s) ( DXX_BEGIN_COMPOUND_STATEMENT { dt &destination_reference = d; destination_reference = *reinterpret_cast<const dt *>(s); } DXX_END_COMPOUND_STATEMENT ) |
||
80 | #endif // WORDS_NEED_ALIGNMENT |
||
81 | |||
82 | static constexpr uint16_t INTEL_SHORT(const uint16_t &x) |
||
83 | { |
||
84 | return byteutil_choose_endian(SWAPSHORT, x); |
||
85 | } |
||
86 | |||
87 | static constexpr int16_t INTEL_SHORT(const int16_t &x) |
||
88 | { |
||
89 | return byteutil_choose_endian(SWAPSHORT, x); |
||
90 | } |
||
91 | |||
92 | static constexpr uint32_t INTEL_INT(const uint32_t &x) |
||
93 | { |
||
94 | return byteutil_choose_endian(SWAPINT, x); |
||
95 | } |
||
96 | |||
97 | static constexpr int32_t INTEL_INT(const int32_t &x) |
||
98 | { |
||
99 | return byteutil_choose_endian(SWAPINT, x); |
||
100 | } |
||
101 | #undef byteutil_choose_endian |
||
102 | |||
103 | template <typename T> |
||
104 | static inline uint32_t GET_INTEL_INT(const T *p) |
||
105 | { |
||
106 | uint32_t u; |
||
107 | byteutil_unaligned_copy(uint32_t, u, p); |
||
108 | return INTEL_INT(u); |
||
109 | } |
||
110 | |||
111 | template <typename T> |
||
112 | static inline uint16_t GET_INTEL_SHORT(const T *p) |
||
113 | { |
||
114 | uint16_t u; |
||
115 | byteutil_unaligned_copy(uint16_t, u, p); |
||
116 | return INTEL_SHORT(u); |
||
117 | } |
||
118 | |||
119 | template <typename T> |
||
120 | static inline void PUT_INTEL_SHORT(uint16_t *d, const T &s) |
||
121 | { |
||
122 | uint16_t u = INTEL_SHORT(s); |
||
123 | byteutil_unaligned_copy(uint16_t, *d, &u); |
||
124 | } |
||
125 | |||
126 | template <typename T> |
||
127 | static inline void PUT_INTEL_SHORT(uint8_t *d, const T &s) |
||
128 | { |
||
129 | PUT_INTEL_SHORT<T>(reinterpret_cast<uint16_t *>(d), s); |
||
130 | } |
||
131 | |||
132 | template <typename T> |
||
133 | static inline void PUT_INTEL_INT(uint32_t *d, const T &s) |
||
134 | { |
||
135 | uint32_t u = INTEL_INT(s); |
||
136 | byteutil_unaligned_copy(uint32_t, *d, &u); |
||
137 | } |
||
138 | |||
139 | template <typename T> |
||
140 | static inline void PUT_INTEL_INT(uint8_t *d, const T &s) |
||
141 | { |
||
142 | PUT_INTEL_INT<T>(reinterpret_cast<uint32_t *>(d), s); |
||
143 | } |
||
144 | |||
145 | #undef byteutil_unaligned_copy |