source: soft/giet_vm/memo/include/libelfpp/dpp/bitfield @ 163

Last change on this file since 163 was 163, checked in by karaoui, 12 years ago

changing mover to memo
changing soft.bin to soft.elf
...

File size: 5.5 KB
Line 
1/* -*- c++ -*-
2
3   C++ bitfield alias class
4
5   This file is part of the dpp library of C++ template classes
6
7   doc: http://diaxen.ssji.net/dpp/index.html
8   repo: https://www.ssji.net/svn/projets/trunk/libdpp
9
10   This program is free software: you can redistribute it and/or
11   modify it under the terms of the GNU Lesser General Public License
12   as published by the Free Software Foundation, either version 3 of
13   the License, or (at your option) any later version.
14
15   This program is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public
21   License along with this program.  If not, see
22   <http://www.gnu.org/licenses/>.
23
24   (c) 2011 Alexandre Becoulet <alexandre.becoulet@free.fr>
25
26*/
27
28#ifndef DPP_BITFIELD_HH_
29#define DPP_BITFIELD_HH_
30
31#include <stdint.h>
32
33/** @file @module{Bitfield alias} */
34
35namespace dpp {
36
37#ifndef  DPP_BITFIELD_SWAP
38  /** @This globally changes swap mask used by @ref bitfield classes
39      by xoring with the swap mask value. */
40# define DPP_BITFIELD_SWAP 0
41#endif
42
43  /** @internal */
44  template <typename X, unsigned int swap_mask>
45  inline X _bitfield_swap(X x)
46  {
47    unsigned int m = swap_mask ^ (DPP_BITFIELD_SWAP);
48
49    if (m & 1)
50      x = ((x & 0x5555555555555555ULL) << 1)  | ((x & 0xaaaaaaaaaaaaaaaaULL) >> 1);
51    if (m & 2)
52      x = ((x & 0x3333333333333333ULL) << 2)  | ((x & 0xccccccccccccccccULL) >> 2);
53    if (m & 4)
54      x = ((x & 0x0f0f0f0f0f0f0f0fULL) << 4)  | ((x & 0xf0f0f0f0f0f0f0f0ULL) >> 4);
55    if (sizeof(X) > 8 && (m & 8))
56      x = ((x & 0x00ff00ff00ff00ffULL) << 8)  | ((x & 0xff00ff00ff00ff00ULL) >> 8);
57    if (sizeof(X) > 16 && (m & 16))
58      x = ((x & 0x0000ffff0000ffffULL) << 16) | ((x & 0xffff0000ffff0000ULL) >> 16);
59    if (sizeof(X) > 32 && (m & 32))
60      x = ((x & 0x00000000ffffffffULL) << 32) | ((x & 0xffffffff00000000ULL) >> 32);
61
62    return x;
63  }
64
65#define _DPP_BITFIELD(C, X)                                             \
66  class C                                                               \
67  {                                                                     \
68    static const X mask = ((1 << width) - 1);                           \
69    static const X smask = mask << position;                            \
70                                                                        \
71  public:                                                               \
72                                                                        \
73    /** @This creates a bitfield alias object for the given integer. */ \
74    C(X &x)                                                             \
75      : _x(x)                                                           \
76    {                                                                   \
77    }                                                                   \
78                                                                        \
79    /** @This extracts bits from given integer. */                      \
80    static const X get(X x)                                             \
81    {                                                                   \
82      return (_bitfield_swap<X, swap_mask>(x) >> position) & mask;      \
83    }                                                                   \
84                                                                        \
85    /** @This sets bits in referenced integer to given value. */        \
86    static void set(X &v, X x)                                          \
87    {                                                                   \
88      static const X wmask = _bitfield_swap<X, swap_mask>(smask);       \
89      x = _bitfield_swap<X, swap_mask>(x << position);                  \
90      v = (v & ~wmask) | (x & wmask);                                   \
91    }                                                                   \
92                                                                        \
93    /** @This extracts bits from aliased integer. */                    \
94    operator X () const                                                 \
95    {                                                                   \
96      return get(_x);                                                   \
97    }                                                                   \
98                                                                        \
99    /** @This sets bits in aliased integer to given value. */           \
100    X operator=(X x)                                                    \
101    {                                                                   \
102      set(_x, x);                                                       \
103      return x;                                                         \
104    }                                                                   \
105                                                                        \
106  private:                                                              \
107    X &_x;                                                              \
108  }
109
110  /** @short Generic bitfield alias class
111      @module {Bitfield alias}
112      @header dpp/bitfield
113      @main
114
115      @This can be used to declare an alias which permit convenient
116      access to specified bits range inside an integer value.
117
118      This class requires the integer type as parameter, other classes
119      are available for 8, 16, 32 and 64 bits, signed and unsigned
120      integer types.
121
122      This class exposes bits at specified @tt position and @tt width
123      in aliased integer value.
124
125      @section {Bits swapping}
126
127      An additional @tt swap_mask parameter can be used to perform a
128      bits permutation on accessed integer. Each bit in the mask specify
129      size of swapped bit packets; for instance a mask value of
130      (8+16+32) performs a bytes swap on 32 bits values.
131
132      The value of the @ref DPP_BITFIELD_SWAP macro is xored with the
133      swap mask. Its default value is 0 and it can be defined before
134      including the @tt bitfield header. This allows globally
135      inverting swap mask depending on processor endianness.
136
137      @end section
138   */
139  template <typename X,
140            unsigned int position,
141            unsigned int width = 1,
142            unsigned int swap_mask = 0>
143  _DPP_BITFIELD(bitfield, X);
144
145
146#define _DPP_BITFIELD_INT(u, s, n, t)                                   \
147  /** @short s bits u bitfield alias class                              \
148      @module {Bitfield alias}                                          \
149      @header dpp/bitfield                                              \
150                                                                        \
151      @This can be used to declare an alias which permit convenient     \
152      access to specified bits range inside an integer value.           \
153                                                                        \
154      This class is designed to work with s bits u values. See the      \
155      @ref bitfield class documentation for details.                    \
156  */                                                                    \
157  template <unsigned int position,                                      \
158            unsigned int width = 1,                                     \
159            unsigned int swap_mask = 0>                                 \
160  _DPP_BITFIELD(bitfield_##n, t);
161
162  _DPP_BITFIELD_INT(unsigned, 8,  u8,  uint8_t);
163  _DPP_BITFIELD_INT(signed, 8,  s8,  int8_t);
164  _DPP_BITFIELD_INT(unsigned, 16, u16, uint16_t);
165  _DPP_BITFIELD_INT(signed, 16, s16, int16_t);
166  _DPP_BITFIELD_INT(unsigned, 32, u32, uint32_t);
167  _DPP_BITFIELD_INT(signed, 32, s32, int32_t);
168  _DPP_BITFIELD_INT(unsigned, 64, u64, uint64_t);
169  _DPP_BITFIELD_INT(signed, 64, s64, int64_t);
170
171}
172
173#endif
174
Note: See TracBrowser for help on using the repository browser.