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)
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 const char *sw = (const 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 x = Rbswap_32(x);
89 memcpy(buf, &x, sizeof(x));
90# else
91 // To work around a stupid optimization bug in MSVC++ 6.0
92 const UInt_t *intermediary = &x;
93 const char *sw = (const char *)intermediary;
94 buf[0] = sw[3];
95 buf[1] = sw[2];
96 buf[2] = sw[1];
97 buf[3] = sw[0];
98# endif
99#else
100 memcpy(buf, &x, sizeof(UInt_t));
101#endif
102 buf += sizeof(UInt_t);
103}
104
105inline void tobuf(char *&buf, ULong_t x)
106{
107#ifdef R__BYTESWAP
108 // To work around a stupid optimization bug in MSVC++ 6.0
109 const ULong_t *intermediary = &x;
110 const char *sw = (const char *)intermediary;
111 if (sizeof(ULong_t) == 8) {
112 buf[0] = sw[7];
113 buf[1] = sw[6];
114 buf[2] = sw[5];
115 buf[3] = sw[4];
116 buf[4] = sw[3];
117 buf[5] = sw[2];
118 buf[6] = sw[1];
119 buf[7] = sw[0];
120 } else {
121 buf[0] = 0;
122 buf[1] = 0;
123 buf[2] = 0;
124 buf[3] = 0;
125 buf[4] = sw[3];
126 buf[5] = sw[2];
127 buf[6] = sw[1];
128 buf[7] = sw[0];
129 }
130#else
131 if (sizeof(ULong_t) == 8) {
132 memcpy(buf, &x, 8);
133 } else {
134 buf[0] = 0;
135 buf[1] = 0;
136 buf[2] = 0;
137 buf[3] = 0;
138 memcpy(buf+4, &x, 4);
139 }
140#endif
141 buf += 8;
142}
143
144inline void tobuf(char *&buf, Long_t x)
145{
146#ifdef R__BYTESWAP
147 // To work around a stupid optimization bug in MSVC++ 6.0
148 const Long_t *intermediary = &x;
149 const char *sw = (const char *)intermediary;
150 if (sizeof(Long_t) == 8) {
151 buf[0] = sw[7];
152 buf[1] = sw[6];
153 buf[2] = sw[5];
154 buf[3] = sw[4];
155 buf[4] = sw[3];
156 buf[5] = sw[2];
157 buf[6] = sw[1];
158 buf[7] = sw[0];
159 } else {
160 if (x < 0) {
161 buf[0] = (char) -1;
162 buf[1] = (char) -1;
163 buf[2] = (char) -1;
164 buf[3] = (char) -1;
165 } else {
166 buf[0] = 0;
167 buf[1] = 0;
168 buf[2] = 0;
169 buf[3] = 0;
170 }
171 buf[4] = sw[3];
172 buf[5] = sw[2];
173 buf[6] = sw[1];
174 buf[7] = sw[0];
175 }
176#else
177 if (sizeof(Long_t) == 8) {
178 memcpy(buf, &x, 8);
179 } else {
180 if (x < 0) {
181 buf[0] = (char) -1;
182 buf[1] = (char) -1;
183 buf[2] = (char) -1;
184 buf[3] = (char) -1;
185 } else {
186 buf[0] = 0;
187 buf[1] = 0;
188 buf[2] = 0;
189 buf[3] = 0;
190 }
191 memcpy(buf+4, &x, 4);
192 }
193#endif
194 buf += 8;
195}
196
197inline void tobuf(char *&buf, ULong64_t x)
198{
199#ifdef R__BYTESWAP
200# if defined(R__USEASMSWAP)
201 x = Rbswap_64(x);
202 memcpy(buf, &x, sizeof(x));
203# else
204 // To work around a stupid optimization bug in MSVC++ 6.0
205 const ULong64_t *intermediary = &x;
206 const char *sw = (const 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
222inline void tobuf(char *&buf, Float_t x)
223{
224#ifdef R__BYTESWAP
225# if defined(R__USEASMSWAP)
226 UInt_t i;
227 memcpy(&i, &x, sizeof(x));
228 i = Rbswap_32(i);
229 memcpy(buf, &i, sizeof(i));
230# else
231 char c[4];
232 memcpy(c, &x, sizeof(x));
233 buf[0] = c[3];
234 buf[1] = c[2];
235 buf[2] = c[1];
236 buf[3] = c[0];
237# endif
238#else
239 memcpy(buf, &x, sizeof(Float_t));
240#endif
241 buf += sizeof(Float_t);
242}
243
244inline void tobuf(char *&buf, Double_t x)
245{
246#ifdef R__BYTESWAP
247# if defined(R__USEASMSWAP)
248 ULong64_t l;
249 memcpy(&l, &x, sizeof(x));
250 l = Rbswap_64(l);
251 memcpy(buf, &l, sizeof(l));
252# else
253 char c[8];
254 memcpy(c, &x, sizeof(x));
255 buf[0] = c[7];
256 buf[1] = c[6];
257 buf[2] = c[5];
258 buf[3] = c[4];
259 buf[4] = c[3];
260 buf[5] = c[2];
261 buf[6] = c[1];
262 buf[7] = c[0];
263# endif
264#else
265 memcpy(buf, &x, sizeof(Double_t));
266#endif
267 buf += sizeof(Double_t);
268}
269
270inline void frombuf(char *&buf, Bool_t *x)
271{
272 UChar_t x1;
273 x1 = *buf++;
274 *x = (Bool_t) (x1 != 0);
275}
276
277inline void frombuf(char *&buf, UChar_t *x)
278{
279 *x = *buf++;
280}
281
282inline void frombuf(char *&buf, UShort_t *x)
283{
284#ifdef R__BYTESWAP
285# if defined(R__USEASMSWAP)
286 Short_t s;
287 memcpy(&s, buf, sizeof(s));
288 *x = Rbswap_16(s);
289# else
290 char *sw = (char *)x;
291 sw[0] = buf[1];
292 sw[1] = buf[0];
293# endif
294#else
295 memcpy(x, buf, sizeof(UShort_t));
296#endif
297 buf += sizeof(UShort_t);
298}
299
300inline void frombuf(char *&buf, UInt_t *x)
301{
302#ifdef R__BYTESWAP
303# if defined(R__USEASMSWAP)
304 UInt_t i;
305 memcpy(&i, buf, sizeof(i));
306 *x = Rbswap_32(i);
307# else
308 char *sw = reinterpret_cast<char *>(x);
309 sw[0] = buf[3];
310 sw[1] = buf[2];
311 sw[2] = buf[1];
312 sw[3] = buf[0];
313# endif
314#else
315 memcpy(x, buf, sizeof(UInt_t));
316#endif
317 buf += sizeof(UInt_t);
318}
319
320inline void frombuf(char *&buf, ULong_t *x)
321{
322#ifdef R__BYTESWAP
323 char *sw = reinterpret_cast<char *>(x);
324 if (sizeof(ULong_t) == 8) {
325 sw[0] = buf[7];
326 sw[1] = buf[6];
327 sw[2] = buf[5];
328 sw[3] = buf[4];
329 sw[4] = buf[3];
330 sw[5] = buf[2];
331 sw[6] = buf[1];
332 sw[7] = buf[0];
333 } else {
334 sw[0] = buf[7];
335 sw[1] = buf[6];
336 sw[2] = buf[5];
337 sw[3] = buf[4];
338 }
339#else
340 if (sizeof(ULong_t) == 8) {
341 memcpy(x, buf, 8);
342 } else {
343 memcpy(x, buf+4, 4);
344 }
345#endif
346 buf += 8;
347}
348
349inline void frombuf(char *&buf, ULong64_t *x)
350{
351#ifdef R__BYTESWAP
352# if defined(R__USEASMSWAP)
353 ULong64_t l;
354 memcpy(&l, buf, sizeof(l));
355 *x = Rbswap_64(l);
356# else
357 char *sw = reinterpret_cast<char *>(x);
358 sw[0] = buf[7];
359 sw[1] = buf[6];
360 sw[2] = buf[5];
361 sw[3] = buf[4];
362 sw[4] = buf[3];
363 sw[5] = buf[2];
364 sw[6] = buf[1];
365 sw[7] = buf[0];
366# endif
367#else
368 memcpy(x, buf, sizeof(ULong64_t));
369#endif
370 buf += sizeof(ULong64_t);
371}
372
373inline void frombuf(char *&buf, Float_t *x)
374{
375#ifdef R__BYTESWAP
376# if defined(R__USEASMSWAP)
377 UInt_t i;
378 memcpy(&i, buf, sizeof(i));
379 i = Rbswap_32(i);
380 memcpy(x, &i, sizeof(i));
381# else
382 char c[4];
383 c[0] = buf[3];
384 c[1] = buf[2];
385 c[2] = buf[1];
386 c[3] = buf[0];
387 memcpy(x, c, sizeof(c));
388# endif
389#else
390 memcpy(x, buf, sizeof(Float_t));
391#endif
392 buf += sizeof(Float_t);
393}
394
395inline void frombuf(char *&buf, Double_t *x)
396{
397#ifdef R__BYTESWAP
398# if defined(R__USEASMSWAP)
399 ULong64_t l;
400 memcpy(&l, buf, sizeof(l));
401 l = Rbswap_64(l);
402 memcpy(x, &l, sizeof(l));
403# else
404 char c[8];
405 c[0] = buf[7];
406 c[1] = buf[6];
407 c[2] = buf[5];
408 c[3] = buf[4];
409 c[4] = buf[3];
410 c[5] = buf[2];
411 c[6] = buf[1];
412 c[7] = buf[0];
413 memcpy(x, c, sizeof(c));
414# endif
415#else
416 memcpy(x, buf, sizeof(Double_t));
417#endif
418 buf += sizeof(Double_t);
419}
420
421inline void tobuf(char *&buf, Char_t x) { tobuf(buf, (UChar_t) x); }
422inline void tobuf(char *&buf, Short_t x) { tobuf(buf, (UShort_t) x); }
423inline void tobuf(char *&buf, Int_t x) { tobuf(buf, (UInt_t) x); }
424inline void tobuf(char *&buf, Long64_t x) { tobuf(buf, (ULong64_t) x); }
425
426inline void frombuf(char *&buf, Char_t *x) { frombuf(buf, (UChar_t *) x); }
427inline void frombuf(char *&buf, Short_t *x) { frombuf(buf, (UShort_t *) x); }
428inline void frombuf(char *&buf, Int_t *x) { frombuf(buf, (UInt_t *) x); }
429inline void frombuf(char *&buf, Long_t *x) { frombuf(buf, (ULong_t *) x); }
430inline void frombuf(char *&buf, Long64_t *x) { frombuf(buf, (ULong64_t *) x); }
431
432
433//______________________________________________________________________________
434#ifdef R__BYTESWAP
436{
437#if defined(R__USEASMSWAP)
438 return Rbswap_16(x);
439#else
440 return (((x & 0x00ff) << 8) | ((x & 0xff00) >> 8));
441#endif
442}
443
444inline UInt_t host2net(UInt_t x)
445{
446#if defined(R__USEASMSWAP)
447 return Rbswap_32(x);
448#else
449 return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
450 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
451#endif
452}
453
455{
456#if defined(R__B64) && !defined(_WIN64)
457# if defined(R__USEASMSWAP)
458 return Rbswap_64(x);
459# else
460 char sw[sizeof(ULong_t)];
461 void *tmp = sw;
462 *(ULong_t *)tmp = x;
463
464 char *sb = (char *)&x;
465 sb[0] = sw[7];
466 sb[1] = sw[6];
467 sb[2] = sw[5];
468 sb[3] = sw[4];
469 sb[4] = sw[3];
470 sb[5] = sw[2];
471 sb[6] = sw[1];
472 sb[7] = sw[0];
473 return x;
474# endif
475#else
476 return (ULong_t)host2net((UInt_t) x);
477#endif
478}
479
481{
482#if defined(R__USEASMSWAP)
483 return Rbswap_64(x);
484#else
485 char sw[sizeof(ULong64_t)];
486 void *tmp = sw;
487 *(ULong64_t *)tmp = x;
488
489 char *sb = (char *)&x;
490 sb[0] = sw[7];
491 sb[1] = sw[6];
492 sb[2] = sw[5];
493 sb[3] = sw[4];
494 sb[4] = sw[3];
495 sb[5] = sw[2];
496 sb[6] = sw[1];
497 sb[7] = sw[0];
498 return x;
499#endif
500}
501
503{
504 // Use a union to allow strict-aliasing
505 union {
506 volatile UInt_t i;
507 volatile Float_t f;
508 } u;
509 u.f = xx;
510#if defined(R__USEASMSWAP)
511 u.i = Rbswap_32(u.i);
512#else
513 u.i = (((u.i & 0x000000ffU) << 24) | ((u.i & 0x0000ff00U) << 8) |
514 ((u.i & 0x00ff0000U) >> 8) | ((u.i & 0xff000000U) >> 24));
515#endif
516 return u.f;
517}
518
520{
521#if defined(R__USEASMSWAP)
522 // Use a union to allow strict-aliasing
523 union {
524 volatile ULong64_t l;
525 volatile Double_t d;
526 } u;
527 u.d = x;
528 u.l = Rbswap_64(u.l);
529 return u.d;
530# else
531 char sw[sizeof(Double_t)];
532 void *tmp = sw;
533 *(Double_t *)tmp = x;
534
535 char *sb = (char *)&x;
536 sb[0] = sw[7];
537 sb[1] = sw[6];
538 sb[2] = sw[5];
539 sb[3] = sw[4];
540 sb[4] = sw[3];
541 sb[5] = sw[2];
542 sb[6] = sw[1];
543 sb[7] = sw[0];
544 return x;
545#endif
546}
547#else /* R__BYTESWAP */
548inline UShort_t host2net(UShort_t x) { return x; }
549inline UInt_t host2net(UInt_t x) { return x; }
550inline ULong_t host2net(ULong_t x) { return x; }
551inline ULong_t host2net(ULong64_t x) { return x; }
552inline Float_t host2net(Float_t x) { return x; }
553inline Double_t host2net(Double_t x) { return x; }
554#endif
555
557inline Int_t host2net(Int_t x) { return host2net((UInt_t)x); }
558inline Long_t host2net(Long_t x) { return host2net((ULong_t)x); }
560
561inline UShort_t net2host(UShort_t x) { return host2net(x); }
562inline Short_t net2host(Short_t x) { return host2net(x); }
563inline UInt_t net2host(UInt_t x) { return host2net(x); }
564inline Int_t net2host(Int_t x) { return host2net(x); }
565inline ULong_t net2host(ULong_t x) { return host2net(x); }
566inline Long_t net2host(Long_t x) { return host2net(x); }
568inline Long64_t net2host(Long64_t x) { return host2net(x); }
569inline Float_t net2host(Float_t x) { return host2net(x); }
570inline Double_t net2host(Double_t x) { return host2net(x); }
571
572#endif
UShort_t host2net(UShort_t x)
Definition Bytes.h:548
void frombuf(char *&buf, Bool_t *x)
Definition Bytes.h:270
UShort_t net2host(UShort_t x)
Definition Bytes.h:561
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
Basic types used by ROOT and required by TInterpreter.
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
unsigned short UShort_t
Unsigned Short integer 2 bytes (unsigned short)
Definition RtypesCore.h:54
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
unsigned char UChar_t
Unsigned Character 1 byte (unsigned char)
Definition RtypesCore.h:52
char Char_t
Character 1 byte (char)
Definition RtypesCore.h:51
unsigned long ULong_t
Unsigned long integer 4 bytes (unsigned long). Size depends on architecture.
Definition RtypesCore.h:69
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:60
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
short Short_t
Signed Short integer 2 bytes (short)
Definition RtypesCore.h:53
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
unsigned long long ULong64_t
Portable unsigned long integer 8 bytes.
Definition RtypesCore.h:84
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char x1
Double_t x[n]
Definition legend1.C:17
TLine l
Definition textangle.C:4