A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 141 lines 4.3 kB view raw
1/* 2 * xrick/system/basic_funcs.h 3 * 4 * Copyright (C) 2008-2014 Pierluigi Vicinanza. All rights reserved. 5 * 6 * The use and distribution terms for this software are contained in the file 7 * named README, which can be found in the root of this distribution. By 8 * using this software in any fashion, you are agreeing to be bound by the 9 * terms of this license. 10 * 11 * You must not remove this notice, or any other, from this software. 12 */ 13 14#ifndef _BASIC_FUNCS_H 15#define _BASIC_FUNCS_H 16 17#include "xrick/system/basic_types.h" 18#include "xrick/system/system.h" 19 20#ifdef __WIN32__ 21/* Windows is little endian only */ 22# define __ORDER_LITTLE_ENDIAN__ 1234 23# define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ 24# define USE_DEFAULT_ENDIANNESS_FUNCTIONS 25# include <stdlib.h> /* _byteswap_XXX */ 26 27#elif defined(ROCKBOX) 28/* Rockbox*/ 29# include "plugin.h" 30# define __ORDER_LITTLE_ENDIAN__ 1234 31# define __ORDER_BIG_ENDIAN__ 4321 32# ifdef ROCKBOX_BIG_ENDIAN 33# define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__ 34# else 35# define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ 36# endif 37 38#elif (defined(__FreeBSD__) && __FreeBSD_version >= 470000) || defined(__OpenBSD__) || defined(__NetBSD__) 39/* *BSD */ 40# include <sys/endian.h> 41# define __ORDER_BIG_ENDIAN__ BIG_ENDIAN 42# define __ORDER_LITTLE_ENDIAN__ LITTLE_ENDIAN 43# define __BYTE_ORDER__ BYTE_ORDER 44 45#elif (defined(BSD) && (BSD >= 199103)) || defined(__MacOSX__) 46/* more BSD */ 47# include <machine/endian.h> 48# define __ORDER_BIG_ENDIAN__ BIG_ENDIAN 49# define __ORDER_LITTLE_ENDIAN__ LITTLE_ENDIAN 50# define __BYTE_ORDER__ BYTE_ORDER 51 52#elif defined(__linux__) /*|| defined (__BEOS__)*/ 53/* Linux, BeOS */ 54# include <endian.h> 55# define betoh16(x) be16toh(x) 56# define letoh16(x) le16toh(x) 57# define betoh32(x) be32toh(x) 58# define letoh32(x) le32toh(x) 59 60#else 61/* shall we just '#include <endian.h>'? */ 62# define USE_DEFAULT_ENDIANNESS_FUNCTIONS 63 64#endif /* __WIN32__ */ 65 66/* define default endianness */ 67#ifndef __ORDER_LITTLE_ENDIAN__ 68# define __ORDER_LITTLE_ENDIAN__ 1234 69#endif 70 71#ifndef __ORDER_BIG_ENDIAN__ 72# define __ORDER_BIG_ENDIAN__ 4321 73#endif 74 75#ifndef __BYTE_ORDER__ 76# warning "Byte order not defined on your system, assuming little endian!" 77# define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ 78#endif 79 80/* provide default endianness functions */ 81#ifdef USE_DEFAULT_ENDIANNESS_FUNCTIONS 82 83# define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) 84 85inline uint32_t swap32(uint32_t x) 86{ 87# ifdef _MSC_VER 88 return _byteswap_ulong(x); 89# elif (GCC_VERSION > 40300) || defined(__clang__) 90 return __builtin_bswap32(x); 91# else 92 return (x >> 24) | 93 ((x >> 8) & 0x0000FF00) | 94 ((x << 8) & 0x00FF0000) | 95 (x << 24); 96# endif /* _MSC_VER */ 97} 98 99inline uint16_t swap16(uint16_t x) 100{ 101# ifdef _MSC_VER 102 return _byteswap_ushort(x); 103# elif (GCC_VERSION > 40800) || defined(__clang__) 104 return __builtin_bswap16(x); 105# else 106 return (x << 8)|(x >> 8); 107# endif /* _MSC_VER */ 108} 109 110# if (__BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__) 111inline uint16_t htobe16(uint16_t host) { return swap16(host); } 112inline uint16_t htole16(uint16_t host) { return host; } 113inline uint16_t betoh16(uint16_t big_endian) { return swap16(big_endian); } 114inline uint16_t letoh16(uint16_t little_endian) { return little_endian; } 115 116inline uint32_t htobe32(uint32_t host) { return swap32(host); } 117inline uint32_t htole32(uint32_t host) { return host; } 118inline uint32_t betoh32(uint32_t big_endian) { return swap32(big_endian); } 119inline uint32_t letoh32(uint32_t little_endian) { return little_endian; } 120 121# elif (__BYTE_ORDER__==__ORDER_BIG_ENDIAN__) 122inline uint16_t htobe16(uint16_t host) { return host; } 123inline uint16_t htole16(uint16_t host) { return swap16(host); } 124inline uint16_t betoh16(uint16_t big_endian) { return big_endian; } 125inline uint16_t letoh16(uint16_t little_endian) { return swap16(little_endian); } 126 127inline uint32_t htobe32(uint32_t host) { return host; } 128inline uint32_t htole32(uint32_t host) { return swap32(host); } 129inline uint32_t betoh32(uint32_t big_endian) { return big_endian; } 130inline uint32_t letoh32(uint32_t little_endian) { return swap32(little_endian); } 131 132# else 133# error "Unknown/unsupported byte order!" 134 135# endif 136 137#endif /* USE_DEFAULT_ENDIANNESS_FUNCTIONS */ 138 139#endif /* ndef _BASIC_FUNCS_H */ 140 141/* eof */