00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ 00002 /* ==================================================================== 00003 * Copyright (c) 1999-2001 Carnegie Mellon University. All rights 00004 * reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in 00015 * the documentation and/or other materials provided with the 00016 * distribution. 00017 * 00018 * This work was supported in part by funding from the Defense Advanced 00019 * Research Projects Agency and the National Science Foundation of the 00020 * United States of America, and the CMU Sphinx Speech Consortium. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 00023 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 00024 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00025 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 00026 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00028 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00029 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00030 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00032 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 * ==================================================================== 00035 * 00036 */ 00037 00038 /* 00039 * byteorder.h -- Byte swapping ordering macros. 00040 * 00041 * ********************************************** 00042 * CMU ARPA Speech Project 00043 * 00044 * Copyright (c) 1996 Carnegie Mellon University. 00045 * ALL RIGHTS RESERVED. 00046 * ********************************************** 00047 * 00048 * HISTORY 00049 * 00050 * $Log: byteorder.h,v $ 00051 * Revision 1.8 2005/09/01 21:09:54 dhdfu 00052 * Really, actually, truly consolidate byteswapping operations into 00053 * byteorder.h. Where unconditional byteswapping is needed, SWAP_INT32() 00054 * and SWAP_INT16() are to be used. The WORDS_BIGENDIAN macro from 00055 * autoconf controls the functioning of the conditional swap macros 00056 * (SWAP_?[LW]) whose names and semantics have been regularized. 00057 * Private, adhoc macros have been removed. 00058 * 00059 */ 00060 00061 #ifndef __S2_BYTEORDER_H__ 00062 #define __S2_BYTEORDER_H__ 1 00063 00064 /* Macro to byteswap an int16 variable. x = ptr to variable */ 00065 #define SWAP_INT16(x) *(x) = ((0x00ff & (*(x))>>8) | (0xff00 & (*(x))<<8)) 00066 00067 /* Macro to byteswap an int32 variable. x = ptr to variable */ 00068 #define SWAP_INT32(x) *(x) = ((0x000000ff & (*(x))>>24) | \ 00069 (0x0000ff00 & (*(x))>>8) | \ 00070 (0x00ff0000 & (*(x))<<8) | \ 00071 (0xff000000 & (*(x))<<24)) 00072 00073 /* Macro to byteswap a float32 variable. x = ptr to variable */ 00074 #define SWAP_FLOAT32(x) SWAP_INT32((int32 *) x) 00075 00076 /* Macro to byteswap a float64 variable. x = ptr to variable */ 00077 #define SWAP_FLOAT64(x) { int *low = (int *) (x), *high = (int *) (x) + 1,\ 00078 temp;\ 00079 SWAP_INT32(low); SWAP_INT32(high);\ 00080 temp = *low; *low = *high; *high = temp;} 00081 00082 /* Rather confusing backwards compatibility macros for dealing with 00083 * explicitly big or little-endian data. */ 00084 #ifdef WORDS_BIGENDIAN 00085 #define SWAP_BE_64(x) 00086 #define SWAP_BE_32(x) 00087 #define SWAP_BE_16(x) 00088 #define SWAP_LE_64(x) SWAP_FLOAT64(x) 00089 #define SWAP_LE_32(x) SWAP_INT32(x) 00090 #define SWAP_LE_16(x) SWAP_INT16(x) 00091 #else 00092 #define SWAP_LE_64(x) 00093 #define SWAP_LE_32(x) 00094 #define SWAP_LE_16(x) 00095 #define SWAP_BE_64(x) SWAP_FLOAT64(x) 00096 #define SWAP_BE_32(x) SWAP_INT32(x) 00097 #define SWAP_BE_16(x) SWAP_INT16(x) 00098 #endif 00099 00100 #endif