home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / SRC / SWAPS.CPP < prev    next >
C/C++ Source or Header  |  1996-07-08  |  7KB  |  252 lines

  1. /****************************************************************************
  2.     $Id: swaps.cpp 501.0 1995/03/07 12:26:24 RON Exp $
  3.  
  4.     Copyright (c) 1991-95 Tarma Software Research. All rights reserved.
  5.  
  6.     Project:    Tarma Library for C++ V5.0
  7.     Author:    Ron van der Wal
  8.  
  9.     Implementation of byte-swapping functions:
  10.  
  11.     - tlSwap16(int16)
  12.     - tlSwap32(int32)
  13.  
  14.     $Log: swaps.cpp $
  15.     Revision 501.0  1995/03/07 12:26:24  RON
  16.     Updated for TLX 5.01
  17.     Revision 1.10  1995/02/28 15:14:28  RON
  18.     Update for release 012
  19.     Added partial support for SunPro C++ compiler
  20.     Revision 1.9  1995/01/18  19:03:29  ron
  21.     Added inline assembly warning for Win32
  22.  
  23.     Revision 1.8  1995/01/06  15:58:35  ron
  24.     Corrected Revision keyword
  25.  
  26.     Revision 1.7  1994/11/16  15:44:10  ron
  27.     Added module info; rearranged #include directives
  28.  
  29.     Revision 1.6  1994/10/07  17:06:11  ron
  30.     Changed WATCOM support
  31.  
  32.     Revision 1.5  1994/10/05  18:44:08  ron
  33.     Added generic implementations of the swap functions
  34.  
  35.     Revision 1.4  1994/09/28  14:46:45  ron
  36.     Removed Macintosh-style #include references
  37.  
  38.     Revision 1.3  1994/09/27  20:23:13  ron
  39.     Changed path separator from / to \
  40.  
  41.     Revision 1.2  1994/09/26  15:49:04  ron
  42.     Added support for DOS-extended platforms
  43.  
  44.     Revision 1.1  1994/08/16  18:13:17  ron
  45.     Initial revision
  46.  
  47. ****************************************************************************/
  48.  
  49. #if defined(__BORLANDC__) && (defined(__OS2__) || defined(__WIN32__))
  50.   #pragma inline        // Warn compiler for inline assembly for OS/2
  51.   #if __BORLANDC__ <= 0x460    // BC++ OS/2 v1.5 bug work around
  52.     #define _NO_LINK_CHECK    // Do not include global objects
  53.     #define NMODINFO    1
  54.   #endif
  55. #endif
  56.  
  57. #include <tlx\501\_build.h>
  58.  
  59. TLX_MODULE_INFO("$Revision: 501.0 $");
  60.  
  61. //-----    System headers
  62.  
  63. #if defined(__IBMC__) || defined(__IBMCPP__)
  64. #include <stdlib.h>        // For _swab()
  65. #endif
  66.  
  67. #if defined(__BORLANDC__)
  68. #pragma warn -rvl        // Disable "function should return a value"
  69. #elif defined(_MSC_VER)
  70. #pragma warning(disable: 4035)    // Disable "function has no return value"
  71. #elif defined(__WATCOMC__) || defined(__SUNPRO_CC)
  72. #define USE_GENERIC
  73. #endif
  74.  
  75. /*-------------------------------------------------------------------------*/
  76.     int16 _TLXFUNC tlSwap16(int16 aInt)
  77.  
  78. /*  Swaps 2 bytes in a 16-bit integer. Returns the swapped value.
  79.  
  80.     NOTE: Ignore compiler warnings about function not returning a value.
  81.     The value is returned alright, but the compiler doesn't know.
  82. ---------------------------------------------------------------------------*/
  83. {
  84. #ifdef USE_GENERIC
  85.     int16 result;
  86.     WORD_LSB(result) = WORD_MSB(aInt);
  87.     WORD_MSB(result) = WORD_LSB(aInt);
  88.     return result;
  89. #else
  90.   #if OS_DOS    // Assumes Intel 80x86 processor
  91.  
  92.     __asm {
  93.     mov    ax,word ptr aInt
  94.     mov    cl,8        /* rol ax,8 is only for 80286 and later */
  95.     rol    ax,cl
  96.     // Fall through with the result in AX
  97.     }
  98.  
  99.   #elif OS_DOS286 || OS_DOS386 || OS_WIN16    // 80286 or better
  100.  
  101.     __asm {
  102.     mov    ax,word ptr aInt
  103.     rol    ax,8
  104.     // Fall through with the result in AX
  105.     }
  106.  
  107.   #elif OS_WIN32 || OS_WIN32S    // Assumes Intel 80386 or better
  108.  
  109.     __asm {
  110.     mov    ax,word ptr aInt
  111.     rol    ax,8
  112.     // Fall through with the result in AX
  113.     }
  114.     TLX_TODO(Check passing of return value);
  115.  
  116.   #elif OS_OS2    // Assumes Intel 80386 or better
  117.  
  118.   #if defined(__BORLANDC__)
  119.     __asm {
  120.     mov    ax,word ptr aInt
  121.     rol    ax,8
  122.     // Fall through with the result in AX
  123.     }
  124.     TLX_TODO(Check passing of return value);
  125.  
  126.   #elif defined(__IBMC__) || defined(__IBMCPP__)
  127.     int16 result;
  128.     _swab((char *)&aInt, (char *)&result, sizeof(int16));
  129.     return result;
  130.   #else
  131.     #error Unsupported compiler; contact Tarma Software Research
  132.   #endif
  133.  
  134.   #elif OS_MAC    // Assumes Motorola 68k processor
  135.  
  136.     asm {
  137.     MOVE.W     aInt(A6),D0
  138.     ROL.W     #8,D0
  139.     // Fall through with the result in D0
  140.     }
  141.   #else
  142.     #error Unsupported platform; contact Tarma Software Research
  143.   #endif
  144. #endif    // USE_GENERIC
  145. }
  146.  
  147. /*-------------------------------------------------------------------------*/
  148.     int32 _TLXFUNC tlSwap32(int32 aInt)
  149.  
  150. /*  Swaps 4 bytes in a 32-bit integer. Bytes 0 and 3 are swapped, as are
  151.     bytes 1 and 2. Returns the swapped value.
  152.  
  153.     NOTE: Ignore compiler warnings about function not returning a value.
  154.     The value is returned alright, but the compiler doesn't know.
  155. ---------------------------------------------------------------------------*/
  156. {
  157. #ifdef USE_GENERIC
  158.     int32 result;
  159.     DWORD_B0(result) = DWORD_B3(aInt);
  160.     DWORD_B1(result) = DWORD_B2(aInt);
  161.     DWORD_B2(result) = DWORD_B1(aInt);
  162.     DWORD_B3(result) = DWORD_B0(aInt);
  163.     return result;
  164. #else
  165.   #if OS_DOS      // Assumes Intel 80x86 processor
  166.  
  167.     __asm {
  168.     mov    ax,word ptr aInt
  169.     mov    dx,word ptr aInt+2
  170.     mov    cl,8        /*  rol ax,8 is only for 80286 and later */
  171.     rol    ax,cl
  172.     rol    dx,cl
  173.     xchg    ax,dx
  174.     // Fall through with the result in DX:AX
  175.     }
  176.  
  177.   #elif OS_DOS286 || OS_DOS386 || OS_WIN16    // 80286 or better
  178.  
  179.     __asm {
  180.     mov    ax,word ptr aInt
  181.     mov    dx,word ptr aInt+2
  182.     rol    ax,8
  183.     rol    dx,8
  184.     xchg    ax,dx
  185.     // Fall through with the result in DX:AX
  186.     }
  187.  
  188.   #elif OS_WIN32 || OS_WIN32S    // Assumes Intel 80386 or better
  189.  
  190.     __asm {
  191.     mov    ax,word ptr aInt
  192.     mov    dx,word ptr aInt+2
  193.     rol    ax,8
  194.     rol    dx,8
  195.     xchg    ax,dx
  196.  
  197.         // Fall through with the result in DX:AX
  198.  
  199.     // The following would be possible on an Intel i486 processor:
  200.     //
  201.     //  mov        eax,dword ptr aInt
  202.     //  bswap    eax
  203.     //
  204.     // But we cannot infer its existence from the compilation pass.
  205.     }
  206.     TLX_TODO(Check passing of return value);
  207.  
  208.   #elif OS_OS2    // Assumes Intel 80386 or better
  209.  
  210.   #if defined(__BORLANDC__)
  211.     __asm {
  212.     mov    ax,word ptr aInt
  213.     mov    dx,word ptr aInt+2
  214.     rol    ax,8
  215.     rol    dx,8
  216.     xchg    ax,dx
  217.  
  218.         // Fall through with the result in DX:AX
  219.  
  220.     // The following would be possible on an Intel i486 processor:
  221.     //
  222.     //  mov        eax,dword ptr aInt
  223.     //  bswap    eax
  224.     //
  225.     // But we cannot infer its existence from the compilation pass.
  226.     }
  227.     TLX_TODO(Check passing of return value);
  228.  
  229.   #elif defined(__IBMC__) || defined(__IBMCPP__)
  230.     swap32_u result;
  231.     _swab((char *)&aInt, (char *)&result.words[1], sizeof(int16));
  232.     _swab(((char *)&aInt)+2, (char *)&result.words[0], sizeof(int16));
  233.     return result.dword;
  234.   #else
  235.     #error Unsupported compiler; contact Tarma Software Research
  236.   #endif
  237.  
  238.   #elif OS_MAC    // Assumes Motorola 68k processor
  239.  
  240.     asm {
  241.     MOVE.L    aInt(A6),D0
  242.     ROL.W    #8,D0
  243.     SWAP    D0
  244.     ROL.W    #8,D0
  245.     // Fall through with the result in D0
  246.     }
  247.   #else
  248.     #error Unsupported platform; contact Tarma Software Research
  249.   #endif
  250. #endif    // USE_GENERIC
  251. }
  252.