The open source OpenXR runtime

xrt: Add byte order constants and conversion functions

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2477>

authored by

Beyley Cardellio and committed by
Marge Bot
682d16db dcf8aeba

+102 -12
+1 -1
src/xrt/drivers/hydra/hydra_driver.c
··· 22 22 #include "os/os_hid.h" 23 23 #include "os/os_time.h" 24 24 #include "os/os_threading.h" 25 + #include "xrt/xrt_byte_order.h" 25 26 26 27 #include "math/m_api.h" 27 28 #include "math/m_relation_history.h" ··· 275 276 hydra_read_int16_le(uint8_t **bufptr) 276 277 { 277 278 uint8_t *buf = *bufptr; 278 - //! @todo nothing actually defines XRT_BIG_ENDIAN when needed! 279 279 #ifdef XRT_BIG_ENDIAN 280 280 uint8_t bytes[2] = {buf[1], buf[0]}; 281 281 #else
+1 -1
src/xrt/drivers/rift_s/rift_s_camera.c
··· 15 15 * @author Jan Schmidt <jan@centricular.com> 16 16 * @ingroup drv_rift_s 17 17 */ 18 - #include <asm/byteorder.h> 19 18 #include <string.h> 20 19 21 20 #include "rift_s.h" 22 21 #include "rift_s_camera.h" 23 22 24 23 #include "os/os_threading.h" 24 + #include "xrt/xrt_byte_order.h" 25 25 26 26 #include "xrt/xrt_defines.h" 27 27 #include "xrt/xrt_frame.h"
+3 -2
src/xrt/drivers/vive/vive.h
··· 10 10 11 11 #pragma once 12 12 13 - #include <stdint.h> 14 - #include <asm/byteorder.h> 13 + #include "xrt/xrt_byte_order.h" 15 14 16 15 #include "util/u_debug.h" 17 16 #include "util/u_time.h" 17 + 18 + #include <stdint.h> 18 19 19 20 /* 20 21 *
+3 -1
src/xrt/drivers/vive/vive_lighthouse.c
··· 10 10 // IWYU pragma: no_include <asm/int-ll64.h> 11 11 // IWYU pragma: no_include <linux/byteorder/little_endian.h> 12 12 13 - #include <asm/byteorder.h> 14 13 #include <stdint.h> 15 14 #include <string.h> 16 15 #include <zlib.h> 17 16 #include <stdio.h> 18 17 19 18 #include "math/m_api.h" 19 + 20 + #include "xrt/xrt_byte_order.h" 21 + 20 22 #include "util/u_debug.h" 21 23 #include "util/u_logging.h" 22 24
+3 -2
src/xrt/drivers/vive/vive_protocol.h
··· 10 10 11 11 #pragma once 12 12 13 - #include <asm/byteorder.h> 13 + #include "os/os_hid.h" 14 + #include "xrt/xrt_byte_order.h" 15 + 14 16 #include <stdint.h> 15 - #include "os/os_hid.h" 16 17 17 18 #define VIVE_CONTROLLER_BUTTON_REPORT_ID 0x01 18 19
+7 -4
src/xrt/drivers/wmr/wmr_camera.c
··· 10 10 * @author Jakob Bornecrantz <jakob@collabora.com> 11 11 * @ingroup drv_wmr 12 12 */ 13 - #include <asm/byteorder.h> 14 - #include <libusb.h> 15 - #include <stdlib.h> 16 - #include <assert.h> 17 13 18 14 #include "math/m_api.h" 15 + 19 16 #include "os/os_threading.h" 17 + #include "xrt/xrt_byte_order.h" 18 + 20 19 #include "util/u_autoexpgain.h" 21 20 #include "util/u_debug.h" 22 21 #include "util/u_var.h" ··· 27 26 #include "wmr_config.h" 28 27 #include "wmr_protocol.h" 29 28 #include "wmr_camera.h" 29 + 30 + #include <libusb.h> 31 + #include <stdlib.h> 32 + #include <assert.h> 30 33 31 34 //! Specifies whether the user wants to enable autoexposure from the start. 32 35 DEBUG_GET_ONCE_BOOL_OPTION(wmr_autoexposure, "WMR_AUTOEXPOSURE", true)
+1 -1
src/xrt/drivers/wmr/wmr_controller_protocol.h
··· 10 10 11 11 #pragma once 12 12 13 - #include <asm/byteorder.h> 13 + #include "xrt/xrt_byte_order.h" 14 14 15 15 #include "wmr_protocol.h" 16 16
+62
src/xrt/include/xrt/xrt_byte_order.h
··· 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
+21
src/xrt/include/xrt/xrt_compiler.h
··· 24 24 #include "xrt_windows.h" 25 25 #endif // _MSC_VER 26 26 27 + #if (defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN) || \ 28 + (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \ 29 + (defined(__STDC_ENDIAN_NATIVE__) && __STDC_ENDIAN_NATIVE__ == __STDC_ENDIAN_BIG__) || defined(__BIG_ENDIAN__) || \ 30 + defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || defined(_MIBSEB) || defined(__MIBSEB) || \ 31 + defined(__MIBSEB__) 32 + 33 + #define XRT_BIG_ENDIAN 34 + 35 + #elif (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \ 36 + (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || \ 37 + (defined(__STDC_ENDIAN_NATIVE__) && __STDC_ENDIAN_NATIVE__ == __STDC_ENDIAN_LITTLE__) || \ 38 + defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \ 39 + defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || defined(__x86_64__) || defined(_M_X64) || \ 40 + defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86) 41 + 42 + #undef XRT_BIG_ENDIAN 43 + 44 + #else 45 + #error "@todo: Unable to determine current architecture." 46 + #endif 47 + 27 48 /*! 28 49 * Array size helper. 29 50 */