openalbridge/endianness.h
changeset 2417 f7ed1ea25050
parent 2416 3278cf087339
child 2418 538a777f90c4
equal deleted inserted replaced
2416:3278cf087339 2417:f7ed1ea25050
     1 /*
       
     2  * OpenAL Bridge - a simple portable library for OpenAL interface
       
     3  * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU Lesser General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU Lesser General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU Lesser General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 #ifndef _OALB_ENDIANNESS_H
       
    20 #define _OALB_ENDIANNESS_H
       
    21 
       
    22 #include "globals.h"
       
    23 
       
    24 
       
    25 #ifdef __CPLUSPLUS
       
    26 extern "C" {
       
    27 #endif 
       
    28 
       
    29 
       
    30 /* check compiler requirements */
       
    31 #if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
       
    32 #error Do not know the endianess of this architecture
       
    33 #endif
       
    34 
       
    35 
       
    36 /* use byteswap macros from the host system, hopefully optimized ones ;-) 
       
    37  * or define our own version, simple, stupid, straight-forward... */
       
    38 #ifdef HAVE_BYTESWAP_H
       
    39 #include <byteswap.h>
       
    40 #else        
       
    41 #define bswap_16(x)	((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8))
       
    42 #define bswap_32(x)	((((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8)  | \
       
    43 (((x) & 0x0000FF00) << 8)  | (((x) & 0x000000FF) << 24) )
       
    44 #endif /* HAVE_BYTESWAP_H */
       
    45 
       
    46 
       
    47 /* swap numbers accordingly to architecture automatically */
       
    48 #ifdef __LITTLE_ENDIAN__
       
    49 #define ENDIAN_LITTLE_32(x) x
       
    50 #define ENDIAN_BIG_32(x)    bswap_32(x)
       
    51 #define ENDIAN_LITTLE_16(x) x
       
    52 #define ENDIAN_BIG_16(x)    bswap_16(x)
       
    53 #elif __BIG_ENDIAN__
       
    54 #define ENDIAN_LITTLE_32(x) bswap_32(x)
       
    55 #define ENDIAN_BIG_32(x)    x
       
    56 #define ENDIAN_LITTLE_16(x) bswap_16(x)
       
    57 #define ENDIAN_BIG_16(x)    x    
       
    58 #endif
       
    59         
       
    60         
       
    61 #ifdef __CPLUSPLUS
       
    62 }
       
    63 #endif
       
    64 
       
    65 #endif /*_OALB_ENDIANNESS_H*/