uint128_t 添加 c++ 重载类型强制转换
发布时间:2021-01-10 04:49:24 所属栏目:系统 来源:网络整理
导读:类型声明: 1 class uint128 2 { 3 public : 4 uint128() :hi( 0 ),lo( 0 ){} 5 uint128(uint32_t l) :hi( 0 ),lo(l){} 6 uint128(int32_t l) :hi(-(l 0 )),lo(l){} 7 uint128(int64_t l) :hi(-(l 0 )),lo(l){} 8 uint128(uint64_t l) :hi( 0 ),lo(l){} 9 ui
|
实现函数: 1 uint128::uint128(const std::string &sz)
2 :hi(0),lo(0)
3 {
4 // do we have at least one character?
5 if (!sz.empty()) {
6 // make some reasonable assumptions
7 int radix = 10;
8 bool minus = false;
9
10 std::string::const_iterator i = sz.begin();
11
12 // check for minus sign,i suppose technically this should only apply
13 // to base 10,but who says that -0x1 should be invalid?
14 if (*i == ‘-‘) {
15 ++i;
16 minus = true;
17 }
18
19 // check if there is radix changing prefix (0 or 0x)
20 if (i != sz.end()) {
21 if (*i == ‘0‘) {
22 radix = 8;
23 ++i;
24 if (i != sz.end()) {
25 if (*i == ‘x‘) {
26 radix = 16;
27 ++i;
28 }
29 }
30 }
31
32 while (i != sz.end()) {
33 unsigned int n = 0;
34 const char ch = *i;
35
36 if (ch >= ‘A‘ && ch <= ‘Z‘) {
37 if (((ch - ‘A‘) + 10) < radix) {
38 n = (ch - ‘A‘) + 10;
39 }
40 else {
41 break;
42 }
43 }
44 else if (ch >= ‘a‘ && ch <= ‘z‘) {
45 if (((ch - ‘a‘) + 10) < radix) {
46 n = (ch - ‘a‘) + 10;
47 }
48 else {
49 break;
50 }
51 }
52 else if (ch >= ‘0‘ && ch <= ‘9‘) {
53 if ((ch - ‘0‘) < radix) {
54 n = (ch - ‘0‘);
55 }
56 else {
57 break;
58 }
59 }
60 else {
61 /* completely invalid character */
62 break;
63 }
64
65 (*this) *= radix;
66 (*this) += n;
67
68 ++i;
69 }
70 }
71
72 if (minus) {
73 *this = -*this;
74 }
75 }
76 }
77
78 uint128& uint128::operator<<=(const uint128& rhs)
79 {
80 if (rhs >= 128)
81 {
82 hi = 0;
83 lo = 0;
84 }
85 else
86 {
87 unsigned int n = rhs.to_integer();
88 const unsigned int halfsize = 128 / 2;
89
90 if (n >= halfsize){
91 n -= halfsize;
92 hi = lo;
93 lo = 0;
94 }
95
96 if (n != 0) {
97 // shift high half
98 hi <<= n;
99
100 const uint64_t mask(~(uint64_t(-1) >> n));
101
102 // and add them to high half
103 hi |= (lo & mask) >> (halfsize - n);
104
105 // and finally shift also low half
106 lo <<= n;
107 }
108 }
109
110 return *this;
111 }
112
113 uint128 & uint128::operator>>=(const uint128& rhs)
114 {
115 if (rhs >= 128)
116 {
117 hi = 0;
118 lo = 0;
119 }
120 else
121 {
122 unsigned int n = rhs.to_integer();
123 const unsigned int halfsize = 128 / 2;
124
125 if (n >= halfsize) {
126 n -= halfsize;
127 lo = hi;
128 hi = 0;
129 }
130
131 if (n != 0) {
132 // shift low half
133 lo >>= n;
134
135 // get lower N bits of high half
136 const uint64_t mask(~(uint64_t(-1) << n));
137
138 // and add them to low qword
139 lo |= (hi & mask) << (halfsize - n);
140
141 // and finally shift also high half
142 hi >>= n;
143 }
144 }
145 return *this;
146 }
147
148 uint128& uint128::operator*=(const uint128 &b)
149 {
150 uint64_t a0 = (uint32_t)(this->lo);
151 uint64_t a1 = (uint32_t)(this->lo >> 0x20);
152 uint64_t a2 = (uint32_t)(this->hi);
153 uint64_t a3 = (uint32_t)(this->hi >> 0x20);
154
155 uint64_t b0 = (uint32_t)(b.lo);
156 uint64_t b1 = (uint32_t)(b.lo >> 0x20);
157 uint64_t b2 = (uint32_t)(b.hi);
158 uint64_t b3 = (uint32_t)(b.hi >> 0x20);
159
160
161 this->hi = 0;
162 this->lo = a3*b0;
163 (*this) += a2*b1;
164 (*this) += a1*b2;
165 (*this) += a0*b3;
166 (*this) <<= 0x20;
167 (*this) += a2*b0;
168 (*this) += a1*b1;
169 (*this) += a0*b2;
170 (*this) <<= 0x20;
171 (*this) += a1*b0;
172 (*this) += a0*b1;
173 (*this) <<= 0x20;
174 (*this) += a0*b0;
175
176 return *this;
177 }
(编辑:清远站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |

