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