~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/src/common/util/testBitmask.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2004-2005 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
15
 
 
16
#include <Bitmask.hpp>
 
17
#include <NdbOut.hpp>
 
18
 
 
19
void
 
20
BitmaskImpl::getFieldImpl(const Uint32 src[],
 
21
                          unsigned shiftL, unsigned len, Uint32 dst[])
 
22
{
 
23
  /* Copy whole words of src to dst, shifting src left
 
24
   * by shiftL.  Undefined bits of the last written dst word
 
25
   * should be zeroed.
 
26
   */
 
27
  assert(shiftL < 32);
 
28
 
 
29
  unsigned shiftR = 32 - shiftL;
 
30
  unsigned undefined = shiftL ? ~0 : 0;
 
31
 
 
32
  /* Merge first word with previously set bits if there's a shift */
 
33
  * dst = shiftL ? * dst : 0;
 
34
 
 
35
  /* Treat the zero-shift case separately to avoid
 
36
   * trampling or reading past the end of src
 
37
   */
 
38
  if (shiftL == 0)
 
39
  {
 
40
    while(len >= 32)
 
41
    {
 
42
      * dst++ = * src++;
 
43
      len -=32;
 
44
    }
 
45
 
 
46
    if (len != 0)
 
47
    {
 
48
      /* Last word has some bits set */
 
49
      Uint32 mask= ((1 << len) -1); // 0000111
 
50
      * dst = (* src) & mask;
 
51
    }
 
52
  }
 
53
  else // shiftL !=0, need to build each word from two words shifted
 
54
  {
 
55
    while(len >= 32)
 
56
    {
 
57
      * dst++ |= (* src) << shiftL;
 
58
      * dst = ((* src++) >> shiftR) & undefined;
 
59
      len -= 32;
 
60
    }
 
61
 
 
62
    /* Have space for shiftR more bits in the current dst word
 
63
     * is that enough?
 
64
     */
 
65
    if(len <= shiftR)
 
66
    {
 
67
      /* Fit the remaining bits in the current dst word */
 
68
      * dst |= ((* src) & ((1 << len) - 1)) << shiftL;
 
69
    }
 
70
    else
 
71
    {
 
72
      /* Need to write to two dst words */
 
73
      * dst++ |= ((* src) << shiftL);
 
74
      * dst = ((* src) >> shiftR) & ((1 << (len - shiftR)) - 1) & undefined;
 
75
    }
 
76
  }
 
77
}
 
78
 
 
79
void
 
80
BitmaskImpl::setFieldImpl(Uint32 dst[],
 
81
                          unsigned shiftL, unsigned len, const Uint32 src[])
 
82
{
 
83
  /**
 
84
   *
 
85
   * abcd ef00
 
86
   * 00ab cdef
 
87
   */
 
88
  assert(shiftL < 32);
 
89
  unsigned shiftR = 32 - shiftL;
 
90
  unsigned undefined = shiftL ? ~0 : 0;  
 
91
  while(len >= 32)
 
92
  {
 
93
    * dst = (* src++) >> shiftL;
 
94
    * dst++ |= ((* src) << shiftR) & undefined;
 
95
    len -= 32;
 
96
  }
 
97
  
 
98
  /* Copy last bits */
 
99
  Uint32 mask = ((1 << len) -1);
 
100
  * dst = (* dst & ~mask);
 
101
  if(len <= shiftR)
 
102
  {
 
103
    /* Remaining bits fit in current word */
 
104
    * dst |= ((* src++) >> shiftL) & mask;
 
105
  }
 
106
  else
 
107
  {
 
108
    /* Remaining bits update 2 words */
 
109
    * dst |= ((* src++) >> shiftL);
 
110
    * dst |= ((* src) & ((1 << (len - shiftR)) - 1)) << shiftR ;
 
111
  }
 
112
}
 
113
 
 
114
/* Bitmask testcase code moved from here to
 
115
 * storage/ndb/test/ndbapi/testBitfield.cpp
 
116
 * to get coverage from automated testing
 
117
 */