/
utils.go
115 lines (99 loc) · 2.95 KB
/
utils.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package internal
import (
"fmt"
"math"
"math/bits"
"reflect"
"strconv"
)
const (
InverseGolden = float64(0.6180339887498949025)
)
const (
DEFAULT_UPDATE_SEED = uint64(9001)
)
const (
DSketchTestGenerateGo = "DSKETCH_TEST_GENERATE_GO"
)
const (
JavaPath = "../serialization_test_data/java_generated_files"
CppPath = "../serialization_test_data/cpp_generated_files"
GoPath = "../serialization_test_data/go_generated_files"
)
// GetShortLE gets a short value from a byte array in little endian format.
func GetShortLE(array []byte, offset int) int {
return int(array[offset]&0xFF) | (int(array[offset+1]&0xFF) << 8)
}
// PutShortLE puts a short value into a byte array in little endian format.
func PutShortLE(array []byte, offset int, value int) {
array[offset] = byte(value)
array[offset+1] = byte(value >> 8)
}
// InvPow2 returns 2^(-e).
func InvPow2(e int) (float64, error) {
if (e | 1024 - e - 1) < 0 {
return 0, fmt.Errorf("e cannot be negative or greater than 1023: " + strconv.Itoa(e))
}
return math.Float64frombits((1023 - uint64(e)) << 52), nil
}
// CeilPowerOf2 returns the smallest power of 2 greater than or equal to n.
func CeilPowerOf2(n int) int {
if n <= 1 {
return 1
}
topIntPwrOf2 := 1 << 30
if n >= topIntPwrOf2 {
return topIntPwrOf2
}
return int(math.Pow(2, math.Ceil(math.Log2(float64(n)))))
}
func ExactLog2(powerOf2 int) (int, error) {
if !IsPowerOf2(powerOf2) {
return 0, fmt.Errorf("argument 'powerOf2' must be a positive power of 2")
}
return bits.TrailingZeros64(uint64(powerOf2)), nil
}
// IsPowerOf2 returns true if the given number is a power of 2.
func IsPowerOf2(powerOf2 int) bool {
return powerOf2 > 0 && (powerOf2&(powerOf2-1)) == 0
}
func BoolToInt(b bool) int {
if b {
return 1
}
return 0
}
func FloorPowerOf2(n int64) int64 {
if n <= 1 {
return 1
}
return n & (math.MinInt64 >> (bits.LeadingZeros64(uint64(n))))
}
func IsNil[T any](t T) bool {
v := reflect.ValueOf(t)
kind := v.Kind()
// Must be one of these types to be nillable
return (kind == reflect.Ptr ||
kind == reflect.Interface ||
kind == reflect.Slice ||
kind == reflect.Map ||
kind == reflect.Chan ||
kind == reflect.Func) &&
v.IsNil()
}