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