SizeTraits.h revision d0fbbb227051be16931a1aa9b4a7722ac039c698
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#ifdef bswap16
70#undef bswap16
71#endif
72#ifdef bswap32
73#undef bswap32
74#endif
75#ifdef bswap64
76#undef bswap64
77#endif
78
79/// bswap16 - byte swap 16-bit version
80/// @ref binary utilities - elfcpp_swap
81inline uint16_t bswap16(uint16_t pData)
82{
83   return ((pData >> 8) & 0xFF) | ((pData & 0xFF) << 8);
84}
85
86/// bswap32 - byte swap 32-bit version
87/// @ref elfcpp_swap
88inline uint32_t bswap32(uint32_t pData)
89{
90   return (((pData & 0xFF000000) >> 24) |
91           ((pData & 0x00FF0000) >>  8) |
92           ((pData & 0x0000FF00) <<  8) |
93           ((pData & 0x000000FF) << 24));
94
95}
96
97/// bswap64 - byte swap 64-bit version
98/// @ref binary utilities - elfcpp_swap
99inline uint64_t bswap64(uint64_t pData)
100{
101   return (((pData & 0xFF00000000000000ULL) >> 56) |
102           ((pData & 0x00FF000000000000ULL) >> 40) |
103           ((pData & 0x0000FF0000000000ULL) >> 24) |
104           ((pData & 0x000000FF00000000ULL) >>  8) |
105           ((pData & 0x00000000FF000000ULL) <<  8) |
106           ((pData & 0x0000000000FF0000ULL) << 24) |
107           ((pData & 0x000000000000FF00ULL) << 40) |
108           ((pData & 0x00000000000000FFULL) << 56));
109}
110
111template <size_t SizeOfStr, typename FieldType>
112class StringSizerHelper
113{
114private:
115  char FIELD_TOO_SMALL[SizeOfStr <= FieldType(~0U) ? 1 : -1];
116public:
117  enum { Size = SizeOfStr };
118};
119
120#define STR_SIZE(str, fieldTy) StringSizerHelper<sizeof(str)-1, fieldTy>::Size
121
122} // namespace of mcld
123
124#endif
125
126