-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcmap_cache.go
More file actions
70 lines (63 loc) · 2.07 KB
/
cmap_cache.go
File metadata and controls
70 lines (63 loc) · 2.07 KB
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
package font
// Code generated by typesetting-utils/generators/cache/gen.go. DO NOT EDIT.
/* Implements caches for integers key->value functions.
*
* The cache is a fixed-size array of 8-bit, 16-bit or 32-bit integers,
* typically 256 elements.
*
* The key is split into two parts: the cache index (high bits)
* and the rest (low bits).
*
* The memory layout is the following :
* KEY = <key bits - cache bits><cache bits>
* VALUE = <key bits - cache bits><value bits>
* with the constraints
* KEY in [0, 2^key bits[
* VALUE in [0, 2^value bits[
*
* The cache index is used to index into the array. The array
* member is an integer that is used BOTH
* to store the low bits of the key, and the value.
*
* The value is stored in the least significant bits of the integer.
* The low bits of the key are stored in the most significant bits
* of the integer.
*
* A cache hit is detected by comparing the low bits of the key
* with the high bits of the integer at the array position indexed
* by the high bits of the key. If they match, the value is extracted
* from the least significant bits of the integer and returned.
* Otherwise, a cache miss is reported.
*
* Cache operations (storage and retrieval) involve just a few
* arithmetic operations and a single memory access.
*/
// cache21_19_8 is a cache for integer (key, value) pairs,
// with 0 <= key < 2097152 and 0 <= value < 524288
type cache21_19_8 [1 << 8]uint32
// clear should be used as init function
func (c *cache21_19_8) clear() {
for i := range c {
c[i] = ^uint32(0)
}
}
func (c cache21_19_8) get(key uint32) (uint32, bool) {
k := key & ((1 << 8) - 1)
v := c[k]
if v == ^uint32(0) || (v>>19) != uint32(key>>8) {
return 0, false
}
return v & ((1 << 19) - 1), true
}
func (c *cache21_19_8) set(key uint32, value uint32) {
if (key>>21) != 0 || (value>>19) != 0 { /* overflows */
return
}
c.setUnchecked(key, value)
}
// assumes key < 2097152 and value < 524288
func (c *cache21_19_8) setUnchecked(key uint32, value uint32) {
k := key & ((1 << 8) - 1)
v := (uint32(key>>8) << 19) | value
c[k] = v
}