GteUIntegerAP32.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #include <GTEnginePCH.h>
11 #include <algorithm>
12 using namespace gte;
13 
14 #if defined(GTE_COLLECT_UINTEGERAP32_STATISTICS)
15 std::atomic<size_t> UIntegerAP32::msMaxSize;
16 #endif
17 
18 
20  :
21  mNumBits(0)
22 {
23 }
24 
26 {
27  *this = number;
28 }
29 
31 {
32  if (number > 0)
33  {
34  int32_t first = GetLeadingBit(number);
35  int32_t last = GetTrailingBit(number);
36  mNumBits = first - last + 1;
37  mBits.resize(1);
38  mBits[0] = (number >> last);
39  }
40  else
41  {
42  mNumBits = 0;
43  }
44 
45 #if defined(GTE_COLLECT_UINTEGERAP32_STATISTICS)
46  AtomicMax(msMaxSize, mBits.size());
47 #endif
48 }
49 
51 {
52  if (number > 0)
53  {
54  int32_t first = GetLeadingBit(number);
55  int32_t last = GetTrailingBit(number);
56  number >>= last;
57  mNumBits = first - last + 1;
58  mBits.resize(1 + (mNumBits - 1) / 32);
59  mBits[0] = GTE_GET_LO_U64(number);
60  if (mBits.size() > 1)
61  {
62  mBits[1] = GTE_GET_HI_U64(number);
63  }
64  }
65  else
66  {
67  mNumBits = 0;
68  }
69 
70 #if defined(GTE_COLLECT_UINTEGERAP32_STATISTICS)
71  AtomicMax(msMaxSize, mBits.size());
72 #endif
73 }
74 
76  :
77  mNumBits(numBits),
78  mBits(1 + (numBits - 1) / 32)
79 {
80 #if defined(GTE_COLLECT_UINTEGERAP32_STATISTICS)
81  AtomicMax(msMaxSize, mBits.size());
82 #endif
83 }
84 
86 {
87  mNumBits = number.mNumBits;
88  mBits = number.mBits;
89  return *this;
90 }
91 
93 {
94  *this = std::move(number);
95 }
96 
98 {
99  mNumBits = number.mNumBits;
100  mBits = std::move(number.mBits);
101  number.mNumBits = 0;
102  return *this;
103 }
104 
105 void UIntegerAP32::SetNumBits(uint32_t numBits)
106 {
107  mNumBits = numBits;
108  if (mNumBits > 0)
109  {
110  mBits.resize(1 + (numBits - 1) / 32);
111  }
112  else
113  {
114  mBits.clear();
115  }
116 
117 #if defined(GTE_COLLECT_UINTEGERAP32_STATISTICS)
118  AtomicMax(msMaxSize, mBits.size());
119 #endif
120 }
121 
122 bool UIntegerAP32::Write(std::ofstream& output) const
123 {
124  if (output.write((char const*)&mNumBits, sizeof(mNumBits)).bad())
125  {
126  return false;
127  }
128 
129  std::size_t size = mBits.size();
130  if (output.write((char const*)&size, sizeof(size)).bad())
131  {
132  return false;
133  }
134 
135  return output.write((char const*)&mBits[0], size*sizeof(mBits[0])).good();
136 }
137 
138 bool UIntegerAP32::Read(std::ifstream& input)
139 {
140  if (input.read((char*)&mNumBits, sizeof(mNumBits)).bad())
141  {
142  return false;
143  }
144 
145  std::size_t size;
146  if (input.read((char*)&size, sizeof(size)).bad())
147  {
148  return false;
149  }
150 
151  mBits.resize(size);
152  return input.read((char*)&mBits[0], size*sizeof(mBits[0])).good();
153 }
154 
GTE_IMPEXP int32_t GetTrailingBit(uint32_t value)
std::vector< uint32_t > mBits
UIntegerAP32 & operator=(UIntegerAP32 const &number)
GTE_IMPEXP int32_t GetLeadingBit(uint32_t value)
Definition: GteBitHacks.cpp:61
void SetNumBits(uint32_t numBits)
GLsizeiptr size
Definition: glcorearb.h:659
bool Read(std::ifstream &input)
#define GTE_GET_HI_U64(v)
Definition: GteBitHacks.h:19
#define GTE_GET_LO_U64(v)
Definition: GteBitHacks.h:18
GLint first
Definition: glcorearb.h:400
GLenum GLenum GLenum input
Definition: glext.h:9913
T AtomicMax(std::atomic< T > &v0, T const &v1)
bool Write(std::ofstream &output) const


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01