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