OpenCOBOL 1.1pre-rel
|
00001 /* 00002 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Lesser General Public 00006 * License as published by the Free Software Foundation; either 00007 * version 2 of the License, or (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Lesser General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public 00015 * License along with this library; if not, write to the 00016 * the Free Software Foundation, 51 Franklin Street, Fifth Floor 00017 * Boston, MA 02110-1301 USA 00018 */ 00019 00020 /* 00021 * Extracted from glib/gtypes.h in GLIB-2.2.2. 00022 * Modified by Roger While 00023 * Copyright (C) 2005-2009 Roger While 00024 */ 00025 00026 #ifndef COB_BYTESWAP_H 00027 #define COB_BYTESWAP_H 00028 00029 /* Basic bit swapping functions 00030 */ 00031 00032 #define COB_BSWAP_16_CONSTANT(val) ((unsigned short) ( \ 00033 (unsigned short) ((unsigned short) (val) >> 8) | \ 00034 (unsigned short) ((unsigned short) (val) << 8))) 00035 00036 #define COB_BSWAP_32_CONSTANT(val) ((unsigned int) ( \ 00037 (((unsigned int) (val) & (unsigned int) 0x000000ffU) << 24) | \ 00038 (((unsigned int) (val) & (unsigned int) 0x0000ff00U) << 8) | \ 00039 (((unsigned int) (val) & (unsigned int) 0x00ff0000U) >> 8) | \ 00040 (((unsigned int) (val) & (unsigned int) 0xff000000U) >> 24))) 00041 00042 #define COB_BSWAP_64_CONSTANT(val) ((unsigned long long) ( \ 00043 (((unsigned long long) (val) & \ 00044 (unsigned long long) 0x00000000000000ffULL) << 56) | \ 00045 (((unsigned long long) (val) & \ 00046 (unsigned long long) 0x000000000000ff00ULL) << 40) | \ 00047 (((unsigned long long) (val) & \ 00048 (unsigned long long) 0x0000000000ff0000ULL) << 24) | \ 00049 (((unsigned long long) (val) & \ 00050 (unsigned long long) 0x00000000ff000000ULL) << 8) | \ 00051 (((unsigned long long) (val) & \ 00052 (unsigned long long) 0x000000ff00000000ULL) >> 8) | \ 00053 (((unsigned long long) (val) & \ 00054 (unsigned long long) 0x0000ff0000000000ULL) >> 24) | \ 00055 (((unsigned long long) (val) & \ 00056 (unsigned long long) 0x00ff000000000000ULL) >> 40) | \ 00057 (((unsigned long long) (val) & \ 00058 (unsigned long long) 0xff00000000000000ULL) >> 56))) 00059 00060 /* Arch specific stuff for speed */ 00061 00062 #if defined (__GNUC__) && (__GNUC__ >= 2) 00063 # if defined (__i386__) 00064 # define COB_BSWAP_16_IA32(val) \ 00065 (__extension__ \ 00066 ({ register unsigned short int __v, __x = ((unsigned short) (val)); \ 00067 if (__builtin_constant_p (__x)) \ 00068 __v = COB_BSWAP_16_CONSTANT (__x); \ 00069 else \ 00070 __asm__ ("rorw $8, %w0" \ 00071 : "=r" (__v) \ 00072 : "0" (__x) \ 00073 : "cc"); \ 00074 __v; })) 00075 # define COB_BSWAP_32_IA32(val) \ 00076 (__extension__ \ 00077 ({ register unsigned int __v, __x = ((unsigned int) (val)); \ 00078 if (__builtin_constant_p (__x)) \ 00079 __v = COB_BSWAP_32_CONSTANT (__x); \ 00080 else \ 00081 __asm__ ("bswap %0" \ 00082 : "=r" (__v) \ 00083 : "0" (__x)); \ 00084 __v; })) 00085 # define COB_BSWAP_64_IA32(val) \ 00086 (__extension__ \ 00087 ({ union { unsigned long long __ll; \ 00088 unsigned int __l[2]; } __w, __r; \ 00089 __w.__ll = ((unsigned long long) (val)); \ 00090 if (__builtin_constant_p (__w.__ll)) \ 00091 __r.__ll = COB_BSWAP_64_CONSTANT (__w.__ll); \ 00092 else \ 00093 { \ 00094 __r.__l[0] = COB_BSWAP_32 (__w.__l[1]); \ 00095 __r.__l[1] = COB_BSWAP_32 (__w.__l[0]); \ 00096 } \ 00097 __r.__ll; })) 00098 # define COB_BSWAP_16(val) (COB_BSWAP_16_IA32 (val)) 00099 # define COB_BSWAP_32(val) (COB_BSWAP_32_IA32 (val)) 00100 # define COB_BSWAP_64(val) (COB_BSWAP_64_IA32 (val)) 00101 # elif defined (__ia64__) 00102 # define COB_BSWAP_16_IA64(val) \ 00103 (__extension__ \ 00104 ({ register unsigned short __v, __x = ((unsigned short) (val)); \ 00105 if (__builtin_constant_p (__x)) \ 00106 __v = COB_BSWAP_16_CONSTANT (__x); \ 00107 else \ 00108 __asm__ __volatile__ ("shl %0 = %1, 48 ;;" \ 00109 "mux1 %0 = %0, @rev ;;" \ 00110 : "=r" (__v) \ 00111 : "r" (__x)); \ 00112 __v; })) 00113 # define COB_BSWAP_32_IA64(val) \ 00114 (__extension__ \ 00115 ({ register unsigned int __v, __x = ((unsigned int) (val)); \ 00116 if (__builtin_constant_p (__x)) \ 00117 __v = COB_BSWAP_32_CONSTANT (__x); \ 00118 else \ 00119 __asm__ __volatile__ ("shl %0 = %1, 32 ;;" \ 00120 "mux1 %0 = %0, @rev ;;" \ 00121 : "=r" (__v) \ 00122 : "r" (__x)); \ 00123 __v; })) 00124 # define COB_BSWAP_64_IA64(val) \ 00125 (__extension__ \ 00126 ({ register unsigned long long __v, \ 00127 __x = ((unsigned long long) (val)); \ 00128 if (__builtin_constant_p (__x)) \ 00129 __v = COB_BSWAP_64_CONSTANT (__x); \ 00130 else \ 00131 __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;" \ 00132 : "=r" (__v) \ 00133 : "r" (__x)); \ 00134 __v; })) 00135 # define COB_BSWAP_16(val) (COB_BSWAP_16_IA64 (val)) 00136 # define COB_BSWAP_32(val) (COB_BSWAP_32_IA64 (val)) 00137 # define COB_BSWAP_64(val) (COB_BSWAP_64_IA64 (val)) 00138 # elif defined (__x86_64__) 00139 # define COB_BSWAP_16_X86_64(val) \ 00140 (__extension__ \ 00141 ({ register unsigned short int __v, __x = ((unsigned short) (val)); \ 00142 if (__builtin_constant_p (__x)) \ 00143 __v = COB_BSWAP_16_CONSTANT (__x); \ 00144 else \ 00145 __asm__ ("rorw $8, %w0" \ 00146 : "=r" (__v) \ 00147 : "0" (__x) \ 00148 : "cc"); \ 00149 __v; })) 00150 # define COB_BSWAP_32_X86_64(val) \ 00151 (__extension__ \ 00152 ({ register unsigned int __v, __x = ((unsigned int) (val)); \ 00153 if (__builtin_constant_p (__x)) \ 00154 __v = COB_BSWAP_32_CONSTANT (__x); \ 00155 else \ 00156 __asm__ ("bswapl %0" \ 00157 : "=r" (__v) \ 00158 : "0" (__x)); \ 00159 __v; })) 00160 # define COB_BSWAP_64_X86_64(val) \ 00161 (__extension__ \ 00162 ({ register unsigned long long __v, __x = ((unsigned long long) (val)); \ 00163 if (__builtin_constant_p (__x)) \ 00164 __v = COB_BSWAP_64_CONSTANT (__x); \ 00165 else \ 00166 __asm__ ("bswapq %0" \ 00167 : "=r" (__v) \ 00168 : "0" (__x)); \ 00169 __v; })) 00170 # define COB_BSWAP_16(val) (COB_BSWAP_16_X86_64 (val)) 00171 # define COB_BSWAP_32(val) (COB_BSWAP_32_X86_64 (val)) 00172 # define COB_BSWAP_64(val) (COB_BSWAP_64_X86_64 (val)) 00173 # else /* generic gcc */ 00174 # define COB_BSWAP_16(val) (COB_BSWAP_16_CONSTANT (val)) 00175 # define COB_BSWAP_32(val) (COB_BSWAP_32_CONSTANT (val)) 00176 # define COB_BSWAP_64(val) (COB_BSWAP_64_CONSTANT (val)) 00177 # endif 00178 # elif defined (_MSC_VER) 00179 # define COB_BSWAP_16(val) (_byteswap_ushort (val)) 00180 # define COB_BSWAP_32(val) (_byteswap_ulong (val)) 00181 # define COB_BSWAP_64(val) (_byteswap_uint64 (val)) 00182 #else /* generic */ 00183 # define COB_BSWAP_16(val) (COB_BSWAP_16_CONSTANT (val)) 00184 # define COB_BSWAP_32(val) (COB_BSWAP_32_CONSTANT (val)) 00185 # define COB_BSWAP_64(val) (COB_BSWAP_64_CONSTANT (val)) 00186 #endif /* generic */ 00187 00188 #endif /* COB_BYTESWAP_H */