cannam@94: /* cannam@94: * Copyright (C) 2004 Steve Harris cannam@94: * cannam@94: * This program is free software; you can redistribute it and/or cannam@94: * modify it under the terms of the GNU Lesser General Public License cannam@94: * as published by the Free Software Foundation; either version 2.1 cannam@94: * of the License, or (at your option) any later version. cannam@94: * cannam@94: * This program is distributed in the hope that it will be useful, cannam@94: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@94: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@94: * GNU Lesser General Public License for more details. cannam@94: * cannam@94: * $Id$ cannam@94: */ cannam@94: cannam@94: #ifndef LO_ENDIAN_H cannam@94: #define LO_ENDIAN_H cannam@94: cannam@94: #include cannam@94: #include cannam@94: cannam@94: #ifdef WIN32 cannam@94: #include cannam@94: #include cannam@94: #else cannam@94: #include cannam@94: #endif cannam@94: cannam@94: #ifdef __cplusplus cannam@94: extern "C" { cannam@94: #endif cannam@94: cannam@94: #define lo_swap16(x) htons(x) cannam@94: cannam@94: #define lo_swap32(x) htonl(x) cannam@94: cannam@94: /* These macros come from the Linux kernel */ cannam@94: cannam@94: #ifndef lo_swap16 cannam@94: #define lo_swap16(x) \ cannam@94: ({ \ cannam@94: uint16_t __x = (x); \ cannam@94: ((uint16_t)( \ cannam@94: (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \ cannam@94: (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \ cannam@94: }) cannam@94: #warning USING UNOPTIMISED ENDIAN STUFF cannam@94: #endif cannam@94: cannam@94: #ifndef lo_swap32 cannam@94: #define lo_swap32(x) \ cannam@94: ({ \ cannam@94: uint32_t __x = (x); \ cannam@94: ((uint32_t)( \ cannam@94: (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \ cannam@94: (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \ cannam@94: (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \ cannam@94: (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \ cannam@94: }) cannam@94: #endif cannam@94: cannam@94: #if 0 cannam@94: #ifndef lo_swap64 cannam@94: #define lo_swap64(x) \ cannam@94: ({ \ cannam@94: uint64_t __x = (x); \ cannam@94: ((uint64_t)( \ cannam@94: (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \ cannam@94: (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ cannam@94: (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ cannam@94: (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ cannam@94: (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ cannam@94: (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ cannam@94: (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ cannam@94: (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \ cannam@94: }) cannam@94: #endif cannam@94: #else cannam@94: cannam@94: typedef union { cannam@94: uint64_t all; cannam@94: struct { cannam@94: uint32_t a; cannam@94: uint32_t b; cannam@94: } part; cannam@94: } lo_split64; cannam@94: cannam@94: static inline uint64_t lo_swap64(uint64_t x) cannam@94: { cannam@94: lo_split64 in, out; cannam@94: cannam@94: in.all = x; cannam@94: out.part.a = lo_swap32(in.part.b); cannam@94: out.part.b = lo_swap32(in.part.a); cannam@94: cannam@94: return out.all; cannam@94: } cannam@94: #endif cannam@94: cannam@94: /* Host to OSC and OSC to Host conversion macros */ cannam@94: cannam@94: #if 0 cannam@94: #define lo_htoo16(x) (x) cannam@94: #define lo_htoo32(x) (x) cannam@94: #define lo_htoo64(x) (x) cannam@94: #define lo_otoh16(x) (x) cannam@94: #define lo_otoh32(x) (x) cannam@94: #define lo_otoh64(x) (x) cannam@94: #else cannam@94: #define lo_htoo16 lo_swap16 cannam@94: #define lo_htoo32 lo_swap32 cannam@94: #define lo_htoo64 lo_swap64 cannam@94: #define lo_otoh16 lo_swap16 cannam@94: #define lo_otoh32 lo_swap32 cannam@94: #define lo_otoh64 lo_swap64 cannam@94: #endif cannam@94: cannam@94: #ifdef __cplusplus cannam@94: } cannam@94: #endif cannam@94: cannam@94: #endif cannam@94: cannam@94: /* vi:set ts=8 sts=4 sw=4: */