-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexenc.go
43 lines (38 loc) · 1.05 KB
/
lexenc.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
// Copyright 2022 V Kontakte LLC
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package statshouse
import (
"math"
"strconv"
)
// Advanced feature.
// Encodes float as a raw tag in a special format, used by Statshouse.
// Ordering of all float values is preserved after encoding.
// Except all NaNs map to single value which is > +inf, and both zeroes map to 0.
func LexEncode(x float32) int32 {
if x == 0 {
return 0 // replace -0 with +0
}
if math.IsNaN(float64(x)) {
return 0x7fc00000 // replace all NaNs with single positive quiet NaN
}
bits := math.Float32bits(x)
if x < 0 {
// flip all except signbit so bigger negatives go before smaller ones
bits ^= 0x7fffffff
}
return int32(bits)
}
func LexEncodeStr(x float32) string {
return strconv.Itoa(int(LexEncode(x)))
}
func LexDecode(x int32) float32 {
bits := uint32(x)
if x < 0 {
bits ^= 0x7fffffff
}
return math.Float32frombits(bits)
}