-
Notifications
You must be signed in to change notification settings - Fork 489
/
test_driver_mydecimal.go
287 lines (266 loc) · 6.31 KB
/
test_driver_mydecimal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
//+build !codes
package test_driver
const panicInfo = "This branch is not implemented. " +
"This is because you are trying to test something specific to TiDB's MyDecimal implementation. " +
"It is recommended to do this in TiDB repository."
// constant values.
const (
maxWordBufLen = 9 // A MyDecimal holds 9 words.
digitsPerWord = 9 // A word holds 9 digits.
digMask = 100000000
)
var (
wordBufLen = 9
)
// fixWordCntError limits word count in wordBufLen, and returns overflow or truncate error.
func fixWordCntError(wordsInt, wordsFrac int) (newWordsInt int, newWordsFrac int, err error) {
if wordsInt+wordsFrac > wordBufLen {
panic(panicInfo)
}
return wordsInt, wordsFrac, nil
}
/*
countLeadingZeroes returns the number of leading zeroes that can be removed from fraction.
@param i start index
@param word value to compare against list of powers of 10
*/
func countLeadingZeroes(i int, word int32) int {
leading := 0
for word < pow10(i) {
i--
leading++
}
return leading
}
func digitsToWords(digits int) int {
return (digits + digitsPerWord - 1) / digitsPerWord
}
// MyDecimal represents a decimal value.
type MyDecimal struct {
digitsInt int8 // the number of *decimal* digits before the point.
digitsFrac int8 // the number of decimal digits after the point.
resultFrac int8 // result fraction digits.
negative bool
// wordBuf is an array of int32 words.
// A word is an int32 value can hold 9 digits.(0 <= word < wordBase)
wordBuf [maxWordBufLen]int32
}
// String returns the decimal string representation rounded to resultFrac.
func (d *MyDecimal) String() string {
tmp := *d
return string(tmp.ToString())
}
func (d *MyDecimal) stringSize() int {
// sign, zero integer and dot.
return int(d.digitsInt + d.digitsFrac + 3)
}
func (d *MyDecimal) removeLeadingZeros() (wordIdx int, digitsInt int) {
digitsInt = int(d.digitsInt)
i := ((digitsInt - 1) % digitsPerWord) + 1
for digitsInt > 0 && d.wordBuf[wordIdx] == 0 {
digitsInt -= i
i = digitsPerWord
wordIdx++
}
if digitsInt > 0 {
digitsInt -= countLeadingZeroes((digitsInt-1)%digitsPerWord, d.wordBuf[wordIdx])
} else {
digitsInt = 0
}
return
}
// ToString converts decimal to its printable string representation without rounding.
//
// RETURN VALUE
//
// str - result string
// errCode - eDecOK/eDecTruncate/eDecOverflow
//
func (d *MyDecimal) ToString() (str []byte) {
str = make([]byte, d.stringSize())
digitsFrac := int(d.digitsFrac)
wordStartIdx, digitsInt := d.removeLeadingZeros()
if digitsInt+digitsFrac == 0 {
digitsInt = 1
wordStartIdx = 0
}
digitsIntLen := digitsInt
if digitsIntLen == 0 {
digitsIntLen = 1
}
digitsFracLen := digitsFrac
length := digitsIntLen + digitsFracLen
if d.negative {
length++
}
if digitsFrac > 0 {
length++
}
str = str[:length]
strIdx := 0
if d.negative {
str[strIdx] = '-'
strIdx++
}
var fill int
if digitsFrac > 0 {
fracIdx := strIdx + digitsIntLen
fill = digitsFracLen - digitsFrac
wordIdx := wordStartIdx + digitsToWords(digitsInt)
str[fracIdx] = '.'
fracIdx++
for ; digitsFrac > 0; digitsFrac -= digitsPerWord {
x := d.wordBuf[wordIdx]
wordIdx++
for i := myMin(digitsFrac, digitsPerWord); i > 0; i-- {
y := x / digMask
str[fracIdx] = byte(y) + '0'
fracIdx++
x -= y * digMask
x *= 10
}
}
for ; fill > 0; fill-- {
str[fracIdx] = '0'
fracIdx++
}
}
fill = digitsIntLen - digitsInt
if digitsInt == 0 {
fill-- /* symbol 0 before digital point */
}
for ; fill > 0; fill-- {
str[strIdx] = '0'
strIdx++
}
if digitsInt > 0 {
strIdx += digitsInt
wordIdx := wordStartIdx + digitsToWords(digitsInt)
for ; digitsInt > 0; digitsInt -= digitsPerWord {
wordIdx--
x := d.wordBuf[wordIdx]
for i := myMin(digitsInt, digitsPerWord); i > 0; i-- {
y := x / 10
strIdx--
str[strIdx] = '0' + byte(x-y*10)
x = y
}
}
} else {
str[strIdx] = '0'
}
return
}
// FromString parses decimal from string.
func (d *MyDecimal) FromString(str []byte) error {
for i := 0; i < len(str); i++ {
if !isSpace(str[i]) {
str = str[i:]
break
}
}
if len(str) == 0 {
panic(panicInfo)
}
switch str[0] {
case '-':
d.negative = true
fallthrough
case '+':
str = str[1:]
}
var strIdx int
for strIdx < len(str) && isDigit(str[strIdx]) {
strIdx++
}
digitsInt := strIdx
var digitsFrac int
var endIdx int
if strIdx < len(str) && str[strIdx] == '.' {
endIdx = strIdx + 1
for endIdx < len(str) && isDigit(str[endIdx]) {
endIdx++
}
digitsFrac = endIdx - strIdx - 1
} else {
digitsFrac = 0
endIdx = strIdx
}
if digitsInt+digitsFrac == 0 {
panic(panicInfo)
}
wordsInt := digitsToWords(digitsInt)
wordsFrac := digitsToWords(digitsFrac)
wordsInt, _, err := fixWordCntError(wordsInt, wordsFrac)
if err != nil {
panic(panicInfo)
}
d.digitsInt = int8(digitsInt)
d.digitsFrac = int8(digitsFrac)
wordIdx := wordsInt
strIdxTmp := strIdx
var word int32
var innerIdx int
for digitsInt > 0 {
digitsInt--
strIdx--
word += int32(str[strIdx]-'0') * pow10(innerIdx)
innerIdx++
if innerIdx == digitsPerWord {
wordIdx--
d.wordBuf[wordIdx] = word
word = 0
innerIdx = 0
}
}
if innerIdx != 0 {
wordIdx--
d.wordBuf[wordIdx] = word
}
wordIdx = wordsInt
strIdx = strIdxTmp
word = 0
innerIdx = 0
for digitsFrac > 0 {
digitsFrac--
strIdx++
word = int32(str[strIdx]-'0') + word*10
innerIdx++
if innerIdx == digitsPerWord {
d.wordBuf[wordIdx] = word
wordIdx++
word = 0
innerIdx = 0
}
}
if innerIdx != 0 {
d.wordBuf[wordIdx] = word * pow10(digitsPerWord-innerIdx)
}
if endIdx+1 <= len(str) && (str[endIdx] == 'e' || str[endIdx] == 'E') {
panic(panicInfo)
}
allZero := true
for i := 0; i < wordBufLen; i++ {
if d.wordBuf[i] != 0 {
allZero = false
break
}
}
if allZero {
d.negative = false
}
d.resultFrac = d.digitsFrac
return err
}