Lorenz Brun | fba5da0 | 2022-12-15 11:20:47 +0000 | [diff] [blame] | 1 | package nvme |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | "math/big" |
| 6 | ) |
| 7 | |
| 8 | // uint128 little endian composed of two uint64s, readable by binary.Read. |
| 9 | // Auxiliary type to simplify structures with uint128s (of which NVMe has |
| 10 | // quite a few). |
| 11 | type uint128le struct { |
| 12 | Lo, Hi uint64 |
| 13 | } |
| 14 | |
| 15 | // BigInt returns u as a bigint |
| 16 | func (u uint128le) BigInt() *big.Int { |
| 17 | v := new(big.Int).SetUint64(u.Hi) |
| 18 | v = v.Lsh(v, 64) |
| 19 | v = v.Or(v, new(big.Int).SetUint64(u.Lo)) |
| 20 | return v |
| 21 | } |
| 22 | |
| 23 | // Uint64 returns u as a clamped uint64 |
| 24 | func (u uint128le) Uint64() uint64 { |
| 25 | if u.Hi > 0 { |
| 26 | return math.MaxUint64 |
| 27 | } |
| 28 | return u.Lo |
| 29 | } |