Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.     SDL - Simple DirectMedia Layer
  3.     Copyright (C) 1997-2004 Sam Lantinga
  4.  
  5.     This library is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU Library General Public
  7.     License as published by the Free Software Foundation; either
  8.     version 2 of the License, or (at your option) any later version.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public
  16.     License along with this library; if not, write to the Free
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     Sam Lantinga
  20.     slouken@libsdl.org
  21. */
  22.  
  23. #ifdef SAVE_RCSID
  24. static char rcsid =
  25.  "@(#) $Id: SDL_endian.h,v 1.6 2004/01/04 16:49:07 slouken Exp $";
  26. #endif
  27.  
  28. /* Functions for reading and writing endian-specific values */
  29.  
  30. #ifndef _SDL_endian_h
  31. #define _SDL_endian_h
  32.  
  33. /* These functions read and write data of the specified endianness,
  34.    dynamically translating to the host machine endianness.
  35.  
  36.    e.g.: If you want to read a 16 bit value on big-endian machine from
  37.          an open file containing little endian values, you would use:
  38.                 value = SDL_ReadLE16(rp);
  39.          Note that the read/write functions use SDL_RWops pointers
  40.          instead of FILE pointers.  This allows you to read and write
  41.          endian values from large chunks of memory as well as files
  42.          and other data sources.
  43. */
  44.  
  45. #include <stdio.h>
  46.  
  47. #include "SDL_types.h"
  48. #include "SDL_rwops.h"
  49. #include "SDL_byteorder.h"
  50.  
  51. #include "begin_code.h"
  52. /* Set up for C function definitions, even when using C++ */
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56.  
  57. /* The macros used to swap values */
  58. /* Try to use superfast macros on systems that support them */
  59. #ifndef __MENUETOS__
  60. #ifdef linux
  61. #include <asm/byteorder.h>
  62. #ifdef __arch__swab16
  63. #define SDL_Swap16  __arch__swab16
  64. #endif
  65. #ifdef __arch__swab32
  66. #define SDL_Swap32  __arch__swab32
  67. #endif
  68. #endif /* linux */
  69. #endif
  70.  
  71. /* Use inline functions for compilers that support them, and static
  72.    functions for those that do not.  Because these functions become
  73.    static for compilers that do not support inline functions, this
  74.    header should only be included in files that actually use them.
  75. */
  76. #ifndef SDL_Swap16
  77. static __inline__ Uint16 SDL_Swap16(Uint16 D) {
  78.         return((D<<8)|(D>>8));
  79. }
  80. #endif
  81. #ifndef SDL_Swap32
  82. static __inline__ Uint32 SDL_Swap32(Uint32 D) {
  83.         return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
  84. }
  85. #endif
  86. #ifdef SDL_HAS_64BIT_TYPE
  87. #ifndef SDL_Swap64
  88. static __inline__ Uint64 SDL_Swap64(Uint64 val) {
  89.         Uint32 hi, lo;
  90.  
  91.         /* Separate into high and low 32-bit values and swap them */
  92.         lo = (Uint32)(val&0xFFFFFFFF);
  93.         val >>= 32;
  94.         hi = (Uint32)(val&0xFFFFFFFF);
  95.         val = SDL_Swap32(lo);
  96.         val <<= 32;
  97.         val |= SDL_Swap32(hi);
  98.         return(val);
  99. }
  100. #endif
  101. #else
  102. #ifndef SDL_Swap64
  103. /* This is mainly to keep compilers from complaining in SDL code.
  104.    If there is no real 64-bit datatype, then compilers will complain about
  105.    the fake 64-bit datatype that SDL provides when it compiles user code.
  106. */
  107. #define SDL_Swap64(X)   (X)
  108. #endif
  109. #endif /* SDL_HAS_64BIT_TYPE */
  110.  
  111.  
  112. /* Byteswap item from the specified endianness to the native endianness */
  113. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  114. #define SDL_SwapLE16(X) (X)
  115. #define SDL_SwapLE32(X) (X)
  116. #define SDL_SwapLE64(X) (X)
  117. #define SDL_SwapBE16(X) SDL_Swap16(X)
  118. #define SDL_SwapBE32(X) SDL_Swap32(X)
  119. #define SDL_SwapBE64(X) SDL_Swap64(X)
  120. #else
  121. #define SDL_SwapLE16(X) SDL_Swap16(X)
  122. #define SDL_SwapLE32(X) SDL_Swap32(X)
  123. #define SDL_SwapLE64(X) SDL_Swap64(X)
  124. #define SDL_SwapBE16(X) (X)
  125. #define SDL_SwapBE32(X) (X)
  126. #define SDL_SwapBE64(X) (X)
  127. #endif
  128.  
  129. /* Read an item of the specified endianness and return in native format */
  130. extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops *src);
  131. extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops *src);
  132. extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops *src);
  133. extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops *src);
  134. extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops *src);
  135. extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops *src);
  136.  
  137. /* Write an item of native format to the specified endianness */
  138. extern DECLSPEC int SDLCALL SDL_WriteLE16(SDL_RWops *dst, Uint16 value);
  139. extern DECLSPEC int SDLCALL SDL_WriteBE16(SDL_RWops *dst, Uint16 value);
  140. extern DECLSPEC int SDLCALL SDL_WriteLE32(SDL_RWops *dst, Uint32 value);
  141. extern DECLSPEC int SDLCALL SDL_WriteBE32(SDL_RWops *dst, Uint32 value);
  142. extern DECLSPEC int SDLCALL SDL_WriteLE64(SDL_RWops *dst, Uint64 value);
  143. extern DECLSPEC int SDLCALL SDL_WriteBE64(SDL_RWops *dst, Uint64 value);
  144.  
  145.  
  146. /* Ends C function definitions when using C++ */
  147. #ifdef __cplusplus
  148. }
  149. #endif
  150. #include "close_code.h"
  151.  
  152. #endif /* _SDL_endian_h */
  153.