SizeTraits.h revision affc150dc44fab1911775a49636d0ce85333b634
1//===- SizeTraits.h -------------------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#ifndef MCLD_SIZE_TRAITS_H
10#define MCLD_SIZE_TRAITS_H
11#ifdef ENABLE_UNITTEST
12#include <gtest.h>
13#endif
14
15#include <llvm/Support/DataTypes.h>
16
17namespace mcld
18{
19
20template<size_t SIZE>
21class SizeTraits;
22
23template<>
24class SizeTraits<32>
25{
26public:
27  typedef uint32_t Address;
28  typedef uint32_t Offset;
29  typedef uint32_t Word;
30  typedef int32_t  SWord;
31};
32
33template<>
34class SizeTraits<64>
35{
36public:
37  typedef uint64_t Address;
38  typedef uint64_t Offset;
39  typedef uint64_t Word;
40  typedef int64_t  SWord;
41};
42
43/// alignAddress - helper function to align an address with given alignment
44/// constraint
45///
46/// @param pAddr - the address to be aligned
47/// @param pAlignConstraint - the alignment used to align the given address
48inline void alignAddress(uint64_t& pAddr, uint64_t pAlignConstraint)
49{
50  if (pAlignConstraint != 0)
51    pAddr = (pAddr + pAlignConstraint - 1) &~ (pAlignConstraint - 1);
52}
53
54template<size_t Constraint>
55uint64_t Align(uint64_t pAddress);
56
57template<>
58inline uint64_t Align<32>(uint64_t pAddress)
59{
60  return (pAddress + 0x1F) & (~0x1F);
61}
62
63template<>
64inline uint64_t Align<64>(uint64_t pAddress)
65{
66  return (pAddress + 0x3F) & (~0x3F);
67}
68
69/// bswap16 - byte swap 16-bit version
70/// @ref binary utilities - elfcpp_swap
71inline uint16_t bswap16(uint16_t pData)
72{
73   return ((pData >> 8) & 0xFF) | ((pData & 0xFF) << 8);
74}
75
76/// bswap32 - byte swap 32-bit version
77/// @ref elfcpp_swap
78inline uint32_t bswap32(uint32_t pData)
79{
80   return (((pData & 0xFF000000) >> 24) |
81           ((pData & 0x00FF0000) >>  8) |
82           ((pData & 0x0000FF00) <<  8) |
83           ((pData & 0x000000FF) << 24));
84
85}
86
87/// bswap64 - byte swap 64-bit version
88/// @ref binary utilities - elfcpp_swap
89inline uint64_t bswap64(uint64_t pData)
90{
91   return (((pData & 0xFF00000000000000ULL) >> 56) |
92           ((pData & 0x00FF000000000000ULL) >> 40) |
93           ((pData & 0x0000FF0000000000ULL) >> 24) |
94           ((pData & 0x000000FF00000000ULL) >>  8) |
95           ((pData & 0x00000000FF000000ULL) <<  8) |
96           ((pData & 0x0000000000FF0000ULL) << 24) |
97           ((pData & 0x000000000000FF00ULL) << 40) |
98           ((pData & 0x00000000000000FFULL) << 56));
99}
100
101template <size_t SizeOfStr, typename FieldType>
102class StringSizerHelper
103{
104private:
105  char FIELD_TOO_SMALL[SizeOfStr <= FieldType(~0U) ? 1 : -1];
106public:
107  enum { Size = SizeOfStr };
108};
109
110#define STR_SIZE(str, fieldTy) StringSizerHelper<sizeof(str)-1, fieldTy>::Size
111
112} // namespace of mcld
113
114#endif
115
116