| 1 |
/*=========================================================================== |
| 2 |
* |
| 3 |
* PUBLIC DOMAIN NOTICE |
| 4 |
* National Center for Biotechnology Information |
| 5 |
* |
| 6 |
* This software/database is a "United States Government Work" under the |
| 7 |
* terms of the United States Copyright Act. It was written as part of |
| 8 |
* the author's official duties as a United States Government employee and |
| 9 |
* thus cannot be copyrighted. This software/database is freely available |
| 10 |
* to the public for use. The National Library of Medicine and the U.S. |
| 11 |
* Government have not placed any restriction on its use or reproduction. |
| 12 |
* |
| 13 |
* Although all reasonable efforts have been taken to ensure the accuracy |
| 14 |
* and reliability of the software and data, the NLM and the U.S. |
| 15 |
* Government do not and cannot warrant the performance or results that |
| 16 |
* may be obtained by using this software or data. The NLM and the U.S. |
| 17 |
* Government disclaim all warranties, express or implied, including |
| 18 |
* warranties of performance, merchantability or fitness for any particular |
| 19 |
* purpose. |
| 20 |
* |
| 21 |
* Please cite the author in any work or product based on this material. |
| 22 |
* |
| 23 |
* =========================================================================== |
| 24 |
* |
| 25 |
*/ |
| 26 |
|
| 27 |
#ifndef _h_byteswap_ |
| 28 |
#define _h_byteswap_ |
| 29 |
|
| 30 |
#ifdef _BYTESWAP_H |
| 31 |
#warning "GNU byteswap.h being used" |
| 32 |
#else |
| 33 |
#define _BYTESWAP_H 1234 |
| 34 |
|
| 35 |
#include <stdint.h> |
| 36 |
|
| 37 |
#ifdef __cplusplus |
| 38 |
extern "C" { |
| 39 |
#endif |
| 40 |
|
| 41 |
/* perform single instruction byte swap */ |
| 42 |
static __inline__ uint16_t bswap_16 ( uint16_t i ) |
| 43 |
{ |
| 44 |
register uint16_t rtn; |
| 45 |
__asm__ |
| 46 |
( |
| 47 |
"rorw $8, %w0" |
| 48 |
: "=r" ( rtn ) |
| 49 |
: "0" ( i ) |
| 50 |
: "cc" |
| 51 |
); |
| 52 |
return rtn; |
| 53 |
} |
| 54 |
|
| 55 |
/* perform single instruction byte swap */ |
| 56 |
static __inline__ uint32_t bswap_32 ( uint32_t i ) |
| 57 |
{ |
| 58 |
register uint32_t rtn; |
| 59 |
__asm__ |
| 60 |
( |
| 61 |
"bswap %0" |
| 62 |
: "=r" ( rtn ) |
| 63 |
: "0" ( i ) |
| 64 |
: "cc" |
| 65 |
); |
| 66 |
return rtn; |
| 67 |
} |
| 68 |
|
| 69 |
/* perform multi-instruction byte swap */ |
| 70 |
static __inline__ uint64_t bswap_64 ( uint64_t i ) |
| 71 |
{ |
| 72 |
union |
| 73 |
{ |
| 74 |
uint64_t ll; |
| 75 |
uint32_t l [ 2 ]; |
| 76 |
} in, rtn; |
| 77 |
in . ll = i; |
| 78 |
rtn . l [ 0 ] = bswap_32 ( in . l [ 1 ] ); |
| 79 |
rtn . l [ 1 ] = bswap_32 ( in . l [ 0 ] ); |
| 80 |
return rtn . ll; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
#ifdef __cplusplus |
| 85 |
} |
| 86 |
#endif |
| 87 |
|
| 88 |
#endif /* _BYTESWAP_H */ |
| 89 |
#endif /* _h_byteswap_ */ |