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