2023-12-29 12:52:34 -08:00
|
|
|
package codes
|
|
|
|
|
|
|
|
|
|
type Code struct {
|
2023-12-31 19:24:17 -08:00
|
|
|
Value uint32
|
|
|
|
|
Bits uint8
|
2023-12-29 12:52:34 -08:00
|
|
|
}
|
|
|
|
|
|
2023-12-31 19:24:17 -08:00
|
|
|
func CodeForIndex(index uint32) Code {
|
|
|
|
|
switch {
|
|
|
|
|
case index < 4:
|
|
|
|
|
return Code{Value: index, Bits: 4}
|
|
|
|
|
case index < 8:
|
|
|
|
|
return Code{Value: 0b01000 + (index & 0b00011), Bits: 5}
|
|
|
|
|
case index < 16:
|
|
|
|
|
return Code{Value: 0b011000 + (index & 0b000111), Bits: 6}
|
|
|
|
|
case index < 32:
|
|
|
|
|
return Code{Value: 0b1000000 + (index & 0b0001111), Bits: 7}
|
|
|
|
|
default:
|
|
|
|
|
set := uint8(index / uint32(64))
|
2023-12-29 12:52:34 -08:00
|
|
|
|
2023-12-31 19:24:17 -08:00
|
|
|
return Code{
|
|
|
|
|
Value: (((2 << set) - 1) << 7) + (index % uint32(64)),
|
|
|
|
|
Bits: set + 8,
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-29 12:52:34 -08:00
|
|
|
}
|