HPCToolkit
ByteUtilities.hpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL: https://hpctoolkit.googlecode.com/svn/branches/hpctoolkit-hpcserver/src/tool/hpcserver/ByteUtilities.hpp $
6 // $Id: ByteUtilities.hpp 4291 2013-07-09 22:25:53Z felipet1326@gmail.com $
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL: https://hpctoolkit.googlecode.com/svn/branches/hpctoolkit-hpcserver/src/tool/hpcserver/ByteUtilities.hpp $
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 
61 
62 #ifndef BYTEUTILITIES_H_
63 #define BYTEUTILITIES_H_
64 
65 #include <stdint.h>
66 
67 namespace TraceviewerServer
68 {
69  typedef int64_t Long;
71  {
72  public:
73 
74  static short readShort(char* buffer)
75  {
76  unsigned char* uBuffer = (unsigned char*) buffer;
77  return (uBuffer[0] << 8)| (uBuffer[1]);
78  }
79 
80  static int readInt(char* buffer)
81  {
82  unsigned char* uBuffer = (unsigned char*) buffer;
83  return ((uBuffer[0] << 24) | (uBuffer[1] << 16) | (uBuffer[2] << 8)
84  | (uBuffer[3]));
85  }
86  static int64_t readLong(char* buffer)
87  {
88  unsigned int highWord = readInt(buffer);
89  unsigned int lowWord = readInt(buffer + 4);
90  uint64_t combined = (((uint64_t) highWord) << 32) | lowWord;
91  return combined;
92  }
93 
94  static void writeShort(char* buffer, short ToWrite)
95  {
96  unsigned short utoWrite = ToWrite;
97  buffer[0] = (utoWrite & MASK_1) >> 8;
98  buffer[1] = utoWrite & MASK_0;
99  }
100 
101  static void writeInt(char* buffer, int ToWrite)
102  {
103  unsigned int utoWrite = ToWrite;
104 
105  buffer[0] = (utoWrite & MASK_3) >> 24;
106  buffer[1] = (utoWrite & MASK_2) >> 16;
107  buffer[2] = (utoWrite & MASK_1) >> 8;
108  buffer[3] = utoWrite & MASK_0;
109  }
110  static void writeLong(char* buffer, int64_t ToWrite)
111  {
112  uint64_t utoWrite = ToWrite;
113  buffer[0] = (utoWrite & MASK_7) >> 56;
114  buffer[1] = (utoWrite & MASK_6) >> 48;
115  buffer[2] = (utoWrite & MASK_5) >> 40;
116  buffer[3] = (utoWrite & MASK_4) >> 32;
117  buffer[4] = (utoWrite & MASK_3) >> 24;
118  buffer[5] = (utoWrite & MASK_2) >> 16;
119  buffer[6] = (utoWrite & MASK_1) >> 8;
120  buffer[7] = utoWrite & MASK_0;
121  }
122  static Long convertDoubleToLong(double d)
123  {
124  union { double d; Long l;} dbLgConv;
125  dbLgConv.d = d;
126  return dbLgConv.l;
127  }
128  static double convertLongToDouble(Long l)
129  {
130  union { double d; Long l;} dbLgConv;
131  dbLgConv.l = l;
132  return dbLgConv.d;
133  }
134  private:
135  static const unsigned int MASK_0 = 0x000000FF, MASK_1 = 0x0000FF00, MASK_2 =
136  0x00FF0000, MASK_3 = 0xFF000000; //For an int
137  static const uint64_t MASK_4 = 0x000000FF00000000ULL,
138  MASK_5 = 0x0000FF0000000000ULL, MASK_6 = 0x00FF000000000000ULL, MASK_7 =
139  0xFF00000000000000ULL; //for a long
140  };
141 
142 } /* namespace TraceviewerServer */
143 #endif /* BYTEUTILITIES_H_ */
static const unsigned int MASK_2
static const unsigned int MASK_3
static void writeInt(char *buffer, int ToWrite)
static const unsigned int MASK_1
static const unsigned int MASK_0
static int64_t readLong(char *buffer)
static Long convertDoubleToLong(double d)
static int readInt(char *buffer)
static short readShort(char *buffer)
static void writeLong(char *buffer, int64_t ToWrite)
static double convertLongToDouble(Long l)
static void writeShort(char *buffer, short ToWrite)