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