forked from DataDog/sketches-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_operation_helper.go
33 lines (27 loc) · 983 Bytes
/
bit_operation_helper.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2020 Datadog, Inc.
package mapping
import (
"math"
)
const (
exponentBias = 1023
exponentMask = uint64(0x7FF0000000000000)
exponentShift = 52
significandMask = uint64(0x000fffffffffffff)
significandWidth = 53
oneMask = uint64(0x3ff0000000000000)
)
func getExponent(float64Bits uint64) float64 {
return float64(int((float64Bits&exponentMask)>>exponentShift) - exponentBias)
}
func getSignificandPlusOne(float64Bits uint64) float64 {
return math.Float64frombits((float64Bits & significandMask) | oneMask)
}
func buildFloat64(exponent int, significandPlusOne float64) float64 {
return math.Float64frombits(
(uint64((exponent+exponentBias)<<exponentShift) & exponentMask) | (math.Float64bits(significandPlusOne) & significandMask),
)
}