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