SphinxBase  5prealpha
byteorder.h
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 1999-2001 Carnegie Mellon University. All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * This work was supported in part by funding from the Defense Advanced
19  * Research Projects Agency and the National Science Foundation of the
20  * United States of America, and the CMU Sphinx Speech Consortium.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * ====================================================================
35  *
36  */
37 
38 /*
39  * byteorder.h -- Byte swapping ordering macros.
40  *
41  * **********************************************
42  * CMU ARPA Speech Project
43  *
44  * Copyright (c) 1996 Carnegie Mellon University.
45  * ALL RIGHTS RESERVED.
46  * **********************************************
47  *
48  * HISTORY
49  *
50  * $Log: byteorder.h,v $
51  * Revision 1.8 2005/09/01 21:09:54 dhdfu
52  * Really, actually, truly consolidate byteswapping operations into
53  * byteorder.h. Where unconditional byteswapping is needed, SWAP_INT32()
54  * and SWAP_INT16() are to be used. The WORDS_BIGENDIAN macro from
55  * autoconf controls the functioning of the conditional swap macros
56  * (SWAP_?[LW]) whose names and semantics have been regularized.
57  * Private, adhoc macros have been removed.
58  *
59  */
60 
61 #ifndef __S2_BYTEORDER_H__
62 #define __S2_BYTEORDER_H__ 1
63 
64 /* Macro to byteswap an int16 variable. x = ptr to variable */
65 #define SWAP_INT16(x) *(x) = ((0x00ff & (*(x))>>8) | (0xff00 & (*(x))<<8))
66 
67 /* Macro to byteswap an int32 variable. x = ptr to variable */
68 #define SWAP_INT32(x) *(x) = ((0x000000ff & (*(x))>>24) | \
69  (0x0000ff00 & (*(x))>>8) | \
70  (0x00ff0000 & (*(x))<<8) | \
71  (0xff000000 & (*(x))<<24))
72 
73 /* Macro to byteswap a float32 variable. x = ptr to variable */
74 #define SWAP_FLOAT32(x) SWAP_INT32((int32 *) x)
75 
76 /* Macro to byteswap a float64 variable. x = ptr to variable */
77 #define SWAP_FLOAT64(x) { int *low = (int *) (x), *high = (int *) (x) + 1,\
78  temp;\
79  SWAP_INT32(low); SWAP_INT32(high);\
80  temp = *low; *low = *high; *high = temp;}
81 
82 #ifdef WORDS_BIGENDIAN
83 #define SWAP_BE_64(x)
84 #define SWAP_BE_32(x)
85 #define SWAP_BE_16(x)
86 #define SWAP_LE_64(x) SWAP_FLOAT64(x)
87 #define SWAP_LE_32(x) SWAP_INT32(x)
88 #define SWAP_LE_16(x) SWAP_INT16(x)
89 #else
90 #define SWAP_LE_64(x)
91 #define SWAP_LE_32(x)
92 #define SWAP_LE_16(x)
93 #define SWAP_BE_64(x) SWAP_FLOAT64(x)
94 #define SWAP_BE_32(x) SWAP_INT32(x)
95 #define SWAP_BE_16(x) SWAP_INT16(x)
96 #endif
97 
98 #endif