-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixed64.go
438 lines (375 loc) · 10.2 KB
/
fixed64.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package fixed
import (
"errors"
"fmt"
"math"
"math/bits"
"strconv"
"strings"
"sync"
"unsafe"
)
const (
mask64S = 1 << 63
size64M = 52
mask64E = (1<<11 - 1) << size64M
mask64M = (1 << size64M) - 1
shift64E = size64M
bias64 = (1 << (11 - 1)) - 1
)
//Precision is the bit Precision of a fixed-point number
var Precision int64
var onceSet = &sync.Once{}
var precisionBitsNum = Precision
var decimalBitsMask uint64 = 1<<precisionBitsNum - 1
//MaxFixed64 is the maximum number of fixed points,and its value is 2**(63 - Precision) + 2 ** Precision - 2
const MaxFixed64 = Fixed64((1 << 63) - 1)
//SmallestFixed64 is the minimum value of a fixed-point number, and its value is 1/2**Precision - 2**(63-precisionBitsNum)
const SmallestFixed64 = ^Fixed64(0)
//Fixed64Zero is the zero value of fixed number
const Fixed64Zero = Fixed64(0)
//PrecisionNumber is the minimum precision value of a fixed-point number, and its value is 1 / 2**Precision
const PrecisionNumber = Fixed64(1)
// Fixed64 uses uint64 type to facilitate bit conversion.Fixed64 can use +-0.
// Range: [-(2**(63 - Precision) + 2 ** Precision - 2), 2**(63 - Precision) + 2 ** Precision - 2].
type Fixed64 uint64
//SetPrecisionOnce can only be successfully set once
func SetPrecisionOnce(precision uint64) {
onceSet.Do(func() {
if precision > 62 {
panic("Precision overflow")
}
Precision = int64(precision)
precisionBitsNum = Precision
decimalBitsMask = 1<<precisionBitsNum - 1
})
}
// Float64ToFixed64 can convert the normalized number and normalized number conforming to IEEE 754
// double precision floating-point number standard to fixed-point number. When the function processes
// Nan and Inf, there may be a panic
func Float64ToFixed64(value float64) Fixed64 {
var valueBits = math.Float64bits(value)
s := valueBits & mask64S
e := valueBits & mask64E
m := valueBits&mask64M | (1 << size64M)
realE := int64(e>>shift64E) - bias64
fixedD := uint64(0)
var result uint64
var fixedP uint64 = 0
if realE >= 0 {
pBitsNum := size64M - realE
if pBitsNum < 0 {
if precisionBitsNum-pBitsNum > 63-53 {
panic("Fixed number: part digital overflow")
}
fixedD = m << -pBitsNum << precisionBitsNum
} else {
fixedD = m >> pBitsNum << precisionBitsNum
pBitsFlowNum := pBitsNum - precisionBitsNum
pMask := uint64((1 << pBitsNum) - 1)
if pBitsFlowNum <= 0 {
fixedP = (m & pMask) << (-1 * pBitsFlowNum)
} else {
fixedP = roundOdd(m&pMask, uint64(pBitsFlowNum)) >> pBitsFlowNum
}
}
} else {
allMBitsNum := 52 - realE
pFlowBitsNum := allMBitsNum - precisionBitsNum
if pFlowBitsNum > 53 {
return 0
}
if pFlowBitsNum <= 0 {
//pFlowBitsNum *= -1
fixedP = m << (pFlowBitsNum * -1)
} else {
fixedP = roundOdd(m, uint64(pFlowBitsNum)) >> pFlowBitsNum
}
}
result |= s
result |= fixedD
result |= fixedP
return Fixed64(result)
}
// Str2Fixed64 produces the result by converting the string value to a two-part float,
// and then performing a combination of division and addition.The efficiency of the function may not be high,
// there should be a better implementation.
//
// Note: the function only supports processing data in (%d+.%d+)
func Str2Fixed64(value string)(Fixed64,error){
s := uint64(0)
if strings.Contains(value,"-"){
value = value[1:]
s = mask64S
}
values := strings.Split(value,".")
if len(values) == 0 || len(values) > 2{
return 0,errors.New(fmt.Sprintf("Fixed64: %s format is err ",value))
}
if len(values) == 1{
values = append(values, "0")
}
if values[0] == ""{
values[0] = "0"
} else if values[1] == ""{
values[1] = "0"
}
di,e1 := parseStringToFixed64(values[0])
de,e2 := parseStringToFixed64(values[1])
if e1 != nil || e2 != nil{
return 0,errors.New(fmt.Sprintf("Fixed64:%s con not parse to Fixed64 ",value))
}
dee := Float64ToFixed64(math.Pow10(len(values[1])))
return Fixed64(uint64(di.Add(de.Div(dee))) | s),nil
}
func parseStringToFixed64(v string)(Fixed64, error){
if f,err := strconv.ParseFloat(v,64);err != nil{
return 0,err
} else {
return Float64ToFixed64(f),nil
}
}
//SafeFloat64ToFixed64 checks NaN and Inf before floating point conversion, which guarantees some accuracy
func SafeFloat64ToFixed64(value float64) (Fixed64, error) {
if math.IsNaN(value) {
return 0, errors.New("Float64 value is NaN ")
}
if math.IsInf(value, 0) {
return 0, errors.New("Float64 value is Inf ")
}
return Float64ToFixed64(value), nil
}
func (fixed Fixed64) Add(oth Fixed64) Fixed64 {
var fS = uint64(fixed) & mask64S
var oS = uint64(oth) & mask64S
fixed &^= mask64S
oth &^= mask64S
if fS == oS {
hi,lo := add64(uint64(fixed),uint64(oth))
if hi >> 31 > 0{
panic("Fixed64: Add Overflow " + fixed.ToBase10N(18) + " " + oth.ToBase10N(18))
}
return Fixed64(hi << 32 | lo | fS)
} else {
if fixed > oth {
return Fixed64(uint64(fixed-oth) | fS)
} else if fixed < oth {
return Fixed64(uint64(oth-fixed) | oS)
} else {
return 0
}
}
}
func add64(x,y uint64)(hi,lo uint64){
const mask32 = 1 << 32 - 1
var x0 = x >> 32
var x1 = x & mask32
var y0 = y >> 32
var y1 = y & mask32
lo = x1 + y1
var u = lo >> 32
lo = lo & mask32
hi = x0 + y0 + u
return
}
func (fixed Fixed64) Sub(oth Fixed64) Fixed64 {
return fixed.Add(oth ^ mask64S)
}
func (fixed Fixed64) Mul(oth Fixed64) Fixed64 {
var fS = fixed & mask64S
var oS = oth & mask64S
fixed &^= mask64S
oth &^= mask64S
hi, lo := bits.Mul64(uint64(fixed), uint64(oth))
lo = roundOdd(lo, uint64(precisionBitsNum)) >> precisionBitsNum
if hi >> precisionBitsNum > 0{
panic("Fixed64: Number OverFlow")
}
hi = (hi & decimalBitsMask) << (64 - precisionBitsNum)
if hi == 0 && lo == 0 {
return 0
}
return Fixed64(uint64(fS^oS) | hi | lo)
}
func (fixed Fixed64) Div(oth Fixed64) Fixed64 {
var fS = fixed & mask64S
var oS = oth & mask64S
fixed &^= mask64S
oth &^= mask64S
quo, _ := bits.Div64(uint64(fixed>>(64-precisionBitsNum)), uint64(fixed<<precisionBitsNum), uint64(oth))
if quo == 0 {
return 0
}
return Fixed64(uint64(fS^oS) | quo)
}
func (fixed Fixed64) Abs() Fixed64 {
return fixed &^ mask64S
}
func (fixed Fixed64) Equal(oth Fixed64) bool {
return fixed == oth
}
func (fixed Fixed64) Less(oth Fixed64) bool {
return (fixed ^ mask64S) < (oth ^ mask64S)
}
func (fixed Fixed64) Great(oth Fixed64) bool {
return (fixed ^ mask64S) > (oth ^ mask64S)
}
//Converts Fixed64 To float64
func (fixed Fixed64) Float64() float64 {
number := uint64(fixed &^ mask64S)
idx := int64(bits.Len64(number))
if idx != 0 {
e := idx - precisionBitsNum - 1
number = ((1 << idx) - 1) & number
if idx > size64M {
number >>= idx - size64M - 1
} else {
number <<= size64M - idx + 1
}
number = number &^ mask64E
number |= uint64(fixed & mask64S)
number |= uint64((e + bias64) << size64M)
}
return *(*float64)(unsafe.Pointer(&number))
}
func (fixed Fixed64) Int64() int64 {
if fixed&mask64S > 0 {
return int64((fixed&^mask64S)>>precisionBitsNum) * -1
}
return int64((fixed &^ mask64S) >> precisionBitsNum)
}
func (fixed Fixed64) Round() int64 {
decimal := uint64(fixed) & decimalBitsMask
var roundUp int64
if precisionBitsNum > 0 && decimal >= (1<<(precisionBitsNum-1)) {
roundUp = 1
}
if fixed&mask64S > 0 {
return (int64((fixed&^mask64S)>>precisionBitsNum) + roundUp) * -1
}
return int64((fixed&^mask64S)>>precisionBitsNum) + roundUp
}
//5 decimal places are retained by default
func (fixed Fixed64) ToBase10() string {
return fixed.ToBase10N(5)
}
//the maximum support is 18 decimals
func (fixed Fixed64) ToBase10N(n uint) string {
if n > 18 {
panic("Fixed64.ToBase10N: Not Support n > 19")
}
floatSlice := make([]byte, 0, 10)
if fixed&mask64S > 0 {
floatSlice = append(floatSlice, '-')
}
if n == 0 {
return string(append(floatSlice, insertToFloatSliceBase10(uint64(fixed.Abs().Round()), 0)...))
}
n = n + 1 // To round to the end
number := uint64(fixed &^ mask64S)
d := int64(number >> precisionBitsNum)
p := number & decimalBitsMask
hi, lo := bits.Mul64(p, uint64(math.Pow10(int(n)))) //todo optimize data processing methods
quo, _ := bits.Div64(hi, lo, 1<<precisionBitsNum)
partD := insertToFloatSliceBase10(uint64(d), 0)
partP, upToTop := carryUpBase10(roundEndBase10(insertToFloatSliceBase10(quo, int(n)))[:n-1])
if upToTop {
partD[len(partD)-1]++
partD, upToTop = carryUpBase10(partD)
}
if upToTop {
floatSlice = append(floatSlice, '1')
}
floatSlice = append(floatSlice, partD...)
floatSlice = append(floatSlice, '.')
floatSlice = append(floatSlice, partP...)
return string(floatSlice)
}
func insertToFloatSliceBase10(v uint64, n int) []byte {
const zeroStr = "00000000000000000000" //len 20
var ret []byte
if n > 0 {
ret = make([]byte, 0, n)
}
for v > 0 {
ret = append(ret, byte(v%10)+'0')
v /= 10
}
if n > len(ret) {
ret = append(ret, zeroStr[:n-len(ret)]...)
}
if len(ret) > 0 {
for i := 0; i < len(ret)>>1; i++ {
ret[i], ret[len(ret)-1-i] = ret[len(ret)-1-i], ret[i]
}
} else {
ret = append(ret, '0')
}
return ret
}
func (fixed Fixed64) String() string {
//return strconv.FormatFloat(fixed.Float64(), 'f', -1, 64)
//s := strings.TrimRightFunc(fmt.Sprintf("%f", fixed.Float64()), func(r rune) bool {
// if r == '0' || r == '.' {
// return true
// }
// return false
//})
//
//if s == "" {
// s = "0"
//}
return string(fixed.ToBase10())
}
func Uint64Bits(v uint64) (r []byte) {
r = make([]byte, 64)
for i := 0; i < 64; i++ {
r[63-i] = '0'
if (v & (1 << i)) > 0 {
r[63-i]++
v &^= 1 << i
}
}
return
}
// round to even
func roundOdd(v, precisionBitsNum uint64) uint64 {
precisionBitsMask := uint64(1<<precisionBitsNum) - 1
flow := v & precisionBitsMask
cond := uint64(1 << (precisionBitsNum - 1))
var endBit uint64
if flow > cond || flow == cond && v&(1<<precisionBitsNum) > 0 {
endBit = 1
}
return (v &^ precisionBitsMask) + (endBit << precisionBitsNum)
}
func roundEndBase10(p []byte) []byte {
//if p == nil || len(p) == 0 {
// return []byte{'0'}
//}
if p[len(p)-1] > '4' {
p[len(p)-1] = 0
//if len(p) == 1 {
// p[0] = '9' + 1
// return p
//}
p[len(p)-2]++
}
return p
}
func carryUpBase10(p []byte) ([]byte, bool) {
i := len(p) - 1
for ; i >= 1; i-- {
if p[i] > '9' {
p[i] = '0'
p[i-1]++
} else {
return p, false
}
}
upToTop := p[0] == '9'+1
if upToTop {
p[0] = '0'
}
return p, upToTop
}