1 // Copyright 2016 Brian Smith.
2 //
3 // Permission to use, copy, modify, and/or distribute this software for any
4 // purpose with or without fee is hereby granted, provided that the above
5 // copyright notice and this permission notice appear in all copies.
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
10 // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 
15 //! Bit lengths.
16 
17 use crate::error;
18 
19 /// The length of something, in bits.
20 ///
21 /// This can represent a bit length that isn't a whole number of bytes.
22 #[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd)]
23 pub struct BitLength(usize);
24 
25 // Lengths measured in bits, where all arithmetic is guaranteed not to
26 // overflow.
27 impl BitLength {
28     /// Constructs a `BitLength` from the given length in bits.
29     #[inline]
from_usize_bits(bits: usize) -> Self30     pub const fn from_usize_bits(bits: usize) -> Self {
31         Self(bits)
32     }
33 
34     /// Constructs a `BitLength` from the given length in bytes.
35     ///
36     /// Fails if `bytes * 8` is too large for a `usize`.
37     #[inline]
from_usize_bytes(bytes: usize) -> Result<Self, error::Unspecified>38     pub fn from_usize_bytes(bytes: usize) -> Result<Self, error::Unspecified> {
39         let bits = bytes.checked_mul(8).ok_or(error::Unspecified)?;
40         Ok(Self::from_usize_bits(bits))
41     }
42 
43     #[cfg(feature = "alloc")]
44     #[inline]
half_rounded_up(&self) -> Self45     pub(crate) fn half_rounded_up(&self) -> Self {
46         let round_up = self.0 & 1;
47         Self((self.0 / 2) + round_up)
48     }
49 
50     /// The number of bits this bit length represents, as a `usize`.
51     #[inline]
as_usize_bits(&self) -> usize52     pub fn as_usize_bits(&self) -> usize {
53         self.0
54     }
55 
56     /// The bit length, rounded up to a whole number of bytes.
57     #[cfg(feature = "alloc")]
58     #[inline]
as_usize_bytes_rounded_up(&self) -> usize59     pub fn as_usize_bytes_rounded_up(&self) -> usize {
60         // Equivalent to (self.0 + 7) / 8, except with no potential for
61         // overflow and without branches.
62 
63         // Branchless round_up = if self.0 & 0b111 != 0 { 1 } else { 0 };
64         let round_up = ((self.0 >> 2) | (self.0 >> 1) | self.0) & 1;
65 
66         (self.0 / 8) + round_up
67     }
68 
69     #[cfg(feature = "alloc")]
70     #[inline]
try_sub_1(self) -> Result<Self, error::Unspecified>71     pub(crate) fn try_sub_1(self) -> Result<Self, error::Unspecified> {
72         let sum = self.0.checked_sub(1).ok_or(error::Unspecified)?;
73         Ok(Self(sum))
74     }
75 }
76