Logo ROOT   6.12/07
Reference Guide
Bytes.h
Go to the documentation of this file.
1 /* @(#)root/base:$Id$ */
2 
3 /*************************************************************************
4  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
5  * All rights reserved. *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  *************************************************************************/
10 
11 #ifndef ROOT_Bytes
12 #define ROOT_Bytes
13 
14 
15 //////////////////////////////////////////////////////////////////////////
16 // //
17 // Bytes //
18 // //
19 // A set of inline byte handling routines. //
20 // //
21 // The set of tobuf() and frombuf() routines take care of packing a //
22 // basic type value into a buffer in network byte order (i.e. they //
23 // perform byte swapping when needed). The buffer does not have to //
24 // start on a machine (long) word boundary. //
25 // //
26 // For __GNUC__ on linux on i486 processors and up //
27 // use the `bswap' opcode provided by the GNU C Library. //
28 // //
29 // The set of host2net() and net2host() routines convert a basic type //
30 // value from host to network byte order and vice versa. On BIG ENDIAN //
31 // machines this is a no op. //
32 // //
33 //////////////////////////////////////////////////////////////////////////
34 
35 #include "Rtypes.h"
36 
37 #ifndef __CINT__
38 #include <string.h>
39 #endif
40 
41 #if (defined(__linux) || defined(__APPLE__)) && \
42  (defined(__i386__) || defined(__x86_64__)) && \
43  defined(__GNUC__)
44 #define R__USEASMSWAP
45 #endif
46 
47 //Big bug in inline byte swapping code with Intel's icc
48 #if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1000
49 #undef R__USEASMSWAP
50 #endif
51 
52 #if defined(R__USEASMSWAP) && !defined(__CINT__)
53 #include "Byteswap.h"
54 #endif
55 
56 //______________________________________________________________________________
57 inline void tobuf(char *&buf, Bool_t x)
58 {
59  UChar_t x1 = x;
60  *buf++ = x1;
61 }
62 
63 inline void tobuf(char *&buf, UChar_t x)
64 {
65  *buf++ = x;
66 }
67 
68 inline void tobuf(char *&buf, UShort_t x)
69 {
70 #ifdef R__BYTESWAP
71 # if defined(R__USEASMSWAP)
72  *((UShort_t *)buf) = Rbswap_16(x);
73 # else
74  // To work around a stupid optimization bug in MSVC++ 6.0
75  const UShort_t *intermediary = &x;
76  char *sw = (char *) intermediary;
77  buf[0] = sw[1];
78  buf[1] = sw[0];
79 # endif
80 #else
81  memcpy(buf, &x, sizeof(UShort_t));
82 #endif
83  buf += sizeof(UShort_t);
84 }
85 
86 inline void tobuf(char *&buf, UInt_t x)
87 {
88 #ifdef R__BYTESWAP
89 # if defined(R__USEASMSWAP)
90  *((UInt_t *)buf) = Rbswap_32(x);
91 # else
92  // To work around a stupid optimization bug in MSVC++ 6.0
93  const UInt_t *intermediary = &x;
94  char *sw = (char *)intermediary;
95  buf[0] = sw[3];
96  buf[1] = sw[2];
97  buf[2] = sw[1];
98  buf[3] = sw[0];
99 # endif
100 #else
101  memcpy(buf, &x, sizeof(UInt_t));
102 #endif
103  buf += sizeof(UInt_t);
104 }
105 
106 inline void tobuf(char *&buf, ULong_t x)
107 {
108 #ifdef R__BYTESWAP
109  // To work around a stupid optimization bug in MSVC++ 6.0
110  const ULong_t *intermediary = &x;
111  char *sw = (char *)intermediary;
112  if (sizeof(ULong_t) == 8) {
113  buf[0] = sw[7];
114  buf[1] = sw[6];
115  buf[2] = sw[5];
116  buf[3] = sw[4];
117  buf[4] = sw[3];
118  buf[5] = sw[2];
119  buf[6] = sw[1];
120  buf[7] = sw[0];
121  } else {
122  buf[0] = 0;
123  buf[1] = 0;
124  buf[2] = 0;
125  buf[3] = 0;
126  buf[4] = sw[3];
127  buf[5] = sw[2];
128  buf[6] = sw[1];
129  buf[7] = sw[0];
130  }
131 #else
132  if (sizeof(ULong_t) == 8) {
133  memcpy(buf, &x, 8);
134  } else {
135  buf[0] = 0;
136  buf[1] = 0;
137  buf[2] = 0;
138  buf[3] = 0;
139  memcpy(buf+4, &x, 4);
140  }
141 #endif
142  buf += 8;
143 }
144 
145 inline void tobuf(char *&buf, Long_t x)
146 {
147 #ifdef R__BYTESWAP
148  // To work around a stupid optimization bug in MSVC++ 6.0
149  const Long_t *intermediary = &x;
150  char *sw = (char *)intermediary;
151  if (sizeof(Long_t) == 8) {
152  buf[0] = sw[7];
153  buf[1] = sw[6];
154  buf[2] = sw[5];
155  buf[3] = sw[4];
156  buf[4] = sw[3];
157  buf[5] = sw[2];
158  buf[6] = sw[1];
159  buf[7] = sw[0];
160  } else {
161  if (x < 0) {
162  buf[0] = (char) -1;
163  buf[1] = (char) -1;
164  buf[2] = (char) -1;
165  buf[3] = (char) -1;
166  } else {
167  buf[0] = 0;
168  buf[1] = 0;
169  buf[2] = 0;
170  buf[3] = 0;
171  }
172  buf[4] = sw[3];
173  buf[5] = sw[2];
174  buf[6] = sw[1];
175  buf[7] = sw[0];
176  }
177 #else
178  if (sizeof(Long_t) == 8) {
179  memcpy(buf, &x, 8);
180  } else {
181  if (x < 0) {
182  buf[0] = (char) -1;
183  buf[1] = (char) -1;
184  buf[2] = (char) -1;
185  buf[3] = (char) -1;
186  } else {
187  buf[0] = 0;
188  buf[1] = 0;
189  buf[2] = 0;
190  buf[3] = 0;
191  }
192  memcpy(buf+4, &x, 4);
193  }
194 #endif
195  buf += 8;
196 }
197 
198 inline void tobuf(char *&buf, ULong64_t x)
199 {
200 #ifdef R__BYTESWAP
201 # if defined(R__USEASMSWAP)
202  *((ULong64_t *)buf) = Rbswap_64(x);
203 # else
204  // To work around a stupid optimization bug in MSVC++ 6.0
205  const ULong64_t *intermediary = &x;
206  char *sw = (char *)intermediary;
207  buf[0] = sw[7];
208  buf[1] = sw[6];
209  buf[2] = sw[5];
210  buf[3] = sw[4];
211  buf[4] = sw[3];
212  buf[5] = sw[2];
213  buf[6] = sw[1];
214  buf[7] = sw[0];
215 # endif
216 #else
217  memcpy(buf, &x, sizeof(ULong64_t));
218 #endif
219  buf += sizeof(ULong64_t);
220 }
221 
222 inline void tobuf(char *&buf, Float_t x)
223 {
224 #ifdef R__BYTESWAP
225 # if defined(R__USEASMSWAP)
226  union {
227  volatile UInt_t i;
228  volatile Float_t f;
229  } u;
230  u.f = x;
231  *((UInt_t *)buf) = Rbswap_32(u.i);
232 # else
233  union {
234  volatile char c[4];
235  volatile Float_t f;
236  } u;
237  u.f = x;
238  buf[0] = u.c[3];
239  buf[1] = u.c[2];
240  buf[2] = u.c[1];
241  buf[3] = u.c[0];
242 # endif
243 #else
244  memcpy(buf, &x, sizeof(Float_t));
245 #endif
246  buf += sizeof(Float_t);
247 }
248 
249 inline void tobuf(char *&buf, Double_t x)
250 {
251 #ifdef R__BYTESWAP
252 # if defined(R__USEASMSWAP)
253  union {
254  volatile ULong64_t l;
255  volatile Double_t d;
256  } u;
257  u.d = x;
258  *((ULong64_t *)buf) = Rbswap_64(u.l);
259 # else
260  union {
261  volatile char c[8];
262  volatile Double_t d;
263  } u;
264  u.d = x;
265  buf[0] = u.c[7];
266  buf[1] = u.c[6];
267  buf[2] = u.c[5];
268  buf[3] = u.c[4];
269  buf[4] = u.c[3];
270  buf[5] = u.c[2];
271  buf[6] = u.c[1];
272  buf[7] = u.c[0];
273 # endif
274 #else
275  memcpy(buf, &x, sizeof(Double_t));
276 #endif
277  buf += sizeof(Double_t);
278 }
279 
280 inline void frombuf(char *&buf, Bool_t *x)
281 {
282  UChar_t x1;
283  x1 = *buf++;
284  *x = (Bool_t) (x1 != 0);
285 }
286 
287 inline void frombuf(char *&buf, UChar_t *x)
288 {
289  *x = *buf++;
290 }
291 
292 inline void frombuf(char *&buf, UShort_t *x)
293 {
294 #ifdef R__BYTESWAP
295 # if defined(R__USEASMSWAP)
296  *x = Rbswap_16(*((UShort_t *)buf));
297 # else
298  char *sw = (char *)x;
299  sw[0] = buf[1];
300  sw[1] = buf[0];
301 # endif
302 #else
303  memcpy(x, buf, sizeof(UShort_t));
304 #endif
305  buf += sizeof(UShort_t);
306 }
307 
308 inline void frombuf(char *&buf, UInt_t *x)
309 {
310 #ifdef R__BYTESWAP
311 # if defined(R__USEASMSWAP)
312  *x = Rbswap_32(*((UInt_t *)buf));
313 # else
314  char *sw = (char *)x;
315  sw[0] = buf[3];
316  sw[1] = buf[2];
317  sw[2] = buf[1];
318  sw[3] = buf[0];
319 # endif
320 #else
321  memcpy(x, buf, sizeof(UInt_t));
322 #endif
323  buf += sizeof(UInt_t);
324 }
325 
326 inline void frombuf(char *&buf, ULong_t *x)
327 {
328 #ifdef R__BYTESWAP
329  char *sw = (char *)x;
330  if (sizeof(ULong_t) == 8) {
331  sw[0] = buf[7];
332  sw[1] = buf[6];
333  sw[2] = buf[5];
334  sw[3] = buf[4];
335  sw[4] = buf[3];
336  sw[5] = buf[2];
337  sw[6] = buf[1];
338  sw[7] = buf[0];
339  } else {
340  sw[0] = buf[7];
341  sw[1] = buf[6];
342  sw[2] = buf[5];
343  sw[3] = buf[4];
344  }
345 #else
346  if (sizeof(ULong_t) == 8) {
347  memcpy(x, buf, 8);
348  } else {
349  memcpy(x, buf+4, 4);
350  }
351 #endif
352  buf += 8;
353 }
354 
355 inline void frombuf(char *&buf, ULong64_t *x)
356 {
357 #ifdef R__BYTESWAP
358 # if defined(R__USEASMSWAP)
359  *x = Rbswap_64(*((ULong64_t *)buf));
360 # else
361  char *sw = (char *)x;
362  sw[0] = buf[7];
363  sw[1] = buf[6];
364  sw[2] = buf[5];
365  sw[3] = buf[4];
366  sw[4] = buf[3];
367  sw[5] = buf[2];
368  sw[6] = buf[1];
369  sw[7] = buf[0];
370 # endif
371 #else
372  memcpy(x, buf, sizeof(ULong64_t));
373 #endif
374  buf += sizeof(ULong64_t);
375 }
376 
377 inline void frombuf(char *&buf, Float_t *x)
378 {
379 #ifdef R__BYTESWAP
380 # if defined(R__USEASMSWAP)
381  // Use a union to allow strict-aliasing
382  union {
383  volatile UInt_t i;
384  volatile Float_t f;
385  } u;
386  u.i = Rbswap_32(*((UInt_t *)buf));
387  *x = u.f;
388 # else
389  union {
390  volatile char c[4];
391  volatile Float_t f;
392  } u;
393  u.c[0] = buf[3];
394  u.c[1] = buf[2];
395  u.c[2] = buf[1];
396  u.c[3] = buf[0];
397  *x = u.f;
398 # endif
399 #else
400  memcpy(x, buf, sizeof(Float_t));
401 #endif
402  buf += sizeof(Float_t);
403 }
404 
405 inline void frombuf(char *&buf, Double_t *x)
406 {
407 #ifdef R__BYTESWAP
408 # if defined(R__USEASMSWAP)
409  // Use a union to allow strict-aliasing
410  union {
411  volatile ULong64_t l;
412  volatile Double_t d;
413  } u;
414  u.l = Rbswap_64(*((ULong64_t *)buf));
415  *x = u.d;
416 # else
417  union {
418  volatile char c[8];
419  volatile Double_t d;
420  } u;
421  u.c[0] = buf[7];
422  u.c[1] = buf[6];
423  u.c[2] = buf[5];
424  u.c[3] = buf[4];
425  u.c[4] = buf[3];
426  u.c[5] = buf[2];
427  u.c[6] = buf[1];
428  u.c[7] = buf[0];
429  *x = u.d;
430 # endif
431 #else
432  memcpy(x, buf, sizeof(Double_t));
433 #endif
434  buf += sizeof(Double_t);
435 }
436 
437 inline void tobuf(char *&buf, Char_t x) { tobuf(buf, (UChar_t) x); }
438 inline void tobuf(char *&buf, Short_t x) { tobuf(buf, (UShort_t) x); }
439 inline void tobuf(char *&buf, Int_t x) { tobuf(buf, (UInt_t) x); }
440 inline void tobuf(char *&buf, Long64_t x) { tobuf(buf, (ULong64_t) x); }
441 
442 inline void frombuf(char *&buf, Char_t *x) { frombuf(buf, (UChar_t *) x); }
443 inline void frombuf(char *&buf, Short_t *x) { frombuf(buf, (UShort_t *) x); }
444 inline void frombuf(char *&buf, Int_t *x) { frombuf(buf, (UInt_t *) x); }
445 inline void frombuf(char *&buf, Long_t *x) { frombuf(buf, (ULong_t *) x); }
446 inline void frombuf(char *&buf, Long64_t *x) { frombuf(buf, (ULong64_t *) x); }
447 
448 
449 //______________________________________________________________________________
450 #ifdef R__BYTESWAP
451 inline UShort_t host2net(UShort_t x)
452 {
453 #if defined(R__USEASMSWAP)
454  return Rbswap_16(x);
455 #else
456  return (((x & 0x00ff) << 8) | ((x & 0xff00) >> 8));
457 #endif
458 }
459 
460 inline UInt_t host2net(UInt_t x)
461 {
462 #if defined(R__USEASMSWAP)
463  return Rbswap_32(x);
464 #else
465  return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
466  ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
467 #endif
468 }
469 
470 inline ULong_t host2net(ULong_t x)
471 {
472 #ifdef R__B64
473 # if defined(R__USEASMSWAP)
474  return Rbswap_64(x);
475 # else
476  char sw[sizeof(ULong_t)];
477  void *tmp = sw;
478  *(ULong_t *)tmp = x;
479 
480  char *sb = (char *)&x;
481  sb[0] = sw[7];
482  sb[1] = sw[6];
483  sb[2] = sw[5];
484  sb[3] = sw[4];
485  sb[4] = sw[3];
486  sb[5] = sw[2];
487  sb[6] = sw[1];
488  sb[7] = sw[0];
489  return x;
490 # endif
491 #else
492  return (ULong_t)host2net((UInt_t) x);
493 #endif
494 }
495 
497 {
498 #if defined(R__USEASMSWAP)
499  return Rbswap_64(x);
500 #else
501  char sw[sizeof(ULong64_t)];
502  void *tmp = sw;
503  *(ULong64_t *)tmp = x;
504 
505  char *sb = (char *)&x;
506  sb[0] = sw[7];
507  sb[1] = sw[6];
508  sb[2] = sw[5];
509  sb[3] = sw[4];
510  sb[4] = sw[3];
511  sb[5] = sw[2];
512  sb[6] = sw[1];
513  sb[7] = sw[0];
514  return x;
515 #endif
516 }
517 
518 inline Float_t host2net(Float_t xx)
519 {
520  // Use a union to allow strict-aliasing
521  union {
522  volatile UInt_t i;
523  volatile Float_t f;
524  } u;
525  u.f = xx;
526 #if defined(R__USEASMSWAP)
527  u.i = Rbswap_32(u.i);
528 #else
529  u.i = (((u.i & 0x000000ffU) << 24) | ((u.i & 0x0000ff00U) << 8) |
530  ((u.i & 0x00ff0000U) >> 8) | ((u.i & 0xff000000U) >> 24));
531 #endif
532  return u.f;
533 }
534 
535 inline Double_t host2net(Double_t x)
536 {
537 #if defined(R__USEASMSWAP)
538  // Use a union to allow strict-aliasing
539  union {
540  volatile ULong64_t l;
541  volatile Double_t d;
542  } u;
543  u.d = x;
544  u.l = Rbswap_64(u.l);
545  return u.d;
546 # else
547  char sw[sizeof(Double_t)];
548  void *tmp = sw;
549  *(Double_t *)tmp = x;
550 
551  char *sb = (char *)&x;
552  sb[0] = sw[7];
553  sb[1] = sw[6];
554  sb[2] = sw[5];
555  sb[3] = sw[4];
556  sb[4] = sw[3];
557  sb[5] = sw[2];
558  sb[6] = sw[1];
559  sb[7] = sw[0];
560  return x;
561 #endif
562 }
563 #else /* R__BYTESWAP */
564 inline UShort_t host2net(UShort_t x) { return x; }
565 inline UInt_t host2net(UInt_t x) { return x; }
566 inline ULong_t host2net(ULong_t x) { return x; }
567 inline ULong_t host2net(ULong64_t x) { return x; }
568 inline Float_t host2net(Float_t x) { return x; }
569 inline Double_t host2net(Double_t x) { return x; }
570 #endif
571 
572 inline Short_t host2net(Short_t x) { return host2net((UShort_t)x); }
573 inline Int_t host2net(Int_t x) { return host2net((UInt_t)x); }
574 inline Long_t host2net(Long_t x) { return host2net((ULong_t)x); }
575 inline Long64_t host2net(Long64_t x) { return host2net((ULong64_t)x); }
576 
577 inline UShort_t net2host(UShort_t x) { return host2net(x); }
578 inline Short_t net2host(Short_t x) { return host2net(x); }
579 inline UInt_t net2host(UInt_t x) { return host2net(x); }
580 inline Int_t net2host(Int_t x) { return host2net(x); }
581 inline ULong_t net2host(ULong_t x) { return host2net(x); }
582 inline Long_t net2host(Long_t x) { return host2net(x); }
583 inline ULong64_t net2host(ULong64_t x) { return host2net(x); }
584 inline Long64_t net2host(Long64_t x) { return host2net(x); }
585 inline Float_t net2host(Float_t x) { return host2net(x); }
586 inline Double_t net2host(Double_t x) { return host2net(x); }
587 
588 #endif
void frombuf(char *&buf, Bool_t *x)
Definition: Bytes.h:280
long long Long64_t
Definition: RtypesCore.h:69
float Float_t
Definition: RtypesCore.h:53
unsigned short UShort_t
Definition: RtypesCore.h:36
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
UShort_t net2host(UShort_t x)
Definition: Bytes.h:577
Double_t x[n]
Definition: legend1.C:17
void tobuf(char *&buf, Bool_t x)
Definition: Bytes.h:57
#define Rbswap_16(x)
Definition: Byteswap.h:122
unsigned int UInt_t
Definition: RtypesCore.h:42
short Short_t
Definition: RtypesCore.h:35
#define Rbswap_32(x)
Definition: Byteswap.h:125
long Long_t
Definition: RtypesCore.h:50
static const double x1[5]
double Double_t
Definition: RtypesCore.h:55
unsigned long long ULong64_t
Definition: RtypesCore.h:70
unsigned long ULong_t
Definition: RtypesCore.h:51
char Char_t
Definition: RtypesCore.h:29
auto * l
Definition: textangle.C:4
unsigned char UChar_t
Definition: RtypesCore.h:34
UShort_t host2net(UShort_t x)
Definition: Bytes.h:564