Monado OpenXR Runtime
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
14#include <stdint.h>
15
16#ifdef __linux__
17
18// On Linux, all these conversion functions are defined for both endians
19#include <asm/byteorder.h>
20
21#elif defined(XRT_BIG_ENDIAN)
22
23#error "@todo: Add byte order constants and functions for this OS or big endian machines."
24
25#else
26
27#define __be64 uint64_t
28#define __be32 uint32_t
29#define __be16 uint16_t
30
31#define __be16_to_cpu(x) ((((uint16_t)x & (uint16_t)0x00FFU) << 8) | (((uint16_t)x & (uint16_t)0xFF00U) >> 8))
32#define __cpu_to_be16(x) __be16_to_cpu(x)
33
34#define __be32_to_cpu(x) \
35 ((((uint32_t)x & (uint32_t)0x000000FFUL) << 24) | (((uint32_t)x & (uint32_t)0x0000FF00UL) << 8) | \
36 (((uint32_t)x & (uint32_t)0x00FF0000UL) >> 8) | (((uint32_t)x & (uint32_t)0xFF000000UL) >> 24))
37#define __cpu_to_be32(x) __be32_to_cpu(x)
38
39#define __be64_to_cpu(x) \
40 ((((uint64_t)x & (uint64_t)0x00000000000000FFULL) << 56) | \
41 (((uint64_t)x & (uint64_t)0x000000000000FF00ULL) << 40) | \
42 (((uint64_t)x & (uint64_t)0x0000000000FF0000ULL) << 24) | \
43 (((uint64_t)x & (uint64_t)0x00000000FF000000ULL) << 8) | \
44 (((uint64_t)x & (uint64_t)0x000000FF00000000ULL) >> 8) | \
45 (((uint64_t)x & (uint64_t)0x0000FF0000000000ULL) >> 24) | \
46 (((uint64_t)x & (uint64_t)0x00FF000000000000ULL) >> 40) | \
47 (((uint64_t)x & (uint64_t)0xFF00000000000000ULL) >> 56)) |
48#define __cpu_to_be64(x) __be64_to_cpu(x)
49
50#define __le64 uint64_t
51#define __le32 uint32_t
52#define __le16 uint16_t
53#define __u8 uint8_t
54#define __s8 int8_t
55#define __cpu_to_le16
56#define __le16_to_cpu
57#define __cpu_to_le32
58#define __le32_to_cpu
59#define __cpu_to_le64
60#define __le64_to_cpu
61
62#endif
63
64/*!
65 * Little endian 32-bit float wrapper struct.
66 */
67typedef struct
68{
69 __le32 val;
70} __lef32;
71
72/*!
73 * Big endian 32-bit float wrapper struct.
74 */
75typedef struct
76{
77 __be32 val;
78} __bef32;
79
80static inline float
81__lef32_to_cpu(__lef32 f)
82{
83 union {
84 uint32_t raw;
85 float f32;
86 } safe_copy;
87
88 safe_copy.raw = __le32_to_cpu(f.val);
89 return safe_copy.f32;
90}
91
92static inline __lef32
93__cpu_to_lef32(float f)
94{
95 union {
96 uint32_t wire;
97 float f32;
98 } safe_copy;
99
100 safe_copy.f32 = f;
101 return (__lef32){.val = __cpu_to_le32(safe_copy.wire)};
102}
103
104static inline float
105__bef32_to_cpu(__bef32 f)
106{
107 union {
108 uint32_t raw;
109 float f32;
110 } safe_copy;
111
112 safe_copy.raw = __be32_to_cpu(f.val);
113 return safe_copy.f32;
114}
115
116static inline __bef32
117__cpu_to_bef32(float f)
118{
119 union {
120 uint32_t wire;
121 float f32;
122 } safe_copy;
123
124 safe_copy.f32 = f;
125 return (__bef32){.val = __cpu_to_be32(safe_copy.wire)};
126}
127
128/*
129 *
130 * Vec2
131 *
132 */
133
135{
136 __lef32 x;
137 __lef32 y;
138};
139
140static inline struct xrt_vec2
141__levec2_to_cpu(struct __levec2 v)
142{
143 return (struct xrt_vec2){
144 .x = __lef32_to_cpu(v.x),
145 .y = __lef32_to_cpu(v.y),
146 };
147}
148
149static inline struct __levec2
150__cpu_to_levec2(struct xrt_vec2 v)
151{
152 return (struct __levec2){
153 .x = __cpu_to_lef32(v.x),
154 .y = __cpu_to_lef32(v.y),
155 };
156}
157
158/*
159 *
160 * Vec3
161 *
162 */
163
165{
166 __lef32 x;
167 __lef32 y;
168 __lef32 z;
169};
170
171static inline struct xrt_vec3
172__levec3_to_cpu(struct __levec3 v)
173{
174 return (struct xrt_vec3){
175 .x = __lef32_to_cpu(v.x),
176 .y = __lef32_to_cpu(v.y),
177 .z = __lef32_to_cpu(v.z),
178 };
179}
180
181static inline struct __levec3
182__cpu_to_levec3(struct xrt_vec3 v)
183{
184 return (struct __levec3){
185 .x = __cpu_to_lef32(v.x),
186 .y = __cpu_to_lef32(v.y),
187 .z = __cpu_to_lef32(v.z),
188 };
189}
Big endian 32-bit float wrapper struct.
Definition: xrt_byte_order.h:76
Little endian 32-bit float wrapper struct.
Definition: xrt_byte_order.h:68
Definition: xrt_byte_order.h:135
Definition: xrt_byte_order.h:165
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.