1/* 2 * Copyright (c) 2004 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; --- 35 unchanged lines hidden (view full) --- 44// If this is a linux system, lets used the optimized definitions if they exist. 45// If one doesn't exist, we pretty much get what is listed below, so it all 46// works out 47#include <byteswap.h> 48#else 49#include <machine/endian.h> 50#endif 51 |
52#if defined(__APPLE__) 53#include <libkern/OSByteOrder.h> 54#endif 55 |
56//These functions actually perform the swapping for parameters 57//of various bit lengths 58static inline uint64_t 59swap_byte64(uint64_t x) 60{ 61#if defined(linux) 62 return bswap_64(x); |
63#elif defined(__APPLE__) 64 return OSSwapInt64(x); |
65#else 66 return (uint64_t)((((uint64_t)(x) & 0xff) << 56) | 67 ((uint64_t)(x) & 0xff00ULL) << 40 | 68 ((uint64_t)(x) & 0xff0000ULL) << 24 | 69 ((uint64_t)(x) & 0xff000000ULL) << 8 | 70 ((uint64_t)(x) & 0xff00000000ULL) >> 8 | 71 ((uint64_t)(x) & 0xff0000000000ULL) >> 24 | 72 ((uint64_t)(x) & 0xff000000000000ULL) >> 40 | 73 ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) ; 74#endif 75} 76 77static inline uint32_t 78swap_byte32(uint32_t x) 79{ 80#if defined(linux) 81 return bswap_32(x); |
82#elif defined(__APPLE__) 83 return OSSwapInt32(x); |
84#else 85 return (uint32_t)(((uint32_t)(x) & 0xff) << 24 | 86 ((uint32_t)(x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 | 87 ((uint32_t)(x) & 0xff000000) >> 24); 88#endif 89} 90 91static inline uint16_t 92swap_byte16(uint16_t x) 93{ 94#if defined(linux) 95 return bswap_16(x); |
96#elif defined(__APPLE__) 97 return OSSwapInt16(x); |
98#else 99 return (uint16_t)(((uint16_t)(x) & 0xff) << 8 | 100 ((uint16_t)(x) & 0xff00) >> 8); 101#endif 102} 103 104// This function lets the compiler figure out how to call the 105// swap_byte functions above for different data types. Since the --- 69 unchanged lines hidden --- |