Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/Bytes.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* @(#)root/base:$Id$ */
0002 
0003 /*************************************************************************
0004  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
0005  * All rights reserved.                                                  *
0006  *                                                                       *
0007  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0008  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0009  *************************************************************************/
0010 
0011 #ifndef ROOT_Bytes
0012 #define ROOT_Bytes
0013 
0014 
0015 //////////////////////////////////////////////////////////////////////////
0016 //                                                                      //
0017 // Bytes                                                                //
0018 //                                                                      //
0019 // A set of inline byte handling routines.                              //
0020 //                                                                      //
0021 // The set of tobuf() and frombuf() routines take care of packing a     //
0022 // basic type value into a buffer in network byte order (i.e. they      //
0023 // perform byte swapping when needed). The buffer does not have to      //
0024 // start on a machine (long) word boundary.                             //
0025 //                                                                      //
0026 // For __GNUC__ on linux on i486 processors and up                      //
0027 // use the `bswap' opcode provided by the GNU C Library.                //
0028 //                                                                      //
0029 // The set of host2net() and net2host() routines convert a basic type   //
0030 // value from host to network byte order and vice versa. On BIG ENDIAN  //
0031 // machines this is a no op.                                            //
0032 //                                                                      //
0033 //////////////////////////////////////////////////////////////////////////
0034 
0035 #include "RtypesCore.h"
0036 
0037 #include <cstring>
0038 
0039 #if (defined(__linux) || defined(__APPLE__)) && \
0040     (defined(__i386__) || defined(__x86_64__)) && \
0041      defined(__GNUC__)
0042 #define R__USEASMSWAP
0043 #endif
0044 
0045 //Big bug in inline byte swapping code with Intel's icc
0046 #if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1000
0047 #undef R__USEASMSWAP
0048 #endif
0049 
0050 #if defined(R__USEASMSWAP)
0051 #include "Byteswap.h"
0052 #endif
0053 
0054 //______________________________________________________________________________
0055 inline void tobuf(char *&buf, Bool_t x)
0056 {
0057    UChar_t x1 = x;
0058    *buf++ = x1;
0059 }
0060 
0061 inline void tobuf(char *&buf, UChar_t x)
0062 {
0063    *buf++ = x;
0064 }
0065 
0066 inline void tobuf(char *&buf, UShort_t x)
0067 {
0068 #ifdef R__BYTESWAP
0069 # if defined(R__USEASMSWAP)
0070    *((UShort_t *)buf) = Rbswap_16(x);
0071 # else
0072    // To work around a stupid optimization bug in MSVC++ 6.0
0073    const UShort_t *intermediary = &x;
0074    char *sw = (char *) intermediary;
0075    buf[0] = sw[1];
0076    buf[1] = sw[0];
0077 # endif
0078 #else
0079    memcpy(buf, &x, sizeof(UShort_t));
0080 #endif
0081    buf += sizeof(UShort_t);
0082 }
0083 
0084 inline void tobuf(char *&buf, UInt_t x)
0085 {
0086 #ifdef R__BYTESWAP
0087 # if defined(R__USEASMSWAP)
0088    *((UInt_t *)buf) = Rbswap_32(x);
0089 # else
0090    // To work around a stupid optimization bug in MSVC++ 6.0
0091    const UInt_t *intermediary = &x;
0092    char *sw = (char *)intermediary;
0093    buf[0] = sw[3];
0094    buf[1] = sw[2];
0095    buf[2] = sw[1];
0096    buf[3] = sw[0];
0097 # endif
0098 #else
0099    memcpy(buf, &x, sizeof(UInt_t));
0100 #endif
0101    buf += sizeof(UInt_t);
0102 }
0103 
0104 inline void tobuf(char *&buf, ULong_t x)
0105 {
0106 #ifdef R__BYTESWAP
0107    // To work around a stupid optimization bug in MSVC++ 6.0
0108    const ULong_t *intermediary = &x;
0109    char *sw = (char *)intermediary;
0110    if (sizeof(ULong_t) == 8) {
0111       buf[0] = sw[7];
0112       buf[1] = sw[6];
0113       buf[2] = sw[5];
0114       buf[3] = sw[4];
0115       buf[4] = sw[3];
0116       buf[5] = sw[2];
0117       buf[6] = sw[1];
0118       buf[7] = sw[0];
0119    } else {
0120       buf[0] = 0;
0121       buf[1] = 0;
0122       buf[2] = 0;
0123       buf[3] = 0;
0124       buf[4] = sw[3];
0125       buf[5] = sw[2];
0126       buf[6] = sw[1];
0127       buf[7] = sw[0];
0128    }
0129 #else
0130    if (sizeof(ULong_t) == 8) {
0131       memcpy(buf, &x, 8);
0132    } else {
0133       buf[0] = 0;
0134       buf[1] = 0;
0135       buf[2] = 0;
0136       buf[3] = 0;
0137       memcpy(buf+4, &x, 4);
0138    }
0139 #endif
0140    buf += 8;
0141 }
0142 
0143 inline void tobuf(char *&buf, Long_t x)
0144 {
0145 #ifdef R__BYTESWAP
0146    // To work around a stupid optimization bug in MSVC++ 6.0
0147    const Long_t *intermediary = &x;
0148    char *sw = (char *)intermediary;
0149    if (sizeof(Long_t) == 8) {
0150       buf[0] = sw[7];
0151       buf[1] = sw[6];
0152       buf[2] = sw[5];
0153       buf[3] = sw[4];
0154       buf[4] = sw[3];
0155       buf[5] = sw[2];
0156       buf[6] = sw[1];
0157       buf[7] = sw[0];
0158    } else {
0159       if (x < 0) {
0160          buf[0] = (char) -1;
0161          buf[1] = (char) -1;
0162          buf[2] = (char) -1;
0163          buf[3] = (char) -1;
0164       } else {
0165          buf[0] = 0;
0166          buf[1] = 0;
0167          buf[2] = 0;
0168          buf[3] = 0;
0169       }
0170       buf[4] = sw[3];
0171       buf[5] = sw[2];
0172       buf[6] = sw[1];
0173       buf[7] = sw[0];
0174    }
0175 #else
0176    if (sizeof(Long_t) == 8) {
0177       memcpy(buf, &x, 8);
0178    } else {
0179       if (x < 0) {
0180          buf[0] = (char) -1;
0181          buf[1] = (char) -1;
0182          buf[2] = (char) -1;
0183          buf[3] = (char) -1;
0184       } else {
0185          buf[0] = 0;
0186          buf[1] = 0;
0187          buf[2] = 0;
0188          buf[3] = 0;
0189       }
0190       memcpy(buf+4, &x, 4);
0191    }
0192 #endif
0193    buf += 8;
0194 }
0195 
0196 inline void tobuf(char *&buf, ULong64_t x)
0197 {
0198 #ifdef R__BYTESWAP
0199 # if defined(R__USEASMSWAP)
0200    *((ULong64_t *)buf) = Rbswap_64(x);
0201 # else
0202    // To work around a stupid optimization bug in MSVC++ 6.0
0203    const ULong64_t *intermediary = &x;
0204    char *sw = (char *)intermediary;
0205    buf[0] = sw[7];
0206    buf[1] = sw[6];
0207    buf[2] = sw[5];
0208    buf[3] = sw[4];
0209    buf[4] = sw[3];
0210    buf[5] = sw[2];
0211    buf[6] = sw[1];
0212    buf[7] = sw[0];
0213 # endif
0214 #else
0215    memcpy(buf, &x, sizeof(ULong64_t));
0216 #endif
0217    buf += sizeof(ULong64_t);
0218 }
0219 
0220 inline void tobuf(char *&buf, Float_t x)
0221 {
0222 #ifdef R__BYTESWAP
0223 # if defined(R__USEASMSWAP)
0224    union {
0225       volatile UInt_t  i;
0226       volatile Float_t f;
0227    } u;
0228    u.f = x;
0229    *((UInt_t *)buf) = Rbswap_32(u.i);
0230 # else
0231    union {
0232       volatile char    c[4];
0233       volatile Float_t f;
0234    } u;
0235    u.f = x;
0236    buf[0] = u.c[3];
0237    buf[1] = u.c[2];
0238    buf[2] = u.c[1];
0239    buf[3] = u.c[0];
0240 # endif
0241 #else
0242    memcpy(buf, &x, sizeof(Float_t));
0243 #endif
0244    buf += sizeof(Float_t);
0245 }
0246 
0247 inline void tobuf(char *&buf, Double_t x)
0248 {
0249 #ifdef R__BYTESWAP
0250 # if defined(R__USEASMSWAP)
0251    union {
0252       volatile ULong64_t l;
0253       volatile Double_t  d;
0254    } u;
0255    u.d = x;
0256    *((ULong64_t *)buf) = Rbswap_64(u.l);
0257 # else
0258    union {
0259       volatile char     c[8];
0260       volatile Double_t d;
0261    } u;
0262    u.d = x;
0263    buf[0] = u.c[7];
0264    buf[1] = u.c[6];
0265    buf[2] = u.c[5];
0266    buf[3] = u.c[4];
0267    buf[4] = u.c[3];
0268    buf[5] = u.c[2];
0269    buf[6] = u.c[1];
0270    buf[7] = u.c[0];
0271 # endif
0272 #else
0273    memcpy(buf, &x, sizeof(Double_t));
0274 #endif
0275    buf += sizeof(Double_t);
0276 }
0277 
0278 inline void frombuf(char *&buf, Bool_t *x)
0279 {
0280    UChar_t x1;
0281    x1 = *buf++;
0282    *x = (Bool_t) (x1 != 0);
0283 }
0284 
0285 inline void frombuf(char *&buf, UChar_t *x)
0286 {
0287    *x = *buf++;
0288 }
0289 
0290 inline void frombuf(char *&buf, UShort_t *x)
0291 {
0292 #ifdef R__BYTESWAP
0293 # if defined(R__USEASMSWAP)
0294    *x = Rbswap_16(*((UShort_t *)buf));
0295 # else
0296    char *sw = (char *)x;
0297    sw[0] = buf[1];
0298    sw[1] = buf[0];
0299 # endif
0300 #else
0301    memcpy(x, buf, sizeof(UShort_t));
0302 #endif
0303    buf += sizeof(UShort_t);
0304 }
0305 
0306 inline void frombuf(char *&buf, UInt_t *x)
0307 {
0308 #ifdef R__BYTESWAP
0309 # if defined(R__USEASMSWAP)
0310    *x = Rbswap_32(*((UInt_t *)buf));
0311 # else
0312    char *sw = (char *)x;
0313    sw[0] = buf[3];
0314    sw[1] = buf[2];
0315    sw[2] = buf[1];
0316    sw[3] = buf[0];
0317 # endif
0318 #else
0319    memcpy(x, buf, sizeof(UInt_t));
0320 #endif
0321    buf += sizeof(UInt_t);
0322 }
0323 
0324 inline void frombuf(char *&buf, ULong_t *x)
0325 {
0326 #ifdef R__BYTESWAP
0327    char *sw = (char *)x;
0328    if (sizeof(ULong_t) == 8) {
0329       sw[0] = buf[7];
0330       sw[1] = buf[6];
0331       sw[2] = buf[5];
0332       sw[3] = buf[4];
0333       sw[4] = buf[3];
0334       sw[5] = buf[2];
0335       sw[6] = buf[1];
0336       sw[7] = buf[0];
0337    } else {
0338       sw[0] = buf[7];
0339       sw[1] = buf[6];
0340       sw[2] = buf[5];
0341       sw[3] = buf[4];
0342    }
0343 #else
0344    if (sizeof(ULong_t) == 8) {
0345       memcpy(x, buf, 8);
0346    } else {
0347       memcpy(x, buf+4, 4);
0348    }
0349 #endif
0350    buf += 8;
0351 }
0352 
0353 inline void frombuf(char *&buf, ULong64_t *x)
0354 {
0355 #ifdef R__BYTESWAP
0356 # if defined(R__USEASMSWAP)
0357    *x = Rbswap_64(*((ULong64_t *)buf));
0358 # else
0359    char *sw = (char *)x;
0360    sw[0] = buf[7];
0361    sw[1] = buf[6];
0362    sw[2] = buf[5];
0363    sw[3] = buf[4];
0364    sw[4] = buf[3];
0365    sw[5] = buf[2];
0366    sw[6] = buf[1];
0367    sw[7] = buf[0];
0368 # endif
0369 #else
0370    memcpy(x, buf, sizeof(ULong64_t));
0371 #endif
0372    buf += sizeof(ULong64_t);
0373 }
0374 
0375 inline void frombuf(char *&buf, Float_t *x)
0376 {
0377 #ifdef R__BYTESWAP
0378 # if defined(R__USEASMSWAP)
0379    // Use a union to allow strict-aliasing
0380    union {
0381       volatile UInt_t  i;
0382       volatile Float_t f;
0383    } u;
0384    u.i = Rbswap_32(*((UInt_t *)buf));
0385    *x = u.f;
0386 # else
0387    union {
0388       volatile char    c[4];
0389       volatile Float_t f;
0390    } u;
0391    u.c[0] = buf[3];
0392    u.c[1] = buf[2];
0393    u.c[2] = buf[1];
0394    u.c[3] = buf[0];
0395    *x = u.f;
0396 # endif
0397 #else
0398    memcpy(x, buf, sizeof(Float_t));
0399 #endif
0400    buf += sizeof(Float_t);
0401 }
0402 
0403 inline void frombuf(char *&buf, Double_t *x)
0404 {
0405 #ifdef R__BYTESWAP
0406 # if defined(R__USEASMSWAP)
0407    // Use a union to allow strict-aliasing
0408    union {
0409       volatile ULong64_t l;
0410       volatile Double_t  d;
0411    } u;
0412    u.l = Rbswap_64(*((ULong64_t *)buf));
0413    *x = u.d;
0414 # else
0415    union {
0416       volatile char     c[8];
0417       volatile Double_t d;
0418    } u;
0419    u.c[0] = buf[7];
0420    u.c[1] = buf[6];
0421    u.c[2] = buf[5];
0422    u.c[3] = buf[4];
0423    u.c[4] = buf[3];
0424    u.c[5] = buf[2];
0425    u.c[6] = buf[1];
0426    u.c[7] = buf[0];
0427    *x = u.d;
0428 # endif
0429 #else
0430    memcpy(x, buf, sizeof(Double_t));
0431 #endif
0432    buf += sizeof(Double_t);
0433 }
0434 
0435 inline void tobuf(char *&buf, Char_t x)   { tobuf(buf, (UChar_t) x); }
0436 inline void tobuf(char *&buf, Short_t x)  { tobuf(buf, (UShort_t) x); }
0437 inline void tobuf(char *&buf, Int_t x)    { tobuf(buf, (UInt_t) x); }
0438 inline void tobuf(char *&buf, Long64_t x) { tobuf(buf, (ULong64_t) x); }
0439 
0440 inline void frombuf(char *&buf, Char_t *x)   { frombuf(buf, (UChar_t *) x); }
0441 inline void frombuf(char *&buf, Short_t *x)  { frombuf(buf, (UShort_t *) x); }
0442 inline void frombuf(char *&buf, Int_t *x)    { frombuf(buf, (UInt_t *) x); }
0443 inline void frombuf(char *&buf, Long_t *x)   { frombuf(buf, (ULong_t *) x); }
0444 inline void frombuf(char *&buf, Long64_t *x) { frombuf(buf, (ULong64_t *) x); }
0445 
0446 
0447 //______________________________________________________________________________
0448 #ifdef R__BYTESWAP
0449 inline UShort_t host2net(UShort_t x)
0450 {
0451 #if defined(R__USEASMSWAP)
0452    return Rbswap_16(x);
0453 #else
0454    return (((x & 0x00ff) << 8) | ((x & 0xff00) >> 8));
0455 #endif
0456 }
0457 
0458 inline UInt_t host2net(UInt_t x)
0459 {
0460 #if defined(R__USEASMSWAP)
0461    return Rbswap_32(x);
0462 #else
0463    return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) <<  8) |
0464            ((x & 0x00ff0000U) >>  8) | ((x & 0xff000000U) >> 24));
0465 #endif
0466 }
0467 
0468 inline ULong_t host2net(ULong_t x)
0469 {
0470 #if defined(R__B64) && !defined(_WIN64)
0471 # if defined(R__USEASMSWAP)
0472    return Rbswap_64(x);
0473 # else
0474    char sw[sizeof(ULong_t)];
0475    void *tmp = sw;
0476    *(ULong_t *)tmp = x;
0477 
0478    char *sb = (char *)&x;
0479    sb[0] = sw[7];
0480    sb[1] = sw[6];
0481    sb[2] = sw[5];
0482    sb[3] = sw[4];
0483    sb[4] = sw[3];
0484    sb[5] = sw[2];
0485    sb[6] = sw[1];
0486    sb[7] = sw[0];
0487    return x;
0488 # endif
0489 #else
0490    return (ULong_t)host2net((UInt_t) x);
0491 #endif
0492 }
0493 
0494 inline ULong64_t host2net(ULong64_t x)
0495 {
0496 #if defined(R__USEASMSWAP)
0497    return Rbswap_64(x);
0498 #else
0499    char sw[sizeof(ULong64_t)];
0500    void *tmp = sw;
0501    *(ULong64_t *)tmp = x;
0502 
0503    char *sb = (char *)&x;
0504    sb[0] = sw[7];
0505    sb[1] = sw[6];
0506    sb[2] = sw[5];
0507    sb[3] = sw[4];
0508    sb[4] = sw[3];
0509    sb[5] = sw[2];
0510    sb[6] = sw[1];
0511    sb[7] = sw[0];
0512    return x;
0513 #endif
0514 }
0515 
0516 inline Float_t host2net(Float_t xx)
0517 {
0518    // Use a union to allow strict-aliasing
0519    union {
0520       volatile UInt_t  i;
0521       volatile Float_t f;
0522    } u;
0523    u.f = xx;
0524 #if defined(R__USEASMSWAP)
0525    u.i = Rbswap_32(u.i);
0526 #else
0527    u.i = (((u.i & 0x000000ffU) << 24) | ((u.i & 0x0000ff00U) <<  8) |
0528           ((u.i & 0x00ff0000U) >>  8) | ((u.i & 0xff000000U) >> 24));
0529 #endif
0530    return u.f;
0531 }
0532 
0533 inline Double_t host2net(Double_t x)
0534 {
0535 #if defined(R__USEASMSWAP)
0536    // Use a union to allow strict-aliasing
0537    union {
0538       volatile ULong64_t l;
0539       volatile Double_t  d;
0540    } u;
0541    u.d = x;
0542    u.l = Rbswap_64(u.l);
0543    return u.d;
0544 # else
0545    char sw[sizeof(Double_t)];
0546    void *tmp = sw;
0547    *(Double_t *)tmp = x;
0548 
0549    char *sb = (char *)&x;
0550    sb[0] = sw[7];
0551    sb[1] = sw[6];
0552    sb[2] = sw[5];
0553    sb[3] = sw[4];
0554    sb[4] = sw[3];
0555    sb[5] = sw[2];
0556    sb[6] = sw[1];
0557    sb[7] = sw[0];
0558    return x;
0559 #endif
0560 }
0561 #else  /* R__BYTESWAP */
0562 inline UShort_t host2net(UShort_t x)   { return x; }
0563 inline UInt_t   host2net(UInt_t x)     { return x; }
0564 inline ULong_t  host2net(ULong_t x)    { return x; }
0565 inline ULong_t  host2net(ULong64_t x)  { return x; }
0566 inline Float_t  host2net(Float_t x)    { return x; }
0567 inline Double_t host2net(Double_t x)   { return x; }
0568 #endif
0569 
0570 inline Short_t  host2net(Short_t x)    { return host2net((UShort_t)x); }
0571 inline Int_t    host2net(Int_t x)      { return host2net((UInt_t)x); }
0572 inline Long_t   host2net(Long_t x)     { return host2net((ULong_t)x); }
0573 inline Long64_t host2net(Long64_t x)   { return host2net((ULong64_t)x); }
0574 
0575 inline UShort_t  net2host(UShort_t x)  { return host2net(x); }
0576 inline Short_t   net2host(Short_t x)   { return host2net(x); }
0577 inline UInt_t    net2host(UInt_t x)    { return host2net(x); }
0578 inline Int_t     net2host(Int_t x)     { return host2net(x); }
0579 inline ULong_t   net2host(ULong_t x)   { return host2net(x); }
0580 inline Long_t    net2host(Long_t x)    { return host2net(x); }
0581 inline ULong64_t net2host(ULong64_t x) { return host2net(x); }
0582 inline Long64_t  net2host(Long64_t x)  { return host2net(x); }
0583 inline Float_t   net2host(Float_t x)   { return host2net(x); }
0584 inline Double_t  net2host(Double_t x)  { return host2net(x); }
0585 
0586 #endif