blob: 3b98f7d322d622274d7b5044e536803baca80f1b [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Lorenz Brunfba5da02022-12-15 11:20:47 +00004package nvme
5
6import (
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).
14type uint128le struct {
15 Lo, Hi uint64
16}
17
18// BigInt returns u as a bigint
19func (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
27func (u uint128le) Uint64() uint64 {
28 if u.Hi > 0 {
29 return math.MaxUint64
30 }
31 return u.Lo
32}