go.temporal.io/server/common/number/compact8.go
98 LOC · 56 covered · 42 uncovered · 21 ranges · 73 concepts · 15 introducers · 40 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
package number
import "math/bits"
// Compact8 is an unsigned 8-bit compact integer encoding with 12 mantissa
// values per exponent level (instead of the standard power-of-2 split).
//
// For a byte value b, let e = b / 12 and m = b % 12:
//
// b=0: 0 (zero)
// e=0, m>=1: m << (offset+1) (subnormal)
// e>=1: (12+m) << (e+offset) (normalized)
//
// This gives approximately 8.3% relative precision (12 values per octave)
// and a representable range from 32 to 503316480 with offset 4.
type Compact8 = uint8
const compact8offset = 4
// DecodeCompact8 converts a Compact8 value to an int64.
if b == 0 {
}
m := int(b % 12)
if e == 0 {
}
}
// EncodeCompact8 encodes a non-negative int64 into Compact8 representation.
// The value is rounded down to the nearest representable value.
// Negative values go to 0 and values above the maximum representable go to 255.
if value <= 0 {
}
bitLen := bits.Len64(uval)
// Find shift such that uval >> shift is in [12, 23].
// This extracts the significand for the normalized representation.
shift := max(bitLen-5, 0)
sig := int(uval >> uint(shift))
if sig >= 24 {
shift++
sig = int(uval >> uint(shift))
}
if e >= 1 {
b := e*12 + m
if b > 255 {
}
}
// Subnormal: value = m << (offset + 1), m in [1, 11]
if m < 1 {
}
m = 11
}
}
// UpdateCompact8 returns the Compact8 encoding of value, but with hysteresis:
// it sticks to prev unless the new code is significantly closer. This prevents
// oscillation when the underlying value fluctuates near a bucket boundary.
newCode := EncodeCompact8(value)
if newCode == prev {
}
oldDist := DecodeCompact8(prev) - value
if oldDist < 0 {
oldDist = -oldDist
}
// Require the new code to be closer by at least half a bucket width
// (at the smaller of the two exponent levels). This shifts the
// transition point from the midpoint to the 3/4 mark of the gap,
// creating a dead zone that prevents chatter.
margin := int64(1) << (e + compact8offset - 1)
if newDist < oldDist-margin {
return newCode
}
}