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