Monado OpenXR Runtime
Loading...
Searching...
No Matches
xrt_byte_order.h
Go to the documentation of this file.
1// Copyright 2025, Beyley Cardellio
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Endian-specific byte order defines.
6 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
7 * @ingroup aux_os
8 */
9
10#pragma once
11
12#include "xrt_compiler.h"
13#include "xrt_defines.h"
14
15#include <stdint.h>
16
17#ifdef __linux__
18
19// On Linux, all these conversion functions are defined for both endians
20#include <asm/byteorder.h>
21
22#elif defined(XRT_BIG_ENDIAN)
23
24#error "@todo: Add byte order constants and functions for this OS or big endian machines."
25
26#else
27
28#define __be64 uint64_t
29#define __be32 uint32_t
30#define __be16 uint16_t
31
32#define __be16_to_cpu(x) ((((uint16_t)x & (uint16_t)0x00FFU) << 8) | (((uint16_t)x & (uint16_t)0xFF00U) >> 8))
33#define __cpu_to_be16(x) __be16_to_cpu(x)
34
35#define __be32_to_cpu(x) \
36 ((((uint32_t)x & (uint32_t)0x000000FFUL) << 24) | (((uint32_t)x & (uint32_t)0x0000FF00UL) << 8) | \
37 (((uint32_t)x & (uint32_t)0x00FF0000UL) >> 8) | (((uint32_t)x & (uint32_t)0xFF000000UL) >> 24))
38#define __cpu_to_be32(x) __be32_to_cpu(x)
39
40#define __be64_to_cpu(x) \
41 ((((uint64_t)x & (uint64_t)0x00000000000000FFULL) << 56) | \
42 (((uint64_t)x & (uint64_t)0x000000000000FF00ULL) << 40) | \
43 (((uint64_t)x & (uint64_t)0x0000000000FF0000ULL) << 24) | \
44 (((uint64_t)x & (uint64_t)0x00000000FF000000ULL) << 8) | \
45 (((uint64_t)x & (uint64_t)0x000000FF00000000ULL) >> 8) | \
46 (((uint64_t)x & (uint64_t)0x0000FF0000000000ULL) >> 24) | \
47 (((uint64_t)x & (uint64_t)0x00FF000000000000ULL) >> 40) | \
48 (((uint64_t)x & (uint64_t)0xFF00000000000000ULL) >> 56))
49#define __cpu_to_be64(x) __be64_to_cpu(x)
50
51#define __le64 uint64_t
52#define __le32 uint32_t
53#define __le16 uint16_t
54#define __u8 uint8_t
55#define __s8 int8_t
56#define __cpu_to_le16(x) (x)
57#define __le16_to_cpu(x) (x)
58#define __cpu_to_le32(x) (x)
59#define __le32_to_cpu(x) (x)
60#define __cpu_to_le64(x) (x)
61#define __le64_to_cpu(x) (x)
62
63#endif
64
65/*
66 * 32-bit float
67 */
68
69/*!
70 * Little endian 32-bit float wrapper struct.
71 */
72typedef struct
73{
74 __le32 val;
75} __lef32;
76
77/*!
78 * Big endian 32-bit float wrapper struct.
79 */
80typedef struct
81{
82 __be32 val;
83} __bef32;
84
85static inline float
86__lef32_to_cpu(__lef32 f)
87{
88 union {
89 uint32_t raw;
90 float f32;
91 } safe_copy;
92
93 safe_copy.raw = __le32_to_cpu(f.val);
94 return safe_copy.f32;
95}
96
97static inline __lef32
98__cpu_to_lef32(float f)
99{
100 union {
101 uint32_t wire;
102 float f32;
103 } safe_copy;
104
105 safe_copy.f32 = f;
106
107 return XRT_C11_COMPOUND(__lef32){.val = __cpu_to_le32(safe_copy.wire)};
108}
109
110static inline float
111__bef32_to_cpu(__bef32 f)
112{
113 union {
114 uint32_t raw;
115 float f32;
116 } safe_copy;
117
118 safe_copy.raw = __be32_to_cpu(f.val);
119 return safe_copy.f32;
120}
121
122static inline __bef32
123__cpu_to_bef32(float f)
124{
125 union {
126 uint32_t wire;
127 float f32;
128 } safe_copy;
129
130 safe_copy.f32 = f;
131 return XRT_C11_COMPOUND(__bef32){.val = __cpu_to_be32(safe_copy.wire)};
132}
133
134/*
135 * 64-bit float
136 */
137
138/*!
139 * Little endian 64-bit float wrapper struct.
140 */
141typedef struct
142{
143 __le64 val;
144} __lef64;
145
146/*!
147 * Big endian 64-bit float wrapper struct.
148 */
149typedef struct
150{
151 __be64 val;
152} __bef64;
153
154static inline double
155__lef64_to_cpu(__lef64 f)
156{
157 union {
158 uint64_t raw;
159 double f64;
160 } safe_copy;
161
162 safe_copy.raw = __le64_to_cpu(f.val);
163 return safe_copy.f64;
164}
165
166static inline __lef64
167__cpu_to_lef64(double f)
168{
169 union {
170 uint64_t wire;
171 double f64;
172 } safe_copy;
173
174 safe_copy.f64 = f;
175
176 return XRT_C11_COMPOUND(__lef64){.val = __cpu_to_le64(safe_copy.wire)};
177}
178
179static inline double
180__bef64_to_cpu(__bef64 f)
181{
182 union {
183 uint64_t raw;
184 double f64;
185 } safe_copy;
186
187 safe_copy.raw = __be64_to_cpu(f.val);
188 return safe_copy.f64;
189}
190
191static inline __bef64
192__cpu_to_bef64(double f)
193{
194 union {
195 uint64_t wire;
196 double f64;
197 } safe_copy;
198
199 safe_copy.f64 = f;
200 return XRT_C11_COMPOUND(__bef64){.val = __cpu_to_be64(safe_copy.wire)};
201}
202
203/*
204 *
205 * Vec2
206 *
207 */
208
210{
211 __lef32 x;
212 __lef32 y;
213};
214
215static inline struct xrt_vec2
216__levec2_to_cpu(struct __levec2 v)
217{
218 return XRT_C11_COMPOUND(struct xrt_vec2){
219 .x = __lef32_to_cpu(v.x),
220 .y = __lef32_to_cpu(v.y),
221 };
222}
223
224static inline struct __levec2
225__cpu_to_levec2(struct xrt_vec2 v)
226{
227 return XRT_C11_COMPOUND(struct __levec2){
228 .x = __cpu_to_lef32(v.x),
229 .y = __cpu_to_lef32(v.y),
230 };
231}
232
233/*
234 *
235 * Vec3
236 *
237 */
238
240{
241 __lef32 x;
242 __lef32 y;
243 __lef32 z;
244};
245
246static inline struct xrt_vec3
247__levec3_to_cpu(struct __levec3 v)
248{
249 return XRT_C11_COMPOUND(struct xrt_vec3){
250 .x = __lef32_to_cpu(v.x),
251 .y = __lef32_to_cpu(v.y),
252 .z = __lef32_to_cpu(v.z),
253 };
254}
255
256static inline struct __levec3
257__cpu_to_levec3(struct xrt_vec3 v)
258{
259 return XRT_C11_COMPOUND(struct __levec3){
260 .x = __cpu_to_lef32(v.x),
261 .y = __cpu_to_lef32(v.y),
262 .z = __cpu_to_lef32(v.z),
263 };
264}
Big endian 32-bit float wrapper struct.
Definition xrt_byte_order.h:81
Big endian 64-bit float wrapper struct.
Definition xrt_byte_order.h:150
Little endian 32-bit float wrapper struct.
Definition xrt_byte_order.h:73
Little endian 64-bit float wrapper struct.
Definition xrt_byte_order.h:142
Definition xrt_byte_order.h:210
Definition xrt_byte_order.h:240
A 2 element vector with single floats.
Definition xrt_defines.h:268
A 3 element vector with single floats.
Definition xrt_defines.h:289
Header holding common defines.
Common defines and enums for XRT.