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