Qore Programming Language  0.8.3
include/qore/qore_bitopts.h (4311)
00001 /* -*- mode: c++; indent-tabs-mode: nil -*- */
00002 /*
00003  qore_bit_opts.h
00004  
00005  Qore Programming Language
00006  
00007  Copyright 2003 - 2011 David Nichols
00008  
00009  This library is free software; you can redistribute it and/or
00010  modify it under the terms of the GNU Lesser General Public
00011  License as published by the Free Software Foundation; either
00012  version 2.1 of the License, or (at your option) any later version.
00013  
00014  This library is distributed in the hope that it will be useful,
00015  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  Lesser General Public License for more details.
00018  
00019  You should have received a copy of the GNU Lesser General Public
00020  License along with this library; if not, write to the Free Software
00021  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022  */
00023 
00024 #ifndef _QORE_BITOPTS_H
00025 
00026 #define _QORE_BITOPTS_H
00027 
00028 #include <qore/common.h>
00029 
00030 static inline int64 i8LSB(int64 i);
00031 static inline int   i4LSB(int i);
00032 static inline short i2LSB(short i);
00033 
00034 static inline int64 LSBi8(int64 i);
00035 static inline int   LSBi4(int i);
00036 static inline short LSBi2(short i);
00037 
00038 static inline int64 i8MSB(int64 i);
00039 static inline int64 MSBi8(int64 i);
00040 
00042 union qore_i8_u {
00043    char buf[8];
00044    int64 i;
00045    double f;
00046 
00047    DLLLOCAL void swap(char *ibuf) {
00048       buf[7] = ibuf[0];
00049       buf[6] = ibuf[1];
00050       buf[5] = ibuf[2];
00051       buf[4] = ibuf[3];
00052       buf[3] = ibuf[4];
00053       buf[2] = ibuf[5];
00054       buf[1] = ibuf[6];
00055       buf[0] = ibuf[7];
00056    }
00057 };
00058 
00060 static inline int64 swapi8(int64 i) { 
00061    char *ibuf = (char *)&i;
00062    qore_i8_u i8;
00063    i8.swap(ibuf);
00064    return i8.i;
00065 }
00066 
00068 static inline double swapf8(double f) { 
00069    char *ibuf = (char *)&f;
00070    qore_i8_u f8;
00071    f8.swap(ibuf);
00072    return f8.f;
00073 }
00074 
00076 union qore_i4_u {
00077    char buf[4];
00078    int i;
00079    float f;
00080 
00081    DLLLOCAL void swap(char *ibuf) {
00082       buf[3] = ibuf[0];
00083       buf[2] = ibuf[1];
00084       buf[1] = ibuf[2];
00085       buf[0] = ibuf[3];
00086    }
00087 };
00088 
00090 static inline int swapi4(int i) { 
00091    char *ibuf = (char *)&i;
00092    qore_i4_u i4;
00093    i4.swap(ibuf);
00094    return i4.i;
00095 }
00096 
00098 static inline float swapf4(float f) { 
00099    char *ibuf = (char *)&f;
00100    qore_i4_u f4;
00101    f4.swap(ibuf);
00102    return f4.f;
00103 }
00104 
00106 union qore_i2_u {
00107    char buf[2];
00108    short i;
00109 
00110    DLLLOCAL void swap(char *ibuf) {
00111       buf[1] = ibuf[0];
00112       buf[0] = ibuf[1];
00113    }
00114 };
00115 
00117 static inline short swapi2(short i) { 
00118    char *ibuf = (char *)&i;
00119    qore_i2_u i2;
00120    i2.swap(ibuf);
00121    return i2.i;
00122 }
00123 
00124 #ifdef WORDS_BIGENDIAN
00125 static inline int64 i8LSB(int64 i) {
00126    return swapi8(i);
00127 }
00128 
00129 static inline int i4LSB(int i) {
00130    return swapi4(i);
00131 }
00132 
00133 static inline short i2LSB(short i) {
00134    return swapi2(i);
00135 }
00136 
00137 static inline int64 LSBi8(int64 i) {
00138    return swapi8(i);
00139 }
00140 
00141 static inline int LSBi4(int i) {
00142    return swapi4(i);
00143 }
00144 
00145 static inline short LSBi2(short i) { 
00146    return swapi2(i);
00147 }
00148 
00149 static inline int64 i8MSB(int64 i) { return i; }
00150 static inline int64 MSBi8(int64 i) { return i; }
00151 
00152 static inline double f8LSB(double f) {
00153    return swapf8(f);
00154 }
00155 
00156 static inline float f4LSB(float f) {
00157    return swapf4(f);
00158 }
00159 
00160 static inline double LSBf8(double f) { 
00161    return swapf8(f);
00162 }
00163 
00164 static inline float LSBf4(float f) {
00165    return swapf4(f);
00166 }
00167 
00168 static inline double f8MSB(double f) { return f; }
00169 static inline double MSBf8(double f) { return f; }
00170 static inline float f4MSB(float f)   { return f; }
00171 static inline float MSBf4(float f)   { return f; }
00172 
00173 #else  // definitions for little endian machines below
00174 
00175 static inline int64 i8LSB(int64 i) { return i; }
00176 static inline int   i4LSB(int i)   { return i; }
00177 static inline short i2LSB(short i) { return i; }
00178 
00179 static inline int64 LSBi8(int64 i) { return i; }
00180 static inline int   LSBi4(int i)   { return i; }
00181 static inline short LSBi2(short i) { return i; }
00182 
00183 static inline int64 i8MSB(int64 i) { 
00184    return swapi8(i);
00185 }
00186 
00187 static inline int64 MSBi8(int64 i) { 
00188    return swapi8(i);
00189 }
00190 
00191 static inline double f8LSB(double f) { return f; }
00192 static inline float  f4LSB(float f)  { return f; }
00193 
00194 static inline double LSBf8(double f) { return f; }
00195 static inline float  LSBf4(float f)  { return f; }
00196 
00197 static inline double f8MSB(double f) { 
00198    return swapf8(f);
00199 }
00200 
00201 static inline double MSBf8(double f) { 
00202    return swapf8(f);
00203 }
00204 
00205 static inline float f4MSB(float f) { 
00206    return swapf4(f);
00207 }
00208 
00209 static inline float MSBf4(float f) { 
00210    return swapf4(f);
00211 }
00212 
00213 #endif
00214 
00215 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines